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.js CHANGED
@@ -415,13 +415,15 @@ const createTable = async (_a) => {
415
415
  logger,
416
416
  getTable,
417
417
  relations,
418
- tables
418
+ tables,
419
+ delayed
419
420
  } = _b, params = __objRest$3(_b, [
420
421
  "ast",
421
422
  "logger",
422
423
  "getTable",
423
424
  "relations",
424
- "tables"
425
+ "tables",
426
+ "delayed"
425
427
  ]);
426
428
  const key = orchidCore.toCamelCase(ast.name);
427
429
  const tablePath = params.tablePath(key);
@@ -451,36 +453,43 @@ const createTable = async (_a) => {
451
453
  pqb.columnsShapeToCode(ast.shape, ast, "t"),
452
454
  "}));"
453
455
  );
454
- const relCode = await getRelations(
455
- ast,
456
- getTable,
457
- tablePath,
458
- imports,
459
- relations,
460
- ast.name
461
- );
462
- if (relCode) {
463
- props.push("", ...relCode);
464
- }
456
+ const importsCode = importsToCode(imports);
465
457
  const code = [
466
- ...Object.entries(imports).map(
467
- ([from, name]) => `import { ${name} } from '${from}';`
468
- ),
469
- "",
470
458
  `export class ${className} extends ${params.baseTable.exportAs} {`,
471
459
  props,
472
460
  "}\n"
473
461
  ];
474
462
  await fs.mkdir(path.dirname(tablePath), { recursive: true });
475
463
  try {
476
- await fs.writeFile(tablePath, orchidCore.codeToString(code, "", " "), { flag: "wx" });
477
- logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(tablePath)}`);
464
+ const content = importsCode + "\n\n" + orchidCore.codeToString(code, "", " ");
465
+ await fs.writeFile(tablePath, content, { flag: "wx" });
466
+ delayed.push(async () => {
467
+ const imports2 = {};
468
+ const relCode = await getRelations(
469
+ ast,
470
+ getTable,
471
+ tablePath,
472
+ imports2,
473
+ relations,
474
+ ast.name
475
+ );
476
+ if (relCode) {
477
+ const code2 = orchidCore.codeToString(relCode, " ", " ");
478
+ const updated = content.slice(0, importsCode.length) + `
479
+ ${importsToCode(imports2)}` + content.slice(importsCode.length, -2) + " \n" + code2 + "\n" + content.slice(-2);
480
+ await fs.writeFile(tablePath, updated);
481
+ }
482
+ logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(tablePath)}`);
483
+ });
478
484
  } catch (err) {
479
485
  if (err.code !== "EEXIST") {
480
486
  throw err;
481
487
  }
482
488
  }
483
489
  };
490
+ function importsToCode(imports) {
491
+ return Object.entries(imports).map(([from, name]) => `import { ${name} } from '${from}';`).join("\n");
492
+ }
484
493
  const getRelations = async (ast, getTable, tablePath, imports, relations, tableName) => {
485
494
  const refs = [];
486
495
  for (const key in ast.shape) {
@@ -1342,6 +1351,7 @@ class AppCodeUpdaterError extends Error {
1342
1351
  const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1343
1352
  let orm;
1344
1353
  return async (tableName) => {
1354
+ var _a;
1345
1355
  if (tables[tableName])
1346
1356
  return tables[tableName];
1347
1357
  if (!orm) {
@@ -1359,14 +1369,19 @@ const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1359
1369
  }
1360
1370
  for (const key in orm) {
1361
1371
  const table = orm[key];
1362
- if (table && typeof table === "object" && table instanceof pqb.Db && table.table === tableName) {
1363
- const tableInfo = {
1364
- key,
1365
- name: table.name,
1366
- path: table.filePath
1367
- };
1368
- return tables[tableName] = tableInfo;
1369
- }
1372
+ if (!table || typeof table !== "object" || !(table instanceof pqb.Db) || table.table !== tableName)
1373
+ continue;
1374
+ const name = table.name;
1375
+ if (!name)
1376
+ continue;
1377
+ const path3 = (_a = table.getFilePath) == null ? void 0 : _a.call(table);
1378
+ if (!path3)
1379
+ continue;
1380
+ return tables[tableName] = {
1381
+ key,
1382
+ name,
1383
+ path: path3
1384
+ };
1370
1385
  }
1371
1386
  return;
1372
1387
  };
@@ -1404,13 +1419,15 @@ const appCodeUpdater = ({
1404
1419
  options,
1405
1420
  logger
1406
1421
  );
1422
+ const delayed = [];
1407
1423
  const promises = [
1408
1424
  updateTableFile(__spreadProps(__spreadValues({}, params), {
1409
1425
  ast,
1410
1426
  baseTable,
1411
1427
  getTable,
1412
1428
  relations: cache.relations,
1413
- tables: cache.tables
1429
+ tables: cache.tables,
1430
+ delayed
1414
1431
  }))
1415
1432
  ];
1416
1433
  if (!cache.createdBaseTable) {
@@ -1421,6 +1438,7 @@ const appCodeUpdater = ({
1421
1438
  );
1422
1439
  }
1423
1440
  await Promise.all(promises);
1441
+ await Promise.all(delayed.map((fn) => fn()));
1424
1442
  },
1425
1443
  async afterAll({ cache, logger }) {
1426
1444
  const { relations } = cache;