orchid-orm 1.17.32 → 1.17.34

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/codegen/index.mjs CHANGED
@@ -395,13 +395,15 @@ const createTable = async (_a) => {
395
395
  logger,
396
396
  getTable,
397
397
  relations,
398
- tables
398
+ tables,
399
+ delayed
399
400
  } = _b, params = __objRest$3(_b, [
400
401
  "ast",
401
402
  "logger",
402
403
  "getTable",
403
404
  "relations",
404
- "tables"
405
+ "tables",
406
+ "delayed"
405
407
  ]);
406
408
  const key = toCamelCase(ast.name);
407
409
  const tablePath = params.tablePath(key);
@@ -431,36 +433,43 @@ const createTable = async (_a) => {
431
433
  columnsShapeToCode(ast.shape, ast, "t"),
432
434
  "}));"
433
435
  );
434
- const relCode = await getRelations(
435
- ast,
436
- getTable,
437
- tablePath,
438
- imports,
439
- relations,
440
- ast.name
441
- );
442
- if (relCode) {
443
- props.push("", ...relCode);
444
- }
436
+ const importsCode = importsToCode(imports);
445
437
  const code = [
446
- ...Object.entries(imports).map(
447
- ([from, name]) => `import { ${name} } from '${from}';`
448
- ),
449
- "",
450
438
  `export class ${className} extends ${params.baseTable.exportAs} {`,
451
439
  props,
452
440
  "}\n"
453
441
  ];
454
442
  await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
455
443
  try {
456
- await fs.writeFile(tablePath, codeToString(code, "", " "), { flag: "wx" });
457
- logger == null ? void 0 : logger.log(`Created ${pathToLog(tablePath)}`);
444
+ const content = importsCode + "\n\n" + codeToString(code, "", " ");
445
+ await fs.writeFile(tablePath, content, { flag: "wx" });
446
+ delayed.push(async () => {
447
+ const imports2 = {};
448
+ const relCode = await getRelations(
449
+ ast,
450
+ getTable,
451
+ tablePath,
452
+ imports2,
453
+ relations,
454
+ ast.name
455
+ );
456
+ if (relCode) {
457
+ const code2 = codeToString(relCode, " ", " ");
458
+ const updated = content.slice(0, importsCode.length) + `
459
+ ${importsToCode(imports2)}` + content.slice(importsCode.length, -2) + " \n" + code2 + "\n" + content.slice(-2);
460
+ await fs.writeFile(tablePath, updated);
461
+ }
462
+ logger == null ? void 0 : logger.log(`Created ${pathToLog(tablePath)}`);
463
+ });
458
464
  } catch (err) {
459
465
  if (err.code !== "EEXIST") {
460
466
  throw err;
461
467
  }
462
468
  }
463
469
  };
470
+ function importsToCode(imports) {
471
+ return Object.entries(imports).map(([from, name]) => `import { ${name} } from '${from}';`).join("\n");
472
+ }
464
473
  const getRelations = async (ast, getTable, tablePath, imports, relations, tableName) => {
465
474
  const refs = [];
466
475
  for (const key in ast.shape) {
@@ -502,7 +511,10 @@ const getRelations = async (ast, getTable, tablePath, imports, relations, tableN
502
511
  imports[path2] = info.name;
503
512
  code.push(
504
513
  `${info.key}: this.belongsTo(() => ${info.name}, {`,
505
- [`primaryKey: '${foreignColumns[0]}',`, `foreignKey: '${columns[0]}',`],
514
+ [
515
+ `columns: [${foreignColumns.map(singleQuote).join(", ")}],`,
516
+ `references: [${columns.map(singleQuote).join(", ")}],`
517
+ ],
506
518
  "}),"
507
519
  );
508
520
  await handleForeignKey({
@@ -1322,6 +1334,7 @@ class AppCodeUpdaterError extends Error {
1322
1334
  const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1323
1335
  let orm;
1324
1336
  return async (tableName) => {
1337
+ var _a;
1325
1338
  if (tables[tableName])
1326
1339
  return tables[tableName];
1327
1340
  if (!orm) {
@@ -1339,14 +1352,19 @@ const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1339
1352
  }
1340
1353
  for (const key in orm) {
1341
1354
  const table = orm[key];
1342
- if (table && typeof table === "object" && table instanceof Db && table.table === tableName) {
1343
- const tableInfo = {
1344
- key,
1345
- name: table.name,
1346
- path: table.filePath
1347
- };
1348
- return tables[tableName] = tableInfo;
1349
- }
1355
+ if (!table || typeof table !== "object" || !(table instanceof Db) || table.table !== tableName)
1356
+ continue;
1357
+ const name = table.name;
1358
+ if (!name)
1359
+ continue;
1360
+ const path3 = (_a = table.getFilePath) == null ? void 0 : _a.call(table);
1361
+ if (!path3)
1362
+ continue;
1363
+ return tables[tableName] = {
1364
+ key,
1365
+ name,
1366
+ path: path3
1367
+ };
1350
1368
  }
1351
1369
  return;
1352
1370
  };
@@ -1384,13 +1402,15 @@ const appCodeUpdater = ({
1384
1402
  options,
1385
1403
  logger
1386
1404
  );
1405
+ const delayed = [];
1387
1406
  const promises = [
1388
1407
  updateTableFile(__spreadProps(__spreadValues({}, params), {
1389
1408
  ast,
1390
1409
  baseTable,
1391
1410
  getTable,
1392
1411
  relations: cache.relations,
1393
- tables: cache.tables
1412
+ tables: cache.tables,
1413
+ delayed
1394
1414
  }))
1395
1415
  ];
1396
1416
  if (!cache.createdBaseTable) {
@@ -1401,6 +1421,7 @@ const appCodeUpdater = ({
1401
1421
  );
1402
1422
  }
1403
1423
  await Promise.all(promises);
1424
+ await Promise.all(delayed.map((fn) => fn()));
1404
1425
  },
1405
1426
  async afterAll({ cache, logger }) {
1406
1427
  const { relations } = cache;