@prisma-next/ids 0.3.0-dev.86 → 0.3.0-dev.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +17 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +91 -31
- package/dist/index.mjs.map +1 -1
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +5 -2
- package/dist/runtime.mjs.map +1 -1
- package/package.json +6 -6
- package/src/generator-ids.ts +10 -0
- package/src/generators.ts +2 -2
- package/src/index.ts +93 -30
- package/src/runtime.ts +9 -2
package/dist/index.d.mts
CHANGED
|
@@ -7,6 +7,10 @@ import { uuidv7 as uuidv7$1 } from "uniku/uuid/v7";
|
|
|
7
7
|
import { ExecutionMutationDefaultValue } from "@prisma-next/contract/types";
|
|
8
8
|
import { ColumnTypeDescriptor } from "@prisma-next/contract-authoring";
|
|
9
9
|
|
|
10
|
+
//#region src/generator-ids.d.ts
|
|
11
|
+
declare const builtinGeneratorIds: readonly ["ulid", "nanoid", "uuidv7", "uuidv4", "cuid2", "ksuid"];
|
|
12
|
+
type BuiltinGeneratorId = (typeof builtinGeneratorIds)[number];
|
|
13
|
+
//#endregion
|
|
10
14
|
//#region src/generators.d.ts
|
|
11
15
|
type FirstArg<TFunction> = TFunction extends ((...args: infer TArgs) => unknown) ? TArgs extends [] ? undefined : TArgs[0] : never;
|
|
12
16
|
type IdGeneratorOptionsById = {
|
|
@@ -19,6 +23,18 @@ type IdGeneratorOptionsById = {
|
|
|
19
23
|
};
|
|
20
24
|
//#endregion
|
|
21
25
|
//#region src/index.d.ts
|
|
26
|
+
type GeneratedColumnDescriptor = {
|
|
27
|
+
readonly type: ColumnTypeDescriptor;
|
|
28
|
+
readonly typeParams?: Record<string, unknown>;
|
|
29
|
+
};
|
|
30
|
+
declare const builtinGeneratorRegistryMetadata: ReadonlyArray<{
|
|
31
|
+
readonly id: BuiltinGeneratorId;
|
|
32
|
+
readonly applicableCodecIds: readonly string[];
|
|
33
|
+
}>;
|
|
34
|
+
declare function resolveBuiltinGeneratedColumnDescriptor(input: {
|
|
35
|
+
readonly id: BuiltinGeneratorId;
|
|
36
|
+
readonly params?: Record<string, unknown>;
|
|
37
|
+
}): GeneratedColumnDescriptor;
|
|
22
38
|
type GeneratedColumnSpec = {
|
|
23
39
|
readonly type: ColumnTypeDescriptor;
|
|
24
40
|
readonly nullable?: false;
|
|
@@ -32,5 +48,5 @@ declare const uuidv4: (options?: IdGeneratorOptionsById["uuidv4"]) => GeneratedC
|
|
|
32
48
|
declare const cuid2: (options?: IdGeneratorOptionsById["cuid2"]) => GeneratedColumnSpec;
|
|
33
49
|
declare const ksuid: (options?: IdGeneratorOptionsById["ksuid"]) => GeneratedColumnSpec;
|
|
34
50
|
//#endregion
|
|
35
|
-
export { GeneratedColumnSpec, cuid2, ksuid, nanoid, ulid, uuidv4, uuidv7 };
|
|
51
|
+
export { GeneratedColumnDescriptor, GeneratedColumnSpec, builtinGeneratorIds, builtinGeneratorRegistryMetadata, cuid2, ksuid, nanoid, resolveBuiltinGeneratedColumnDescriptor, ulid, uuidv4, uuidv7 };
|
|
36
52
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/generators.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/generator-ids.ts","../src/generators.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;cAAa;KASD,kBAAA,WAA6B;;;KCDpC,sBAAsB,uFAGrB;KAGM,sBAAA;iBACK,gBAAgB;mBACd,gBAAgB;EDhBtB,SAAA,MAAA,ECiBM,QDVT,CAAA,OCUyB,QDVzB,CAAA;EAEE,SAAA,MAAA,ECSO,QDTW,CAAA,OCSK,QDTM,CAAA;kBCUvB,gBAAgB;kBA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-ids.ts
|
|
4
|
+
const builtinGeneratorIds = [
|
|
5
|
+
"ulid",
|
|
6
|
+
"nanoid",
|
|
7
|
+
"uuidv7",
|
|
8
|
+
"uuidv4",
|
|
9
|
+
"cuid2",
|
|
10
|
+
"ksuid"
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
3
14
|
//#region src/index.ts
|
|
4
|
-
|
|
5
|
-
|
|
15
|
+
function resolveNanoidColumnDescriptor(params) {
|
|
16
|
+
const rawSize = params?.["size"];
|
|
17
|
+
if (rawSize === void 0) return {
|
|
6
18
|
type: {
|
|
7
19
|
codecId: "sql/char@1",
|
|
8
20
|
nativeType: "character"
|
|
9
21
|
},
|
|
10
|
-
typeParams: { length:
|
|
11
|
-
}
|
|
12
|
-
nanoid
|
|
22
|
+
typeParams: { length: 21 }
|
|
23
|
+
};
|
|
24
|
+
if (typeof rawSize !== "number" || !Number.isInteger(rawSize) || rawSize < 2 || rawSize > 255) throw new Error("nanoid size must be an integer between 2 and 255");
|
|
25
|
+
return {
|
|
13
26
|
type: {
|
|
14
27
|
codecId: "sql/char@1",
|
|
15
28
|
nativeType: "character"
|
|
16
29
|
},
|
|
17
|
-
typeParams: { length:
|
|
30
|
+
typeParams: { length: rawSize }
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const builtinGeneratorMetadataById = {
|
|
34
|
+
ulid: {
|
|
35
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
36
|
+
generatedColumnDescriptor: {
|
|
37
|
+
type: {
|
|
38
|
+
codecId: "sql/char@1",
|
|
39
|
+
nativeType: "character"
|
|
40
|
+
},
|
|
41
|
+
typeParams: { length: 26 }
|
|
42
|
+
}
|
|
18
43
|
},
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
44
|
+
nanoid: {
|
|
45
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
46
|
+
generatedColumnDescriptor: {
|
|
47
|
+
type: {
|
|
48
|
+
codecId: "sql/char@1",
|
|
49
|
+
nativeType: "character"
|
|
50
|
+
},
|
|
51
|
+
typeParams: { length: 21 }
|
|
23
52
|
},
|
|
24
|
-
|
|
53
|
+
resolveGeneratedColumnDescriptor: resolveNanoidColumnDescriptor
|
|
54
|
+
},
|
|
55
|
+
uuidv7: {
|
|
56
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
57
|
+
generatedColumnDescriptor: {
|
|
58
|
+
type: {
|
|
59
|
+
codecId: "sql/char@1",
|
|
60
|
+
nativeType: "character"
|
|
61
|
+
},
|
|
62
|
+
typeParams: { length: 36 }
|
|
63
|
+
}
|
|
25
64
|
},
|
|
26
65
|
uuidv4: {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
66
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
67
|
+
generatedColumnDescriptor: {
|
|
68
|
+
type: {
|
|
69
|
+
codecId: "sql/char@1",
|
|
70
|
+
nativeType: "character"
|
|
71
|
+
},
|
|
72
|
+
typeParams: { length: 36 }
|
|
73
|
+
}
|
|
32
74
|
},
|
|
33
75
|
cuid2: {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
76
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
77
|
+
generatedColumnDescriptor: {
|
|
78
|
+
type: {
|
|
79
|
+
codecId: "sql/char@1",
|
|
80
|
+
nativeType: "character"
|
|
81
|
+
},
|
|
82
|
+
typeParams: { length: 24 }
|
|
83
|
+
}
|
|
39
84
|
},
|
|
40
85
|
ksuid: {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
86
|
+
applicableCodecIds: ["pg/text@1", "sql/char@1"],
|
|
87
|
+
generatedColumnDescriptor: {
|
|
88
|
+
type: {
|
|
89
|
+
codecId: "sql/char@1",
|
|
90
|
+
nativeType: "character"
|
|
91
|
+
},
|
|
92
|
+
typeParams: { length: 27 }
|
|
93
|
+
}
|
|
46
94
|
}
|
|
47
95
|
};
|
|
96
|
+
const builtinGeneratorRegistryMetadata = builtinGeneratorIds.map((id) => ({
|
|
97
|
+
id,
|
|
98
|
+
applicableCodecIds: builtinGeneratorMetadataById[id].applicableCodecIds
|
|
99
|
+
}));
|
|
100
|
+
function resolveBuiltinGeneratedColumnDescriptor(input) {
|
|
101
|
+
const metadata = builtinGeneratorMetadataById[input.id];
|
|
102
|
+
const resolver = metadata.resolveGeneratedColumnDescriptor;
|
|
103
|
+
if (resolver) return resolver(input.params);
|
|
104
|
+
return metadata.generatedColumnDescriptor;
|
|
105
|
+
}
|
|
48
106
|
function createGeneratedSpec(id, options) {
|
|
49
|
-
const { type } = generatedColumnDescriptors[id];
|
|
50
|
-
const typeParams = id === "nanoid" && typeof options === "object" && options !== null && "size" in options && typeof options.size === "number" ? { length: options.size } : generatedColumnDescriptors[id].typeParams;
|
|
51
107
|
const params = options;
|
|
108
|
+
const resolvedDescriptor = resolveBuiltinGeneratedColumnDescriptor({
|
|
109
|
+
id,
|
|
110
|
+
...ifDefined("params", params)
|
|
111
|
+
});
|
|
52
112
|
return {
|
|
53
|
-
type,
|
|
113
|
+
type: resolvedDescriptor.type,
|
|
54
114
|
nullable: false,
|
|
55
|
-
...ifDefined("typeParams", typeParams),
|
|
115
|
+
...ifDefined("typeParams", resolvedDescriptor.typeParams),
|
|
56
116
|
generated: {
|
|
57
117
|
kind: "generator",
|
|
58
118
|
id,
|
|
@@ -68,5 +128,5 @@ const cuid2 = (options) => createGeneratedSpec("cuid2", options);
|
|
|
68
128
|
const ksuid = (options) => createGeneratedSpec("ksuid", options);
|
|
69
129
|
|
|
70
130
|
//#endregion
|
|
71
|
-
export { cuid2, ksuid, nanoid, ulid, uuidv4, uuidv7 };
|
|
131
|
+
export { builtinGeneratorIds, builtinGeneratorRegistryMetadata, cuid2, ksuid, nanoid, resolveBuiltinGeneratedColumnDescriptor, ulid, uuidv4, uuidv7 };
|
|
72
132
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["builtinGeneratorMetadataById: Record<BuiltinGeneratorId, BuiltinGeneratorMetadata>","builtinGeneratorRegistryMetadata: ReadonlyArray<{\n readonly id: BuiltinGeneratorId;\n readonly applicableCodecIds: readonly string[];\n}>"],"sources":["../src/generator-ids.ts","../src/index.ts"],"sourcesContent":["export const builtinGeneratorIds = [\n 'ulid',\n 'nanoid',\n 'uuidv7',\n 'uuidv4',\n 'cuid2',\n 'ksuid',\n] as const;\n\nexport type BuiltinGeneratorId = (typeof builtinGeneratorIds)[number];\n","import type { ExecutionMutationDefaultValue } from '@prisma-next/contract/types';\nimport type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { type BuiltinGeneratorId, builtinGeneratorIds } from './generator-ids';\nimport type { IdGeneratorOptionsById } from './generators';\n\nexport { builtinGeneratorIds };\n\nexport type GeneratedColumnDescriptor = {\n readonly type: ColumnTypeDescriptor;\n readonly typeParams?: Record<string, unknown>;\n};\n\ntype BuiltinGeneratorMetadata = {\n readonly applicableCodecIds: readonly string[];\n readonly generatedColumnDescriptor: GeneratedColumnDescriptor;\n readonly resolveGeneratedColumnDescriptor?: (\n params?: Record<string, unknown>,\n ) => GeneratedColumnDescriptor;\n};\n\nfunction resolveNanoidColumnDescriptor(\n params?: Record<string, unknown>,\n): GeneratedColumnDescriptor {\n const rawSize = params?.['size'];\n if (rawSize === undefined) {\n return {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 21 },\n };\n }\n\n if (typeof rawSize !== 'number' || !Number.isInteger(rawSize) || rawSize < 2 || rawSize > 255) {\n throw new Error('nanoid size must be an integer between 2 and 255');\n }\n\n return {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: rawSize },\n };\n}\n\nconst builtinGeneratorMetadataById: Record<BuiltinGeneratorId, BuiltinGeneratorMetadata> = {\n ulid: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 26 },\n },\n },\n nanoid: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 21 },\n },\n resolveGeneratedColumnDescriptor: resolveNanoidColumnDescriptor,\n },\n uuidv7: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 36 },\n },\n },\n uuidv4: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 36 },\n },\n },\n cuid2: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 24 },\n },\n },\n ksuid: {\n applicableCodecIds: ['pg/text@1', 'sql/char@1'],\n generatedColumnDescriptor: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 27 },\n },\n },\n};\n\nexport const builtinGeneratorRegistryMetadata: ReadonlyArray<{\n readonly id: BuiltinGeneratorId;\n readonly applicableCodecIds: readonly string[];\n}> = builtinGeneratorIds.map((id) => ({\n id,\n applicableCodecIds: builtinGeneratorMetadataById[id].applicableCodecIds,\n}));\n\nexport function resolveBuiltinGeneratedColumnDescriptor(input: {\n readonly id: BuiltinGeneratorId;\n readonly params?: Record<string, unknown>;\n}): GeneratedColumnDescriptor {\n const metadata = builtinGeneratorMetadataById[input.id];\n const resolver = metadata.resolveGeneratedColumnDescriptor;\n if (resolver) {\n return resolver(input.params);\n }\n return metadata.generatedColumnDescriptor;\n}\n\nexport type GeneratedColumnSpec = {\n readonly type: ColumnTypeDescriptor;\n readonly nullable?: false;\n readonly typeParams?: Record<string, unknown>;\n readonly generated: ExecutionMutationDefaultValue;\n};\n\nfunction createGeneratedSpec<TId extends BuiltinGeneratorId>(\n id: TId,\n options?: IdGeneratorOptionsById[TId],\n): GeneratedColumnSpec {\n const params = options as Record<string, unknown> | undefined;\n const resolvedDescriptor = resolveBuiltinGeneratedColumnDescriptor({\n id,\n ...ifDefined('params', params),\n });\n return {\n type: resolvedDescriptor.type,\n nullable: false,\n ...ifDefined('typeParams', resolvedDescriptor.typeParams),\n generated: {\n kind: 'generator',\n id,\n ...ifDefined('params', params),\n },\n };\n}\n\nexport const ulid = (options?: IdGeneratorOptionsById['ulid']): GeneratedColumnSpec =>\n createGeneratedSpec('ulid', options);\nexport const nanoid = (options?: IdGeneratorOptionsById['nanoid']): GeneratedColumnSpec =>\n createGeneratedSpec('nanoid', options);\nexport const uuidv7 = (options?: IdGeneratorOptionsById['uuidv7']): GeneratedColumnSpec =>\n createGeneratedSpec('uuidv7', options);\nexport const uuidv4 = (options?: IdGeneratorOptionsById['uuidv4']): GeneratedColumnSpec =>\n createGeneratedSpec('uuidv4', options);\nexport const cuid2 = (options?: IdGeneratorOptionsById['cuid2']): GeneratedColumnSpec =>\n createGeneratedSpec('cuid2', options);\nexport const ksuid = (options?: IdGeneratorOptionsById['ksuid']): GeneratedColumnSpec =>\n createGeneratedSpec('ksuid', options);\n"],"mappings":";;;AAAA,MAAa,sBAAsB;CACjC;CACA;CACA;CACA;CACA;CACA;CACD;;;;ACcD,SAAS,8BACP,QAC2B;CAC3B,MAAM,UAAU,SAAS;AACzB,KAAI,YAAY,OACd,QAAO;EACL,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;AAGH,KAAI,OAAO,YAAY,YAAY,CAAC,OAAO,UAAU,QAAQ,IA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,KAAI,SACF,QAAO,SAAS,MAAM,OAAO;AAE/B,QAAO,SAAS;;AAUlB,SAAS,oBACP,IACA,SACqB;CACrB,MAAM,SAAS;CACf,MAAM,qBAAqB,wCAAwC;EACjE;EACA,GAAG,UAAU,UAAU,OAAO;EAC/B,CAAC;AACF,QAAO;EACL,MAAM,mBAAmB;EACzB,UAAU;EACV,GAAG,UAAU,cAAc,mBAAmB,WAAW;EACzD,WAAW;GACT,MAAM;GACN;GACA,GAAG,UAAU,UAAU,OAAO;GAC/B;EACF;;AAGH,MAAa,QAAQ,YACnB,oBAAoB,QAAQ,QAAQ;AACtC,MAAa,UAAU,YACrB,oBAAoB,UAAU,QAAQ;AACxC,MAAa,UAAU,YACrB,oBAAoB,UAAU,QAAQ;AACxC,MAAa,UAAU,YACrB,oBAAoB,UAAU,QAAQ;AACxC,MAAa,SAAS,YACpB,oBAAoB,SAAS,QAAQ;AACvC,MAAa,SAAS,YACpB,oBAAoB,SAAS,QAAQ"}
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"sourcesContent":[],"mappings":";;;iBAQgB,UAAA,OAAiB"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -21,9 +21,12 @@ const idGenerators = {
|
|
|
21
21
|
|
|
22
22
|
//#endregion
|
|
23
23
|
//#region src/runtime.ts
|
|
24
|
+
function isBuiltinGeneratorId(id) {
|
|
25
|
+
return Object.hasOwn(idGenerators, id);
|
|
26
|
+
}
|
|
24
27
|
function generateId(spec) {
|
|
25
|
-
|
|
26
|
-
return
|
|
28
|
+
if (!isBuiltinGeneratorId(spec.id)) throw new Error(`Unknown built-in ID generator "${spec.id}".`);
|
|
29
|
+
return idGenerators[spec.id](spec.params);
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
//#endregion
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":["ulidGenerator","nanoidGenerator","uuidv7Generator","uuidv4Generator","cuid2Generator","ksuidGenerator"],"sources":["../src/generators.ts","../src/runtime.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"runtime.mjs","names":["ulidGenerator","nanoidGenerator","uuidv7Generator","uuidv4Generator","cuid2Generator","ksuidGenerator"],"sources":["../src/generators.ts","../src/runtime.ts"],"sourcesContent":["import { cuid2 as cuid2Generator } from 'uniku/cuid2';\nimport { ksuid as ksuidGenerator } from 'uniku/ksuid';\nimport { nanoid as nanoidGenerator } from 'uniku/nanoid';\nimport { ulid as ulidGenerator } from 'uniku/ulid';\nimport { uuidv4 as uuidv4Generator } from 'uniku/uuid/v4';\nimport { uuidv7 as uuidv7Generator } from 'uniku/uuid/v7';\nimport type { BuiltinGeneratorId } from './generator-ids';\n\ntype FirstArg<TFunction> = TFunction extends (...args: infer TArgs) => unknown\n ? TArgs extends []\n ? undefined\n : TArgs[0]\n : never;\n\nexport type IdGeneratorOptionsById = {\n readonly ulid: FirstArg<typeof ulidGenerator>;\n readonly nanoid: FirstArg<typeof nanoidGenerator>;\n readonly uuidv7: FirstArg<typeof uuidv7Generator>;\n readonly uuidv4: FirstArg<typeof uuidv4Generator>;\n readonly cuid2: FirstArg<typeof cuid2Generator>;\n readonly ksuid: FirstArg<typeof ksuidGenerator>;\n};\n\ntype IdGenerator = (params?: Record<string, unknown>) => string;\n\nfunction invokeGenerator<TOptions>(\n generator: (options?: TOptions) => string,\n params?: Record<string, unknown>,\n): string {\n if (params === undefined) {\n return generator();\n }\n return generator(params as TOptions);\n}\n\nexport const idGenerators = {\n ulid: (params?: Record<string, unknown>) => invokeGenerator(ulidGenerator, params),\n nanoid: (params?: Record<string, unknown>) => invokeGenerator(nanoidGenerator, params),\n uuidv7: (params?: Record<string, unknown>) => invokeGenerator(uuidv7Generator, params),\n uuidv4: (params?: Record<string, unknown>) => invokeGenerator(uuidv4Generator, params),\n cuid2: (params?: Record<string, unknown>) => invokeGenerator(cuid2Generator, params),\n ksuid: (params?: Record<string, unknown>) => invokeGenerator(ksuidGenerator, params),\n} as const satisfies Record<BuiltinGeneratorId, IdGenerator>;\n","import type { GeneratedValueSpec } from '@prisma-next/contract/types';\nimport type { BuiltinGeneratorId } from './generator-ids';\nimport { idGenerators } from './generators';\n\nfunction isBuiltinGeneratorId(id: string): id is BuiltinGeneratorId {\n return Object.hasOwn(idGenerators, id);\n}\n\nexport function generateId(spec: GeneratedValueSpec): string {\n if (!isBuiltinGeneratorId(spec.id)) {\n throw new Error(`Unknown built-in ID generator \"${spec.id}\".`);\n }\n return idGenerators[spec.id](spec.params);\n}\n"],"mappings":";;;;;;;;AAyBA,SAAS,gBACP,WACA,QACQ;AACR,KAAI,WAAW,OACb,QAAO,WAAW;AAEpB,QAAO,UAAU,OAAmB;;AAGtC,MAAa,eAAe;CAC1B,OAAO,WAAqC,gBAAgBA,MAAe,OAAO;CAClF,SAAS,WAAqC,gBAAgBC,QAAiB,OAAO;CACtF,SAAS,WAAqC,gBAAgBC,QAAiB,OAAO;CACtF,SAAS,WAAqC,gBAAgBC,QAAiB,OAAO;CACtF,QAAQ,WAAqC,gBAAgBC,OAAgB,OAAO;CACpF,QAAQ,WAAqC,gBAAgBC,OAAgB,OAAO;CACrF;;;;ACtCD,SAAS,qBAAqB,IAAsC;AAClE,QAAO,OAAO,OAAO,cAAc,GAAG;;AAGxC,SAAgB,WAAW,MAAkC;AAC3D,KAAI,CAAC,qBAAqB,KAAK,GAAG,CAChC,OAAM,IAAI,MAAM,kCAAkC,KAAK,GAAG,IAAI;AAEhE,QAAO,aAAa,KAAK,IAAI,KAAK,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/ids",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.88",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "ID generator helpers for Prisma Next contracts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"uniku": "^0.0.12",
|
|
9
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
10
|
-
"@prisma-next/contract-authoring": "0.3.0-dev.
|
|
11
|
-
"@prisma-next/utils": "0.3.0-dev.
|
|
9
|
+
"@prisma-next/contract": "0.3.0-dev.88",
|
|
10
|
+
"@prisma-next/contract-authoring": "0.3.0-dev.88",
|
|
11
|
+
"@prisma-next/utils": "0.3.0-dev.88"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"tsdown": "0.18.4",
|
|
15
15
|
"typescript": "5.9.3",
|
|
16
16
|
"vitest": "4.0.17",
|
|
17
|
-
"@prisma-next/
|
|
18
|
-
"@prisma-next/
|
|
17
|
+
"@prisma-next/tsdown": "0.0.0",
|
|
18
|
+
"@prisma-next/tsconfig": "0.0.0"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"dist",
|
package/src/generators.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { GeneratedValueSpec } from '@prisma-next/contract/types';
|
|
2
1
|
import { cuid2 as cuid2Generator } from 'uniku/cuid2';
|
|
3
2
|
import { ksuid as ksuidGenerator } from 'uniku/ksuid';
|
|
4
3
|
import { nanoid as nanoidGenerator } from 'uniku/nanoid';
|
|
5
4
|
import { ulid as ulidGenerator } from 'uniku/ulid';
|
|
6
5
|
import { uuidv4 as uuidv4Generator } from 'uniku/uuid/v4';
|
|
7
6
|
import { uuidv7 as uuidv7Generator } from 'uniku/uuid/v7';
|
|
7
|
+
import type { BuiltinGeneratorId } from './generator-ids';
|
|
8
8
|
|
|
9
9
|
type FirstArg<TFunction> = TFunction extends (...args: infer TArgs) => unknown
|
|
10
10
|
? TArgs extends []
|
|
@@ -40,4 +40,4 @@ export const idGenerators = {
|
|
|
40
40
|
uuidv4: (params?: Record<string, unknown>) => invokeGenerator(uuidv4Generator, params),
|
|
41
41
|
cuid2: (params?: Record<string, unknown>) => invokeGenerator(cuid2Generator, params),
|
|
42
42
|
ksuid: (params?: Record<string, unknown>) => invokeGenerator(ksuidGenerator, params),
|
|
43
|
-
} as const satisfies Record<
|
|
43
|
+
} as const satisfies Record<BuiltinGeneratorId, IdGenerator>;
|
package/src/index.ts
CHANGED
|
@@ -1,43 +1,111 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ExecutionMutationDefaultValue,
|
|
3
|
-
GeneratedValueSpec,
|
|
4
|
-
} from '@prisma-next/contract/types';
|
|
1
|
+
import type { ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
|
|
5
2
|
import type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';
|
|
6
3
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
4
|
+
import { type BuiltinGeneratorId, builtinGeneratorIds } from './generator-ids';
|
|
7
5
|
import type { IdGeneratorOptionsById } from './generators';
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
export { builtinGeneratorIds };
|
|
8
|
+
|
|
9
|
+
export type GeneratedColumnDescriptor = {
|
|
10
10
|
readonly type: ColumnTypeDescriptor;
|
|
11
11
|
readonly typeParams?: Record<string, unknown>;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
type BuiltinGeneratorMetadata = {
|
|
15
|
+
readonly applicableCodecIds: readonly string[];
|
|
16
|
+
readonly generatedColumnDescriptor: GeneratedColumnDescriptor;
|
|
17
|
+
readonly resolveGeneratedColumnDescriptor?: (
|
|
18
|
+
params?: Record<string, unknown>,
|
|
19
|
+
) => GeneratedColumnDescriptor;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function resolveNanoidColumnDescriptor(
|
|
23
|
+
params?: Record<string, unknown>,
|
|
24
|
+
): GeneratedColumnDescriptor {
|
|
25
|
+
const rawSize = params?.['size'];
|
|
26
|
+
if (rawSize === undefined) {
|
|
27
|
+
return {
|
|
28
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
29
|
+
typeParams: { length: 21 },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof rawSize !== 'number' || !Number.isInteger(rawSize) || rawSize < 2 || rawSize > 255) {
|
|
34
|
+
throw new Error('nanoid size must be an integer between 2 and 255');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
16
38
|
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
17
|
-
typeParams: { length:
|
|
39
|
+
typeParams: { length: rawSize },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const builtinGeneratorMetadataById: Record<BuiltinGeneratorId, BuiltinGeneratorMetadata> = {
|
|
44
|
+
ulid: {
|
|
45
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
46
|
+
generatedColumnDescriptor: {
|
|
47
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
48
|
+
typeParams: { length: 26 },
|
|
49
|
+
},
|
|
18
50
|
},
|
|
19
51
|
nanoid: {
|
|
20
|
-
|
|
21
|
-
|
|
52
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
53
|
+
generatedColumnDescriptor: {
|
|
54
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
55
|
+
typeParams: { length: 21 },
|
|
56
|
+
},
|
|
57
|
+
resolveGeneratedColumnDescriptor: resolveNanoidColumnDescriptor,
|
|
22
58
|
},
|
|
23
59
|
uuidv7: {
|
|
24
|
-
|
|
25
|
-
|
|
60
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
61
|
+
generatedColumnDescriptor: {
|
|
62
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
63
|
+
typeParams: { length: 36 },
|
|
64
|
+
},
|
|
26
65
|
},
|
|
27
66
|
uuidv4: {
|
|
28
|
-
|
|
29
|
-
|
|
67
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
68
|
+
generatedColumnDescriptor: {
|
|
69
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
70
|
+
typeParams: { length: 36 },
|
|
71
|
+
},
|
|
30
72
|
},
|
|
31
73
|
cuid2: {
|
|
32
|
-
|
|
33
|
-
|
|
74
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
75
|
+
generatedColumnDescriptor: {
|
|
76
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
77
|
+
typeParams: { length: 24 },
|
|
78
|
+
},
|
|
34
79
|
},
|
|
35
80
|
ksuid: {
|
|
36
|
-
|
|
37
|
-
|
|
81
|
+
applicableCodecIds: ['pg/text@1', 'sql/char@1'],
|
|
82
|
+
generatedColumnDescriptor: {
|
|
83
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
84
|
+
typeParams: { length: 27 },
|
|
85
|
+
},
|
|
38
86
|
},
|
|
39
87
|
};
|
|
40
88
|
|
|
89
|
+
export const builtinGeneratorRegistryMetadata: ReadonlyArray<{
|
|
90
|
+
readonly id: BuiltinGeneratorId;
|
|
91
|
+
readonly applicableCodecIds: readonly string[];
|
|
92
|
+
}> = builtinGeneratorIds.map((id) => ({
|
|
93
|
+
id,
|
|
94
|
+
applicableCodecIds: builtinGeneratorMetadataById[id].applicableCodecIds,
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
export function resolveBuiltinGeneratedColumnDescriptor(input: {
|
|
98
|
+
readonly id: BuiltinGeneratorId;
|
|
99
|
+
readonly params?: Record<string, unknown>;
|
|
100
|
+
}): GeneratedColumnDescriptor {
|
|
101
|
+
const metadata = builtinGeneratorMetadataById[input.id];
|
|
102
|
+
const resolver = metadata.resolveGeneratedColumnDescriptor;
|
|
103
|
+
if (resolver) {
|
|
104
|
+
return resolver(input.params);
|
|
105
|
+
}
|
|
106
|
+
return metadata.generatedColumnDescriptor;
|
|
107
|
+
}
|
|
108
|
+
|
|
41
109
|
export type GeneratedColumnSpec = {
|
|
42
110
|
readonly type: ColumnTypeDescriptor;
|
|
43
111
|
readonly nullable?: false;
|
|
@@ -45,24 +113,19 @@ export type GeneratedColumnSpec = {
|
|
|
45
113
|
readonly generated: ExecutionMutationDefaultValue;
|
|
46
114
|
};
|
|
47
115
|
|
|
48
|
-
function createGeneratedSpec<TId extends
|
|
116
|
+
function createGeneratedSpec<TId extends BuiltinGeneratorId>(
|
|
49
117
|
id: TId,
|
|
50
118
|
options?: IdGeneratorOptionsById[TId],
|
|
51
119
|
): GeneratedColumnSpec {
|
|
52
|
-
const { type } = generatedColumnDescriptors[id];
|
|
53
|
-
const typeParams =
|
|
54
|
-
id === 'nanoid' &&
|
|
55
|
-
typeof options === 'object' &&
|
|
56
|
-
options !== null &&
|
|
57
|
-
'size' in options &&
|
|
58
|
-
typeof options.size === 'number'
|
|
59
|
-
? { length: options.size }
|
|
60
|
-
: generatedColumnDescriptors[id].typeParams;
|
|
61
120
|
const params = options as Record<string, unknown> | undefined;
|
|
121
|
+
const resolvedDescriptor = resolveBuiltinGeneratedColumnDescriptor({
|
|
122
|
+
id,
|
|
123
|
+
...ifDefined('params', params),
|
|
124
|
+
});
|
|
62
125
|
return {
|
|
63
|
-
type,
|
|
126
|
+
type: resolvedDescriptor.type,
|
|
64
127
|
nullable: false,
|
|
65
|
-
...ifDefined('typeParams', typeParams),
|
|
128
|
+
...ifDefined('typeParams', resolvedDescriptor.typeParams),
|
|
66
129
|
generated: {
|
|
67
130
|
kind: 'generator',
|
|
68
131
|
id,
|
package/src/runtime.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import type { GeneratedValueSpec } from '@prisma-next/contract/types';
|
|
2
|
+
import type { BuiltinGeneratorId } from './generator-ids';
|
|
2
3
|
import { idGenerators } from './generators';
|
|
3
4
|
|
|
5
|
+
function isBuiltinGeneratorId(id: string): id is BuiltinGeneratorId {
|
|
6
|
+
return Object.hasOwn(idGenerators, id);
|
|
7
|
+
}
|
|
8
|
+
|
|
4
9
|
export function generateId(spec: GeneratedValueSpec): string {
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
if (!isBuiltinGeneratorId(spec.id)) {
|
|
11
|
+
throw new Error(`Unknown built-in ID generator "${spec.id}".`);
|
|
12
|
+
}
|
|
13
|
+
return idGenerators[spec.id](spec.params);
|
|
7
14
|
}
|