@prisma-next/sql-contract-emitter 0.3.0-dev.7 → 0.3.0-dev.71
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/LICENSE +201 -0
- package/dist/index.d.mts +42 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +315 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -17
- package/src/index.ts +249 -47
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -379
- package/dist/index.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2026] [Prisma Data, Inc]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ContractIR } from "@prisma-next/contract/ir";
|
|
2
|
+
import { GenerateContractTypesOptions, TypeRenderContext, TypeRenderEntry, TypesImportSpec, ValidationContext } from "@prisma-next/contract/types";
|
|
3
|
+
import { ModelDefinition, SqlStorage, StorageColumn } from "@prisma-next/sql-contract/types";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
declare const sqlTargetFamilyHook: {
|
|
7
|
+
readonly id: "sql";
|
|
8
|
+
readonly validateTypes: (ir: ContractIR, _ctx: ValidationContext) => void;
|
|
9
|
+
readonly validateStructure: (ir: ContractIR) => void;
|
|
10
|
+
readonly generateContractTypes: (ir: ContractIR, codecTypeImports: ReadonlyArray<TypesImportSpec>, operationTypeImports: ReadonlyArray<TypesImportSpec>, hashes: {
|
|
11
|
+
readonly storageHash: string;
|
|
12
|
+
readonly executionHash?: string;
|
|
13
|
+
readonly profileHash: string;
|
|
14
|
+
}, options?: GenerateContractTypesOptions) => string;
|
|
15
|
+
readonly generateStorageType: (storage: SqlStorage) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Generates the TypeScript type for storage.types with literal types.
|
|
18
|
+
* This preserves type params as literal values for precise typing.
|
|
19
|
+
*/
|
|
20
|
+
readonly generateStorageTypesType: (types: SqlStorage["types"]) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Serializes a typeParams object to a TypeScript literal type.
|
|
23
|
+
* Converts { length: 1536 } to "{ readonly length: 1536 }".
|
|
24
|
+
*/
|
|
25
|
+
readonly serializeTypeParamsLiteral: (params: Record<string, unknown>) => string;
|
|
26
|
+
/**
|
|
27
|
+
* Serializes a value to a TypeScript literal type expression.
|
|
28
|
+
*/
|
|
29
|
+
readonly serializeValue: (value: unknown) => string;
|
|
30
|
+
readonly generateModelsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage, parameterizedRenderers?: Map<string, TypeRenderEntry>) => string;
|
|
31
|
+
/**
|
|
32
|
+
* Generates the TypeScript type expression for a column.
|
|
33
|
+
* Uses parameterized renderer if the column has typeParams and a matching renderer exists,
|
|
34
|
+
* otherwise falls back to CodecTypes[codecId]['output'].
|
|
35
|
+
*/
|
|
36
|
+
readonly generateColumnType: (column: StorageColumn, storage: SqlStorage, parameterizedRenderers: Map<string, TypeRenderEntry> | undefined, renderCtx: TypeRenderContext) => string;
|
|
37
|
+
readonly generateRelationsType: (relations: Record<string, unknown> | undefined) => string;
|
|
38
|
+
readonly generateMappingsType: (models: Record<string, ModelDefinition> | undefined, storage: SqlStorage, codecTypes: string, operationTypes: string) => string;
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { sqlTargetFamilyHook };
|
|
42
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;cAwCa;;EAAA,SAAA,aAsoBH,EAAA,CAAA,EAAA,EAnoBU,UAmoBV,EAAA,IAAA,EAnoB4B,iBAmoB5B,EAAA,GAAA,IAAA;EAnoBU,SAAA,iBAAA,EAAA,CAAA,EAAA,EA6BI,UA7BJ,EAAA,GAAA,IAAA;EAAkB,SAAA,qBAAA,EAAA,CAAA,EAAA,EAsL9B,UAtL8B,EAAA,gBAAA,EAuLhB,aAvLgB,CAuLF,eAvLE,CAAA,EAAA,oBAAA,EAwLZ,aAxLY,CAwLE,eAxLF,CAAA,EAAA,MAAA,EAAA;IA6Bd,SAAA,WAAA,EAAA,MAAA;IAyJhB,SAAA,aAAA,CAAA,EAAA,MAAA;IAC4B,SAAA,WAAA,EAAA,MAAA;EAAd,CAAA,EAAA,OAAA,CAAA,EAOR,4BAPQ,EAAA,GAAA,MAAA;EACkB,SAAA,mBAAA,EAAA,CAAA,OAAA,EA0GT,UA1GS,EAAA,GAAA,MAAA;EAAd;;;;EAsMW,SAAA,wBAAA,EAAA,CAAA,KAAA,EAtBH,UAsBG,CAAA,OAAA,CAAA,EAAA,GAAA,MAAA;EAkDV;;;;EAEE,SAAA,0BAAA,EAAA,CAAA,MAAA,EApDQ,MAoDR,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,GAAA,MAAA;EAuEjB;;;EAEgB,SAAA,cAAA,EAAA,CAAA,KAAA,EAAA,OAAA,EAAA,GAAA,MAAA;EACb,SAAA,kBAAA,EAAA,CAAA,MAAA,EA5EH,MA4EG,CAAA,MAAA,EA5EY,eA4EZ,CAAA,GAAA,SAAA,EAAA,OAAA,EA3EF,UA2EE,EAAA,sBAAA,CAAA,EA1Ec,GA0Ed,CAAA,MAAA,EA1E0B,eA0E1B,CAAA,EAAA,GAAA,MAAA;EAWoB;;;;;wCAdvB,wBACC,oCACe,YAAY,yCACzB;8CAWoB;0CAkEvB,eAAe,uCACd"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { assertDefined } from "@prisma-next/utils/assertions";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the typeParams for a column, either from inline typeParams or from typeRef.
|
|
6
|
+
* Returns undefined if no typeParams are available.
|
|
7
|
+
*/
|
|
8
|
+
function resolveColumnTypeParams(column, storage) {
|
|
9
|
+
if (column.typeParams && Object.keys(column.typeParams).length > 0) return column.typeParams;
|
|
10
|
+
if (column.typeRef && storage.types) {
|
|
11
|
+
const typeInstance = storage.types[column.typeRef];
|
|
12
|
+
if (typeInstance?.typeParams) return typeInstance.typeParams;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const sqlTargetFamilyHook = {
|
|
16
|
+
id: "sql",
|
|
17
|
+
validateTypes(ir, _ctx) {
|
|
18
|
+
const storage = ir.storage;
|
|
19
|
+
if (!storage || !storage.tables) return;
|
|
20
|
+
const typeIdRegex = /^([^/]+)\/([^@]+)@(\d+)$/;
|
|
21
|
+
for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {
|
|
22
|
+
const table = tableUnknown;
|
|
23
|
+
for (const [colName, colUnknown] of Object.entries(table.columns)) {
|
|
24
|
+
const codecId = colUnknown.codecId;
|
|
25
|
+
if (!codecId) throw new Error(`Column "${colName}" in table "${tableName}" is missing codecId`);
|
|
26
|
+
const match = codecId.match(typeIdRegex);
|
|
27
|
+
if (!match || !match[1]) throw new Error(`Column "${colName}" in table "${tableName}" has invalid codec ID format "${codecId}". Expected format: ns/name@version`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
validateStructure(ir) {
|
|
32
|
+
if (ir.targetFamily !== "sql") throw new Error(`Expected targetFamily "sql", got "${ir.targetFamily}"`);
|
|
33
|
+
const storage = ir.storage;
|
|
34
|
+
if (!storage || !storage.tables) throw new Error("SQL contract must have storage.tables");
|
|
35
|
+
const models = ir.models;
|
|
36
|
+
const tableNames = new Set(Object.keys(storage.tables));
|
|
37
|
+
if (models) for (const [modelName, modelUnknown] of Object.entries(models)) {
|
|
38
|
+
const model = modelUnknown;
|
|
39
|
+
if (!model.storage?.table) throw new Error(`Model "${modelName}" is missing storage.table`);
|
|
40
|
+
const tableName = model.storage.table;
|
|
41
|
+
if (!tableNames.has(tableName)) throw new Error(`Model "${modelName}" references non-existent table "${tableName}"`);
|
|
42
|
+
const table = storage.tables[tableName];
|
|
43
|
+
assertDefined(table, `Model "${modelName}" references non-existent table "${tableName}"`);
|
|
44
|
+
if (!table.primaryKey) throw new Error(`Model "${modelName}" table "${tableName}" is missing a primary key`);
|
|
45
|
+
const columnNames = new Set(Object.keys(table.columns));
|
|
46
|
+
if (!model.fields || Object.keys(model.fields).length === 0) throw new Error(`Model "${modelName}" is missing fields`);
|
|
47
|
+
for (const [fieldName, fieldUnknown] of Object.entries(model.fields)) {
|
|
48
|
+
const field = fieldUnknown;
|
|
49
|
+
if (!field.column) throw new Error(`Model "${modelName}" field "${fieldName}" is missing column property`);
|
|
50
|
+
if (!columnNames.has(field.column)) throw new Error(`Model "${modelName}" field "${fieldName}" references non-existent column "${field.column}" in table "${tableName}"`);
|
|
51
|
+
}
|
|
52
|
+
if (!model.relations || typeof model.relations !== "object") throw new Error(`Model "${modelName}" is missing required field "relations" (must be an object)`);
|
|
53
|
+
}
|
|
54
|
+
for (const [tableName, tableUnknown] of Object.entries(storage.tables)) {
|
|
55
|
+
const table = tableUnknown;
|
|
56
|
+
const columnNames = new Set(Object.keys(table.columns));
|
|
57
|
+
if (!Array.isArray(table.uniques)) throw new Error(`Table "${tableName}" is missing required field "uniques" (must be an array)`);
|
|
58
|
+
if (!Array.isArray(table.indexes)) throw new Error(`Table "${tableName}" is missing required field "indexes" (must be an array)`);
|
|
59
|
+
if (!Array.isArray(table.foreignKeys)) throw new Error(`Table "${tableName}" is missing required field "foreignKeys" (must be an array)`);
|
|
60
|
+
if (table.primaryKey) {
|
|
61
|
+
for (const colName of table.primaryKey.columns) if (!columnNames.has(colName)) throw new Error(`Table "${tableName}" primaryKey references non-existent column "${colName}"`);
|
|
62
|
+
}
|
|
63
|
+
for (const unique of table.uniques) for (const colName of unique.columns) if (!columnNames.has(colName)) throw new Error(`Table "${tableName}" unique constraint references non-existent column "${colName}"`);
|
|
64
|
+
for (const index of table.indexes) for (const colName of index.columns) if (!columnNames.has(colName)) throw new Error(`Table "${tableName}" index references non-existent column "${colName}"`);
|
|
65
|
+
for (const fk of table.foreignKeys) {
|
|
66
|
+
for (const colName of fk.columns) if (!columnNames.has(colName)) throw new Error(`Table "${tableName}" foreignKey references non-existent column "${colName}"`);
|
|
67
|
+
if (!tableNames.has(fk.references.table)) throw new Error(`Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`);
|
|
68
|
+
const referencedTable = storage.tables[fk.references.table];
|
|
69
|
+
assertDefined(referencedTable, `Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`);
|
|
70
|
+
const referencedColumnNames = new Set(Object.keys(referencedTable.columns));
|
|
71
|
+
for (const colName of fk.references.columns) if (!referencedColumnNames.has(colName)) throw new Error(`Table "${tableName}" foreignKey references non-existent column "${colName}" in table "${fk.references.table}"`);
|
|
72
|
+
if (fk.columns.length !== fk.references.columns.length) throw new Error(`Table "${tableName}" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
generateContractTypes(ir, codecTypeImports, operationTypeImports, hashes, options) {
|
|
77
|
+
const parameterizedRenderers = options?.parameterizedRenderers;
|
|
78
|
+
const parameterizedTypeImports = options?.parameterizedTypeImports;
|
|
79
|
+
const storage = ir.storage;
|
|
80
|
+
const models = ir.models;
|
|
81
|
+
const allImports = [...codecTypeImports, ...operationTypeImports];
|
|
82
|
+
if (parameterizedTypeImports) allImports.push(...parameterizedTypeImports);
|
|
83
|
+
const seenImportKeys = /* @__PURE__ */ new Set();
|
|
84
|
+
const uniqueImports = [];
|
|
85
|
+
for (const imp of allImports) {
|
|
86
|
+
const key = `${imp.package}::${imp.named}`;
|
|
87
|
+
if (!seenImportKeys.has(key)) {
|
|
88
|
+
seenImportKeys.add(key);
|
|
89
|
+
uniqueImports.push(imp);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const importLines = uniqueImports.map((imp) => {
|
|
93
|
+
return `import type { ${imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`} } from '${imp.package}';`;
|
|
94
|
+
});
|
|
95
|
+
const codecTypes = codecTypeImports.filter((imp) => imp.named === "CodecTypes").map((imp) => imp.alias).join(" & ");
|
|
96
|
+
const operationTypes = operationTypeImports.filter((imp) => imp.named === "OperationTypes").map((imp) => imp.alias).join(" & ");
|
|
97
|
+
const storageType = this.generateStorageType(storage);
|
|
98
|
+
const modelsType = this.generateModelsType(models, storage, parameterizedRenderers);
|
|
99
|
+
const relationsType = this.generateRelationsType(ir.relations);
|
|
100
|
+
const mappingsType = this.generateMappingsType(models, storage, codecTypes, operationTypes);
|
|
101
|
+
const executionHashType = hashes.executionHash ? `ExecutionHashBase<'${hashes.executionHash}'>` : "ExecutionHashBase<string>";
|
|
102
|
+
return `// ⚠️ GENERATED FILE - DO NOT EDIT
|
|
103
|
+
// This file is automatically generated by 'prisma-next contract emit'.
|
|
104
|
+
// To regenerate, run: prisma-next contract emit
|
|
105
|
+
${importLines.join("\n")}
|
|
106
|
+
|
|
107
|
+
import type { ExecutionHashBase, ProfileHashBase, StorageHashBase } from '@prisma-next/contract/types';
|
|
108
|
+
import type { SqlContract, SqlStorage, SqlMappings, ModelDefinition } from '@prisma-next/sql-contract/types';
|
|
109
|
+
|
|
110
|
+
export type StorageHash = StorageHashBase<'${hashes.storageHash}'>;
|
|
111
|
+
export type ExecutionHash = ${executionHashType};
|
|
112
|
+
export type ProfileHash = ProfileHashBase<'${hashes.profileHash}'>;
|
|
113
|
+
|
|
114
|
+
export type CodecTypes = ${codecTypes || "Record<string, never>"};
|
|
115
|
+
export type LaneCodecTypes = CodecTypes;
|
|
116
|
+
export type OperationTypes = ${operationTypes || "Record<string, never>"};
|
|
117
|
+
type DefaultLiteralValue<CodecId extends string, Encoded> =
|
|
118
|
+
CodecId extends keyof CodecTypes
|
|
119
|
+
? CodecTypes[CodecId] extends { readonly output: infer O }
|
|
120
|
+
? O extends Date | bigint ? O : Encoded
|
|
121
|
+
: Encoded
|
|
122
|
+
: Encoded;
|
|
123
|
+
|
|
124
|
+
export type Contract = SqlContract<
|
|
125
|
+
${storageType},
|
|
126
|
+
${modelsType},
|
|
127
|
+
${relationsType},
|
|
128
|
+
${mappingsType},
|
|
129
|
+
StorageHash,
|
|
130
|
+
ExecutionHash,
|
|
131
|
+
ProfileHash
|
|
132
|
+
>;
|
|
133
|
+
|
|
134
|
+
export type Tables = Contract['storage']['tables'];
|
|
135
|
+
export type Models = Contract['models'];
|
|
136
|
+
export type Relations = Contract['relations'];
|
|
137
|
+
`;
|
|
138
|
+
},
|
|
139
|
+
generateStorageType(storage) {
|
|
140
|
+
const tables = [];
|
|
141
|
+
for (const [tableName, table] of Object.entries(storage.tables)) {
|
|
142
|
+
const columns = [];
|
|
143
|
+
for (const [colName, col] of Object.entries(table.columns)) {
|
|
144
|
+
const nullable = col.nullable ? "true" : "false";
|
|
145
|
+
const nativeType = `'${col.nativeType}'`;
|
|
146
|
+
const codecId = `'${col.codecId}'`;
|
|
147
|
+
const defaultSpec = col.default ? col.default.kind === "literal" ? `; readonly default: { readonly kind: 'literal'; readonly value: DefaultLiteralValue<${codecId}, ${this.serializeValue(col.default.value)}> }` : `; readonly default: { readonly kind: 'function'; readonly expression: ${this.serializeValue(col.default.expression)} }` : "";
|
|
148
|
+
columns.push(`readonly ${colName}: { readonly nativeType: ${nativeType}; readonly codecId: ${codecId}; readonly nullable: ${nullable}${defaultSpec} }`);
|
|
149
|
+
}
|
|
150
|
+
const tableParts = [`columns: { ${columns.join("; ")} }`];
|
|
151
|
+
if (table.primaryKey) {
|
|
152
|
+
const pkCols = table.primaryKey.columns.map((c) => `'${c}'`).join(", ");
|
|
153
|
+
const pkName = table.primaryKey.name ? `; readonly name: '${table.primaryKey.name}'` : "";
|
|
154
|
+
tableParts.push(`primaryKey: { readonly columns: readonly [${pkCols}]${pkName} }`);
|
|
155
|
+
}
|
|
156
|
+
const uniques = table.uniques.map((u) => {
|
|
157
|
+
return `{ readonly columns: readonly [${u.columns.map((c) => `'${c}'`).join(", ")}]${u.name ? `; readonly name: '${u.name}'` : ""} }`;
|
|
158
|
+
}).join(", ");
|
|
159
|
+
tableParts.push(`uniques: readonly [${uniques}]`);
|
|
160
|
+
const indexes = table.indexes.map((i) => {
|
|
161
|
+
return `{ readonly columns: readonly [${i.columns.map((c) => `'${c}'`).join(", ")}]${i.name ? `; readonly name: '${i.name}'` : ""} }`;
|
|
162
|
+
}).join(", ");
|
|
163
|
+
tableParts.push(`indexes: readonly [${indexes}]`);
|
|
164
|
+
const fks = table.foreignKeys.map((fk) => {
|
|
165
|
+
const cols = fk.columns.map((c) => `'${c}'`).join(", ");
|
|
166
|
+
const refCols = fk.references.columns.map((c) => `'${c}'`).join(", ");
|
|
167
|
+
const name = fk.name ? `; readonly name: '${fk.name}'` : "";
|
|
168
|
+
return `{ readonly columns: readonly [${cols}]; readonly references: { readonly table: '${fk.references.table}'; readonly columns: readonly [${refCols}] }${name}; readonly constraint: ${fk.constraint}; readonly index: ${fk.index} }`;
|
|
169
|
+
}).join(", ");
|
|
170
|
+
tableParts.push(`foreignKeys: readonly [${fks}]`);
|
|
171
|
+
tables.push(`readonly ${tableName}: { ${tableParts.join("; ")} }`);
|
|
172
|
+
}
|
|
173
|
+
const typesType = this.generateStorageTypesType(storage.types);
|
|
174
|
+
return `{ readonly tables: { ${tables.join("; ")} }; readonly types: ${typesType} }`;
|
|
175
|
+
},
|
|
176
|
+
generateStorageTypesType(types) {
|
|
177
|
+
if (!types || Object.keys(types).length === 0) return "Record<string, never>";
|
|
178
|
+
const typeEntries = [];
|
|
179
|
+
for (const [typeName, typeInstance] of Object.entries(types)) {
|
|
180
|
+
const codecId = `'${typeInstance.codecId}'`;
|
|
181
|
+
const nativeType = `'${typeInstance.nativeType}'`;
|
|
182
|
+
const typeParamsStr = this.serializeTypeParamsLiteral(typeInstance.typeParams);
|
|
183
|
+
typeEntries.push(`readonly ${typeName}: { readonly codecId: ${codecId}; readonly nativeType: ${nativeType}; readonly typeParams: ${typeParamsStr} }`);
|
|
184
|
+
}
|
|
185
|
+
return `{ ${typeEntries.join("; ")} }`;
|
|
186
|
+
},
|
|
187
|
+
serializeTypeParamsLiteral(params) {
|
|
188
|
+
if (!params || Object.keys(params).length === 0) return "Record<string, never>";
|
|
189
|
+
const entries = [];
|
|
190
|
+
for (const [key, value] of Object.entries(params)) {
|
|
191
|
+
const serialized = this.serializeValue(value);
|
|
192
|
+
entries.push(`readonly ${key}: ${serialized}`);
|
|
193
|
+
}
|
|
194
|
+
return `{ ${entries.join("; ")} }`;
|
|
195
|
+
},
|
|
196
|
+
serializeValue(value) {
|
|
197
|
+
if (value === null) return "null";
|
|
198
|
+
if (value === void 0) return "undefined";
|
|
199
|
+
if (typeof value === "string") return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
200
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
201
|
+
if (typeof value === "bigint") return `${value}n`;
|
|
202
|
+
if (Array.isArray(value)) return `readonly [${value.map((v) => this.serializeValue(v)).join(", ")}]`;
|
|
203
|
+
if (typeof value === "object") {
|
|
204
|
+
const entries = [];
|
|
205
|
+
for (const [k, v] of Object.entries(value)) entries.push(`readonly ${k}: ${this.serializeValue(v)}`);
|
|
206
|
+
return `{ ${entries.join("; ")} }`;
|
|
207
|
+
}
|
|
208
|
+
return "unknown";
|
|
209
|
+
},
|
|
210
|
+
generateModelsType(models, storage, parameterizedRenderers) {
|
|
211
|
+
if (!models) return "Record<string, never>";
|
|
212
|
+
const renderCtx = { codecTypesName: "CodecTypes" };
|
|
213
|
+
const modelTypes = [];
|
|
214
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
215
|
+
const fields = [];
|
|
216
|
+
const tableName = model.storage.table;
|
|
217
|
+
const table = storage.tables[tableName];
|
|
218
|
+
if (table) for (const [fieldName, field] of Object.entries(model.fields)) {
|
|
219
|
+
const column = table.columns[field.column];
|
|
220
|
+
if (!column) {
|
|
221
|
+
fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const jsType = this.generateColumnType(column, storage, parameterizedRenderers, renderCtx);
|
|
225
|
+
fields.push(`readonly ${fieldName}: ${jsType}`);
|
|
226
|
+
}
|
|
227
|
+
else for (const [fieldName, field] of Object.entries(model.fields)) fields.push(`readonly ${fieldName}: { readonly column: '${field.column}' }`);
|
|
228
|
+
const relations = [];
|
|
229
|
+
for (const [relName, rel] of Object.entries(model.relations)) if (typeof rel === "object" && rel !== null && "on" in rel) {
|
|
230
|
+
const on = rel.on;
|
|
231
|
+
if (on.parentCols && on.childCols) {
|
|
232
|
+
const parentCols = on.parentCols.map((c) => `'${c}'`).join(", ");
|
|
233
|
+
const childCols = on.childCols.map((c) => `'${c}'`).join(", ");
|
|
234
|
+
relations.push(`readonly ${relName}: { readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] } }`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const modelParts = [`storage: { readonly table: '${tableName}' }`, `fields: { ${fields.join("; ")} }`];
|
|
238
|
+
if (relations.length > 0) modelParts.push(`relations: { ${relations.join("; ")} }`);
|
|
239
|
+
modelTypes.push(`readonly ${modelName}: { ${modelParts.join("; ")} }`);
|
|
240
|
+
}
|
|
241
|
+
return `{ ${modelTypes.join("; ")} }`;
|
|
242
|
+
},
|
|
243
|
+
generateColumnType(column, storage, parameterizedRenderers, renderCtx) {
|
|
244
|
+
const typeParams = resolveColumnTypeParams(column, storage);
|
|
245
|
+
const nullable = column.nullable ?? false;
|
|
246
|
+
const fallbackType = `CodecTypes['${column.codecId}']['output']`;
|
|
247
|
+
const renderer = typeParams && parameterizedRenderers?.get(column.codecId);
|
|
248
|
+
const baseType = renderer ? renderer.render(typeParams, renderCtx) : fallbackType;
|
|
249
|
+
return nullable ? `${baseType} | null` : baseType;
|
|
250
|
+
},
|
|
251
|
+
generateRelationsType(relations) {
|
|
252
|
+
if (!relations || Object.keys(relations).length === 0) return "Record<string, never>";
|
|
253
|
+
const tableEntries = [];
|
|
254
|
+
for (const [tableName, relsValue] of Object.entries(relations)) {
|
|
255
|
+
if (typeof relsValue !== "object" || relsValue === null) continue;
|
|
256
|
+
const rels = relsValue;
|
|
257
|
+
const relationEntries = [];
|
|
258
|
+
for (const [relName, relValue] of Object.entries(rels)) {
|
|
259
|
+
if (typeof relValue !== "object" || relValue === null) {
|
|
260
|
+
relationEntries.push(`readonly ${relName}: unknown`);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const { to, cardinality, on, through } = relValue;
|
|
264
|
+
const parts = [];
|
|
265
|
+
if (to) parts.push(`readonly to: '${to}'`);
|
|
266
|
+
if (cardinality) parts.push(`readonly cardinality: '${cardinality}'`);
|
|
267
|
+
if (on?.parentCols && on.childCols) {
|
|
268
|
+
const parentCols = on.parentCols.map((c) => `'${c}'`).join(", ");
|
|
269
|
+
const childCols = on.childCols.map((c) => `'${c}'`).join(", ");
|
|
270
|
+
parts.push(`readonly on: { readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`);
|
|
271
|
+
}
|
|
272
|
+
if (through) {
|
|
273
|
+
const parentCols = through.parentCols.map((c) => `'${c}'`).join(", ");
|
|
274
|
+
const childCols = through.childCols.map((c) => `'${c}'`).join(", ");
|
|
275
|
+
parts.push(`readonly through: { readonly table: '${through.table}'; readonly parentCols: readonly [${parentCols}]; readonly childCols: readonly [${childCols}] }`);
|
|
276
|
+
}
|
|
277
|
+
relationEntries.push(parts.length > 0 ? `readonly ${relName}: { ${parts.join("; ")} }` : `readonly ${relName}: unknown`);
|
|
278
|
+
}
|
|
279
|
+
tableEntries.push(`readonly ${tableName}: { ${relationEntries.join("; ")} }`);
|
|
280
|
+
}
|
|
281
|
+
return `{ ${tableEntries.join("; ")} }`;
|
|
282
|
+
},
|
|
283
|
+
generateMappingsType(models, storage, codecTypes, operationTypes) {
|
|
284
|
+
if (!models) return `SqlMappings & { readonly codecTypes: ${codecTypes || "Record<string, never>"}; readonly operationTypes: ${operationTypes || "Record<string, never>"}; }`;
|
|
285
|
+
const modelToTable = [];
|
|
286
|
+
const tableToModel = [];
|
|
287
|
+
const fieldToColumn = [];
|
|
288
|
+
const columnToField = [];
|
|
289
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
290
|
+
const tableName = model.storage.table;
|
|
291
|
+
modelToTable.push(`readonly ${modelName}: '${tableName}'`);
|
|
292
|
+
tableToModel.push(`readonly ${tableName}: '${modelName}'`);
|
|
293
|
+
const fieldMap = [];
|
|
294
|
+
for (const [fieldName, field] of Object.entries(model.fields)) fieldMap.push(`readonly ${fieldName}: '${field.column}'`);
|
|
295
|
+
if (fieldMap.length > 0) fieldToColumn.push(`readonly ${modelName}: { ${fieldMap.join("; ")} }`);
|
|
296
|
+
if (storage.tables[tableName]) {
|
|
297
|
+
const colMap = [];
|
|
298
|
+
for (const [fieldName, field] of Object.entries(model.fields)) colMap.push(`readonly ${field.column}: '${fieldName}'`);
|
|
299
|
+
if (colMap.length > 0) columnToField.push(`readonly ${tableName}: { ${colMap.join("; ")} }`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const parts = [];
|
|
303
|
+
if (modelToTable.length > 0) parts.push(`modelToTable: { ${modelToTable.join("; ")} }`);
|
|
304
|
+
if (tableToModel.length > 0) parts.push(`tableToModel: { ${tableToModel.join("; ")} }`);
|
|
305
|
+
if (fieldToColumn.length > 0) parts.push(`fieldToColumn: { ${fieldToColumn.join("; ")} }`);
|
|
306
|
+
if (columnToField.length > 0) parts.push(`columnToField: { ${columnToField.join("; ")} }`);
|
|
307
|
+
parts.push(`codecTypes: ${codecTypes || "Record<string, never>"}`);
|
|
308
|
+
parts.push(`operationTypes: ${operationTypes || "Record<string, never>"}`);
|
|
309
|
+
return `{ ${parts.join("; ")} }`;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
//#endregion
|
|
314
|
+
export { sqlTargetFamilyHook };
|
|
315
|
+
//# sourceMappingURL=index.mjs.map
|