@prisma-next/sql-contract-ts 0.3.0-pr.73.2 → 0.3.0-pr.75.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/dist/contract-builder.d.ts +87 -0
- package/dist/contract-builder.d.ts.map +1 -0
- package/dist/contract.d.ts +50 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/exports/contract-builder.d.ts +3 -87
- package/dist/exports/contract-builder.d.ts.map +1 -0
- package/dist/exports/contract.d.ts +2 -51
- package/dist/exports/contract.d.ts.map +1 -0
- package/package.json +12 -11
- package/src/contract-builder.ts +561 -0
- package/src/contract.ts +533 -0
- package/src/exports/contract-builder.ts +2 -0
- package/src/exports/contract.ts +1 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ExtensionPackRef, TargetPackRef } from '@prisma-next/contract/framework-components';
|
|
2
|
+
import type { ColumnBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState } from '@prisma-next/contract-authoring';
|
|
3
|
+
import { type BuildModels, type BuildRelations, type BuildStorageColumn, ContractBuilder, type ExtractColumns, type ExtractPrimaryKey, ModelBuilder, TableBuilder } from '@prisma-next/contract-authoring';
|
|
4
|
+
import type { SqlContract, SqlMappings } from '@prisma-next/sql-contract/types';
|
|
5
|
+
/**
|
|
6
|
+
* Type-level mappings structure for contracts built via `defineContract()`.
|
|
7
|
+
*
|
|
8
|
+
* Compile-time type helper (not a runtime object) that ensures mappings match what the builder
|
|
9
|
+
* produces. `codecTypes` uses the generic `CodecTypes` parameter; `operationTypes` is always
|
|
10
|
+
* empty since operations are added via extensions at runtime.
|
|
11
|
+
*
|
|
12
|
+
* **Difference from RuntimeContext**: This is a compile-time type for contract construction.
|
|
13
|
+
* `RuntimeContext` is a runtime object with populated registries for query execution.
|
|
14
|
+
*
|
|
15
|
+
* @template C - The `CodecTypes` generic parameter passed to `defineContract<CodecTypes>()`
|
|
16
|
+
*/
|
|
17
|
+
type ContractBuilderMappings<C extends Record<string, {
|
|
18
|
+
output: unknown;
|
|
19
|
+
}>> = Omit<SqlMappings, 'codecTypes' | 'operationTypes'> & {
|
|
20
|
+
readonly codecTypes: C;
|
|
21
|
+
readonly operationTypes: Record<string, never>;
|
|
22
|
+
};
|
|
23
|
+
type BuildStorageTable<_TableName extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PK extends readonly string[] | undefined> = {
|
|
24
|
+
readonly columns: {
|
|
25
|
+
readonly [K in keyof Columns]: Columns[K] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
|
|
26
|
+
};
|
|
27
|
+
readonly uniques: ReadonlyArray<never>;
|
|
28
|
+
readonly indexes: ReadonlyArray<never>;
|
|
29
|
+
readonly foreignKeys: ReadonlyArray<never>;
|
|
30
|
+
} & (PK extends readonly string[] ? {
|
|
31
|
+
readonly primaryKey: {
|
|
32
|
+
readonly columns: PK;
|
|
33
|
+
};
|
|
34
|
+
} : Record<string, never>);
|
|
35
|
+
type BuildStorage<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
|
|
36
|
+
readonly tables: {
|
|
37
|
+
readonly [K in keyof Tables]: BuildStorageTable<K & string, ExtractColumns<Tables[K]>, ExtractPrimaryKey<Tables[K]>>;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
|
|
41
|
+
nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
|
|
42
|
+
type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
|
|
43
|
+
build(): ColumnBuilderState<Name, Nullable, Type>;
|
|
44
|
+
}
|
|
45
|
+
declare class SqlContractBuilder<CodecTypes extends Record<string, {
|
|
46
|
+
output: unknown;
|
|
47
|
+
}> = Record<string, never>, Target extends string | undefined = undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, never>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, never>, CoreHash extends string | undefined = undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> extends ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {
|
|
48
|
+
/**
|
|
49
|
+
* This method is responsible for normalizing the contract IR by setting default values
|
|
50
|
+
* for all required fields:
|
|
51
|
+
* - `nullable`: defaults to `false` if not provided
|
|
52
|
+
* - `uniques`: defaults to `[]` (empty array)
|
|
53
|
+
* - `indexes`: defaults to `[]` (empty array)
|
|
54
|
+
* - `foreignKeys`: defaults to `[]` (empty array)
|
|
55
|
+
* - `relations`: defaults to `{}` (empty object) for both model-level and contract-level
|
|
56
|
+
* - `nativeType`: required field set from column type descriptor when columns are defined
|
|
57
|
+
*
|
|
58
|
+
* The contract builder is the **only** place where normalization should occur.
|
|
59
|
+
* Validators, parsers, and emitters should assume the contract is already normalized.
|
|
60
|
+
*
|
|
61
|
+
* **Required**: Use column type descriptors (e.g., `int4Column`, `textColumn`) when defining columns.
|
|
62
|
+
* This ensures `nativeType` is set correctly at build time.
|
|
63
|
+
*
|
|
64
|
+
* @returns A normalized SqlContract with all required fields present
|
|
65
|
+
*/
|
|
66
|
+
build(): Target extends string ? SqlContract<BuildStorage<Tables>, BuildModels<Models>, BuildRelations<Models>, ContractBuilderMappings<CodecTypes>> & {
|
|
67
|
+
readonly schemaVersion: '1';
|
|
68
|
+
readonly target: Target;
|
|
69
|
+
readonly targetFamily: 'sql';
|
|
70
|
+
readonly coreHash: CoreHash extends string ? CoreHash : string;
|
|
71
|
+
} & (ExtensionPacks extends Record<string, unknown> ? {
|
|
72
|
+
readonly extensionPacks: ExtensionPacks;
|
|
73
|
+
} : Record<string, never>) & (Capabilities extends Record<string, Record<string, boolean>> ? {
|
|
74
|
+
readonly capabilities: Capabilities;
|
|
75
|
+
} : Record<string, never>) : never;
|
|
76
|
+
target<T extends string>(packRef: TargetPackRef<'sql', T>): SqlContractBuilder<CodecTypes, T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
77
|
+
extensionPacks(packs: Record<string, ExtensionPackRef<'sql', string>>): SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
78
|
+
capabilities<C extends Record<string, Record<string, boolean>>>(capabilities: C): SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, C>;
|
|
79
|
+
coreHash<H extends string>(hash: H): SqlContractBuilder<CodecTypes, Target, Tables, Models, H, ExtensionPacks, Capabilities>;
|
|
80
|
+
table<TableName extends string, T extends TableBuilder<TableName, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>(name: TableName, callback: (t: TableBuilder<TableName>) => T | undefined): SqlContractBuilder<CodecTypes, Target, Tables & Record<TableName, ReturnType<T['build']>>, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
81
|
+
model<ModelName extends string, TableName extends string, M extends ModelBuilder<ModelName, TableName, Record<string, string>, Record<string, RelationDefinition>>>(name: ModelName, table: TableName, callback: (m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>) => M | undefined): SqlContractBuilder<CodecTypes, Target, Tables, Models & Record<ModelName, ReturnType<M['build']>>, CoreHash, ExtensionPacks, Capabilities>;
|
|
82
|
+
}
|
|
83
|
+
export declare function defineContract<CodecTypes extends Record<string, {
|
|
84
|
+
output: unknown;
|
|
85
|
+
}> = Record<string, never>>(): SqlContractBuilder<CodecTypes>;
|
|
86
|
+
export {};
|
|
87
|
+
//# sourceMappingURL=contract-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract-builder.d.ts","sourceRoot":"","sources":["../src/contract-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MA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AAA;CAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,KAC3E,kBAAkB,CAAC,UAAU,CAAC,CAElC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
2
|
+
/**
|
|
3
|
+
* Computes mapping dictionaries from models and storage structures.
|
|
4
|
+
* Assumes valid input - validation happens separately in validateContractLogic().
|
|
5
|
+
*
|
|
6
|
+
* @param models - Models object from contract
|
|
7
|
+
* @param storage - Storage object from contract
|
|
8
|
+
* @param existingMappings - Existing mappings from contract input (optional)
|
|
9
|
+
* @returns Computed mappings dictionary
|
|
10
|
+
*/
|
|
11
|
+
export declare function computeMappings(models: Record<string, ModelDefinition>, _storage: SqlStorage, existingMappings?: Partial<SqlMappings>): SqlMappings;
|
|
12
|
+
export declare function normalizeContract(contract: unknown): SqlContract<SqlStorage>;
|
|
13
|
+
/**
|
|
14
|
+
* Validates that a JSON import conforms to the SqlContract structure
|
|
15
|
+
* and returns a fully typed SqlContract.
|
|
16
|
+
*
|
|
17
|
+
* This function is specifically for validating JSON imports (e.g., from contract.json).
|
|
18
|
+
* Contracts created via the builder API (defineContract) are already valid and should
|
|
19
|
+
* not be passed to this function - use them directly without validation.
|
|
20
|
+
*
|
|
21
|
+
* Performs both structural validation (using Arktype) and logical validation
|
|
22
|
+
* (ensuring all references are valid).
|
|
23
|
+
*
|
|
24
|
+
*
|
|
25
|
+
* The type parameter `TContract` must be a fully-typed contract type (e.g., from `contract.d.ts`),
|
|
26
|
+
* NOT a generic `SqlContract<SqlStorage>`.
|
|
27
|
+
*
|
|
28
|
+
* **Correct:**
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import type { Contract } from './contract.d';
|
|
31
|
+
* const contract = validateContract<Contract>(contractJson);
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* **Incorrect:**
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
37
|
+
* const contract = validateContract<SqlContract<SqlStorage>>(contractJson);
|
|
38
|
+
* // ❌ Types will be inferred as 'unknown' - this won't work!
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* The type parameter provides the specific table structure, column types, and model definitions.
|
|
42
|
+
* This function validates the runtime structure matches the type, but does not infer types
|
|
43
|
+
* from JSON (as JSON imports lose literal type information).
|
|
44
|
+
*
|
|
45
|
+
* @param value - The contract value to validate (must be from a JSON import, not a builder)
|
|
46
|
+
* @returns A validated contract matching the TContract type
|
|
47
|
+
* @throws Error if the contract structure or logic is invalid
|
|
48
|
+
*/
|
|
49
|
+
export declare function validateContract<TContract extends SqlContract<SqlStorage>>(value: unknown): TContract;
|
|
50
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,eAAe,EAIf,WAAW,EACX,WAAW,EACX,UAAU,EAIX,MAAM,iCAAiC,CAAC;AA2HzC;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,EACvC,QAAQ,EAAE,UAAU,EACpB,gBAAgB,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,GACtC,WAAW,CAiCb;AAyMD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,GAAG,WAAW,CAAC,UAAU,CAAC,CA2E5E;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,SAAS,WAAW,CAAC,UAAU,CAAC,EACxE,KAAK,EAAE,OAAO,GACb,SAAS,CAiCX"}
|
|
@@ -1,87 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Type-level mappings structure for contracts built via `defineContract()`.
|
|
7
|
-
*
|
|
8
|
-
* Compile-time type helper (not a runtime object) that ensures mappings match what the builder
|
|
9
|
-
* produces. `codecTypes` uses the generic `CodecTypes` parameter; `operationTypes` is always
|
|
10
|
-
* empty since operations are added via extensions at runtime.
|
|
11
|
-
*
|
|
12
|
-
* **Difference from RuntimeContext**: This is a compile-time type for contract construction.
|
|
13
|
-
* `RuntimeContext` is a runtime object with populated registries for query execution.
|
|
14
|
-
*
|
|
15
|
-
* @template C - The `CodecTypes` generic parameter passed to `defineContract<CodecTypes>()`
|
|
16
|
-
*/
|
|
17
|
-
type ContractBuilderMappings<C extends Record<string, {
|
|
18
|
-
output: unknown;
|
|
19
|
-
}>> = Omit<SqlMappings, 'codecTypes' | 'operationTypes'> & {
|
|
20
|
-
readonly codecTypes: C;
|
|
21
|
-
readonly operationTypes: Record<string, never>;
|
|
22
|
-
};
|
|
23
|
-
type BuildStorageTable<_TableName extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PK extends readonly string[] | undefined> = {
|
|
24
|
-
readonly columns: {
|
|
25
|
-
readonly [K in keyof Columns]: Columns[K] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
|
|
26
|
-
};
|
|
27
|
-
readonly uniques: ReadonlyArray<never>;
|
|
28
|
-
readonly indexes: ReadonlyArray<never>;
|
|
29
|
-
readonly foreignKeys: ReadonlyArray<never>;
|
|
30
|
-
} & (PK extends readonly string[] ? {
|
|
31
|
-
readonly primaryKey: {
|
|
32
|
-
readonly columns: PK;
|
|
33
|
-
};
|
|
34
|
-
} : Record<string, never>);
|
|
35
|
-
type BuildStorage<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
|
|
36
|
-
readonly tables: {
|
|
37
|
-
readonly [K in keyof Tables]: BuildStorageTable<K & string, ExtractColumns<Tables[K]>, ExtractPrimaryKey<Tables[K]>>;
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
|
|
41
|
-
nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
|
|
42
|
-
type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
|
|
43
|
-
build(): ColumnBuilderState<Name, Nullable, Type>;
|
|
44
|
-
}
|
|
45
|
-
declare class SqlContractBuilder<CodecTypes extends Record<string, {
|
|
46
|
-
output: unknown;
|
|
47
|
-
}> = Record<string, never>, Target extends string | undefined = undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, never>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, never>, CoreHash extends string | undefined = undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> extends ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {
|
|
48
|
-
/**
|
|
49
|
-
* This method is responsible for normalizing the contract IR by setting default values
|
|
50
|
-
* for all required fields:
|
|
51
|
-
* - `nullable`: defaults to `false` if not provided
|
|
52
|
-
* - `uniques`: defaults to `[]` (empty array)
|
|
53
|
-
* - `indexes`: defaults to `[]` (empty array)
|
|
54
|
-
* - `foreignKeys`: defaults to `[]` (empty array)
|
|
55
|
-
* - `relations`: defaults to `{}` (empty object) for both model-level and contract-level
|
|
56
|
-
* - `nativeType`: required field set from column type descriptor when columns are defined
|
|
57
|
-
*
|
|
58
|
-
* The contract builder is the **only** place where normalization should occur.
|
|
59
|
-
* Validators, parsers, and emitters should assume the contract is already normalized.
|
|
60
|
-
*
|
|
61
|
-
* **Required**: Use column type descriptors (e.g., `int4Column`, `textColumn`) when defining columns.
|
|
62
|
-
* This ensures `nativeType` is set correctly at build time.
|
|
63
|
-
*
|
|
64
|
-
* @returns A normalized SqlContract with all required fields present
|
|
65
|
-
*/
|
|
66
|
-
build(): Target extends string ? SqlContract<BuildStorage<Tables>, BuildModels<Models>, BuildRelations<Models>, ContractBuilderMappings<CodecTypes>> & {
|
|
67
|
-
readonly schemaVersion: '1';
|
|
68
|
-
readonly target: Target;
|
|
69
|
-
readonly targetFamily: 'sql';
|
|
70
|
-
readonly coreHash: CoreHash extends string ? CoreHash : string;
|
|
71
|
-
} & (ExtensionPacks extends Record<string, unknown> ? {
|
|
72
|
-
readonly extensionPacks: ExtensionPacks;
|
|
73
|
-
} : Record<string, never>) & (Capabilities extends Record<string, Record<string, boolean>> ? {
|
|
74
|
-
readonly capabilities: Capabilities;
|
|
75
|
-
} : Record<string, never>) : never;
|
|
76
|
-
target<T extends string>(packRef: TargetPackRef<'sql', T>): SqlContractBuilder<CodecTypes, T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
77
|
-
extensionPacks(packs: Record<string, ExtensionPackRef<'sql', string>>): SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
78
|
-
capabilities<C extends Record<string, Record<string, boolean>>>(capabilities: C): SqlContractBuilder<CodecTypes, Target, Tables, Models, CoreHash, ExtensionPacks, C>;
|
|
79
|
-
coreHash<H extends string>(hash: H): SqlContractBuilder<CodecTypes, Target, Tables, Models, H, ExtensionPacks, Capabilities>;
|
|
80
|
-
table<TableName extends string, T extends TableBuilder<TableName, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>(name: TableName, callback: (t: TableBuilder<TableName>) => T | undefined): SqlContractBuilder<CodecTypes, Target, Tables & Record<TableName, ReturnType<T['build']>>, Models, CoreHash, ExtensionPacks, Capabilities>;
|
|
81
|
-
model<ModelName extends string, TableName extends string, M extends ModelBuilder<ModelName, TableName, Record<string, string>, Record<string, RelationDefinition>>>(name: ModelName, table: TableName, callback: (m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>) => M | undefined): SqlContractBuilder<CodecTypes, Target, Tables, Models & Record<ModelName, ReturnType<M['build']>>, CoreHash, ExtensionPacks, Capabilities>;
|
|
82
|
-
}
|
|
83
|
-
declare function defineContract<CodecTypes extends Record<string, {
|
|
84
|
-
output: unknown;
|
|
85
|
-
}> = Record<string, never>>(): SqlContractBuilder<CodecTypes>;
|
|
86
|
-
|
|
87
|
-
export { type ColumnBuilder, defineContract };
|
|
1
|
+
export type { ColumnBuilder } from '../contract-builder';
|
|
2
|
+
export { defineContract } from '../contract-builder';
|
|
3
|
+
//# sourceMappingURL=contract-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract-builder.d.ts","sourceRoot":"","sources":["../../src/exports/contract-builder.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -1,51 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Computes mapping dictionaries from models and storage structures.
|
|
5
|
-
* Assumes valid input - validation happens separately in validateContractLogic().
|
|
6
|
-
*
|
|
7
|
-
* @param models - Models object from contract
|
|
8
|
-
* @param storage - Storage object from contract
|
|
9
|
-
* @param existingMappings - Existing mappings from contract input (optional)
|
|
10
|
-
* @returns Computed mappings dictionary
|
|
11
|
-
*/
|
|
12
|
-
declare function computeMappings(models: Record<string, ModelDefinition>, _storage: SqlStorage, existingMappings?: Partial<SqlMappings>): SqlMappings;
|
|
13
|
-
/**
|
|
14
|
-
* Validates that a JSON import conforms to the SqlContract structure
|
|
15
|
-
* and returns a fully typed SqlContract.
|
|
16
|
-
*
|
|
17
|
-
* This function is specifically for validating JSON imports (e.g., from contract.json).
|
|
18
|
-
* Contracts created via the builder API (defineContract) are already valid and should
|
|
19
|
-
* not be passed to this function - use them directly without validation.
|
|
20
|
-
*
|
|
21
|
-
* Performs both structural validation (using Arktype) and logical validation
|
|
22
|
-
* (ensuring all references are valid).
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* The type parameter `TContract` must be a fully-typed contract type (e.g., from `contract.d.ts`),
|
|
26
|
-
* NOT a generic `SqlContract<SqlStorage>`.
|
|
27
|
-
*
|
|
28
|
-
* **Correct:**
|
|
29
|
-
* ```typescript
|
|
30
|
-
* import type { Contract } from './contract.d';
|
|
31
|
-
* const contract = validateContract<Contract>(contractJson);
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* **Incorrect:**
|
|
35
|
-
* ```typescript
|
|
36
|
-
* import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
37
|
-
* const contract = validateContract<SqlContract<SqlStorage>>(contractJson);
|
|
38
|
-
* // ❌ Types will be inferred as 'unknown' - this won't work!
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* The type parameter provides the specific table structure, column types, and model definitions.
|
|
42
|
-
* This function validates the runtime structure matches the type, but does not infer types
|
|
43
|
-
* from JSON (as JSON imports lose literal type information).
|
|
44
|
-
*
|
|
45
|
-
* @param value - The contract value to validate (must be from a JSON import, not a builder)
|
|
46
|
-
* @returns A validated contract matching the TContract type
|
|
47
|
-
* @throws Error if the contract structure or logic is invalid
|
|
48
|
-
*/
|
|
49
|
-
declare function validateContract<TContract extends SqlContract<SqlStorage>>(value: unknown): TContract;
|
|
50
|
-
|
|
51
|
-
export { computeMappings, validateContract };
|
|
1
|
+
export { computeMappings, validateContract } from '../contract';
|
|
2
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/exports/contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-contract-ts",
|
|
3
|
-
"version": "0.3.0-pr.
|
|
3
|
+
"version": "0.3.0-pr.75.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "SQL-specific TypeScript contract authoring surface for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"arktype": "^2.1.25",
|
|
9
9
|
"ts-toolbelt": "^9.6.0",
|
|
10
|
-
"@prisma-next/contract": "0.3.0-pr.
|
|
11
|
-
"@prisma-next/contract
|
|
12
|
-
"@prisma-next/sql-contract": "0.3.0-pr.
|
|
10
|
+
"@prisma-next/contract-authoring": "0.3.0-pr.75.1",
|
|
11
|
+
"@prisma-next/contract": "0.3.0-pr.75.1",
|
|
12
|
+
"@prisma-next/sql-contract": "0.3.0-pr.75.1"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@types/pg": "
|
|
16
|
-
"@vitest/coverage-v8": "
|
|
17
|
-
"pg": "
|
|
18
|
-
"tsup": "
|
|
19
|
-
"typescript": "
|
|
20
|
-
"vitest": "
|
|
15
|
+
"@types/pg": "8.16.0",
|
|
16
|
+
"@vitest/coverage-v8": "4.0.16",
|
|
17
|
+
"pg": "8.16.3",
|
|
18
|
+
"tsup": "8.5.1",
|
|
19
|
+
"typescript": "5.9.3",
|
|
20
|
+
"vitest": "4.0.16",
|
|
21
21
|
"@prisma-next/test-utils": "0.0.1"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"dist",
|
|
25
|
+
"src",
|
|
25
26
|
"schemas"
|
|
26
27
|
],
|
|
27
28
|
"exports": {
|
|
@@ -36,7 +37,7 @@
|
|
|
36
37
|
"./schema-sql": "./schemas/data-contract-sql-v1.json"
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
|
-
"build": "tsup --config tsup.config.ts",
|
|
40
|
+
"build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json --declaration --declarationMap --emitDeclarationOnly",
|
|
40
41
|
"test": "vitest run",
|
|
41
42
|
"test:coverage": "vitest run --coverage",
|
|
42
43
|
"typecheck": "tsc --project tsconfig.json --noEmit",
|