@prisma-next/sql-contract 0.3.0-dev.3 → 0.3.0-dev.31
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/exports/factories.d.ts +2 -41
- package/dist/exports/factories.d.ts.map +1 -0
- package/dist/exports/pack-types.d.ts +2 -11
- package/dist/exports/pack-types.d.ts.map +1 -0
- package/dist/exports/types.d.ts +2 -70
- package/dist/exports/types.d.ts.map +1 -0
- package/dist/exports/validators.d.ts +2 -38
- package/dist/exports/validators.d.ts.map +1 -0
- package/dist/exports/validators.js +15 -2
- package/dist/exports/validators.js.map +1 -1
- package/dist/factories.d.ts +38 -0
- package/dist/factories.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/pack-types.d.ts +10 -0
- package/dist/pack-types.d.ts.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/validators.d.ts +35 -0
- package/dist/validators.d.ts.map +1 -0
- package/package.json +15 -14
- package/src/exports/factories.ts +11 -0
- package/src/exports/pack-types.ts +1 -0
- package/src/exports/types.ts +18 -0
- package/src/exports/validators.ts +1 -0
- package/src/factories.ts +133 -0
- package/src/index.ts +3 -0
- package/src/pack-types.ts +9 -0
- package/src/types.ts +126 -0
- package/src/validators.ts +173 -0
|
@@ -1,41 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a StorageColumn with nativeType and codecId.
|
|
6
|
-
*
|
|
7
|
-
* @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
|
|
8
|
-
* @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
|
|
9
|
-
* @param nullable - Whether the column is nullable (default: false)
|
|
10
|
-
* @returns StorageColumn with nativeType and codecId
|
|
11
|
-
*/
|
|
12
|
-
declare function col(nativeType: string, codecId: string, nullable?: boolean): StorageColumn;
|
|
13
|
-
declare function pk(...columns: readonly string[]): PrimaryKey;
|
|
14
|
-
declare function unique(...columns: readonly string[]): UniqueConstraint;
|
|
15
|
-
declare function index(...columns: readonly string[]): Index;
|
|
16
|
-
declare function fk(columns: readonly string[], refTable: string, refColumns: readonly string[], name?: string): ForeignKey;
|
|
17
|
-
declare function table(columns: Record<string, StorageColumn>, opts?: {
|
|
18
|
-
pk?: PrimaryKey;
|
|
19
|
-
uniques?: readonly UniqueConstraint[];
|
|
20
|
-
indexes?: readonly Index[];
|
|
21
|
-
fks?: readonly ForeignKey[];
|
|
22
|
-
}): StorageTable;
|
|
23
|
-
declare function model(table: string, fields: Record<string, ModelField>, relations?: Record<string, unknown>): ModelDefinition;
|
|
24
|
-
declare function storage(tables: Record<string, StorageTable>): SqlStorage;
|
|
25
|
-
declare function contract(opts: {
|
|
26
|
-
target: string;
|
|
27
|
-
coreHash: string;
|
|
28
|
-
storage: SqlStorage;
|
|
29
|
-
models?: Record<string, ModelDefinition>;
|
|
30
|
-
relations?: Record<string, unknown>;
|
|
31
|
-
mappings?: Partial<SqlMappings>;
|
|
32
|
-
schemaVersion?: '1';
|
|
33
|
-
targetFamily?: 'sql';
|
|
34
|
-
profileHash?: string;
|
|
35
|
-
capabilities?: Record<string, Record<string, boolean>>;
|
|
36
|
-
extensionPacks?: Record<string, unknown>;
|
|
37
|
-
meta?: Record<string, unknown>;
|
|
38
|
-
sources?: Record<string, unknown>;
|
|
39
|
-
}): SqlContract;
|
|
40
|
-
|
|
41
|
-
export { col, contract, fk, index, model, pk, storage, table, unique };
|
|
1
|
+
export { col, contract, fk, index, model, pk, storage, table, unique, } from '../factories';
|
|
2
|
+
//# sourceMappingURL=factories.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.d.ts","sourceRoot":"","sources":["../../src/exports/factories.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,EAAE,EACF,KAAK,EACL,KAAK,EACL,EAAE,EACF,OAAO,EACP,KAAK,EACL,MAAM,GACP,MAAM,cAAc,CAAC"}
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
interface StorageTypeMetadata {
|
|
5
|
-
readonly typeId: string;
|
|
6
|
-
readonly familyId: string;
|
|
7
|
-
readonly targetId: string;
|
|
8
|
-
readonly nativeType?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type { StorageTypeMetadata };
|
|
1
|
+
export type { StorageTypeMetadata } from '../pack-types';
|
|
2
|
+
//# sourceMappingURL=pack-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pack-types.d.ts","sourceRoot":"","sources":["../../src/exports/pack-types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/exports/types.d.ts
CHANGED
|
@@ -1,70 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type StorageColumn = {
|
|
4
|
-
readonly nativeType: string;
|
|
5
|
-
readonly codecId: string;
|
|
6
|
-
readonly nullable: boolean;
|
|
7
|
-
};
|
|
8
|
-
type PrimaryKey = {
|
|
9
|
-
readonly columns: readonly string[];
|
|
10
|
-
readonly name?: string;
|
|
11
|
-
};
|
|
12
|
-
type UniqueConstraint = {
|
|
13
|
-
readonly columns: readonly string[];
|
|
14
|
-
readonly name?: string;
|
|
15
|
-
};
|
|
16
|
-
type Index = {
|
|
17
|
-
readonly columns: readonly string[];
|
|
18
|
-
readonly name?: string;
|
|
19
|
-
};
|
|
20
|
-
type ForeignKeyReferences = {
|
|
21
|
-
readonly table: string;
|
|
22
|
-
readonly columns: readonly string[];
|
|
23
|
-
};
|
|
24
|
-
type ForeignKey = {
|
|
25
|
-
readonly columns: readonly string[];
|
|
26
|
-
readonly references: ForeignKeyReferences;
|
|
27
|
-
readonly name?: string;
|
|
28
|
-
};
|
|
29
|
-
type StorageTable = {
|
|
30
|
-
readonly columns: Record<string, StorageColumn>;
|
|
31
|
-
readonly primaryKey?: PrimaryKey;
|
|
32
|
-
readonly uniques: ReadonlyArray<UniqueConstraint>;
|
|
33
|
-
readonly indexes: ReadonlyArray<Index>;
|
|
34
|
-
readonly foreignKeys: ReadonlyArray<ForeignKey>;
|
|
35
|
-
};
|
|
36
|
-
type SqlStorage = {
|
|
37
|
-
readonly tables: Record<string, StorageTable>;
|
|
38
|
-
};
|
|
39
|
-
type ModelField = {
|
|
40
|
-
readonly column: string;
|
|
41
|
-
};
|
|
42
|
-
type ModelStorage = {
|
|
43
|
-
readonly table: string;
|
|
44
|
-
};
|
|
45
|
-
type ModelDefinition = {
|
|
46
|
-
readonly storage: ModelStorage;
|
|
47
|
-
readonly fields: Record<string, ModelField>;
|
|
48
|
-
readonly relations: Record<string, unknown>;
|
|
49
|
-
};
|
|
50
|
-
type SqlMappings = {
|
|
51
|
-
readonly modelToTable?: Record<string, string>;
|
|
52
|
-
readonly tableToModel?: Record<string, string>;
|
|
53
|
-
readonly fieldToColumn?: Record<string, Record<string, string>>;
|
|
54
|
-
readonly columnToField?: Record<string, Record<string, string>>;
|
|
55
|
-
readonly codecTypes: Record<string, {
|
|
56
|
-
readonly output: unknown;
|
|
57
|
-
}>;
|
|
58
|
-
readonly operationTypes: Record<string, Record<string, unknown>>;
|
|
59
|
-
};
|
|
60
|
-
type SqlContract<S extends SqlStorage = SqlStorage, M extends Record<string, unknown> = Record<string, unknown>, R extends Record<string, unknown> = Record<string, unknown>, Map extends SqlMappings = SqlMappings> = ContractBase & {
|
|
61
|
-
readonly targetFamily: string;
|
|
62
|
-
readonly storage: S;
|
|
63
|
-
readonly models: M;
|
|
64
|
-
readonly relations: R;
|
|
65
|
-
readonly mappings: Map;
|
|
66
|
-
};
|
|
67
|
-
type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['codecTypes'];
|
|
68
|
-
type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['operationTypes'];
|
|
69
|
-
|
|
70
|
-
export type { ExtractCodecTypes, ExtractOperationTypes, ForeignKey, ForeignKeyReferences, Index, ModelDefinition, ModelField, ModelStorage, PrimaryKey, SqlContract, SqlMappings, SqlStorage, StorageColumn, StorageTable, UniqueConstraint };
|
|
1
|
+
export type { ExtractCodecTypes, ExtractOperationTypes, ForeignKey, ForeignKeyReferences, Index, ModelDefinition, ModelField, ModelStorage, PrimaryKey, SqlContract, SqlMappings, SqlStorage, StorageColumn, StorageTable, StorageTypeInstance, UniqueConstraint, } from '../types';
|
|
2
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/exports/types.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,qBAAqB,EACrB,UAAU,EACV,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,UAAU,EACV,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,aAAa,EACb,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,UAAU,CAAC"}
|
|
@@ -1,38 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Validates the structural shape of SqlStorage using Arktype.
|
|
6
|
-
*
|
|
7
|
-
* @param value - The storage value to validate
|
|
8
|
-
* @returns The validated storage if structure is valid
|
|
9
|
-
* @throws Error if the storage structure is invalid
|
|
10
|
-
*/
|
|
11
|
-
declare function validateStorage(value: unknown): SqlStorage;
|
|
12
|
-
/**
|
|
13
|
-
* Validates the structural shape of ModelDefinition using Arktype.
|
|
14
|
-
*
|
|
15
|
-
* @param value - The model value to validate
|
|
16
|
-
* @returns The validated model if structure is valid
|
|
17
|
-
* @throws Error if the model structure is invalid
|
|
18
|
-
*/
|
|
19
|
-
declare function validateModel(value: unknown): ModelDefinition;
|
|
20
|
-
/**
|
|
21
|
-
* Validates the structural shape of a SqlContract using Arktype.
|
|
22
|
-
*
|
|
23
|
-
* **Responsibility: Validation Only**
|
|
24
|
-
* This function validates that the contract has the correct structure and types.
|
|
25
|
-
* It does NOT normalize the contract - normalization must happen in the contract builder.
|
|
26
|
-
*
|
|
27
|
-
* The contract passed to this function must already be normalized (all required fields present).
|
|
28
|
-
* If normalization is needed, it should be done by the contract builder before calling this function.
|
|
29
|
-
*
|
|
30
|
-
* This ensures all required fields are present and have the correct types.
|
|
31
|
-
*
|
|
32
|
-
* @param value - The contract value to validate (typically from a JSON import)
|
|
33
|
-
* @returns The validated contract if structure is valid
|
|
34
|
-
* @throws Error if the contract structure is invalid
|
|
35
|
-
*/
|
|
36
|
-
declare function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T;
|
|
37
|
-
|
|
38
|
-
export { validateModel, validateSqlContract, validateStorage };
|
|
1
|
+
export { validateModel, validateSqlContract, validateStorage } from '../validators';
|
|
2
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/exports/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -3,7 +3,19 @@ import { type } from "arktype";
|
|
|
3
3
|
var StorageColumnSchema = type.declare().type({
|
|
4
4
|
nativeType: "string",
|
|
5
5
|
codecId: "string",
|
|
6
|
-
nullable: "boolean"
|
|
6
|
+
nullable: "boolean",
|
|
7
|
+
"typeParams?": "Record<string, unknown>",
|
|
8
|
+
"typeRef?": "string"
|
|
9
|
+
}).narrow((col, ctx) => {
|
|
10
|
+
if (col.typeParams !== void 0 && col.typeRef !== void 0) {
|
|
11
|
+
return ctx.mustBe("a column with either typeParams or typeRef, not both");
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
var StorageTypeInstanceSchema = type.declare().type({
|
|
16
|
+
codecId: "string",
|
|
17
|
+
nativeType: "string",
|
|
18
|
+
typeParams: "Record<string, unknown>"
|
|
7
19
|
});
|
|
8
20
|
var PrimaryKeySchema = type.declare().type({
|
|
9
21
|
columns: type.string.array().readonly(),
|
|
@@ -34,7 +46,8 @@ var StorageTableSchema = type.declare().type({
|
|
|
34
46
|
foreignKeys: ForeignKeySchema.array().readonly()
|
|
35
47
|
});
|
|
36
48
|
var StorageSchema = type.declare().type({
|
|
37
|
-
tables: type({ "[string]": StorageTableSchema })
|
|
49
|
+
tables: type({ "[string]": StorageTableSchema }),
|
|
50
|
+
"types?": type({ "[string]": StorageTypeInstanceSchema })
|
|
38
51
|
});
|
|
39
52
|
var ModelFieldSchema = type.declare().type({
|
|
40
53
|
column: "string"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlStorage,\n StorageColumn,\n StorageTable,\n UniqueConstraint,\n} from './types';\n\nconst StorageColumnSchema = type.declare<StorageColumn>().type({\n
|
|
1
|
+
{"version":3,"sources":["../../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlStorage,\n StorageColumn,\n StorageTable,\n StorageTypeInstance,\n UniqueConstraint,\n} from './types';\n\nconst StorageColumnSchema = type\n .declare<StorageColumn>()\n .type({\n nativeType: 'string',\n codecId: 'string',\n nullable: 'boolean',\n 'typeParams?': 'Record<string, unknown>',\n 'typeRef?': 'string',\n })\n .narrow((col, ctx) => {\n if (col.typeParams !== undefined && col.typeRef !== undefined) {\n return ctx.mustBe('a column with either typeParams or typeRef, not both');\n }\n return true;\n });\n\nconst StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({\n codecId: 'string',\n nativeType: 'string',\n typeParams: 'Record<string, unknown>',\n});\n\nconst PrimaryKeySchema = type.declare<PrimaryKey>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst UniqueConstraintSchema = type.declare<UniqueConstraint>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst IndexSchema = type.declare<Index>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({\n table: 'string',\n columns: type.string.array().readonly(),\n});\n\nconst ForeignKeySchema = type.declare<ForeignKey>().type({\n columns: type.string.array().readonly(),\n references: ForeignKeyReferencesSchema,\n 'name?': 'string',\n});\n\nconst StorageTableSchema = type.declare<StorageTable>().type({\n columns: type({ '[string]': StorageColumnSchema }),\n 'primaryKey?': PrimaryKeySchema,\n uniques: UniqueConstraintSchema.array().readonly(),\n indexes: IndexSchema.array().readonly(),\n foreignKeys: ForeignKeySchema.array().readonly(),\n});\n\nconst StorageSchema = type.declare<SqlStorage>().type({\n tables: type({ '[string]': StorageTableSchema }),\n 'types?': type({ '[string]': StorageTypeInstanceSchema }),\n});\n\nconst ModelFieldSchema = type.declare<ModelField>().type({\n column: 'string',\n});\n\nconst ModelStorageSchema = type.declare<ModelStorage>().type({\n table: 'string',\n});\n\nconst ModelSchema = type.declare<ModelDefinition>().type({\n storage: ModelStorageSchema,\n fields: type({ '[string]': ModelFieldSchema }),\n relations: type({ '[string]': 'unknown' }),\n});\n\nconst SqlContractSchema = type({\n 'schemaVersion?': \"'1'\",\n target: 'string',\n targetFamily: \"'sql'\",\n coreHash: 'string',\n 'profileHash?': 'string',\n 'capabilities?': 'Record<string, Record<string, boolean>>',\n 'extensionPacks?': 'Record<string, unknown>',\n 'meta?': 'Record<string, unknown>',\n 'sources?': 'Record<string, unknown>',\n models: type({ '[string]': ModelSchema }),\n storage: StorageSchema,\n});\n\n/**\n * Validates the structural shape of SqlStorage using Arktype.\n *\n * @param value - The storage value to validate\n * @returns The validated storage if structure is valid\n * @throws Error if the storage structure is invalid\n */\nexport function validateStorage(value: unknown): SqlStorage {\n const result = StorageSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Storage validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of ModelDefinition using Arktype.\n *\n * @param value - The model value to validate\n * @returns The validated model if structure is valid\n * @throws Error if the model structure is invalid\n */\nexport function validateModel(value: unknown): ModelDefinition {\n const result = ModelSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Model validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of a SqlContract using Arktype.\n *\n * **Responsibility: Validation Only**\n * This function validates that the contract has the correct structure and types.\n * It does NOT normalize the contract - normalization must happen in the contract builder.\n *\n * The contract passed to this function must already be normalized (all required fields present).\n * If normalization is needed, it should be done by the contract builder before calling this function.\n *\n * This ensures all required fields are present and have the correct types.\n *\n * @param value - The contract value to validate (typically from a JSON import)\n * @returns The validated contract if structure is valid\n * @throws Error if the contract structure is invalid\n */\nexport function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {\n // Check targetFamily first to provide a clear error message for unsupported target families\n const rawValue = value as { targetFamily?: string };\n if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {\n throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);\n }\n\n const contractResult = SqlContractSchema(value);\n\n if (contractResult instanceof type.errors) {\n const messages = contractResult.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Contract structural validation failed: ${messages}`);\n }\n\n // After validation, contractResult matches the schema and preserves the input structure\n // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences\n // between Arktype's inferred type and the generic T, but runtime-wise they're compatible\n return contractResult as T;\n}\n"],"mappings":";A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E,UAAM,IAAI,MAAM,4BAA4B,QAAQ,EAAE;AAAA,EACxD;AACA,SAAO;AACT;AAkBO,SAAS,oBAAuD,OAAmB;AAExF,QAAM,WAAW;AACjB,MAAI,SAAS,iBAAiB,UAAa,SAAS,iBAAiB,OAAO;AAC1E,UAAM,IAAI,MAAM,8BAA8B,SAAS,YAAY,EAAE;AAAA,EACvE;AAEA,QAAM,iBAAiB,kBAAkB,KAAK;AAE9C,MAAI,0BAA0B,KAAK,QAAQ;AACzC,UAAM,WAAW,eAAe,IAAI,CAAC,MAA2B,EAAE,OAAO,EAAE,KAAK,IAAI;AACpF,UAAM,IAAI,MAAM,0CAA0C,QAAQ,EAAE;AAAA,EACtE;AAKA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ForeignKey, Index, ModelDefinition, ModelField, PrimaryKey, SqlContract, SqlMappings, SqlStorage, StorageColumn, StorageTable, UniqueConstraint } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a StorageColumn with nativeType and codecId.
|
|
4
|
+
*
|
|
5
|
+
* @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
|
|
6
|
+
* @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
|
|
7
|
+
* @param nullable - Whether the column is nullable (default: false)
|
|
8
|
+
* @returns StorageColumn with nativeType and codecId
|
|
9
|
+
*/
|
|
10
|
+
export declare function col(nativeType: string, codecId: string, nullable?: boolean): StorageColumn;
|
|
11
|
+
export declare function pk(...columns: readonly string[]): PrimaryKey;
|
|
12
|
+
export declare function unique(...columns: readonly string[]): UniqueConstraint;
|
|
13
|
+
export declare function index(...columns: readonly string[]): Index;
|
|
14
|
+
export declare function fk(columns: readonly string[], refTable: string, refColumns: readonly string[], name?: string): ForeignKey;
|
|
15
|
+
export declare function table(columns: Record<string, StorageColumn>, opts?: {
|
|
16
|
+
pk?: PrimaryKey;
|
|
17
|
+
uniques?: readonly UniqueConstraint[];
|
|
18
|
+
indexes?: readonly Index[];
|
|
19
|
+
fks?: readonly ForeignKey[];
|
|
20
|
+
}): StorageTable;
|
|
21
|
+
export declare function model(table: string, fields: Record<string, ModelField>, relations?: Record<string, unknown>): ModelDefinition;
|
|
22
|
+
export declare function storage(tables: Record<string, StorageTable>): SqlStorage;
|
|
23
|
+
export declare function contract(opts: {
|
|
24
|
+
target: string;
|
|
25
|
+
coreHash: string;
|
|
26
|
+
storage: SqlStorage;
|
|
27
|
+
models?: Record<string, ModelDefinition>;
|
|
28
|
+
relations?: Record<string, unknown>;
|
|
29
|
+
mappings?: Partial<SqlMappings>;
|
|
30
|
+
schemaVersion?: '1';
|
|
31
|
+
targetFamily?: 'sql';
|
|
32
|
+
profileHash?: string;
|
|
33
|
+
capabilities?: Record<string, Record<string, boolean>>;
|
|
34
|
+
extensionPacks?: Record<string, unknown>;
|
|
35
|
+
meta?: Record<string, unknown>;
|
|
36
|
+
sources?: Record<string, unknown>;
|
|
37
|
+
}): SqlContract;
|
|
38
|
+
//# sourceMappingURL=factories.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.d.ts","sourceRoot":"","sources":["../src/factories.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EAEV,KAAK,EACL,eAAe,EACf,UAAU,EAEV,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,aAAa,EACb,YAAY,EACZ,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB;;;;;;;GAOG;AACH,wBAAgB,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,UAAQ,GAAG,aAAa,CAMxF;AAED,wBAAgB,EAAE,CAAC,GAAG,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,UAAU,CAI5D;AAED,wBAAgB,MAAM,CAAC,GAAG,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,gBAAgB,CAItE;AAED,wBAAgB,KAAK,CAAC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C,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACvD,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,GAAG,WAAW,CAgBd"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pack-types.d.ts","sourceRoot":"","sources":["../src/pack-types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { ContractBase } from '@prisma-next/contract/types';
|
|
2
|
+
/**
|
|
3
|
+
* A column definition in storage.
|
|
4
|
+
*
|
|
5
|
+
* `typeParams` is optional because most columns use non-parameterized types.
|
|
6
|
+
* Columns with parameterized types can either inline `typeParams` or reference
|
|
7
|
+
* a named {@link StorageTypeInstance} via `typeRef`.
|
|
8
|
+
*/
|
|
9
|
+
export type StorageColumn = {
|
|
10
|
+
readonly nativeType: string;
|
|
11
|
+
readonly codecId: string;
|
|
12
|
+
readonly nullable: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Opaque, codec-owned JS/type parameters.
|
|
15
|
+
* The codec that owns `codecId` defines the shape and semantics.
|
|
16
|
+
* Mutually exclusive with `typeRef`.
|
|
17
|
+
*/
|
|
18
|
+
readonly typeParams?: Record<string, unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* Reference to a named type instance in `storage.types`.
|
|
21
|
+
* Mutually exclusive with `typeParams`.
|
|
22
|
+
*/
|
|
23
|
+
readonly typeRef?: string;
|
|
24
|
+
};
|
|
25
|
+
export type PrimaryKey = {
|
|
26
|
+
readonly columns: readonly string[];
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
};
|
|
29
|
+
export type UniqueConstraint = {
|
|
30
|
+
readonly columns: readonly string[];
|
|
31
|
+
readonly name?: string;
|
|
32
|
+
};
|
|
33
|
+
export type Index = {
|
|
34
|
+
readonly columns: readonly string[];
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
};
|
|
37
|
+
export type ForeignKeyReferences = {
|
|
38
|
+
readonly table: string;
|
|
39
|
+
readonly columns: readonly string[];
|
|
40
|
+
};
|
|
41
|
+
export type ForeignKey = {
|
|
42
|
+
readonly columns: readonly string[];
|
|
43
|
+
readonly references: ForeignKeyReferences;
|
|
44
|
+
readonly name?: string;
|
|
45
|
+
};
|
|
46
|
+
export type StorageTable = {
|
|
47
|
+
readonly columns: Record<string, StorageColumn>;
|
|
48
|
+
readonly primaryKey?: PrimaryKey;
|
|
49
|
+
readonly uniques: ReadonlyArray<UniqueConstraint>;
|
|
50
|
+
readonly indexes: ReadonlyArray<Index>;
|
|
51
|
+
readonly foreignKeys: ReadonlyArray<ForeignKey>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* A named, parameterized type instance.
|
|
55
|
+
* These are registered in `storage.types` for reuse across columns
|
|
56
|
+
* and to enable ergonomic schema surfaces like `schema.types.MyType`.
|
|
57
|
+
*
|
|
58
|
+
* Unlike {@link StorageColumn}, `typeParams` is required here because
|
|
59
|
+
* `StorageTypeInstance` exists specifically to define reusable parameterized types.
|
|
60
|
+
* A type instance without parameters would be redundant—columns can reference
|
|
61
|
+
* the codec directly via `codecId`.
|
|
62
|
+
*/
|
|
63
|
+
export type StorageTypeInstance = {
|
|
64
|
+
readonly codecId: string;
|
|
65
|
+
readonly nativeType: string;
|
|
66
|
+
readonly typeParams: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
export type SqlStorage = {
|
|
69
|
+
readonly tables: Record<string, StorageTable>;
|
|
70
|
+
/**
|
|
71
|
+
* Named type instances for parameterized/custom types.
|
|
72
|
+
* Columns can reference these via `typeRef`.
|
|
73
|
+
*/
|
|
74
|
+
readonly types?: Record<string, StorageTypeInstance>;
|
|
75
|
+
};
|
|
76
|
+
export type ModelField = {
|
|
77
|
+
readonly column: string;
|
|
78
|
+
};
|
|
79
|
+
export type ModelStorage = {
|
|
80
|
+
readonly table: string;
|
|
81
|
+
};
|
|
82
|
+
export type ModelDefinition = {
|
|
83
|
+
readonly storage: ModelStorage;
|
|
84
|
+
readonly fields: Record<string, ModelField>;
|
|
85
|
+
readonly relations: Record<string, unknown>;
|
|
86
|
+
};
|
|
87
|
+
export type SqlMappings = {
|
|
88
|
+
readonly modelToTable?: Record<string, string>;
|
|
89
|
+
readonly tableToModel?: Record<string, string>;
|
|
90
|
+
readonly fieldToColumn?: Record<string, Record<string, string>>;
|
|
91
|
+
readonly columnToField?: Record<string, Record<string, string>>;
|
|
92
|
+
readonly codecTypes: Record<string, {
|
|
93
|
+
readonly output: unknown;
|
|
94
|
+
}>;
|
|
95
|
+
readonly operationTypes: Record<string, Record<string, unknown>>;
|
|
96
|
+
};
|
|
97
|
+
export type SqlContract<S extends SqlStorage = SqlStorage, M extends Record<string, unknown> = Record<string, unknown>, R extends Record<string, unknown> = Record<string, unknown>, Map extends SqlMappings = SqlMappings> = ContractBase & {
|
|
98
|
+
readonly targetFamily: string;
|
|
99
|
+
readonly storage: S;
|
|
100
|
+
readonly models: M;
|
|
101
|
+
readonly relations: R;
|
|
102
|
+
readonly mappings: Map;
|
|
103
|
+
};
|
|
104
|
+
export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['codecTypes'];
|
|
105
|
+
export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['operationTypes'];
|
|
106
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEhE;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9C;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAA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AE,MAAM,CAAC;IAC9B,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,WAAW,CAAC,UAAU,CAAC,IACrE,SAAS,CAAC,UAAU,CAAC,CAAC,YAAY,CAAC,CAAC;AAEtC,MAAM,MAAM,qBAAqB,CAAC,SAAS,SAAS,WAAW,CAAC,UAAU,CAAC,IACzE,SAAS,CAAC,UAAU,CAAC,CAAC,gBAAgB,CAAC,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ModelDefinition, SqlContract, SqlStorage } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validates the structural shape of SqlStorage using Arktype.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The storage value to validate
|
|
6
|
+
* @returns The validated storage if structure is valid
|
|
7
|
+
* @throws Error if the storage structure is invalid
|
|
8
|
+
*/
|
|
9
|
+
export declare function validateStorage(value: unknown): SqlStorage;
|
|
10
|
+
/**
|
|
11
|
+
* Validates the structural shape of ModelDefinition using Arktype.
|
|
12
|
+
*
|
|
13
|
+
* @param value - The model value to validate
|
|
14
|
+
* @returns The validated model if structure is valid
|
|
15
|
+
* @throws Error if the model structure is invalid
|
|
16
|
+
*/
|
|
17
|
+
export declare function validateModel(value: unknown): ModelDefinition;
|
|
18
|
+
/**
|
|
19
|
+
* Validates the structural shape of a SqlContract using Arktype.
|
|
20
|
+
*
|
|
21
|
+
* **Responsibility: Validation Only**
|
|
22
|
+
* This function validates that the contract has the correct structure and types.
|
|
23
|
+
* It does NOT normalize the contract - normalization must happen in the contract builder.
|
|
24
|
+
*
|
|
25
|
+
* The contract passed to this function must already be normalized (all required fields present).
|
|
26
|
+
* If normalization is needed, it should be done by the contract builder before calling this function.
|
|
27
|
+
*
|
|
28
|
+
* This ensures all required fields are present and have the correct types.
|
|
29
|
+
*
|
|
30
|
+
* @param value - The contract value to validate (typically from a JSON import)
|
|
31
|
+
* @returns The validated contract if structure is valid
|
|
32
|
+
* @throws Error if the contract structure is invalid
|
|
33
|
+
*/
|
|
34
|
+
export declare function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T;
|
|
35
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../src/validators.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIV,eAAe,EAIf,WAAW,EACX,UAAU,EAKX,MAAM,SAAS,CAAC;AA2FjB;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAO1D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CAO7D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,WAAW,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAkBxF"}
|
package/package.json
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.31",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "SQL contract types, validators, and IR factories for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"arktype": "^2.1.25",
|
|
9
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
9
|
+
"@prisma-next/contract": "0.3.0-dev.31"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"@prisma-next/
|
|
12
|
+
"tsup": "8.5.1",
|
|
13
|
+
"typescript": "5.9.3",
|
|
14
|
+
"vitest": "4.0.16",
|
|
15
|
+
"@prisma-next/test-utils": "0.0.1",
|
|
16
|
+
"@prisma-next/tsconfig": "0.0.0"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"dist"
|
|
19
|
+
"dist",
|
|
20
|
+
"src"
|
|
20
21
|
],
|
|
21
22
|
"exports": {
|
|
22
23
|
"./types": {
|
|
@@ -37,13 +38,13 @@
|
|
|
37
38
|
}
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
|
-
"build": "tsup --config tsup.config.ts",
|
|
41
|
+
"build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
|
|
41
42
|
"test": "vitest run",
|
|
42
43
|
"test:coverage": "vitest run --coverage",
|
|
43
|
-
"typecheck": "tsc --
|
|
44
|
-
"lint": "biome check . --
|
|
45
|
-
"lint:fix": "biome check --write .
|
|
46
|
-
"lint:fix:unsafe": "biome check --write --unsafe .
|
|
47
|
-
"clean": "
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"lint": "biome check . --error-on-warnings",
|
|
46
|
+
"lint:fix": "biome check --write .",
|
|
47
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
48
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { StorageTypeMetadata } from '../pack-types';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ExtractCodecTypes,
|
|
3
|
+
ExtractOperationTypes,
|
|
4
|
+
ForeignKey,
|
|
5
|
+
ForeignKeyReferences,
|
|
6
|
+
Index,
|
|
7
|
+
ModelDefinition,
|
|
8
|
+
ModelField,
|
|
9
|
+
ModelStorage,
|
|
10
|
+
PrimaryKey,
|
|
11
|
+
SqlContract,
|
|
12
|
+
SqlMappings,
|
|
13
|
+
SqlStorage,
|
|
14
|
+
StorageColumn,
|
|
15
|
+
StorageTable,
|
|
16
|
+
StorageTypeInstance,
|
|
17
|
+
UniqueConstraint,
|
|
18
|
+
} from '../types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { validateModel, validateSqlContract, validateStorage } from '../validators';
|
package/src/factories.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ForeignKey,
|
|
3
|
+
ForeignKeyReferences,
|
|
4
|
+
Index,
|
|
5
|
+
ModelDefinition,
|
|
6
|
+
ModelField,
|
|
7
|
+
ModelStorage,
|
|
8
|
+
PrimaryKey,
|
|
9
|
+
SqlContract,
|
|
10
|
+
SqlMappings,
|
|
11
|
+
SqlStorage,
|
|
12
|
+
StorageColumn,
|
|
13
|
+
StorageTable,
|
|
14
|
+
UniqueConstraint,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a StorageColumn with nativeType and codecId.
|
|
19
|
+
*
|
|
20
|
+
* @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
|
|
21
|
+
* @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
|
|
22
|
+
* @param nullable - Whether the column is nullable (default: false)
|
|
23
|
+
* @returns StorageColumn with nativeType and codecId
|
|
24
|
+
*/
|
|
25
|
+
export function col(nativeType: string, codecId: string, nullable = false): StorageColumn {
|
|
26
|
+
return {
|
|
27
|
+
nativeType,
|
|
28
|
+
codecId,
|
|
29
|
+
nullable,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function pk(...columns: readonly string[]): PrimaryKey {
|
|
34
|
+
return {
|
|
35
|
+
columns,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function unique(...columns: readonly string[]): UniqueConstraint {
|
|
40
|
+
return {
|
|
41
|
+
columns,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function index(...columns: readonly string[]): Index {
|
|
46
|
+
return {
|
|
47
|
+
columns,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function fk(
|
|
52
|
+
columns: readonly string[],
|
|
53
|
+
refTable: string,
|
|
54
|
+
refColumns: readonly string[],
|
|
55
|
+
name?: string,
|
|
56
|
+
): ForeignKey {
|
|
57
|
+
const references: ForeignKeyReferences = {
|
|
58
|
+
table: refTable,
|
|
59
|
+
columns: refColumns,
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
columns,
|
|
63
|
+
references,
|
|
64
|
+
...(name !== undefined && { name }),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function table(
|
|
69
|
+
columns: Record<string, StorageColumn>,
|
|
70
|
+
opts?: {
|
|
71
|
+
pk?: PrimaryKey;
|
|
72
|
+
uniques?: readonly UniqueConstraint[];
|
|
73
|
+
indexes?: readonly Index[];
|
|
74
|
+
fks?: readonly ForeignKey[];
|
|
75
|
+
},
|
|
76
|
+
): StorageTable {
|
|
77
|
+
return {
|
|
78
|
+
columns,
|
|
79
|
+
...(opts?.pk !== undefined && { primaryKey: opts.pk }),
|
|
80
|
+
uniques: opts?.uniques ?? [],
|
|
81
|
+
indexes: opts?.indexes ?? [],
|
|
82
|
+
foreignKeys: opts?.fks ?? [],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function model(
|
|
87
|
+
table: string,
|
|
88
|
+
fields: Record<string, ModelField>,
|
|
89
|
+
relations: Record<string, unknown> = {},
|
|
90
|
+
): ModelDefinition {
|
|
91
|
+
const storage: ModelStorage = { table };
|
|
92
|
+
return {
|
|
93
|
+
storage,
|
|
94
|
+
fields,
|
|
95
|
+
relations,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function storage(tables: Record<string, StorageTable>): SqlStorage {
|
|
100
|
+
return { tables };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function contract(opts: {
|
|
104
|
+
target: string;
|
|
105
|
+
coreHash: string;
|
|
106
|
+
storage: SqlStorage;
|
|
107
|
+
models?: Record<string, ModelDefinition>;
|
|
108
|
+
relations?: Record<string, unknown>;
|
|
109
|
+
mappings?: Partial<SqlMappings>;
|
|
110
|
+
schemaVersion?: '1';
|
|
111
|
+
targetFamily?: 'sql';
|
|
112
|
+
profileHash?: string;
|
|
113
|
+
capabilities?: Record<string, Record<string, boolean>>;
|
|
114
|
+
extensionPacks?: Record<string, unknown>;
|
|
115
|
+
meta?: Record<string, unknown>;
|
|
116
|
+
sources?: Record<string, unknown>;
|
|
117
|
+
}): SqlContract {
|
|
118
|
+
return {
|
|
119
|
+
schemaVersion: opts.schemaVersion ?? '1',
|
|
120
|
+
target: opts.target,
|
|
121
|
+
targetFamily: opts.targetFamily ?? 'sql',
|
|
122
|
+
coreHash: opts.coreHash,
|
|
123
|
+
storage: opts.storage,
|
|
124
|
+
models: opts.models ?? {},
|
|
125
|
+
relations: opts.relations ?? {},
|
|
126
|
+
mappings: (opts.mappings ?? {}) as SqlMappings,
|
|
127
|
+
...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),
|
|
128
|
+
...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),
|
|
129
|
+
...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),
|
|
130
|
+
...(opts.meta !== undefined && { meta: opts.meta }),
|
|
131
|
+
...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),
|
|
132
|
+
} as SqlContract;
|
|
133
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { ContractBase } from '@prisma-next/contract/types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A column definition in storage.
|
|
5
|
+
*
|
|
6
|
+
* `typeParams` is optional because most columns use non-parameterized types.
|
|
7
|
+
* Columns with parameterized types can either inline `typeParams` or reference
|
|
8
|
+
* a named {@link StorageTypeInstance} via `typeRef`.
|
|
9
|
+
*/
|
|
10
|
+
export type StorageColumn = {
|
|
11
|
+
readonly nativeType: string;
|
|
12
|
+
readonly codecId: string;
|
|
13
|
+
readonly nullable: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Opaque, codec-owned JS/type parameters.
|
|
16
|
+
* The codec that owns `codecId` defines the shape and semantics.
|
|
17
|
+
* Mutually exclusive with `typeRef`.
|
|
18
|
+
*/
|
|
19
|
+
readonly typeParams?: Record<string, unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* Reference to a named type instance in `storage.types`.
|
|
22
|
+
* Mutually exclusive with `typeParams`.
|
|
23
|
+
*/
|
|
24
|
+
readonly typeRef?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type PrimaryKey = {
|
|
28
|
+
readonly columns: readonly string[];
|
|
29
|
+
readonly name?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type UniqueConstraint = {
|
|
33
|
+
readonly columns: readonly string[];
|
|
34
|
+
readonly name?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type Index = {
|
|
38
|
+
readonly columns: readonly string[];
|
|
39
|
+
readonly name?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type ForeignKeyReferences = {
|
|
43
|
+
readonly table: string;
|
|
44
|
+
readonly columns: readonly string[];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type ForeignKey = {
|
|
48
|
+
readonly columns: readonly string[];
|
|
49
|
+
readonly references: ForeignKeyReferences;
|
|
50
|
+
readonly name?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type StorageTable = {
|
|
54
|
+
readonly columns: Record<string, StorageColumn>;
|
|
55
|
+
readonly primaryKey?: PrimaryKey;
|
|
56
|
+
readonly uniques: ReadonlyArray<UniqueConstraint>;
|
|
57
|
+
readonly indexes: ReadonlyArray<Index>;
|
|
58
|
+
readonly foreignKeys: ReadonlyArray<ForeignKey>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A named, parameterized type instance.
|
|
63
|
+
* These are registered in `storage.types` for reuse across columns
|
|
64
|
+
* and to enable ergonomic schema surfaces like `schema.types.MyType`.
|
|
65
|
+
*
|
|
66
|
+
* Unlike {@link StorageColumn}, `typeParams` is required here because
|
|
67
|
+
* `StorageTypeInstance` exists specifically to define reusable parameterized types.
|
|
68
|
+
* A type instance without parameters would be redundant—columns can reference
|
|
69
|
+
* the codec directly via `codecId`.
|
|
70
|
+
*/
|
|
71
|
+
export type StorageTypeInstance = {
|
|
72
|
+
readonly codecId: string;
|
|
73
|
+
readonly nativeType: string;
|
|
74
|
+
readonly typeParams: Record<string, unknown>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type SqlStorage = {
|
|
78
|
+
readonly tables: Record<string, StorageTable>;
|
|
79
|
+
/**
|
|
80
|
+
* Named type instances for parameterized/custom types.
|
|
81
|
+
* Columns can reference these via `typeRef`.
|
|
82
|
+
*/
|
|
83
|
+
readonly types?: Record<string, StorageTypeInstance>;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export type ModelField = {
|
|
87
|
+
readonly column: string;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type ModelStorage = {
|
|
91
|
+
readonly table: string;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type ModelDefinition = {
|
|
95
|
+
readonly storage: ModelStorage;
|
|
96
|
+
readonly fields: Record<string, ModelField>;
|
|
97
|
+
readonly relations: Record<string, unknown>;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type SqlMappings = {
|
|
101
|
+
readonly modelToTable?: Record<string, string>;
|
|
102
|
+
readonly tableToModel?: Record<string, string>;
|
|
103
|
+
readonly fieldToColumn?: Record<string, Record<string, string>>;
|
|
104
|
+
readonly columnToField?: Record<string, Record<string, string>>;
|
|
105
|
+
readonly codecTypes: Record<string, { readonly output: unknown }>;
|
|
106
|
+
readonly operationTypes: Record<string, Record<string, unknown>>;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export type SqlContract<
|
|
110
|
+
S extends SqlStorage = SqlStorage,
|
|
111
|
+
M extends Record<string, unknown> = Record<string, unknown>,
|
|
112
|
+
R extends Record<string, unknown> = Record<string, unknown>,
|
|
113
|
+
Map extends SqlMappings = SqlMappings,
|
|
114
|
+
> = ContractBase & {
|
|
115
|
+
readonly targetFamily: string;
|
|
116
|
+
readonly storage: S;
|
|
117
|
+
readonly models: M;
|
|
118
|
+
readonly relations: R;
|
|
119
|
+
readonly mappings: Map;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =
|
|
123
|
+
TContract['mappings']['codecTypes'];
|
|
124
|
+
|
|
125
|
+
export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =
|
|
126
|
+
TContract['mappings']['operationTypes'];
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type } from 'arktype';
|
|
2
|
+
import type {
|
|
3
|
+
ForeignKey,
|
|
4
|
+
ForeignKeyReferences,
|
|
5
|
+
Index,
|
|
6
|
+
ModelDefinition,
|
|
7
|
+
ModelField,
|
|
8
|
+
ModelStorage,
|
|
9
|
+
PrimaryKey,
|
|
10
|
+
SqlContract,
|
|
11
|
+
SqlStorage,
|
|
12
|
+
StorageColumn,
|
|
13
|
+
StorageTable,
|
|
14
|
+
StorageTypeInstance,
|
|
15
|
+
UniqueConstraint,
|
|
16
|
+
} from './types';
|
|
17
|
+
|
|
18
|
+
const StorageColumnSchema = type
|
|
19
|
+
.declare<StorageColumn>()
|
|
20
|
+
.type({
|
|
21
|
+
nativeType: 'string',
|
|
22
|
+
codecId: 'string',
|
|
23
|
+
nullable: 'boolean',
|
|
24
|
+
'typeParams?': 'Record<string, unknown>',
|
|
25
|
+
'typeRef?': 'string',
|
|
26
|
+
})
|
|
27
|
+
.narrow((col, ctx) => {
|
|
28
|
+
if (col.typeParams !== undefined && col.typeRef !== undefined) {
|
|
29
|
+
return ctx.mustBe('a column with either typeParams or typeRef, not both');
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({
|
|
35
|
+
codecId: 'string',
|
|
36
|
+
nativeType: 'string',
|
|
37
|
+
typeParams: 'Record<string, unknown>',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const PrimaryKeySchema = type.declare<PrimaryKey>().type({
|
|
41
|
+
columns: type.string.array().readonly(),
|
|
42
|
+
'name?': 'string',
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const UniqueConstraintSchema = type.declare<UniqueConstraint>().type({
|
|
46
|
+
columns: type.string.array().readonly(),
|
|
47
|
+
'name?': 'string',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const IndexSchema = type.declare<Index>().type({
|
|
51
|
+
columns: type.string.array().readonly(),
|
|
52
|
+
'name?': 'string',
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({
|
|
56
|
+
table: 'string',
|
|
57
|
+
columns: type.string.array().readonly(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const ForeignKeySchema = type.declare<ForeignKey>().type({
|
|
61
|
+
columns: type.string.array().readonly(),
|
|
62
|
+
references: ForeignKeyReferencesSchema,
|
|
63
|
+
'name?': 'string',
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const StorageTableSchema = type.declare<StorageTable>().type({
|
|
67
|
+
columns: type({ '[string]': StorageColumnSchema }),
|
|
68
|
+
'primaryKey?': PrimaryKeySchema,
|
|
69
|
+
uniques: UniqueConstraintSchema.array().readonly(),
|
|
70
|
+
indexes: IndexSchema.array().readonly(),
|
|
71
|
+
foreignKeys: ForeignKeySchema.array().readonly(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const StorageSchema = type.declare<SqlStorage>().type({
|
|
75
|
+
tables: type({ '[string]': StorageTableSchema }),
|
|
76
|
+
'types?': type({ '[string]': StorageTypeInstanceSchema }),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const ModelFieldSchema = type.declare<ModelField>().type({
|
|
80
|
+
column: 'string',
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const ModelStorageSchema = type.declare<ModelStorage>().type({
|
|
84
|
+
table: 'string',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const ModelSchema = type.declare<ModelDefinition>().type({
|
|
88
|
+
storage: ModelStorageSchema,
|
|
89
|
+
fields: type({ '[string]': ModelFieldSchema }),
|
|
90
|
+
relations: type({ '[string]': 'unknown' }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const SqlContractSchema = type({
|
|
94
|
+
'schemaVersion?': "'1'",
|
|
95
|
+
target: 'string',
|
|
96
|
+
targetFamily: "'sql'",
|
|
97
|
+
coreHash: 'string',
|
|
98
|
+
'profileHash?': 'string',
|
|
99
|
+
'capabilities?': 'Record<string, Record<string, boolean>>',
|
|
100
|
+
'extensionPacks?': 'Record<string, unknown>',
|
|
101
|
+
'meta?': 'Record<string, unknown>',
|
|
102
|
+
'sources?': 'Record<string, unknown>',
|
|
103
|
+
models: type({ '[string]': ModelSchema }),
|
|
104
|
+
storage: StorageSchema,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Validates the structural shape of SqlStorage using Arktype.
|
|
109
|
+
*
|
|
110
|
+
* @param value - The storage value to validate
|
|
111
|
+
* @returns The validated storage if structure is valid
|
|
112
|
+
* @throws Error if the storage structure is invalid
|
|
113
|
+
*/
|
|
114
|
+
export function validateStorage(value: unknown): SqlStorage {
|
|
115
|
+
const result = StorageSchema(value);
|
|
116
|
+
if (result instanceof type.errors) {
|
|
117
|
+
const messages = result.map((p: { message: string }) => p.message).join('; ');
|
|
118
|
+
throw new Error(`Storage validation failed: ${messages}`);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Validates the structural shape of ModelDefinition using Arktype.
|
|
125
|
+
*
|
|
126
|
+
* @param value - The model value to validate
|
|
127
|
+
* @returns The validated model if structure is valid
|
|
128
|
+
* @throws Error if the model structure is invalid
|
|
129
|
+
*/
|
|
130
|
+
export function validateModel(value: unknown): ModelDefinition {
|
|
131
|
+
const result = ModelSchema(value);
|
|
132
|
+
if (result instanceof type.errors) {
|
|
133
|
+
const messages = result.map((p: { message: string }) => p.message).join('; ');
|
|
134
|
+
throw new Error(`Model validation failed: ${messages}`);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Validates the structural shape of a SqlContract using Arktype.
|
|
141
|
+
*
|
|
142
|
+
* **Responsibility: Validation Only**
|
|
143
|
+
* This function validates that the contract has the correct structure and types.
|
|
144
|
+
* It does NOT normalize the contract - normalization must happen in the contract builder.
|
|
145
|
+
*
|
|
146
|
+
* The contract passed to this function must already be normalized (all required fields present).
|
|
147
|
+
* If normalization is needed, it should be done by the contract builder before calling this function.
|
|
148
|
+
*
|
|
149
|
+
* This ensures all required fields are present and have the correct types.
|
|
150
|
+
*
|
|
151
|
+
* @param value - The contract value to validate (typically from a JSON import)
|
|
152
|
+
* @returns The validated contract if structure is valid
|
|
153
|
+
* @throws Error if the contract structure is invalid
|
|
154
|
+
*/
|
|
155
|
+
export function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {
|
|
156
|
+
// Check targetFamily first to provide a clear error message for unsupported target families
|
|
157
|
+
const rawValue = value as { targetFamily?: string };
|
|
158
|
+
if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {
|
|
159
|
+
throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const contractResult = SqlContractSchema(value);
|
|
163
|
+
|
|
164
|
+
if (contractResult instanceof type.errors) {
|
|
165
|
+
const messages = contractResult.map((p: { message: string }) => p.message).join('; ');
|
|
166
|
+
throw new Error(`Contract structural validation failed: ${messages}`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// After validation, contractResult matches the schema and preserves the input structure
|
|
170
|
+
// TypeScript needs an assertion here due to exactOptionalPropertyTypes differences
|
|
171
|
+
// between Arktype's inferred type and the generic T, but runtime-wise they're compatible
|
|
172
|
+
return contractResult as T;
|
|
173
|
+
}
|