orchid-orm 1.17.32 → 1.17.33

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) {
@@ -1322,6 +1331,7 @@ class AppCodeUpdaterError extends Error {
1322
1331
  const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1323
1332
  let orm;
1324
1333
  return async (tableName) => {
1334
+ var _a;
1325
1335
  if (tables[tableName])
1326
1336
  return tables[tableName];
1327
1337
  if (!orm) {
@@ -1339,14 +1349,19 @@ const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1339
1349
  }
1340
1350
  for (const key in orm) {
1341
1351
  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
- }
1352
+ if (!table || typeof table !== "object" || !(table instanceof Db) || table.table !== tableName)
1353
+ continue;
1354
+ const name = table.name;
1355
+ if (!name)
1356
+ continue;
1357
+ const path3 = (_a = table.getFilePath) == null ? void 0 : _a.call(table);
1358
+ if (!path3)
1359
+ continue;
1360
+ return tables[tableName] = {
1361
+ key,
1362
+ name,
1363
+ path: path3
1364
+ };
1350
1365
  }
1351
1366
  return;
1352
1367
  };
@@ -1384,13 +1399,15 @@ const appCodeUpdater = ({
1384
1399
  options,
1385
1400
  logger
1386
1401
  );
1402
+ const delayed = [];
1387
1403
  const promises = [
1388
1404
  updateTableFile(__spreadProps(__spreadValues({}, params), {
1389
1405
  ast,
1390
1406
  baseTable,
1391
1407
  getTable,
1392
1408
  relations: cache.relations,
1393
- tables: cache.tables
1409
+ tables: cache.tables,
1410
+ delayed
1394
1411
  }))
1395
1412
  ];
1396
1413
  if (!cache.createdBaseTable) {
@@ -1401,6 +1418,7 @@ const appCodeUpdater = ({
1401
1418
  );
1402
1419
  }
1403
1420
  await Promise.all(promises);
1421
+ await Promise.all(delayed.map((fn) => fn()));
1404
1422
  },
1405
1423
  async afterAll({ cache, logger }) {
1406
1424
  const { relations } = cache;