schemock 0.0.4-alpha.1 → 0.0.4-alpha.2

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/dist/cli/index.js CHANGED
@@ -480,6 +480,12 @@ function toCamelCase(str) {
480
480
  function toSnakeCase(str) {
481
481
  return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "").replace(/[-\s]+/g, "_");
482
482
  }
483
+ function toSafePropertyName(str) {
484
+ if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(str)) {
485
+ return str;
486
+ }
487
+ return toCamelCase(str);
488
+ }
483
489
 
484
490
  // src/cli/utils/faker-mapping.ts
485
491
  function escapeJsString(value) {
@@ -1251,11 +1257,12 @@ function deriveEndpointName(path) {
1251
1257
  const parts = cleaned.split("/");
1252
1258
  const nameParts = [];
1253
1259
  for (let i = 0; i < parts.length; i++) {
1254
- const part = parts[i];
1260
+ let part = parts[i];
1255
1261
  if (part.startsWith(":")) {
1256
- const paramName = part.slice(1);
1262
+ const paramName = toCamelCaseFromHyphen(part.slice(1));
1257
1263
  nameParts.push("By" + capitalize(paramName));
1258
1264
  } else if (part) {
1265
+ part = toCamelCaseFromHyphen(part);
1259
1266
  if (i === 0) {
1260
1267
  nameParts.push(part);
1261
1268
  } else {
@@ -1275,6 +1282,9 @@ function toPascalCase2(str) {
1275
1282
  function capitalize(str) {
1276
1283
  return str.charAt(0).toUpperCase() + str.slice(1);
1277
1284
  }
1285
+ function toCamelCaseFromHyphen(str) {
1286
+ return str.split("-").map((part, index) => index === 0 ? part : capitalize(part)).join("");
1287
+ }
1278
1288
  function analyzeFields(fields) {
1279
1289
  return Object.entries(fields).map(([name, field]) => analyzeField2(name, field));
1280
1290
  }
@@ -1587,7 +1597,7 @@ function generateCommonTypes(code) {
1587
1597
  // src/cli/generators/mock/db.ts
1588
1598
  function generateMockDb(schemas, config) {
1589
1599
  const code = new CodeBuilder();
1590
- const entityNames = schemas.map((s) => s.name);
1600
+ const entityNames = schemas.map((s) => toSafePropertyName(s.name));
1591
1601
  const persist = config.persist !== false;
1592
1602
  code.comment("GENERATED BY SCHEMOCK - DO NOT EDIT");
1593
1603
  code.line("import { factory, primaryKey, nullable } from '@mswjs/data';");
@@ -1743,7 +1753,8 @@ function generatePersistenceLayer(code, entityNames, storageKey) {
1743
1753
  code.line("wrapDbMethods();");
1744
1754
  }
1745
1755
  function generateEntityFactory(code, schema) {
1746
- code.block(`${schema.name}: {`, () => {
1756
+ const safeName = toSafePropertyName(schema.name);
1757
+ code.block(`${safeName}: {`, () => {
1747
1758
  for (const field of schema.fields) {
1748
1759
  if (field.name === "id") {
1749
1760
  code.line("id: primaryKey(faker.string.uuid),");
@@ -2465,15 +2476,17 @@ function generateSeed(schemas, config) {
2465
2476
  code.block("export interface SeedCounts {", () => {
2466
2477
  for (const schema of schemas) {
2467
2478
  if (schema.isJunctionTable) continue;
2468
- code.line(`${schema.name}?: number;`);
2479
+ const safeName = toSafePropertyName(schema.name);
2480
+ code.line(`${safeName}?: number;`);
2469
2481
  }
2470
2482
  });
2471
2483
  code.line();
2472
2484
  code.block("const defaultCounts: Required<SeedCounts> = {", () => {
2473
2485
  for (const schema of schemas) {
2474
2486
  if (schema.isJunctionTable) continue;
2487
+ const safeName = toSafePropertyName(schema.name);
2475
2488
  const count = config.seed?.[schema.name] ?? 10;
2476
- code.line(`${schema.name}: ${count},`);
2489
+ code.line(`${safeName}: ${count},`);
2477
2490
  }
2478
2491
  }, "};");
2479
2492
  code.line();
@@ -2490,16 +2503,17 @@ function generateSeed(schemas, config) {
2490
2503
  code.line();
2491
2504
  for (const schema of schemas) {
2492
2505
  if (schema.isJunctionTable) continue;
2506
+ const safeName = toSafePropertyName(schema.name);
2493
2507
  const belongsToRels = schema.relations.filter((r) => r.type === "belongsTo");
2494
2508
  const fkFields = belongsToRels.map((r) => ({
2495
2509
  fieldName: r.localField || r.foreignKey,
2496
- target: r.target,
2510
+ target: toSafePropertyName(r.target),
2497
2511
  nullable: schema.fields.find((f) => f.name === (r.localField || r.foreignKey))?.nullable ?? false
2498
2512
  }));
2499
- code.line(`ids.${schema.name} = [];`);
2500
- code.block(`for (let i = 0; i < merged.${schema.name}; i++) {`, () => {
2513
+ code.line(`ids.${safeName} = [];`);
2514
+ code.block(`for (let i = 0; i < merged.${safeName}; i++) {`, () => {
2501
2515
  if (fkFields.length > 0) {
2502
- code.line(`const item = db.${schema.name}.create({`);
2516
+ code.line(`const item = db.${safeName}.create({`);
2503
2517
  code.indent();
2504
2518
  for (const fk of fkFields) {
2505
2519
  if (fk.nullable) {
@@ -2512,9 +2526,9 @@ function generateSeed(schemas, config) {
2512
2526
  code.line(`// eslint-disable-next-line @typescript-eslint/no-explicit-any`);
2513
2527
  code.line("} as any);");
2514
2528
  } else {
2515
- code.line(`const item = db.${schema.name}.create({});`);
2529
+ code.line(`const item = db.${safeName}.create({});`);
2516
2530
  }
2517
- code.line(`ids.${schema.name}.push(item.id);`);
2531
+ code.line(`ids.${safeName}.push(item.id);`);
2518
2532
  });
2519
2533
  code.line();
2520
2534
  }
@@ -2522,14 +2536,16 @@ function generateSeed(schemas, config) {
2522
2536
  code.line();
2523
2537
  code.block("export function reset(): void {", () => {
2524
2538
  for (const schema of [...schemas].reverse()) {
2525
- code.line(`db.${schema.name}.deleteMany({ where: {} });`);
2539
+ const safeName = toSafePropertyName(schema.name);
2540
+ code.line(`db.${safeName}.deleteMany({ where: {} });`);
2526
2541
  }
2527
2542
  });
2528
2543
  code.line();
2529
2544
  code.block("export function getAll(): Record<string, unknown[]> {", () => {
2530
2545
  code.block("return {", () => {
2531
2546
  for (const schema of schemas) {
2532
- code.line(`${schema.name}: db.${schema.name}.getAll(),`);
2547
+ const safeName = toSafePropertyName(schema.name);
2548
+ code.line(`${safeName}: db.${safeName}.getAll(),`);
2533
2549
  }
2534
2550
  }, "};");
2535
2551
  });