orchid-orm 1.8.1 → 1.9.1

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/index.js CHANGED
@@ -28,16 +28,25 @@ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
28
28
 
29
29
  const createBaseTable = ({
30
30
  columnTypes,
31
- snakeCase
31
+ snakeCase,
32
+ filePath
32
33
  } = { columnTypes: pqb.columnTypes }) => {
33
34
  const ct = typeof columnTypes === "function" ? columnTypes(pqb.columnTypes) : columnTypes || pqb.columnTypes;
35
+ filePath != null ? filePath : filePath = orchidCore.getCallerFilePath();
36
+ if (!filePath) {
37
+ throw new Error(
38
+ `Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
39
+ );
40
+ }
34
41
  return create(
35
42
  ct,
43
+ filePath,
36
44
  snakeCase
37
45
  );
38
46
  };
39
- const create = (columnTypes, snakeCase) => {
40
- const base = class BaseTable {
47
+ const create = (columnTypes, filePath, snakeCase) => {
48
+ var _a;
49
+ const base = (_a = class {
41
50
  constructor() {
42
51
  this.snakeCase = snakeCase;
43
52
  this.setColumns = (fn) => {
@@ -93,7 +102,7 @@ const create = (columnTypes, snakeCase) => {
93
102
  options
94
103
  };
95
104
  }
96
- };
105
+ }, _a.filePath = filePath, _a);
97
106
  base.prototype.columnTypes = columnTypes;
98
107
  return base;
99
108
  };
@@ -1793,7 +1802,7 @@ const createTable = async (_a) => {
1793
1802
  "logger"
1794
1803
  ]);
1795
1804
  const tablePath = params.tablePath(orchidCore.toCamelCase(ast.name));
1796
- const baseTablePath = orchidCore.getImportPath(tablePath, params.baseTablePath);
1805
+ const baseTablePath = orchidCore.getImportPath(tablePath, params.baseTable.filePath);
1797
1806
  const props = [];
1798
1807
  if (ast.schema) {
1799
1808
  props.push(`schema = ${orchidCore.singleQuote(ast.schema)};`);
@@ -1808,9 +1817,9 @@ const createTable = async (_a) => {
1808
1817
  "}));"
1809
1818
  );
1810
1819
  const code = [
1811
- `import { ${params.baseTableName} } from '${baseTablePath}';
1820
+ `import { ${params.baseTable.name} } from '${baseTablePath}';
1812
1821
  `,
1813
- `export class ${orchidCore.toPascalCase(ast.name)}Table extends ${params.baseTableName} {`,
1822
+ `export class ${orchidCore.toPascalCase(ast.name)}Table extends ${params.baseTable.name} {`,
1814
1823
  props,
1815
1824
  "}\n"
1816
1825
  ];
@@ -2422,22 +2431,21 @@ const updateTableFile = async (params) => {
2422
2431
  };
2423
2432
 
2424
2433
  const createBaseTableFile = async ({
2425
- baseTableName,
2426
- baseTablePath,
2434
+ baseTable,
2427
2435
  logger
2428
2436
  }) => {
2429
- await fs.mkdir(path.dirname(baseTablePath), { recursive: true });
2437
+ await fs.mkdir(path.dirname(baseTable.filePath), { recursive: true });
2430
2438
  await fs.writeFile(
2431
- baseTablePath,
2439
+ baseTable.filePath,
2432
2440
  `import { createBaseTable } from 'orchid-orm';
2433
2441
 
2434
- export const ${baseTableName} = createBaseTable();
2442
+ export const ${baseTable.name} = createBaseTable();
2435
2443
  `,
2436
2444
  {
2437
2445
  flag: "wx"
2438
2446
  }
2439
2447
  ).then(() => {
2440
- logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(baseTablePath)}`);
2448
+ logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(baseTable.filePath)}`);
2441
2449
  }).catch((err) => {
2442
2450
  if (err.code === "EEXIST")
2443
2451
  return;
@@ -2468,18 +2476,21 @@ class AppCodeUpdaterError extends Error {
2468
2476
  }
2469
2477
  const appCodeUpdater = ({
2470
2478
  tablePath,
2471
- baseTablePath,
2472
- baseTableName,
2473
2479
  mainFilePath
2474
2480
  }) => {
2475
- return async ({ ast, options, basePath, cache: cacheObject, logger }) => {
2481
+ return async ({
2482
+ ast,
2483
+ options,
2484
+ basePath,
2485
+ cache: cacheObject,
2486
+ logger,
2487
+ baseTable
2488
+ }) => {
2476
2489
  const params = {
2477
2490
  tablePath(name) {
2478
2491
  const file = tablePath(name);
2479
2492
  return resolvePath(basePath, file);
2480
2493
  },
2481
- baseTablePath: resolvePath(basePath, baseTablePath),
2482
- baseTableName: baseTableName || "BaseTable",
2483
2494
  mainFilePath: resolvePath(basePath, mainFilePath),
2484
2495
  logger
2485
2496
  };
@@ -2491,12 +2502,12 @@ const appCodeUpdater = ({
2491
2502
  options,
2492
2503
  logger
2493
2504
  ),
2494
- updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2505
+ updateTableFile(__spreadProps(__spreadValues({}, params), { ast, baseTable }))
2495
2506
  ];
2496
2507
  const cache = cacheObject;
2497
2508
  if (!cache.createdBaseTable) {
2498
2509
  promises.push(
2499
- createBaseTableFile(params).then(() => {
2510
+ createBaseTableFile({ logger: params.logger, baseTable }).then(() => {
2500
2511
  cache.createdBaseTable = true;
2501
2512
  })
2502
2513
  );