prisma-zero 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/generator.cjs +47 -27
- package/dist/generator.js +47 -27
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -93,6 +93,14 @@ generator zero {
|
|
|
93
93
|
// Optional list of Prisma Model names you want to exclude from the generated schema
|
|
94
94
|
// This does *not* change tables that replicate to zero-cache - only the types on the client
|
|
95
95
|
excludeTables = ["Posts", "Comments", ...]
|
|
96
|
+
// When true, skip generating the query builder (`zql` and `builder` exports)
|
|
97
|
+
skipBuilder = true
|
|
98
|
+
// When true, skip generating the module augmentation for default types in Zero
|
|
99
|
+
skipDeclare = true
|
|
100
|
+
// When true, enable legacy CRUD mutators (sets `enableLegacyMutators` to `true` in the generated schema)
|
|
101
|
+
enableLegacyMutators = true
|
|
102
|
+
// When true, enable legacy CRUD queries (sets `enableLegacyQueries` to `true` in the generated schema)
|
|
103
|
+
enableLegacyQueries = true
|
|
96
104
|
}
|
|
97
105
|
```
|
|
98
106
|
|
package/dist/generator.cjs
CHANGED
|
@@ -39,14 +39,16 @@ var import_promises = require("fs/promises");
|
|
|
39
39
|
var import_path = require("path");
|
|
40
40
|
|
|
41
41
|
// package.json
|
|
42
|
-
var version = "0.1.
|
|
42
|
+
var version = "0.1.1";
|
|
43
43
|
|
|
44
44
|
// src/generators/code-generator.ts
|
|
45
|
-
function generateImports(schema) {
|
|
45
|
+
function generateImports(schema, config) {
|
|
46
46
|
const usedImports = /* @__PURE__ */ new Set();
|
|
47
47
|
usedImports.add("table");
|
|
48
48
|
usedImports.add("createSchema");
|
|
49
|
-
|
|
49
|
+
if (!config.skipBuilder) {
|
|
50
|
+
usedImports.add("createBuilder");
|
|
51
|
+
}
|
|
50
52
|
schema.models.forEach((model) => {
|
|
51
53
|
Object.values(model.columns).forEach((mapping) => {
|
|
52
54
|
const match = mapping.type.match(/^([a-z]+)/);
|
|
@@ -150,7 +152,7 @@ ${relationshipsStr}
|
|
|
150
152
|
const filteredRelationships = modelRelationships.filter(Boolean);
|
|
151
153
|
return filteredRelationships.length > 0 ? filteredRelationships.join("") : "";
|
|
152
154
|
}
|
|
153
|
-
function generateSchema(schema) {
|
|
155
|
+
function generateSchema(schema, config) {
|
|
154
156
|
let output = "/**\n";
|
|
155
157
|
output += " * The Zero schema object.\n";
|
|
156
158
|
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
@@ -175,43 +177,53 @@ function generateSchema(schema) {
|
|
|
175
177
|
});
|
|
176
178
|
output += " ],\n";
|
|
177
179
|
}
|
|
180
|
+
if (config.enableLegacyMutators) {
|
|
181
|
+
output += " enableLegacyMutators: true,\n";
|
|
182
|
+
}
|
|
183
|
+
if (config.enableLegacyQueries) {
|
|
184
|
+
output += " enableLegacyQueries: true,\n";
|
|
185
|
+
}
|
|
178
186
|
output += "});\n\n";
|
|
179
187
|
output += "/**\n";
|
|
180
188
|
output += " * Represents the Zero schema type.\n";
|
|
181
189
|
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
182
190
|
output += " */\n";
|
|
183
191
|
output += "export type Schema = typeof schema;\n";
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
192
|
+
if (!config.skipBuilder) {
|
|
193
|
+
output += "/**\n";
|
|
194
|
+
output += " * Represents the ZQL query builder.\n";
|
|
195
|
+
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
196
|
+
output += " */\n";
|
|
197
|
+
output += "export const zql = createBuilder(schema);\n";
|
|
198
|
+
output += "/**\n";
|
|
199
|
+
output += " * Represents the Zero schema query builder.\n";
|
|
200
|
+
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
201
|
+
output += " *\n";
|
|
202
|
+
output += " * @deprecated Use `zql` instead.\n";
|
|
203
|
+
output += " */\n";
|
|
204
|
+
output += "export const builder = zql;\n";
|
|
205
|
+
}
|
|
206
|
+
if (!config.skipDeclare) {
|
|
207
|
+
output += "/** Defines the default types for Zero */\n";
|
|
208
|
+
output += 'declare module "@rocicorp/zero" {\n';
|
|
209
|
+
output += " interface DefaultTypes {\n";
|
|
210
|
+
output += " schema: Schema;\n";
|
|
211
|
+
output += " }\n";
|
|
212
|
+
output += "}\n";
|
|
213
|
+
}
|
|
202
214
|
return output;
|
|
203
215
|
}
|
|
204
|
-
function generateCode(schema) {
|
|
216
|
+
function generateCode(schema, config) {
|
|
205
217
|
let output = `${HEADER_PREFIX}
|
|
206
218
|
|
|
207
219
|
`;
|
|
208
|
-
output += generateImports(schema);
|
|
220
|
+
output += generateImports(schema, config);
|
|
209
221
|
output += generateUnionTypes(schema);
|
|
210
222
|
schema.models.forEach((model) => {
|
|
211
223
|
output += generateModelSchema(model);
|
|
212
224
|
});
|
|
213
225
|
output += generateRelationships(schema.models);
|
|
214
|
-
output += generateSchema(schema);
|
|
226
|
+
output += generateSchema(schema, config);
|
|
215
227
|
return output;
|
|
216
228
|
}
|
|
217
229
|
var HEADER_PREFIX = `// This file was automatically generated by prisma-zero.
|
|
@@ -515,10 +527,18 @@ async function onGenerate(options) {
|
|
|
515
527
|
// Default true
|
|
516
528
|
camelCase: generator.config.camelCase === "true",
|
|
517
529
|
// Default false
|
|
518
|
-
excludeTables: loadExcludeTables(generator)
|
|
530
|
+
excludeTables: loadExcludeTables(generator),
|
|
531
|
+
skipBuilder: generator.config.skipBuilder === "true",
|
|
532
|
+
// Default false
|
|
533
|
+
skipDeclare: generator.config.skipDeclare === "true",
|
|
534
|
+
// Default false
|
|
535
|
+
enableLegacyMutators: generator.config.enableLegacyMutators === "true",
|
|
536
|
+
// Default false
|
|
537
|
+
enableLegacyQueries: generator.config.enableLegacyQueries === "true"
|
|
538
|
+
// Default false
|
|
519
539
|
};
|
|
520
540
|
const transformedSchema = transformSchema(dmmf, config);
|
|
521
|
-
let output = generateCode(transformedSchema);
|
|
541
|
+
let output = generateCode(transformedSchema, config);
|
|
522
542
|
if (config.prettier) {
|
|
523
543
|
let prettier;
|
|
524
544
|
try {
|
package/dist/generator.js
CHANGED
|
@@ -6,14 +6,16 @@ import { mkdir, writeFile } from "fs/promises";
|
|
|
6
6
|
import { join } from "path";
|
|
7
7
|
|
|
8
8
|
// package.json
|
|
9
|
-
var version = "0.1.
|
|
9
|
+
var version = "0.1.1";
|
|
10
10
|
|
|
11
11
|
// src/generators/code-generator.ts
|
|
12
|
-
function generateImports(schema) {
|
|
12
|
+
function generateImports(schema, config) {
|
|
13
13
|
const usedImports = /* @__PURE__ */ new Set();
|
|
14
14
|
usedImports.add("table");
|
|
15
15
|
usedImports.add("createSchema");
|
|
16
|
-
|
|
16
|
+
if (!config.skipBuilder) {
|
|
17
|
+
usedImports.add("createBuilder");
|
|
18
|
+
}
|
|
17
19
|
schema.models.forEach((model) => {
|
|
18
20
|
Object.values(model.columns).forEach((mapping) => {
|
|
19
21
|
const match = mapping.type.match(/^([a-z]+)/);
|
|
@@ -117,7 +119,7 @@ ${relationshipsStr}
|
|
|
117
119
|
const filteredRelationships = modelRelationships.filter(Boolean);
|
|
118
120
|
return filteredRelationships.length > 0 ? filteredRelationships.join("") : "";
|
|
119
121
|
}
|
|
120
|
-
function generateSchema(schema) {
|
|
122
|
+
function generateSchema(schema, config) {
|
|
121
123
|
let output = "/**\n";
|
|
122
124
|
output += " * The Zero schema object.\n";
|
|
123
125
|
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
@@ -142,43 +144,53 @@ function generateSchema(schema) {
|
|
|
142
144
|
});
|
|
143
145
|
output += " ],\n";
|
|
144
146
|
}
|
|
147
|
+
if (config.enableLegacyMutators) {
|
|
148
|
+
output += " enableLegacyMutators: true,\n";
|
|
149
|
+
}
|
|
150
|
+
if (config.enableLegacyQueries) {
|
|
151
|
+
output += " enableLegacyQueries: true,\n";
|
|
152
|
+
}
|
|
145
153
|
output += "});\n\n";
|
|
146
154
|
output += "/**\n";
|
|
147
155
|
output += " * Represents the Zero schema type.\n";
|
|
148
156
|
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
149
157
|
output += " */\n";
|
|
150
158
|
output += "export type Schema = typeof schema;\n";
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
159
|
+
if (!config.skipBuilder) {
|
|
160
|
+
output += "/**\n";
|
|
161
|
+
output += " * Represents the ZQL query builder.\n";
|
|
162
|
+
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
163
|
+
output += " */\n";
|
|
164
|
+
output += "export const zql = createBuilder(schema);\n";
|
|
165
|
+
output += "/**\n";
|
|
166
|
+
output += " * Represents the Zero schema query builder.\n";
|
|
167
|
+
output += " * This type is auto-generated from your Prisma schema definition.\n";
|
|
168
|
+
output += " *\n";
|
|
169
|
+
output += " * @deprecated Use `zql` instead.\n";
|
|
170
|
+
output += " */\n";
|
|
171
|
+
output += "export const builder = zql;\n";
|
|
172
|
+
}
|
|
173
|
+
if (!config.skipDeclare) {
|
|
174
|
+
output += "/** Defines the default types for Zero */\n";
|
|
175
|
+
output += 'declare module "@rocicorp/zero" {\n';
|
|
176
|
+
output += " interface DefaultTypes {\n";
|
|
177
|
+
output += " schema: Schema;\n";
|
|
178
|
+
output += " }\n";
|
|
179
|
+
output += "}\n";
|
|
180
|
+
}
|
|
169
181
|
return output;
|
|
170
182
|
}
|
|
171
|
-
function generateCode(schema) {
|
|
183
|
+
function generateCode(schema, config) {
|
|
172
184
|
let output = `${HEADER_PREFIX}
|
|
173
185
|
|
|
174
186
|
`;
|
|
175
|
-
output += generateImports(schema);
|
|
187
|
+
output += generateImports(schema, config);
|
|
176
188
|
output += generateUnionTypes(schema);
|
|
177
189
|
schema.models.forEach((model) => {
|
|
178
190
|
output += generateModelSchema(model);
|
|
179
191
|
});
|
|
180
192
|
output += generateRelationships(schema.models);
|
|
181
|
-
output += generateSchema(schema);
|
|
193
|
+
output += generateSchema(schema, config);
|
|
182
194
|
return output;
|
|
183
195
|
}
|
|
184
196
|
var HEADER_PREFIX = `// This file was automatically generated by prisma-zero.
|
|
@@ -482,10 +494,18 @@ async function onGenerate(options) {
|
|
|
482
494
|
// Default true
|
|
483
495
|
camelCase: generator.config.camelCase === "true",
|
|
484
496
|
// Default false
|
|
485
|
-
excludeTables: loadExcludeTables(generator)
|
|
497
|
+
excludeTables: loadExcludeTables(generator),
|
|
498
|
+
skipBuilder: generator.config.skipBuilder === "true",
|
|
499
|
+
// Default false
|
|
500
|
+
skipDeclare: generator.config.skipDeclare === "true",
|
|
501
|
+
// Default false
|
|
502
|
+
enableLegacyMutators: generator.config.enableLegacyMutators === "true",
|
|
503
|
+
// Default false
|
|
504
|
+
enableLegacyQueries: generator.config.enableLegacyQueries === "true"
|
|
505
|
+
// Default false
|
|
486
506
|
};
|
|
487
507
|
const transformedSchema = transformSchema(dmmf, config);
|
|
488
|
-
let output = generateCode(transformedSchema);
|
|
508
|
+
let output = generateCode(transformedSchema, config);
|
|
489
509
|
if (config.prettier) {
|
|
490
510
|
let prettier;
|
|
491
511
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-zero",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Generate Zero schemas from Prisma ORM schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@rocicorp/prettier-config": "^0.4.0",
|
|
44
|
-
"@rocicorp/zero": "0.25.
|
|
44
|
+
"@rocicorp/zero": "0.25.2",
|
|
45
45
|
"@types/node": "^24.10.1",
|
|
46
46
|
"@types/pg": "^8.15.6",
|
|
47
47
|
"@types/pluralize": "^0.0.33",
|