orchid-orm 1.8.0 → 1.9.0

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.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Adapter, Db, anyShape, getClonedQueryData, columnsShapeToCode, primaryKeyToCode, indexToCode, constraintToCode, columnIndexesToCode, columnForeignKeysToCode, columnCheckToCode, identityToCode } from 'pqb';
2
2
  export { OrchidOrmError, OrchidOrmInternalError, columnTypes } from 'pqb';
3
- import { snakeCaseKey, toSnakeCase, emptyObject, pathToLog, toCamelCase, toPascalCase, getImportPath, singleQuote, codeToString, quoteObjectKey, addCode, columnDefaultArgumentToCode, deepCompare } from 'orchid-core';
3
+ import { getCallerFilePath, snakeCaseKey, toSnakeCase, emptyObject, pathToLog, toCamelCase, toPascalCase, getImportPath, singleQuote, codeToString, quoteObjectKey, addCode, columnDefaultArgumentToCode, deepCompare } from 'orchid-core';
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
5
5
  import * as path from 'path';
6
6
  import path__default from 'path';
@@ -9,16 +9,25 @@ import typescript from 'typescript';
9
9
 
10
10
  const createBaseTable = ({
11
11
  columnTypes: columnTypes$1,
12
- snakeCase
12
+ snakeCase,
13
+ filePath
13
14
  } = { columnTypes: columnTypes }) => {
14
15
  const ct = typeof columnTypes$1 === "function" ? columnTypes$1(columnTypes) : columnTypes$1 || columnTypes;
16
+ filePath != null ? filePath : filePath = getCallerFilePath();
17
+ if (!filePath) {
18
+ throw new Error(
19
+ `Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
20
+ );
21
+ }
15
22
  return create(
16
23
  ct,
24
+ filePath,
17
25
  snakeCase
18
26
  );
19
27
  };
20
- const create = (columnTypes, snakeCase) => {
21
- const base = class BaseTable {
28
+ const create = (columnTypes, filePath, snakeCase) => {
29
+ var _a;
30
+ const base = (_a = class {
22
31
  constructor() {
23
32
  this.snakeCase = snakeCase;
24
33
  this.setColumns = (fn) => {
@@ -74,7 +83,7 @@ const create = (columnTypes, snakeCase) => {
74
83
  options
75
84
  };
76
85
  }
77
- };
86
+ }, _a.filePath = filePath, _a);
78
87
  base.prototype.columnTypes = columnTypes;
79
88
  return base;
80
89
  };
@@ -1774,7 +1783,7 @@ const createTable = async (_a) => {
1774
1783
  "logger"
1775
1784
  ]);
1776
1785
  const tablePath = params.tablePath(toCamelCase(ast.name));
1777
- const baseTablePath = getImportPath(tablePath, params.baseTablePath);
1786
+ const baseTablePath = getImportPath(tablePath, params.baseTable.filePath);
1778
1787
  const props = [];
1779
1788
  if (ast.schema) {
1780
1789
  props.push(`schema = ${singleQuote(ast.schema)};`);
@@ -1789,9 +1798,9 @@ const createTable = async (_a) => {
1789
1798
  "}));"
1790
1799
  );
1791
1800
  const code = [
1792
- `import { ${params.baseTableName} } from '${baseTablePath}';
1801
+ `import { ${params.baseTable.name} } from '${baseTablePath}';
1793
1802
  `,
1794
- `export class ${toPascalCase(ast.name)}Table extends ${params.baseTableName} {`,
1803
+ `export class ${toPascalCase(ast.name)}Table extends ${params.baseTable.name} {`,
1795
1804
  props,
1796
1805
  "}\n"
1797
1806
  ];
@@ -2403,22 +2412,21 @@ const updateTableFile = async (params) => {
2403
2412
  };
2404
2413
 
2405
2414
  const createBaseTableFile = async ({
2406
- baseTableName,
2407
- baseTablePath,
2415
+ baseTable,
2408
2416
  logger
2409
2417
  }) => {
2410
- await fs.mkdir(path__default.dirname(baseTablePath), { recursive: true });
2418
+ await fs.mkdir(path__default.dirname(baseTable.filePath), { recursive: true });
2411
2419
  await fs.writeFile(
2412
- baseTablePath,
2420
+ baseTable.filePath,
2413
2421
  `import { createBaseTable } from 'orchid-orm';
2414
2422
 
2415
- export const ${baseTableName} = createBaseTable();
2423
+ export const ${baseTable.name} = createBaseTable();
2416
2424
  `,
2417
2425
  {
2418
2426
  flag: "wx"
2419
2427
  }
2420
2428
  ).then(() => {
2421
- logger == null ? void 0 : logger.log(`Created ${pathToLog(baseTablePath)}`);
2429
+ logger == null ? void 0 : logger.log(`Created ${pathToLog(baseTable.filePath)}`);
2422
2430
  }).catch((err) => {
2423
2431
  if (err.code === "EEXIST")
2424
2432
  return;
@@ -2449,18 +2457,21 @@ class AppCodeUpdaterError extends Error {
2449
2457
  }
2450
2458
  const appCodeUpdater = ({
2451
2459
  tablePath,
2452
- baseTablePath,
2453
- baseTableName,
2454
2460
  mainFilePath
2455
2461
  }) => {
2456
- return async ({ ast, options, basePath, cache: cacheObject, logger }) => {
2462
+ return async ({
2463
+ ast,
2464
+ options,
2465
+ basePath,
2466
+ cache: cacheObject,
2467
+ logger,
2468
+ baseTable
2469
+ }) => {
2457
2470
  const params = {
2458
2471
  tablePath(name) {
2459
2472
  const file = tablePath(name);
2460
2473
  return resolvePath(basePath, file);
2461
2474
  },
2462
- baseTablePath: resolvePath(basePath, baseTablePath),
2463
- baseTableName: baseTableName || "BaseTable",
2464
2475
  mainFilePath: resolvePath(basePath, mainFilePath),
2465
2476
  logger
2466
2477
  };
@@ -2472,12 +2483,12 @@ const appCodeUpdater = ({
2472
2483
  options,
2473
2484
  logger
2474
2485
  ),
2475
- updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2486
+ updateTableFile(__spreadProps(__spreadValues({}, params), { ast, baseTable }))
2476
2487
  ];
2477
2488
  const cache = cacheObject;
2478
2489
  if (!cache.createdBaseTable) {
2479
2490
  promises.push(
2480
- createBaseTableFile(params).then(() => {
2491
+ createBaseTableFile({ logger: params.logger, baseTable }).then(() => {
2481
2492
  cache.createdBaseTable = true;
2482
2493
  })
2483
2494
  );