@prisma-next/sql-contract-ts 0.3.0-pr.99.5 → 0.3.0
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/README.md +206 -73
- package/dist/config-types.d.mts +8 -0
- package/dist/config-types.d.mts.map +1 -0
- package/dist/config-types.mjs +14 -0
- package/dist/config-types.mjs.map +1 -0
- package/dist/contract-builder.d.mts +769 -0
- package/dist/contract-builder.d.mts.map +1 -0
- package/dist/contract-builder.mjs +1288 -0
- package/dist/contract-builder.mjs.map +1 -0
- package/package.json +20 -17
- package/schemas/data-contract-sql-v1.json +189 -23
- package/src/authoring-helper-runtime.ts +139 -0
- package/src/authoring-type-utils.ts +168 -0
- package/src/build-contract.ts +463 -0
- package/src/composed-authoring-helpers.ts +256 -0
- package/src/config-types.ts +11 -0
- package/src/contract-builder.ts +232 -551
- package/src/contract-definition.ts +103 -0
- package/src/contract-dsl.ts +1492 -0
- package/src/contract-lowering.ts +703 -0
- package/src/contract-types.ts +534 -0
- package/src/contract-warnings.ts +242 -0
- package/src/exports/config-types.ts +2 -0
- package/src/exports/contract-builder.ts +23 -2
- package/dist/chunk-HTNUNGA2.js +0 -346
- package/dist/chunk-HTNUNGA2.js.map +0 -1
- package/dist/contract-builder.d.ts +0 -101
- package/dist/contract-builder.d.ts.map +0 -1
- package/dist/contract.d.ts +0 -50
- package/dist/contract.d.ts.map +0 -1
- package/dist/exports/contract-builder.d.ts +0 -3
- package/dist/exports/contract-builder.d.ts.map +0 -1
- package/dist/exports/contract-builder.js +0 -231
- package/dist/exports/contract-builder.js.map +0 -1
- package/dist/exports/contract.d.ts +0 -2
- package/dist/exports/contract.d.ts.map +0 -1
- package/dist/exports/contract.js +0 -9
- package/dist/exports/contract.js.map +0 -1
- package/src/contract.ts +0 -582
- package/src/exports/contract.ts +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/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @prisma-next/sql-contract-ts
|
|
2
2
|
|
|
3
|
-
**Status:**
|
|
3
|
+
**Status:** Current SQL TypeScript contract authoring surface
|
|
4
4
|
|
|
5
|
-
This package
|
|
5
|
+
This package owns the SQL TypeScript authoring API for Prisma Next.
|
|
6
6
|
|
|
7
7
|
## Package Classification
|
|
8
8
|
|
|
@@ -10,122 +10,255 @@ This package contains the SQL-specific TypeScript contract authoring surface for
|
|
|
10
10
|
- **Layer**: authoring
|
|
11
11
|
- **Plane**: migration
|
|
12
12
|
|
|
13
|
-
**Note**: SQL authoring may depend on SQL core layer (e.g., `@prisma-next/sql-contract/types`) within the same domain.
|
|
14
|
-
|
|
15
13
|
## Overview
|
|
16
14
|
|
|
17
15
|
This package is part of the SQL family namespace (`packages/2-sql/2-authoring/contract-ts`) and provides:
|
|
18
|
-
|
|
19
|
-
- SQL contract
|
|
20
|
-
-
|
|
16
|
+
|
|
17
|
+
- the SQL contract DSL centered on `defineContract(...)`
|
|
18
|
+
- the base structural helpers exported from `./contract-builder`: `field.column(...)`, `field.generated(...)`, `field.namedType(...)`, plus `model(...)` and `rel.*`
|
|
19
|
+
- an optional callback overload that exposes pack-composed helper namespaces such as `field.id.uuidv7()`, `field.text()`, `field.createdAt()`, and `type.enum(...)`
|
|
20
|
+
- lowering from authored model definitions into the canonical SQL `Contract`
|
|
21
21
|
|
|
22
22
|
## Responsibilities
|
|
23
23
|
|
|
24
|
-
- **SQL
|
|
25
|
-
- **
|
|
26
|
-
- **
|
|
27
|
-
- **
|
|
24
|
+
- **SQL contract authoring**: Build SQL contracts programmatically with type safety
|
|
25
|
+
- **Pack-composed helper vocabulary**: Merge family, target, and extension authoring contributions into the callback helper namespaces
|
|
26
|
+
- **Lowering pipeline**: Turn authored model definitions into the canonical SQL contract artifacts consumed by the rest of the stack
|
|
27
|
+
- **Config helper**: Provide `typescriptContract(...)` for `prisma-next.config.ts`
|
|
28
|
+
- **Schema validation**: Contract JSON validation integration via `@prisma-next/sql-contract/validate`
|
|
28
29
|
|
|
29
30
|
## Package Status
|
|
30
31
|
|
|
31
|
-
This
|
|
32
|
+
This is the current SQL TypeScript authoring implementation. Shared descriptor types live in `@prisma-next/contract-authoring`. Contract validation lives in `@prisma-next/sql-contract/validate`.
|
|
32
33
|
|
|
33
34
|
## Architecture
|
|
34
35
|
|
|
35
|
-
- **
|
|
36
|
-
- **
|
|
37
|
-
- **SQL
|
|
38
|
-
- **
|
|
36
|
+
- **Base DSL**: `./contract-builder` exports the stable structural DSL (`defineContract`, `field`, `model`, `rel`)
|
|
37
|
+
- **Composed helper namespaces**: `defineContract(config, (helpers) => ...)` synthesizes `helpers.field.*` and `helpers.type.*` from the selected family, target, and extension packs
|
|
38
|
+
- **SQL resolution and contract generation**: internal resolution normalizes names, relations, indexes, and FK materialization before producing the canonical SQL contract artifacts
|
|
39
|
+
- **Shared descriptor layer**: `@prisma-next/contract-authoring` provides the target-neutral descriptor types used by the DSL and by authoring-adjacent packs
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
Contributor-facing lowering notes and detailed warning semantics live in [DEVELOPING.md](./DEVELOPING.md).
|
|
42
|
+
|
|
43
|
+
```mermaid
|
|
44
|
+
flowchart LR
|
|
45
|
+
builderInput[TypeScript contract input] --> sqlContractTs[@prisma-next/sql-contract-ts]
|
|
46
|
+
sqlContractTs --> authoringCore[@prisma-next/contract-authoring]
|
|
47
|
+
sqlContractTs --> sqlTypes[@prisma-next/sql-contract/types]
|
|
48
|
+
sqlContractTs --> contract[SQL Contract]
|
|
49
|
+
```
|
|
43
50
|
|
|
44
51
|
## Exports
|
|
45
52
|
|
|
46
|
-
- `./contract-builder` -
|
|
47
|
-
- `./
|
|
48
|
-
- `./schema-sql` - SQL contract JSON schema (`data-contract-sql-v1.json`)
|
|
53
|
+
- `./contract-builder` - SQL contract DSL (`defineContract`, `field`, `model`, `rel`)
|
|
54
|
+
- `./config-types` - `typescriptContract(...)` config helper
|
|
49
55
|
|
|
50
56
|
## Usage
|
|
51
57
|
|
|
52
|
-
###
|
|
58
|
+
### Direct Structural DSL
|
|
59
|
+
|
|
60
|
+
Direct imports expose the base structural helpers. Use this surface when you want to author with explicit column descriptors, explicit generators, or named storage types.
|
|
61
|
+
|
|
62
|
+
Built-in ID helpers from `@prisma-next/ids` already return the generated-field spec accepted by `field.generated(...)`, so `field.generated(uuidv4())` is a valid structural DSL call.
|
|
53
63
|
|
|
54
64
|
```typescript
|
|
55
|
-
import {
|
|
56
|
-
import
|
|
65
|
+
import { textColumn, timestamptzColumn } from '@prisma-next/adapter-postgres/column-types';
|
|
66
|
+
import sqlFamily from '@prisma-next/family-sql/pack';
|
|
67
|
+
import { uuidv4 } from '@prisma-next/ids';
|
|
68
|
+
import { defineContract, field, model, rel } from '@prisma-next/sql-contract-ts/contract-builder';
|
|
57
69
|
import postgresPack from '@prisma-next/target-postgres/pack';
|
|
70
|
+
|
|
71
|
+
const User = model('User', {
|
|
72
|
+
fields: {
|
|
73
|
+
id: field.generated(uuidv4()).id(),
|
|
74
|
+
email: field.column(textColumn).unique(),
|
|
75
|
+
createdAt: field.column(timestamptzColumn).defaultSql('now()'),
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
.relations({
|
|
79
|
+
posts: rel.hasMany('Post', { by: 'userId' }),
|
|
80
|
+
})
|
|
81
|
+
.sql({
|
|
82
|
+
table: 'app_user',
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const Post = model('Post', {
|
|
86
|
+
fields: {
|
|
87
|
+
id: field.generated(uuidv4()).id(),
|
|
88
|
+
userId: field.column(textColumn),
|
|
89
|
+
title: field.column(textColumn),
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
.relations({
|
|
93
|
+
user: rel.belongsTo(User, { from: 'userId', to: 'id' }),
|
|
94
|
+
})
|
|
95
|
+
.sql({
|
|
96
|
+
table: 'blog_post',
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export const contract = defineContract({
|
|
100
|
+
family: sqlFamily,
|
|
101
|
+
target: postgresPack,
|
|
102
|
+
naming: { tables: 'snake_case', columns: 'snake_case' },
|
|
103
|
+
models: {
|
|
104
|
+
User,
|
|
105
|
+
Post,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Callback Helper Vocabulary
|
|
111
|
+
|
|
112
|
+
Pack-provided helper presets are available through the callback overload. This is the surface that exposes `field.id.*`, `field.text()`, `field.createdAt()`, `type.sql.String(...)`, and extension helpers such as `type.pgvector.Vector(...)`.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
58
115
|
import pgvector from '@prisma-next/extension-pgvector/pack';
|
|
59
|
-
import
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
116
|
+
import sqlFamily from '@prisma-next/family-sql/pack';
|
|
117
|
+
import { defineContract } from '@prisma-next/sql-contract-ts/contract-builder';
|
|
118
|
+
import postgresPack from '@prisma-next/target-postgres/pack';
|
|
119
|
+
|
|
120
|
+
export const contract = defineContract(
|
|
121
|
+
{
|
|
122
|
+
family: sqlFamily,
|
|
123
|
+
target: postgresPack,
|
|
124
|
+
extensionPacks: { pgvector },
|
|
125
|
+
},
|
|
126
|
+
({ type, field, model, rel }) => {
|
|
127
|
+
const types = {
|
|
128
|
+
ShortName: type.sql.String(35),
|
|
129
|
+
Role: type.enum('role', ['USER', 'ADMIN'] as const),
|
|
130
|
+
Embedding1536: type.pgvector.Vector(1536),
|
|
131
|
+
} as const;
|
|
132
|
+
|
|
133
|
+
const User = model('User', {
|
|
134
|
+
fields: {
|
|
135
|
+
id: field.id.uuidv7().sql({ id: { name: 'user_pkey' } }),
|
|
136
|
+
shortName: field.namedType(types.ShortName),
|
|
137
|
+
role: field.namedType(types.Role),
|
|
138
|
+
embedding: field.namedType(types.Embedding1536).optional(),
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const Post = model('Post', {
|
|
143
|
+
fields: {
|
|
144
|
+
id: field.id.uuidv7(),
|
|
145
|
+
authorId: field.uuid(),
|
|
146
|
+
title: field.text(),
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
types,
|
|
152
|
+
models: {
|
|
153
|
+
User: User.relations({
|
|
154
|
+
posts: rel.hasMany(Post, { by: 'authorId' }),
|
|
155
|
+
}).sql({
|
|
156
|
+
table: 'user',
|
|
157
|
+
}),
|
|
158
|
+
Post: Post.relations({
|
|
159
|
+
author: rel.belongsTo(User, { from: 'authorId', to: 'id' }),
|
|
160
|
+
}).sql({
|
|
161
|
+
table: 'post',
|
|
162
|
+
}),
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
);
|
|
83
167
|
```
|
|
84
168
|
|
|
85
|
-
|
|
169
|
+
### Constraint Placement
|
|
86
170
|
|
|
87
|
-
|
|
171
|
+
Single-field constraints are usually most readable inline on the field, while compound constraints live in `.attributes(...)` or model-level `.sql(...)`.
|
|
88
172
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
173
|
+
```typescript
|
|
174
|
+
const Membership = model('Membership', {
|
|
175
|
+
fields: {
|
|
176
|
+
orgId: field.column(textColumn).column('org_id'),
|
|
177
|
+
userId: field.column(textColumn).column('user_id'),
|
|
178
|
+
role: field.column(textColumn),
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
.attributes(({ fields, constraints }) => ({
|
|
182
|
+
id: constraints.id([fields.orgId, fields.userId], { name: 'membership_pkey' }),
|
|
183
|
+
uniques: [
|
|
184
|
+
constraints.unique([fields.orgId, fields.role], {
|
|
185
|
+
name: 'membership_org_role_key',
|
|
186
|
+
}),
|
|
187
|
+
],
|
|
188
|
+
}))
|
|
189
|
+
.sql({ table: 'membership' });
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Helper Notes
|
|
193
|
+
|
|
194
|
+
- Structural helpers: `field.column(...)`, `field.generated(...)`, `field.namedType(...)`, plus `model(...)` and `rel.*`
|
|
195
|
+
- Callback helper presets: `field.id.uuidv4()`, `field.id.uuidv7()`, `field.id.nanoid({ size })`, `field.uuid()`, `field.text()`, `field.timestamp()`, `field.createdAt()`, and `type.*`
|
|
196
|
+
- Keep field-local and FK-local storage overrides next to the authoring site with `field.sql(...)` and `rel.belongsTo(...).sql({ fk })`
|
|
197
|
+
- Prefer typed local refs such as `field.namedType(types.Role)`, `User.refs.id`, and `User.ref('id')` when those tokens are available
|
|
198
|
+
- See [API.md](./API.md) for generated-field spec semantics, validation rules, and typed-reference warning behavior
|
|
199
|
+
|
|
200
|
+
### Foreign Key Defaults
|
|
201
|
+
|
|
202
|
+
Use the root-level `foreignKeyDefaults` option when a contract wants non-default FK materialization:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
const contract = defineContract({
|
|
206
|
+
family: sqlFamily,
|
|
207
|
+
target: postgresPack,
|
|
208
|
+
foreignKeyDefaults: { constraint: true, index: false },
|
|
209
|
+
models: {
|
|
210
|
+
// ...
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Per-FK overrides still live next to the FK authoring site, either via `constraints.foreignKey(...)` inside model `.sql(...)` or via `rel.belongsTo(...).sql({ fk: ... })`. See [ADR 161](../../../../docs/architecture%20docs/adrs/ADR%20161%20-%20Explicit%20foreign%20key%20constraint%20and%20index%20configuration.md).
|
|
95
216
|
|
|
96
217
|
### Validating Contracts
|
|
97
218
|
|
|
219
|
+
Contract JSON validation lives in `@prisma-next/sql-contract/validate`, while this package focuses on authoring and lowering.
|
|
220
|
+
|
|
98
221
|
```typescript
|
|
99
|
-
import { validateContract } from '@prisma-next/sql-contract
|
|
100
|
-
import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
222
|
+
import { validateContract } from '@prisma-next/sql-contract/validate';
|
|
101
223
|
import type { Contract } from './contract.d';
|
|
102
224
|
|
|
103
|
-
// From JSON import
|
|
104
225
|
const contract = validateContract<Contract>(contractJson);
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Config Helper
|
|
105
229
|
|
|
106
|
-
|
|
107
|
-
|
|
230
|
+
Use `typescriptContract` from this package when wiring TS-authored contracts in `prisma-next.config.ts`.
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import { defineConfig } from '@prisma-next/cli/config-types';
|
|
234
|
+
import { typescriptContract } from '@prisma-next/sql-contract-ts/config-types';
|
|
235
|
+
import { contract } from './src/prisma/contract';
|
|
236
|
+
|
|
237
|
+
export default defineConfig({
|
|
238
|
+
contract: typescriptContract(contract, 'src/prisma/contract.json'),
|
|
239
|
+
});
|
|
108
240
|
```
|
|
109
241
|
|
|
110
242
|
## Dependencies
|
|
111
243
|
|
|
112
|
-
- **`@prisma-next/
|
|
113
|
-
- **`@prisma-next/contract`** -
|
|
114
|
-
- **`@prisma-next/
|
|
115
|
-
-
|
|
116
|
-
- **`ts-toolbelt`** - Type utilities
|
|
244
|
+
- **`@prisma-next/config`** - `ContractConfig` types used by `typescriptContract(...)`
|
|
245
|
+
- **`@prisma-next/contract-authoring`** - Shared descriptor types
|
|
246
|
+
- **`@prisma-next/framework-components`** - Pack refs, authoring contributions, and codec lookup types
|
|
247
|
+
- **`@prisma-next/sql-contract`** - SQL contract types and validation target
|
|
117
248
|
|
|
118
249
|
## Testing
|
|
119
250
|
|
|
120
|
-
|
|
251
|
+
Unit tests for the authoring DSL live in this package. Broader integration tests that span authoring, emission, CLI, and runtime packages live in `@prisma-next/integration-tests`.
|
|
121
252
|
|
|
122
253
|
## Migration Notes
|
|
123
254
|
|
|
124
|
-
-
|
|
125
|
-
-
|
|
126
|
-
-
|
|
255
|
+
- Direct imports give you the structural DSL
|
|
256
|
+
- The callback overload gives you pack-composed helper vocabularies
|
|
257
|
+
- Import authoring helpers directly from `@prisma-next/sql-contract-ts`
|
|
258
|
+
- Import validation from `@prisma-next/sql-contract/validate`
|
|
127
259
|
|
|
128
260
|
## See Also
|
|
129
261
|
|
|
130
|
-
- `@prisma-next/contract-authoring` -
|
|
131
|
-
|
|
262
|
+
- `@prisma-next/contract-authoring` - Shared target-neutral authoring descriptor types
|
|
263
|
+
- `@prisma-next/sql-contract-psl` - PSL parser-output to SQL contract interpreter
|
|
264
|
+
- `@prisma-next/sql-contract-psl/provider` - SQL PSL-first `prismaContract()` helper
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Contract } from "@prisma-next/contract/types";
|
|
2
|
+
import { ContractConfig, ContractConfig as ContractConfig$1 } from "@prisma-next/config/config-types";
|
|
3
|
+
|
|
4
|
+
//#region src/config-types.d.ts
|
|
5
|
+
declare function typescriptContract(contract: Contract, output?: string): ContractConfig$1;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { type ContractConfig, typescriptContract };
|
|
8
|
+
//# sourceMappingURL=config-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-types.d.mts","names":[],"sources":["../src/config-types.ts"],"sourcesContent":[],"mappings":";;;;iBAKgB,kBAAA,WAA6B,4BAA4B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
2
|
+
import { ok } from "@prisma-next/utils/result";
|
|
3
|
+
|
|
4
|
+
//#region src/config-types.ts
|
|
5
|
+
function typescriptContract(contract, output) {
|
|
6
|
+
return {
|
|
7
|
+
source: async (_context) => ok(contract),
|
|
8
|
+
...ifDefined("output", output)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { typescriptContract };
|
|
14
|
+
//# sourceMappingURL=config-types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-types.mjs","names":[],"sources":["../src/config-types.ts"],"sourcesContent":["import type { ContractConfig } from '@prisma-next/config/config-types';\nimport type { Contract } from '@prisma-next/contract/types';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { ok } from '@prisma-next/utils/result';\n\nexport function typescriptContract(contract: Contract, output?: string): ContractConfig {\n return {\n source: async (_context) => ok(contract),\n ...ifDefined('output', output),\n };\n}\n"],"mappings":";;;;AAKA,SAAgB,mBAAmB,UAAoB,QAAiC;AACtF,QAAO;EACL,QAAQ,OAAO,aAAa,GAAG,SAAS;EACxC,GAAG,UAAU,UAAU,OAAO;EAC/B"}
|