schemock 0.0.4-alpha.3 → 0.0.4-alpha.5

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.
@@ -225,7 +225,10 @@ interface AnalyzedField {
225
225
  interface AnalyzedRelation {
226
226
  name: string;
227
227
  type: 'hasOne' | 'hasMany' | 'belongsTo' | 'manyToMany';
228
+ /** Original target as defined in relation (e.g., 'user') */
228
229
  target: string;
230
+ /** Resolved target - actual schema name found (e.g., 'authUser'). Falls back to target if not found. */
231
+ resolvedTarget: string;
229
232
  targetPascal: string;
230
233
  foreignKey: string;
231
234
  /** For belongsTo - FK is on this entity */
@@ -225,7 +225,10 @@ interface AnalyzedField {
225
225
  interface AnalyzedRelation {
226
226
  name: string;
227
227
  type: 'hasOne' | 'hasMany' | 'belongsTo' | 'manyToMany';
228
+ /** Original target as defined in relation (e.g., 'user') */
228
229
  target: string;
230
+ /** Resolved target - actual schema name found (e.g., 'authUser'). Falls back to target if not found. */
231
+ resolvedTarget: string;
229
232
  targetPascal: string;
230
233
  foreignKey: string;
231
234
  /** For belongsTo - FK is on this entity */
package/dist/cli/index.js CHANGED
@@ -1011,11 +1011,14 @@ function analyzeRelation(name, rel, sourceEntitySingular, localFields, schemaMap
1011
1011
  \x1B[2mTo fix: Add 'foreignKey' option to the relation, e.g.: ${rel.type}('${rel.target}', { foreignKey: 'yourFieldName' })\x1B[0m`
1012
1012
  );
1013
1013
  }
1014
+ const resolvedTargetSchema = findSchemaByName(schemaMap, rel.target);
1015
+ const resolvedTarget = resolvedTargetSchema?.name ?? rel.target;
1014
1016
  const result = {
1015
1017
  name,
1016
1018
  type: rel.type,
1017
1019
  target: rel.target,
1018
- targetPascal: toPascalCase(singularize(rel.target)),
1020
+ resolvedTarget,
1021
+ targetPascal: toPascalCase(singularize(resolvedTarget)),
1019
1022
  foreignKey,
1020
1023
  eager: rel.eager ?? false,
1021
1024
  inferred: fkInferred
@@ -1758,7 +1761,7 @@ function generateEntityFactory(code, schema) {
1758
1761
  for (const field of schema.fields) {
1759
1762
  if (field.name === "id") {
1760
1763
  code.line("id: primaryKey(faker.string.uuid),");
1761
- } else if (field.isObject) {
1764
+ } else if (field.isArray || field.isObject) {
1762
1765
  if (field.nullable) {
1763
1766
  code.line(`${field.name}: nullable(() => JSON.stringify(${field.fakerCall})),`);
1764
1767
  } else {
@@ -2379,24 +2382,26 @@ function generateEntityApi(code, schema, allSchemas, hasRLS) {
2379
2382
  code.line();
2380
2383
  }
2381
2384
  function generateRelationLoad(code, schema, rel) {
2385
+ const targetDbName = toSafePropertyName(rel.resolvedTarget);
2382
2386
  if (rel.type === "hasMany") {
2383
- code.line(`result.${rel.name} = db.${rel.target}.findMany({`);
2387
+ code.line(`result.${rel.name} = db.${targetDbName}.findMany({`);
2384
2388
  code.line(` where: { ${rel.foreignKey}: { equals: item.id } }`);
2385
2389
  code.line("});");
2386
2390
  } else if (rel.type === "hasOne") {
2387
- code.line(`result.${rel.name} = db.${rel.target}.findFirst({`);
2391
+ code.line(`result.${rel.name} = db.${targetDbName}.findFirst({`);
2388
2392
  code.line(` where: { ${rel.foreignKey}: { equals: item.id } }`);
2389
2393
  code.line("});");
2390
2394
  } else if (rel.type === "belongsTo") {
2391
- code.line(`result.${rel.name} = db.${rel.target}.findFirst({`);
2395
+ code.line(`result.${rel.name} = db.${targetDbName}.findFirst({`);
2392
2396
  code.line(` where: { id: { equals: (item as Record<string, unknown>).${rel.localField} as string } }`);
2393
2397
  code.line("});");
2394
2398
  } else if (rel.type === "manyToMany") {
2395
- code.line(`const junctions = db.${rel.through}.findMany({`);
2399
+ const throughDbName = toSafePropertyName(rel.through);
2400
+ code.line(`const junctions = db.${throughDbName}.findMany({`);
2396
2401
  code.line(` where: { ${rel.foreignKey}: { equals: item.id } }`);
2397
2402
  code.line("});");
2398
2403
  code.line(`result.${rel.name} = junctions`);
2399
- code.line(` .map(j => db.${rel.target}.findFirst({`);
2404
+ code.line(` .map(j => db.${targetDbName}.findFirst({`);
2400
2405
  code.line(` where: { id: { equals: (j as Record<string, unknown>).${rel.otherKey} as string } }`);
2401
2406
  code.line(" }))");
2402
2407
  code.line(" .filter(Boolean);");
@@ -2429,15 +2434,16 @@ function generateCreateMethod(code, schema, hasJsonFields, jsonFieldsStr, hasRLS
2429
2434
  code.line();
2430
2435
  }
2431
2436
  for (const rel of nestedRels) {
2437
+ const targetDbName = toSafePropertyName(rel.resolvedTarget);
2432
2438
  code.block(`if (${rel.name}) {`, () => {
2433
2439
  if (rel.type === "hasMany") {
2434
2440
  code.block(`for (const nested of ${rel.name}) {`, () => {
2435
2441
  code.line(`// eslint-disable-next-line @typescript-eslint/no-explicit-any`);
2436
- code.line(`db.${rel.target}.create({ ...nested, ${rel.foreignKey}: item.id } as any);`);
2442
+ code.line(`db.${targetDbName}.create({ ...nested, ${rel.foreignKey}: item.id } as any);`);
2437
2443
  });
2438
2444
  } else {
2439
2445
  code.line(`// eslint-disable-next-line @typescript-eslint/no-explicit-any`);
2440
- code.line(`db.${rel.target}.create({ ...${rel.name}, ${rel.foreignKey}: item.id } as any);`);
2446
+ code.line(`db.${targetDbName}.create({ ...${rel.name}, ${rel.foreignKey}: item.id } as any);`);
2441
2447
  }
2442
2448
  });
2443
2449
  }