|
|
@@ -1,17 +1,6 @@
|
|
|
-import { ID } from '@vendure/common/lib/shared-types';
|
|
|
-
|
|
|
import { InjectableStrategy } from '../../common/types/injectable-strategy';
|
|
|
|
|
|
-/**
|
|
|
- * @description
|
|
|
- * Defines the type of primary key used for all entities in the database.
|
|
|
- * "increment" uses an auto-incrementing integer, whereas "uuid" uses a
|
|
|
- * uuid string.
|
|
|
- *
|
|
|
- * @docsCategory entities
|
|
|
- * @docsPage Entity Configuration
|
|
|
- */
|
|
|
-export type PrimaryKeyType = 'increment' | 'uuid';
|
|
|
+export type PrimaryKeyType<T> = T extends 'uuid' ? string : T extends 'increment' ? number : any;
|
|
|
|
|
|
/**
|
|
|
* @description
|
|
|
@@ -22,20 +11,29 @@ export type PrimaryKeyType = 'increment' | 'uuid';
|
|
|
* @docsCategory entities
|
|
|
* @docsPage Entity Configuration
|
|
|
* */
|
|
|
-export interface EntityIdStrategy<T extends ID = ID> extends InjectableStrategy {
|
|
|
- readonly primaryKeyType: PrimaryKeyType;
|
|
|
- encodeId: (primaryKey: T) => string;
|
|
|
- decodeId: (id: string) => T;
|
|
|
-}
|
|
|
-
|
|
|
-export interface IntegerIdStrategy extends EntityIdStrategy<number> {
|
|
|
- readonly primaryKeyType: 'increment';
|
|
|
- encodeId: (primaryKey: number) => string;
|
|
|
- decodeId: (id: string) => number;
|
|
|
-}
|
|
|
-
|
|
|
-export interface StringIdStrategy extends EntityIdStrategy<string> {
|
|
|
- readonly primaryKeyType: 'uuid';
|
|
|
- encodeId: (primaryKey: string) => string;
|
|
|
- decodeId: (id: string) => string;
|
|
|
+export interface EntityIdStrategy<T extends 'increment' | 'uuid'> extends InjectableStrategy {
|
|
|
+ /**
|
|
|
+ * @description
|
|
|
+ * Defines how the primary key will be stored in the database - either
|
|
|
+ * `'increment'` for auto-increment integer IDs, or `'uuid'` for a unique
|
|
|
+ * string ID.
|
|
|
+ */
|
|
|
+ readonly primaryKeyType: T;
|
|
|
+ /**
|
|
|
+ * @description
|
|
|
+ * Allows the raw ID from the database to be transformed in some way before exposing
|
|
|
+ * it in the GraphQL API.
|
|
|
+ *
|
|
|
+ * For example, you may need to use auto-increment integer IDs due to some business
|
|
|
+ * constraint, but you may not want to expose this data publicly in your API. In this
|
|
|
+ * case, you can use the encode/decode methods to obfuscate the ID with some kind of
|
|
|
+ * encoding scheme, such as base64 (or something more sophisticated).
|
|
|
+ */
|
|
|
+ encodeId: (primaryKey: PrimaryKeyType<T>) => string;
|
|
|
+ /**
|
|
|
+ * @description
|
|
|
+ * Reverses the transformation performed by the `encodeId` method in order to get
|
|
|
+ * back to the raw ID value.
|
|
|
+ */
|
|
|
+ decodeId: (id: string) => PrimaryKeyType<T>;
|
|
|
}
|