@prisma-next/ids 0.0.1
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/README.md +69 -0
- package/dist/index.d.mts +36 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +71 -0
- package/dist/index.mjs.map +1 -0
- package/dist/runtime.d.mts +7 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +31 -0
- package/dist/runtime.mjs.map +1 -0
- package/package.json +47 -0
- package/src/generators.ts +43 -0
- package/src/index.ts +77 -0
- package/src/runtime.ts +7 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @prisma-next/ids
|
|
2
|
+
|
|
3
|
+
ID generator helpers for Prisma Next contracts. This package provides ergonomic helpers that
|
|
4
|
+
produce contract-safe, JSON-serializable execution defaults for client-generated IDs, plus
|
|
5
|
+
runtime generation utilities that Prisma Next uses before sending data to adapters.
|
|
6
|
+
|
|
7
|
+
Each helper owns the column descriptor metadata associated with that generator, so callers only
|
|
8
|
+
pass options supported by the underlying `uniku` generator.
|
|
9
|
+
|
|
10
|
+
## Responsibilities
|
|
11
|
+
|
|
12
|
+
- Provide ID helper functions (`ulid`, `nanoid`, `uuidv7`, `uuidv4`, `cuid2`, `ksuid`) for contract authoring.
|
|
13
|
+
- Emit contract-safe execution defaults (no executable code stored in the contract).
|
|
14
|
+
- Generate values at runtime using `uniku` when mutation defaults require them.
|
|
15
|
+
|
|
16
|
+
## Dependencies
|
|
17
|
+
|
|
18
|
+
- `@prisma-next/contract` for shared contract types (`ExecutionMutationDefaultValue`).
|
|
19
|
+
- `uniku` for ID generator implementations.
|
|
20
|
+
|
|
21
|
+
## Architecture
|
|
22
|
+
|
|
23
|
+
```mermaid
|
|
24
|
+
flowchart LR
|
|
25
|
+
Authoring[ContractAuthoring] --> IdsHelpers[ids_helpers]
|
|
26
|
+
IdsHelpers --> ContractJson[contract_json]
|
|
27
|
+
Runtime[orm_lane] --> IdsRuntime[ids_runtime]
|
|
28
|
+
IdsRuntime --> Uniku[uniku_generators]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { defineContract } from '@prisma-next/sql-contract-ts/contract-builder';
|
|
35
|
+
import { uuidv4 } from '@prisma-next/ids';
|
|
36
|
+
|
|
37
|
+
export const contract = defineContract()
|
|
38
|
+
.table('user', (t) =>
|
|
39
|
+
t.generated('id', uuidv4()).column('email', { type: { codecId: 'pg/text@1', nativeType: 'text' } }),
|
|
40
|
+
)
|
|
41
|
+
.build();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Pass generator options directly (for helpers whose `uniku` implementation supports them):
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { nanoid } from '@prisma-next/ids';
|
|
48
|
+
|
|
49
|
+
const idSpec = nanoid({ size: 12 });
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Generator-owned codec mapping
|
|
53
|
+
|
|
54
|
+
- Each helper binds its own descriptor internally (today all helpers map to `pg/text@1`).
|
|
55
|
+
- Different helpers can move to different codecs independently (e.g. `ulid` binary and `nanoid` char/varchar).
|
|
56
|
+
- Parameterized codec metadata can be added per helper when adapter-side parameterized codecs are available.
|
|
57
|
+
|
|
58
|
+
Runtime usage:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { generateId } from '@prisma-next/ids/runtime';
|
|
62
|
+
|
|
63
|
+
const value = generateId({ id: 'uuidv4' });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Related docs
|
|
67
|
+
|
|
68
|
+
- [Data Contract](../../../docs/architecture%20docs/subsystems/1.%20Data%20Contract.md)
|
|
69
|
+
- [Query Lanes](../../../docs/architecture%20docs/subsystems/3.%20Query%20Lanes.md)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { cuid2 as cuid2$1 } from "uniku/cuid2";
|
|
2
|
+
import { ksuid as ksuid$1 } from "uniku/ksuid";
|
|
3
|
+
import { nanoid as nanoid$1 } from "uniku/nanoid";
|
|
4
|
+
import { ulid as ulid$1 } from "uniku/ulid";
|
|
5
|
+
import { uuidv4 as uuidv4$1 } from "uniku/uuid/v4";
|
|
6
|
+
import { uuidv7 as uuidv7$1 } from "uniku/uuid/v7";
|
|
7
|
+
import { ExecutionMutationDefaultValue } from "@prisma-next/contract/types";
|
|
8
|
+
import { ColumnTypeDescriptor } from "@prisma-next/contract-authoring";
|
|
9
|
+
|
|
10
|
+
//#region src/generators.d.ts
|
|
11
|
+
type FirstArg<TFunction> = TFunction extends ((...args: infer TArgs) => unknown) ? TArgs extends [] ? undefined : TArgs[0] : never;
|
|
12
|
+
type IdGeneratorOptionsById = {
|
|
13
|
+
readonly ulid: FirstArg<typeof ulid$1>;
|
|
14
|
+
readonly nanoid: FirstArg<typeof nanoid$1>;
|
|
15
|
+
readonly uuidv7: FirstArg<typeof uuidv7$1>;
|
|
16
|
+
readonly uuidv4: FirstArg<typeof uuidv4$1>;
|
|
17
|
+
readonly cuid2: FirstArg<typeof cuid2$1>;
|
|
18
|
+
readonly ksuid: FirstArg<typeof ksuid$1>;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/index.d.ts
|
|
22
|
+
type GeneratedColumnSpec = {
|
|
23
|
+
readonly type: ColumnTypeDescriptor;
|
|
24
|
+
readonly nullable?: false;
|
|
25
|
+
readonly typeParams?: Record<string, unknown>;
|
|
26
|
+
readonly generated: ExecutionMutationDefaultValue;
|
|
27
|
+
};
|
|
28
|
+
declare const ulid: (options?: IdGeneratorOptionsById["ulid"]) => GeneratedColumnSpec;
|
|
29
|
+
declare const nanoid: (options?: IdGeneratorOptionsById["nanoid"]) => GeneratedColumnSpec;
|
|
30
|
+
declare const uuidv7: (options?: IdGeneratorOptionsById["uuidv7"]) => GeneratedColumnSpec;
|
|
31
|
+
declare const uuidv4: (options?: IdGeneratorOptionsById["uuidv4"]) => GeneratedColumnSpec;
|
|
32
|
+
declare const cuid2: (options?: IdGeneratorOptionsById["cuid2"]) => GeneratedColumnSpec;
|
|
33
|
+
declare const ksuid: (options?: IdGeneratorOptionsById["ksuid"]) => GeneratedColumnSpec;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { GeneratedColumnSpec, cuid2, ksuid, nanoid, ulid, uuidv4, uuidv7 };
|
|
36
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/generators.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;KAQK,sBAAsB,uFAGrB;KAGM,sBAAA;iBACK,gBAAgB;mBACd,gBAAgB;EAR9B,SAAA,MAAQ,EASM,QATN,CAAA,OASsB,QANxB,CAAA;EAGC,SAAA,MAAA,EAIO,QAJe,CAAA,OAIC,QAJD,CAAA;EACD,SAAA,KAAA,EAIf,QAJe,CAAA,OAIC,OAJD,CAAA;EAAhB,SAAA,KAAA,EAKC,QALD,CAAA,OAKiB,OALjB,CAAA;CACkB;;;KCwBvB,mBAAA;iBACK;;wBAEO;sBACF;;cAqBT,iBAAkB,mCAAiC;ADzD3D,cC2DQ,MD3DA,EAAA,CAAA,OAGF,CAHgB,EC2DM,sBDxDtB,CAAA,QAAA,CAAA,EAAA,GCwDyD,mBDxDzD;AAGC,cCuDC,MDvDD,EAAsB,CAAA,OAAA,CAAA,ECuDD,sBDvDC,CAAA,QAAA,CAAA,EAAA,GCuDkC,mBDvDlC;AACD,cCwDpB,MDxDoB,EAAA,CAAA,OAAA,CAAA,ECwDA,sBDxDA,CAAA,QAAA,CAAA,EAAA,GCwDmC,mBDxDnC;AAAhB,cC0DJ,KD1DI,EAAA,CAAA,OAAA,CAAA,EC0De,sBD1Df,CAAA,OAAA,CAAA,EAAA,GC0DiD,mBD1DjD;AACkB,cC2DtB,KD3DsB,EAAA,CAAA,OAAA,CAAA,EC2DH,sBD3DG,CAAA,OAAA,CAAA,EAAA,GC2D+B,mBD3D/B"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const generatedColumnDescriptors = {
|
|
5
|
+
ulid: {
|
|
6
|
+
type: {
|
|
7
|
+
codecId: "sql/char@1",
|
|
8
|
+
nativeType: "character"
|
|
9
|
+
},
|
|
10
|
+
typeParams: { length: 26 }
|
|
11
|
+
},
|
|
12
|
+
nanoid: {
|
|
13
|
+
type: {
|
|
14
|
+
codecId: "sql/char@1",
|
|
15
|
+
nativeType: "character"
|
|
16
|
+
},
|
|
17
|
+
typeParams: { length: 21 }
|
|
18
|
+
},
|
|
19
|
+
uuidv7: {
|
|
20
|
+
type: {
|
|
21
|
+
codecId: "sql/char@1",
|
|
22
|
+
nativeType: "character"
|
|
23
|
+
},
|
|
24
|
+
typeParams: { length: 36 }
|
|
25
|
+
},
|
|
26
|
+
uuidv4: {
|
|
27
|
+
type: {
|
|
28
|
+
codecId: "sql/char@1",
|
|
29
|
+
nativeType: "character"
|
|
30
|
+
},
|
|
31
|
+
typeParams: { length: 36 }
|
|
32
|
+
},
|
|
33
|
+
cuid2: {
|
|
34
|
+
type: {
|
|
35
|
+
codecId: "sql/char@1",
|
|
36
|
+
nativeType: "character"
|
|
37
|
+
},
|
|
38
|
+
typeParams: { length: 24 }
|
|
39
|
+
},
|
|
40
|
+
ksuid: {
|
|
41
|
+
type: {
|
|
42
|
+
codecId: "sql/char@1",
|
|
43
|
+
nativeType: "character"
|
|
44
|
+
},
|
|
45
|
+
typeParams: { length: 27 }
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
function createGeneratedSpec(id, options) {
|
|
49
|
+
const { type, typeParams } = generatedColumnDescriptors[id];
|
|
50
|
+
const params = options;
|
|
51
|
+
return {
|
|
52
|
+
type,
|
|
53
|
+
nullable: false,
|
|
54
|
+
...ifDefined("typeParams", typeParams),
|
|
55
|
+
generated: {
|
|
56
|
+
kind: "generator",
|
|
57
|
+
id,
|
|
58
|
+
...ifDefined("params", params)
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const ulid = (options) => createGeneratedSpec("ulid", options);
|
|
63
|
+
const nanoid = (options) => createGeneratedSpec("nanoid", options);
|
|
64
|
+
const uuidv7 = (options) => createGeneratedSpec("uuidv7", options);
|
|
65
|
+
const uuidv4 = (options) => createGeneratedSpec("uuidv4", options);
|
|
66
|
+
const cuid2 = (options) => createGeneratedSpec("cuid2", options);
|
|
67
|
+
const ksuid = (options) => createGeneratedSpec("ksuid", options);
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { cuid2, ksuid, nanoid, ulid, uuidv4, uuidv7 };
|
|
71
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["generatedColumnDescriptors: Record<GeneratedValueSpec['id'], GeneratedColumnDescriptor>"],"sources":["../src/index.ts"],"sourcesContent":["import type {\n ExecutionMutationDefaultValue,\n GeneratedValueSpec,\n} from '@prisma-next/contract/types';\nimport type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport type { IdGeneratorOptionsById } from './generators';\n\ntype GeneratedColumnDescriptor = {\n readonly type: ColumnTypeDescriptor;\n readonly typeParams?: Record<string, unknown>;\n};\n\nconst generatedColumnDescriptors: Record<GeneratedValueSpec['id'], GeneratedColumnDescriptor> = {\n ulid: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 26 },\n },\n nanoid: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 21 },\n },\n uuidv7: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 36 },\n },\n uuidv4: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 36 },\n },\n cuid2: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 24 },\n },\n ksuid: {\n type: { codecId: 'sql/char@1', nativeType: 'character' },\n typeParams: { length: 27 },\n },\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 GeneratedValueSpec['id']>(\n id: TId,\n options?: IdGeneratorOptionsById[TId],\n): GeneratedColumnSpec {\n const { type, typeParams } = generatedColumnDescriptors[id];\n const params = options as Record<string, unknown> | undefined;\n return {\n type,\n nullable: false,\n ...ifDefined('typeParams', 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,MAAMA,6BAA0F;CAC9F,MAAM;EACJ,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACD,QAAQ;EACN,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACD,QAAQ;EACN,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACD,QAAQ;EACN,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACD,OAAO;EACL,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACD,OAAO;EACL,MAAM;GAAE,SAAS;GAAc,YAAY;GAAa;EACxD,YAAY,EAAE,QAAQ,IAAI;EAC3B;CACF;AASD,SAAS,oBACP,IACA,SACqB;CACrB,MAAM,EAAE,MAAM,eAAe,2BAA2B;CACxD,MAAM,SAAS;AACf,QAAO;EACL;EACA,UAAU;EACV,GAAG,UAAU,cAAc,WAAW;EACtC,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"sourcesContent":[],"mappings":";;;iBAGgB,UAAA,OAAiB"}
|
package/dist/runtime.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { cuid2 } from "uniku/cuid2";
|
|
2
|
+
import { ksuid } from "uniku/ksuid";
|
|
3
|
+
import { nanoid } from "uniku/nanoid";
|
|
4
|
+
import { ulid } from "uniku/ulid";
|
|
5
|
+
import { uuidv4 } from "uniku/uuid/v4";
|
|
6
|
+
import { uuidv7 } from "uniku/uuid/v7";
|
|
7
|
+
|
|
8
|
+
//#region src/generators.ts
|
|
9
|
+
function invokeGenerator(generator, params) {
|
|
10
|
+
if (params === void 0) return generator();
|
|
11
|
+
return generator(params);
|
|
12
|
+
}
|
|
13
|
+
const idGenerators = {
|
|
14
|
+
ulid: (params) => invokeGenerator(ulid, params),
|
|
15
|
+
nanoid: (params) => invokeGenerator(nanoid, params),
|
|
16
|
+
uuidv7: (params) => invokeGenerator(uuidv7, params),
|
|
17
|
+
uuidv4: (params) => invokeGenerator(uuidv4, params),
|
|
18
|
+
cuid2: (params) => invokeGenerator(cuid2, params),
|
|
19
|
+
ksuid: (params) => invokeGenerator(ksuid, params)
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/runtime.ts
|
|
24
|
+
function generateId(spec) {
|
|
25
|
+
const generator = idGenerators[spec.id];
|
|
26
|
+
return generator(spec.params);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { generateId };
|
|
31
|
+
//# sourceMappingURL=runtime.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.mjs","names":["ulidGenerator","nanoidGenerator","uuidv7Generator","uuidv4Generator","cuid2Generator","ksuidGenerator"],"sources":["../src/generators.ts","../src/runtime.ts"],"sourcesContent":["import type { GeneratedValueSpec } from '@prisma-next/contract/types';\nimport { 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';\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<GeneratedValueSpec['id'], IdGenerator>;\n","import type { GeneratedValueSpec } from '@prisma-next/contract/types';\nimport { idGenerators } from './generators';\n\nexport function generateId(spec: GeneratedValueSpec): string {\n const generator = idGenerators[spec.id];\n return generator(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;;;;ACvCD,SAAgB,WAAW,MAAkC;CAC3D,MAAM,YAAY,aAAa,KAAK;AACpC,QAAO,UAAU,KAAK,OAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma-next/ids",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "ID generator helpers for Prisma Next contracts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"uniku": "^0.0.12",
|
|
9
|
+
"@prisma-next/contract": "0.0.1",
|
|
10
|
+
"@prisma-next/utils": "0.0.1",
|
|
11
|
+
"@prisma-next/contract-authoring": "0.0.1"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"tsdown": "0.18.4",
|
|
15
|
+
"typescript": "5.9.3",
|
|
16
|
+
"vitest": "4.0.17",
|
|
17
|
+
"@prisma-next/tsconfig": "0.0.0",
|
|
18
|
+
"@prisma-next/tsdown": "0.0.0"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./dist/index.mjs",
|
|
26
|
+
"./runtime": "./dist/runtime.mjs",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.mjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
35
|
+
"directory": "packages/1-framework/2-authoring/ids"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:coverage": "vitest run --coverage",
|
|
41
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
42
|
+
"lint": "biome check . --error-on-warnings",
|
|
43
|
+
"lint:fix": "biome check --write .",
|
|
44
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
45
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { GeneratedValueSpec } from '@prisma-next/contract/types';
|
|
2
|
+
import { cuid2 as cuid2Generator } from 'uniku/cuid2';
|
|
3
|
+
import { ksuid as ksuidGenerator } from 'uniku/ksuid';
|
|
4
|
+
import { nanoid as nanoidGenerator } from 'uniku/nanoid';
|
|
5
|
+
import { ulid as ulidGenerator } from 'uniku/ulid';
|
|
6
|
+
import { uuidv4 as uuidv4Generator } from 'uniku/uuid/v4';
|
|
7
|
+
import { uuidv7 as uuidv7Generator } from 'uniku/uuid/v7';
|
|
8
|
+
|
|
9
|
+
type FirstArg<TFunction> = TFunction extends (...args: infer TArgs) => unknown
|
|
10
|
+
? TArgs extends []
|
|
11
|
+
? undefined
|
|
12
|
+
: TArgs[0]
|
|
13
|
+
: never;
|
|
14
|
+
|
|
15
|
+
export type IdGeneratorOptionsById = {
|
|
16
|
+
readonly ulid: FirstArg<typeof ulidGenerator>;
|
|
17
|
+
readonly nanoid: FirstArg<typeof nanoidGenerator>;
|
|
18
|
+
readonly uuidv7: FirstArg<typeof uuidv7Generator>;
|
|
19
|
+
readonly uuidv4: FirstArg<typeof uuidv4Generator>;
|
|
20
|
+
readonly cuid2: FirstArg<typeof cuid2Generator>;
|
|
21
|
+
readonly ksuid: FirstArg<typeof ksuidGenerator>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type IdGenerator = (params?: Record<string, unknown>) => string;
|
|
25
|
+
|
|
26
|
+
function invokeGenerator<TOptions>(
|
|
27
|
+
generator: (options?: TOptions) => string,
|
|
28
|
+
params?: Record<string, unknown>,
|
|
29
|
+
): string {
|
|
30
|
+
if (params === undefined) {
|
|
31
|
+
return generator();
|
|
32
|
+
}
|
|
33
|
+
return generator(params as TOptions);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const idGenerators = {
|
|
37
|
+
ulid: (params?: Record<string, unknown>) => invokeGenerator(ulidGenerator, params),
|
|
38
|
+
nanoid: (params?: Record<string, unknown>) => invokeGenerator(nanoidGenerator, params),
|
|
39
|
+
uuidv7: (params?: Record<string, unknown>) => invokeGenerator(uuidv7Generator, params),
|
|
40
|
+
uuidv4: (params?: Record<string, unknown>) => invokeGenerator(uuidv4Generator, params),
|
|
41
|
+
cuid2: (params?: Record<string, unknown>) => invokeGenerator(cuid2Generator, params),
|
|
42
|
+
ksuid: (params?: Record<string, unknown>) => invokeGenerator(ksuidGenerator, params),
|
|
43
|
+
} as const satisfies Record<GeneratedValueSpec['id'], IdGenerator>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExecutionMutationDefaultValue,
|
|
3
|
+
GeneratedValueSpec,
|
|
4
|
+
} from '@prisma-next/contract/types';
|
|
5
|
+
import type { ColumnTypeDescriptor } from '@prisma-next/contract-authoring';
|
|
6
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
7
|
+
import type { IdGeneratorOptionsById } from './generators';
|
|
8
|
+
|
|
9
|
+
type GeneratedColumnDescriptor = {
|
|
10
|
+
readonly type: ColumnTypeDescriptor;
|
|
11
|
+
readonly typeParams?: Record<string, unknown>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const generatedColumnDescriptors: Record<GeneratedValueSpec['id'], GeneratedColumnDescriptor> = {
|
|
15
|
+
ulid: {
|
|
16
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
17
|
+
typeParams: { length: 26 },
|
|
18
|
+
},
|
|
19
|
+
nanoid: {
|
|
20
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
21
|
+
typeParams: { length: 21 },
|
|
22
|
+
},
|
|
23
|
+
uuidv7: {
|
|
24
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
25
|
+
typeParams: { length: 36 },
|
|
26
|
+
},
|
|
27
|
+
uuidv4: {
|
|
28
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
29
|
+
typeParams: { length: 36 },
|
|
30
|
+
},
|
|
31
|
+
cuid2: {
|
|
32
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
33
|
+
typeParams: { length: 24 },
|
|
34
|
+
},
|
|
35
|
+
ksuid: {
|
|
36
|
+
type: { codecId: 'sql/char@1', nativeType: 'character' },
|
|
37
|
+
typeParams: { length: 27 },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type GeneratedColumnSpec = {
|
|
42
|
+
readonly type: ColumnTypeDescriptor;
|
|
43
|
+
readonly nullable?: false;
|
|
44
|
+
readonly typeParams?: Record<string, unknown>;
|
|
45
|
+
readonly generated: ExecutionMutationDefaultValue;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function createGeneratedSpec<TId extends GeneratedValueSpec['id']>(
|
|
49
|
+
id: TId,
|
|
50
|
+
options?: IdGeneratorOptionsById[TId],
|
|
51
|
+
): GeneratedColumnSpec {
|
|
52
|
+
const { type, typeParams } = generatedColumnDescriptors[id];
|
|
53
|
+
const params = options as Record<string, unknown> | undefined;
|
|
54
|
+
return {
|
|
55
|
+
type,
|
|
56
|
+
nullable: false,
|
|
57
|
+
...ifDefined('typeParams', typeParams),
|
|
58
|
+
generated: {
|
|
59
|
+
kind: 'generator',
|
|
60
|
+
id,
|
|
61
|
+
...ifDefined('params', params),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const ulid = (options?: IdGeneratorOptionsById['ulid']): GeneratedColumnSpec =>
|
|
67
|
+
createGeneratedSpec('ulid', options);
|
|
68
|
+
export const nanoid = (options?: IdGeneratorOptionsById['nanoid']): GeneratedColumnSpec =>
|
|
69
|
+
createGeneratedSpec('nanoid', options);
|
|
70
|
+
export const uuidv7 = (options?: IdGeneratorOptionsById['uuidv7']): GeneratedColumnSpec =>
|
|
71
|
+
createGeneratedSpec('uuidv7', options);
|
|
72
|
+
export const uuidv4 = (options?: IdGeneratorOptionsById['uuidv4']): GeneratedColumnSpec =>
|
|
73
|
+
createGeneratedSpec('uuidv4', options);
|
|
74
|
+
export const cuid2 = (options?: IdGeneratorOptionsById['cuid2']): GeneratedColumnSpec =>
|
|
75
|
+
createGeneratedSpec('cuid2', options);
|
|
76
|
+
export const ksuid = (options?: IdGeneratorOptionsById['ksuid']): GeneratedColumnSpec =>
|
|
77
|
+
createGeneratedSpec('ksuid', options);
|
package/src/runtime.ts
ADDED