prisma-zero 0.1.0-canary.2 → 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 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
 
@@ -39,18 +39,20 @@ var import_promises = require("fs/promises");
39
39
  var import_path = require("path");
40
40
 
41
41
  // package.json
42
- var version = "0.1.0-canary.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
- usedImports.add("createBuilder");
50
- usedImports.add("createCRUDBuilder");
49
+ if (!config.skipBuilder) {
50
+ usedImports.add("createBuilder");
51
+ }
51
52
  schema.models.forEach((model) => {
52
53
  Object.values(model.columns).forEach((mapping) => {
53
- const baseType = mapping.type.split("(")[0];
54
+ const match = mapping.type.match(/^([a-z]+)/);
55
+ const baseType = match == null ? void 0 : match[1];
54
56
  if (baseType) {
55
57
  usedImports.add(baseType);
56
58
  }
@@ -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,48 +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
- output += "/**\n";
185
- output += " * Represents the ZQL query builder.\n";
186
- output += " * This type is auto-generated from your Prisma schema definition.\n";
187
- output += " */\n";
188
- output += "export const zql = createBuilder(schema);\n";
189
- output += "/**\n";
190
- output += " * Represents the Zero schema query builder.\n";
191
- output += " * This type is auto-generated from your Prisma schema definition.\n";
192
- output += " *\n";
193
- output += " * @deprecated Use `zql` instead.\n";
194
- output += " */\n";
195
- output += "export const builder = zql;\n";
196
- output += "/**\n";
197
- output += " * Represents the Zero schema CRUD builder.\n";
198
- output += " * This type is auto-generated from your Prisma schema definition.\n";
199
- output += " */\n";
200
- output += "export const crud = createCRUDBuilder(schema);\n";
201
- output += "/** Defines the default types for Zero */\n";
202
- output += 'declare module "@rocicorp/zero" {\n';
203
- output += " interface DefaultTypes {\n";
204
- output += " schema: Schema;\n";
205
- output += " }\n";
206
- output += "}\n";
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
+ }
207
214
  return output;
208
215
  }
209
- function generateCode(schema) {
216
+ function generateCode(schema, config) {
210
217
  let output = `${HEADER_PREFIX}
211
218
 
212
219
  `;
213
- output += generateImports(schema);
220
+ output += generateImports(schema, config);
214
221
  output += generateUnionTypes(schema);
215
222
  schema.models.forEach((model) => {
216
223
  output += generateModelSchema(model);
217
224
  });
218
225
  output += generateRelationships(schema.models);
219
- output += generateSchema(schema);
226
+ output += generateSchema(schema, config);
220
227
  return output;
221
228
  }
222
229
  var HEADER_PREFIX = `// This file was automatically generated by prisma-zero.
@@ -372,7 +379,8 @@ function mapRelationships(model, dmmf, config) {
372
379
  return;
373
380
  }
374
381
  const backReference = targetModel.fields.find(
375
- (f) => f.relationName === field.relationName && f.type === model.name
382
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
383
+ // Exclude current field for self-referential relations
376
384
  );
377
385
  if (field.isList) {
378
386
  if (backReference == null ? void 0 : backReference.isList) {
@@ -389,7 +397,8 @@ function mapRelationships(model, dmmf, config) {
389
397
  `Implicit relation ${field.name}: Model ${model.name} or ${targetModel.name} not found.`
390
398
  );
391
399
  }
392
- const isModelA = model.name === modelA.name;
400
+ const isSelfReferential = model.name === targetModel.name;
401
+ const isModelA = isSelfReferential ? backReference ? field.name.localeCompare(backReference.name) < 0 : true : model.name === modelA.name;
393
402
  relationships[field.name] = {
394
403
  type: "many",
395
404
  chain: [
@@ -448,12 +457,12 @@ function mapModel(model, dmmf, config) {
448
457
  if (!primaryKey[0]) {
449
458
  throw new Error(`No primary key found for ${model.name}`);
450
459
  }
451
- const tableName = getTableNameFromModel(model);
452
- const camelCasedName = (config == null ? void 0 : config.camelCase) ? toCamelCase(tableName) : tableName;
453
- const shouldRemap = config.camelCase && camelCasedName !== tableName;
460
+ const databaseTableName = getTableNameFromModel(model);
461
+ const tableName = getTableName(model.name, config);
462
+ const shouldRemap = tableName !== databaseTableName;
454
463
  return {
455
- tableName: shouldRemap ? camelCasedName : tableName,
456
- originalTableName: shouldRemap ? tableName : null,
464
+ tableName,
465
+ originalTableName: shouldRemap ? databaseTableName : null,
457
466
  modelName: model.name,
458
467
  zeroTableName: getZeroTableName(model.name),
459
468
  columns,
@@ -476,10 +485,13 @@ function transformSchema(dmmf, config) {
476
485
  if (!targetModel) return null;
477
486
  if ((_a = config.excludeTables) == null ? void 0 : _a.includes(targetModel.name)) return null;
478
487
  const backReference = targetModel.fields.find(
479
- (f) => f.relationName === field.relationName && f.type === model.name
488
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
489
+ // Exclude current field for self-referential relations
480
490
  );
481
491
  if (backReference == null ? void 0 : backReference.isList) {
482
- if (model.name.localeCompare(targetModel.name) < 0) {
492
+ const isSelfReferential = model.name === targetModel.name;
493
+ const shouldCreate = isSelfReferential ? field.name.localeCompare(backReference.name) < 0 : model.name.localeCompare(targetModel.name) < 0;
494
+ if (shouldCreate) {
483
495
  return createImplicitManyToManyModel(
484
496
  model,
485
497
  targetModel,
@@ -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,18 +6,20 @@ import { mkdir, writeFile } from "fs/promises";
6
6
  import { join } from "path";
7
7
 
8
8
  // package.json
9
- var version = "0.1.0-canary.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
- usedImports.add("createBuilder");
17
- usedImports.add("createCRUDBuilder");
16
+ if (!config.skipBuilder) {
17
+ usedImports.add("createBuilder");
18
+ }
18
19
  schema.models.forEach((model) => {
19
20
  Object.values(model.columns).forEach((mapping) => {
20
- const baseType = mapping.type.split("(")[0];
21
+ const match = mapping.type.match(/^([a-z]+)/);
22
+ const baseType = match == null ? void 0 : match[1];
21
23
  if (baseType) {
22
24
  usedImports.add(baseType);
23
25
  }
@@ -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,48 +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
- output += "/**\n";
152
- output += " * Represents the ZQL query builder.\n";
153
- output += " * This type is auto-generated from your Prisma schema definition.\n";
154
- output += " */\n";
155
- output += "export const zql = createBuilder(schema);\n";
156
- output += "/**\n";
157
- output += " * Represents the Zero schema query builder.\n";
158
- output += " * This type is auto-generated from your Prisma schema definition.\n";
159
- output += " *\n";
160
- output += " * @deprecated Use `zql` instead.\n";
161
- output += " */\n";
162
- output += "export const builder = zql;\n";
163
- output += "/**\n";
164
- output += " * Represents the Zero schema CRUD builder.\n";
165
- output += " * This type is auto-generated from your Prisma schema definition.\n";
166
- output += " */\n";
167
- output += "export const crud = createCRUDBuilder(schema);\n";
168
- output += "/** Defines the default types for Zero */\n";
169
- output += 'declare module "@rocicorp/zero" {\n';
170
- output += " interface DefaultTypes {\n";
171
- output += " schema: Schema;\n";
172
- output += " }\n";
173
- output += "}\n";
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
+ }
174
181
  return output;
175
182
  }
176
- function generateCode(schema) {
183
+ function generateCode(schema, config) {
177
184
  let output = `${HEADER_PREFIX}
178
185
 
179
186
  `;
180
- output += generateImports(schema);
187
+ output += generateImports(schema, config);
181
188
  output += generateUnionTypes(schema);
182
189
  schema.models.forEach((model) => {
183
190
  output += generateModelSchema(model);
184
191
  });
185
192
  output += generateRelationships(schema.models);
186
- output += generateSchema(schema);
193
+ output += generateSchema(schema, config);
187
194
  return output;
188
195
  }
189
196
  var HEADER_PREFIX = `// This file was automatically generated by prisma-zero.
@@ -339,7 +346,8 @@ function mapRelationships(model, dmmf, config) {
339
346
  return;
340
347
  }
341
348
  const backReference = targetModel.fields.find(
342
- (f) => f.relationName === field.relationName && f.type === model.name
349
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
350
+ // Exclude current field for self-referential relations
343
351
  );
344
352
  if (field.isList) {
345
353
  if (backReference == null ? void 0 : backReference.isList) {
@@ -356,7 +364,8 @@ function mapRelationships(model, dmmf, config) {
356
364
  `Implicit relation ${field.name}: Model ${model.name} or ${targetModel.name} not found.`
357
365
  );
358
366
  }
359
- const isModelA = model.name === modelA.name;
367
+ const isSelfReferential = model.name === targetModel.name;
368
+ const isModelA = isSelfReferential ? backReference ? field.name.localeCompare(backReference.name) < 0 : true : model.name === modelA.name;
360
369
  relationships[field.name] = {
361
370
  type: "many",
362
371
  chain: [
@@ -415,12 +424,12 @@ function mapModel(model, dmmf, config) {
415
424
  if (!primaryKey[0]) {
416
425
  throw new Error(`No primary key found for ${model.name}`);
417
426
  }
418
- const tableName = getTableNameFromModel(model);
419
- const camelCasedName = (config == null ? void 0 : config.camelCase) ? toCamelCase(tableName) : tableName;
420
- const shouldRemap = config.camelCase && camelCasedName !== tableName;
427
+ const databaseTableName = getTableNameFromModel(model);
428
+ const tableName = getTableName(model.name, config);
429
+ const shouldRemap = tableName !== databaseTableName;
421
430
  return {
422
- tableName: shouldRemap ? camelCasedName : tableName,
423
- originalTableName: shouldRemap ? tableName : null,
431
+ tableName,
432
+ originalTableName: shouldRemap ? databaseTableName : null,
424
433
  modelName: model.name,
425
434
  zeroTableName: getZeroTableName(model.name),
426
435
  columns,
@@ -443,10 +452,13 @@ function transformSchema(dmmf, config) {
443
452
  if (!targetModel) return null;
444
453
  if ((_a = config.excludeTables) == null ? void 0 : _a.includes(targetModel.name)) return null;
445
454
  const backReference = targetModel.fields.find(
446
- (f) => f.relationName === field.relationName && f.type === model.name
455
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
456
+ // Exclude current field for self-referential relations
447
457
  );
448
458
  if (backReference == null ? void 0 : backReference.isList) {
449
- if (model.name.localeCompare(targetModel.name) < 0) {
459
+ const isSelfReferential = model.name === targetModel.name;
460
+ const shouldCreate = isSelfReferential ? field.name.localeCompare(backReference.name) < 0 : model.name.localeCompare(targetModel.name) < 0;
461
+ if (shouldCreate) {
450
462
  return createImplicitManyToManyModel(
451
463
  model,
452
464
  targetModel,
@@ -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.0-canary.2",
3
+ "version": "0.1.1",
4
4
  "description": "Generate Zero schemas from Prisma ORM schemas",
5
5
  "type": "module",
6
6
  "bin": {
@@ -41,8 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@rocicorp/prettier-config": "^0.4.0",
44
- "@rocicorp/zero": "0.24.3000000000",
45
- "@ts-morph/common": "^0.28.1",
44
+ "@rocicorp/zero": "0.25.2",
46
45
  "@types/node": "^24.10.1",
47
46
  "@types/pg": "^8.15.6",
48
47
  "@types/pluralize": "^0.0.33",