prisma-zero 0.1.0-canary.2 → 0.1.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.
@@ -39,7 +39,7 @@ 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.0";
43
43
 
44
44
  // src/generators/code-generator.ts
45
45
  function generateImports(schema) {
@@ -47,10 +47,10 @@ function generateImports(schema) {
47
47
  usedImports.add("table");
48
48
  usedImports.add("createSchema");
49
49
  usedImports.add("createBuilder");
50
- usedImports.add("createCRUDBuilder");
51
50
  schema.models.forEach((model) => {
52
51
  Object.values(model.columns).forEach((mapping) => {
53
- const baseType = mapping.type.split("(")[0];
52
+ const match = mapping.type.match(/^([a-z]+)/);
53
+ const baseType = match == null ? void 0 : match[1];
54
54
  if (baseType) {
55
55
  usedImports.add(baseType);
56
56
  }
@@ -193,11 +193,6 @@ function generateSchema(schema) {
193
193
  output += " * @deprecated Use `zql` instead.\n";
194
194
  output += " */\n";
195
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
196
  output += "/** Defines the default types for Zero */\n";
202
197
  output += 'declare module "@rocicorp/zero" {\n';
203
198
  output += " interface DefaultTypes {\n";
@@ -372,7 +367,8 @@ function mapRelationships(model, dmmf, config) {
372
367
  return;
373
368
  }
374
369
  const backReference = targetModel.fields.find(
375
- (f) => f.relationName === field.relationName && f.type === model.name
370
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
371
+ // Exclude current field for self-referential relations
376
372
  );
377
373
  if (field.isList) {
378
374
  if (backReference == null ? void 0 : backReference.isList) {
@@ -389,7 +385,8 @@ function mapRelationships(model, dmmf, config) {
389
385
  `Implicit relation ${field.name}: Model ${model.name} or ${targetModel.name} not found.`
390
386
  );
391
387
  }
392
- const isModelA = model.name === modelA.name;
388
+ const isSelfReferential = model.name === targetModel.name;
389
+ const isModelA = isSelfReferential ? backReference ? field.name.localeCompare(backReference.name) < 0 : true : model.name === modelA.name;
393
390
  relationships[field.name] = {
394
391
  type: "many",
395
392
  chain: [
@@ -448,12 +445,12 @@ function mapModel(model, dmmf, config) {
448
445
  if (!primaryKey[0]) {
449
446
  throw new Error(`No primary key found for ${model.name}`);
450
447
  }
451
- const tableName = getTableNameFromModel(model);
452
- const camelCasedName = (config == null ? void 0 : config.camelCase) ? toCamelCase(tableName) : tableName;
453
- const shouldRemap = config.camelCase && camelCasedName !== tableName;
448
+ const databaseTableName = getTableNameFromModel(model);
449
+ const tableName = getTableName(model.name, config);
450
+ const shouldRemap = tableName !== databaseTableName;
454
451
  return {
455
- tableName: shouldRemap ? camelCasedName : tableName,
456
- originalTableName: shouldRemap ? tableName : null,
452
+ tableName,
453
+ originalTableName: shouldRemap ? databaseTableName : null,
457
454
  modelName: model.name,
458
455
  zeroTableName: getZeroTableName(model.name),
459
456
  columns,
@@ -476,10 +473,13 @@ function transformSchema(dmmf, config) {
476
473
  if (!targetModel) return null;
477
474
  if ((_a = config.excludeTables) == null ? void 0 : _a.includes(targetModel.name)) return null;
478
475
  const backReference = targetModel.fields.find(
479
- (f) => f.relationName === field.relationName && f.type === model.name
476
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
477
+ // Exclude current field for self-referential relations
480
478
  );
481
479
  if (backReference == null ? void 0 : backReference.isList) {
482
- if (model.name.localeCompare(targetModel.name) < 0) {
480
+ const isSelfReferential = model.name === targetModel.name;
481
+ const shouldCreate = isSelfReferential ? field.name.localeCompare(backReference.name) < 0 : model.name.localeCompare(targetModel.name) < 0;
482
+ if (shouldCreate) {
483
483
  return createImplicitManyToManyModel(
484
484
  model,
485
485
  targetModel,
package/dist/generator.js CHANGED
@@ -6,7 +6,7 @@ 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.0";
10
10
 
11
11
  // src/generators/code-generator.ts
12
12
  function generateImports(schema) {
@@ -14,10 +14,10 @@ function generateImports(schema) {
14
14
  usedImports.add("table");
15
15
  usedImports.add("createSchema");
16
16
  usedImports.add("createBuilder");
17
- usedImports.add("createCRUDBuilder");
18
17
  schema.models.forEach((model) => {
19
18
  Object.values(model.columns).forEach((mapping) => {
20
- const baseType = mapping.type.split("(")[0];
19
+ const match = mapping.type.match(/^([a-z]+)/);
20
+ const baseType = match == null ? void 0 : match[1];
21
21
  if (baseType) {
22
22
  usedImports.add(baseType);
23
23
  }
@@ -160,11 +160,6 @@ function generateSchema(schema) {
160
160
  output += " * @deprecated Use `zql` instead.\n";
161
161
  output += " */\n";
162
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
163
  output += "/** Defines the default types for Zero */\n";
169
164
  output += 'declare module "@rocicorp/zero" {\n';
170
165
  output += " interface DefaultTypes {\n";
@@ -339,7 +334,8 @@ function mapRelationships(model, dmmf, config) {
339
334
  return;
340
335
  }
341
336
  const backReference = targetModel.fields.find(
342
- (f) => f.relationName === field.relationName && f.type === model.name
337
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
338
+ // Exclude current field for self-referential relations
343
339
  );
344
340
  if (field.isList) {
345
341
  if (backReference == null ? void 0 : backReference.isList) {
@@ -356,7 +352,8 @@ function mapRelationships(model, dmmf, config) {
356
352
  `Implicit relation ${field.name}: Model ${model.name} or ${targetModel.name} not found.`
357
353
  );
358
354
  }
359
- const isModelA = model.name === modelA.name;
355
+ const isSelfReferential = model.name === targetModel.name;
356
+ const isModelA = isSelfReferential ? backReference ? field.name.localeCompare(backReference.name) < 0 : true : model.name === modelA.name;
360
357
  relationships[field.name] = {
361
358
  type: "many",
362
359
  chain: [
@@ -415,12 +412,12 @@ function mapModel(model, dmmf, config) {
415
412
  if (!primaryKey[0]) {
416
413
  throw new Error(`No primary key found for ${model.name}`);
417
414
  }
418
- const tableName = getTableNameFromModel(model);
419
- const camelCasedName = (config == null ? void 0 : config.camelCase) ? toCamelCase(tableName) : tableName;
420
- const shouldRemap = config.camelCase && camelCasedName !== tableName;
415
+ const databaseTableName = getTableNameFromModel(model);
416
+ const tableName = getTableName(model.name, config);
417
+ const shouldRemap = tableName !== databaseTableName;
421
418
  return {
422
- tableName: shouldRemap ? camelCasedName : tableName,
423
- originalTableName: shouldRemap ? tableName : null,
419
+ tableName,
420
+ originalTableName: shouldRemap ? databaseTableName : null,
424
421
  modelName: model.name,
425
422
  zeroTableName: getZeroTableName(model.name),
426
423
  columns,
@@ -443,10 +440,13 @@ function transformSchema(dmmf, config) {
443
440
  if (!targetModel) return null;
444
441
  if ((_a = config.excludeTables) == null ? void 0 : _a.includes(targetModel.name)) return null;
445
442
  const backReference = targetModel.fields.find(
446
- (f) => f.relationName === field.relationName && f.type === model.name
443
+ (f) => f.relationName === field.relationName && f.type === model.name && f.name !== field.name
444
+ // Exclude current field for self-referential relations
447
445
  );
448
446
  if (backReference == null ? void 0 : backReference.isList) {
449
- if (model.name.localeCompare(targetModel.name) < 0) {
447
+ const isSelfReferential = model.name === targetModel.name;
448
+ const shouldCreate = isSelfReferential ? field.name.localeCompare(backReference.name) < 0 : model.name.localeCompare(targetModel.name) < 0;
449
+ if (shouldCreate) {
450
450
  return createImplicitManyToManyModel(
451
451
  model,
452
452
  targetModel,
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.0",
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.1",
46
45
  "@types/node": "^24.10.1",
47
46
  "@types/pg": "^8.15.6",
48
47
  "@types/pluralize": "^0.0.33",