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.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) {
@@ -522,7 +531,10 @@ const getRelations = async (ast, getTable, tablePath, imports, relations, tableN
522
531
  imports[path2] = info.name;
523
532
  code.push(
524
533
  `${info.key}: this.belongsTo(() => ${info.name}, {`,
525
- [`primaryKey: '${foreignColumns[0]}',`, `foreignKey: '${columns[0]}',`],
534
+ [
535
+ `columns: [${foreignColumns.map(orchidCore.singleQuote).join(", ")}],`,
536
+ `references: [${columns.map(orchidCore.singleQuote).join(", ")}],`
537
+ ],
526
538
  "}),"
527
539
  );
528
540
  await handleForeignKey({
@@ -1342,6 +1354,7 @@ class AppCodeUpdaterError extends Error {
1342
1354
  const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1343
1355
  let orm;
1344
1356
  return async (tableName) => {
1357
+ var _a;
1345
1358
  if (tables[tableName])
1346
1359
  return tables[tableName];
1347
1360
  if (!orm) {
@@ -1359,14 +1372,19 @@ const makeGetTable = (path2, ormExportedAs, tables, imp) => {
1359
1372
  }
1360
1373
  for (const key in orm) {
1361
1374
  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
- }
1375
+ if (!table || typeof table !== "object" || !(table instanceof pqb.Db) || table.table !== tableName)
1376
+ continue;
1377
+ const name = table.name;
1378
+ if (!name)
1379
+ continue;
1380
+ const path3 = (_a = table.getFilePath) == null ? void 0 : _a.call(table);
1381
+ if (!path3)
1382
+ continue;
1383
+ return tables[tableName] = {
1384
+ key,
1385
+ name,
1386
+ path: path3
1387
+ };
1370
1388
  }
1371
1389
  return;
1372
1390
  };
@@ -1404,13 +1422,15 @@ const appCodeUpdater = ({
1404
1422
  options,
1405
1423
  logger
1406
1424
  );
1425
+ const delayed = [];
1407
1426
  const promises = [
1408
1427
  updateTableFile(__spreadProps(__spreadValues({}, params), {
1409
1428
  ast,
1410
1429
  baseTable,
1411
1430
  getTable,
1412
1431
  relations: cache.relations,
1413
- tables: cache.tables
1432
+ tables: cache.tables,
1433
+ delayed
1414
1434
  }))
1415
1435
  ];
1416
1436
  if (!cache.createdBaseTable) {
@@ -1421,6 +1441,7 @@ const appCodeUpdater = ({
1421
1441
  );
1422
1442
  }
1423
1443
  await Promise.all(promises);
1444
+ await Promise.all(delayed.map((fn) => fn()));
1424
1445
  },
1425
1446
  async afterAll({ cache, logger }) {
1426
1447
  const { relations } = cache;