orchid-orm 1.6.1 → 1.6.3

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,13 +1,16 @@
1
1
  import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Db, Adapter, anyShape, getClonedQueryData, columnsShapeToCode, codeToString, primaryKeyToCode, indexToCode, foreignKeyToCode, columnIndexesToCode, columnForeignKeysToCode, columnDefaultArgumentToCode } from 'pqb';
2
2
  export { columnTypes } from 'pqb';
3
- import { emptyObject, singleQuote, quoteObjectKey, addCode } from 'orchid-core';
3
+ import { snakeCaseKey, emptyObject, pathToLog, singleQuote, quoteObjectKey, addCode } from 'orchid-core';
4
4
  import * as path from 'path';
5
5
  import path__default from 'path';
6
6
  import fs from 'fs/promises';
7
7
  import typescript from 'typescript';
8
8
 
9
9
  const createBaseTable = (options = { columnTypes }) => {
10
- const ct = typeof options.columnTypes === "function" ? options.columnTypes(columnTypes) : options.columnTypes;
10
+ const ct = typeof options.columnTypes === "function" ? options.columnTypes(columnTypes) : options.columnTypes || columnTypes;
11
+ if (options.snakeCase) {
12
+ ct[snakeCaseKey] = true;
13
+ }
11
14
  return create(ct);
12
15
  };
13
16
  const create = (columnTypes2) => {
@@ -1208,8 +1211,11 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
1208
1211
  toTable._take();
1209
1212
  }
1210
1213
  const query = this.isSubQuery ? toTable : toTable._whereExists(
1211
- this.baseQuery,
1212
- (q) => data.reverseJoin(this, toTable)
1214
+ this,
1215
+ (q) => data.reverseJoin(
1216
+ q,
1217
+ toTable
1218
+ )
1213
1219
  );
1214
1220
  query.query[relationQueryKey] = {
1215
1221
  relationName,
@@ -1597,7 +1603,7 @@ const optionsToString = (options) => {
1597
1603
  }
1598
1604
  return lines.join("\n ");
1599
1605
  };
1600
- const updateMainFile = async (filePath, tablePath, ast, options) => {
1606
+ const updateMainFile = async (filePath, tablePath, ast, options, logger) => {
1601
1607
  const result = await fs.readFile(filePath, "utf-8").then(
1602
1608
  (content2) => ({ error: void 0, content: content2 }),
1603
1609
  (error) => {
@@ -1644,6 +1650,9 @@ const updateMainFile = async (filePath, tablePath, ast, options) => {
1644
1650
  await fs.mkdir(path__default.dirname(filePath), { recursive: true });
1645
1651
  }
1646
1652
  await fs.writeFile(filePath, write);
1653
+ logger == null ? void 0 : logger.log(
1654
+ `${result.content ? "Updated" : "Created"} ${pathToLog(filePath)}`
1655
+ );
1647
1656
  }
1648
1657
  };
1649
1658
  const createTable$1 = ({ filePath, tablePath, statements, object, content, spaces }, ast) => {
@@ -1739,9 +1748,11 @@ var __objRest$2 = (source, exclude) => {
1739
1748
  };
1740
1749
  const createTable = async (_a) => {
1741
1750
  var _b = _a, {
1742
- ast
1751
+ ast,
1752
+ logger
1743
1753
  } = _b, params = __objRest$2(_b, [
1744
- "ast"
1754
+ "ast",
1755
+ "logger"
1745
1756
  ]);
1746
1757
  const tablePath = params.tablePath(ast.name);
1747
1758
  const baseTablePath = getImportPath(tablePath, params.baseTablePath);
@@ -1768,6 +1779,7 @@ const createTable = async (_a) => {
1768
1779
  await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
1769
1780
  try {
1770
1781
  await fs.writeFile(tablePath, codeToString(code, "", " "), { flag: "wx" });
1782
+ logger == null ? void 0 : logger.log(`Created ${pathToLog(tablePath)}`);
1771
1783
  } catch (err) {
1772
1784
  if (err.code !== "EEXIST") {
1773
1785
  throw err;
@@ -1792,9 +1804,11 @@ var __objRest$1 = (source, exclude) => {
1792
1804
  };
1793
1805
  const changeTable = async (_a) => {
1794
1806
  var _b = _a, {
1795
- ast
1807
+ ast,
1808
+ logger
1796
1809
  } = _b, params = __objRest$1(_b, [
1797
- "ast"
1810
+ "ast",
1811
+ "logger"
1798
1812
  ]);
1799
1813
  const tablePath = params.tablePath(ast.name);
1800
1814
  const content = await fs.readFile(tablePath, "utf-8").catch(() => void 0);
@@ -1812,6 +1826,7 @@ const changeTable = async (_a) => {
1812
1826
  addTableData(context);
1813
1827
  }
1814
1828
  await fs.writeFile(tablePath, changes.apply());
1829
+ logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
1815
1830
  };
1816
1831
  function* iterateColumnsShapes(statements, className) {
1817
1832
  var _a, _b;
@@ -1982,7 +1997,7 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
1982
1997
  continue;
1983
1998
  let remove = true;
1984
1999
  if (!replaced[key]) {
1985
- const code = getColumnMethodArgs(to, key);
2000
+ const code = getColumnMethodArgs(t, to, key);
1986
2001
  if (code) {
1987
2002
  changes.replace(
1988
2003
  item.expression.expression.end,
@@ -2002,7 +2017,7 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
2002
2017
  for (const key in propsToChange) {
2003
2018
  if (changedProps[key])
2004
2019
  continue;
2005
- const code = getColumnMethodArgs(to, key);
2020
+ const code = getColumnMethodArgs(t, to, key);
2006
2021
  if (code) {
2007
2022
  append += codeToString(code, spaces + " ", " ").trim();
2008
2023
  }
@@ -2055,7 +2070,7 @@ ${spaces}`);
2055
2070
  ${spaces}`);
2056
2071
  }
2057
2072
  };
2058
- const getColumnMethodArgs = (to, key) => {
2073
+ const getColumnMethodArgs = (t, to, key) => {
2059
2074
  const value = to[key];
2060
2075
  if (!value)
2061
2076
  return;
@@ -2069,7 +2084,7 @@ const getColumnMethodArgs = (to, key) => {
2069
2084
  if (key === "collate" || key === "compression") {
2070
2085
  addCode(code, singleQuote(value));
2071
2086
  } else if (key === "default") {
2072
- addCode(code, columnDefaultArgumentToCode(value));
2087
+ addCode(code, columnDefaultArgumentToCode(t, value));
2073
2088
  } else if (key !== "nullable" && key !== "primaryKey") {
2074
2089
  return;
2075
2090
  }
@@ -2202,9 +2217,11 @@ var __objRest = (source, exclude) => {
2202
2217
  };
2203
2218
  const renameTable = async (_a) => {
2204
2219
  var _b = _a, {
2205
- ast
2220
+ ast,
2221
+ logger
2206
2222
  } = _b, params = __objRest(_b, [
2207
- "ast"
2223
+ "ast",
2224
+ "logger"
2208
2225
  ]);
2209
2226
  var _a2;
2210
2227
  const tablePath = params.tablePath(ast.from);
@@ -2249,6 +2266,7 @@ const renameTable = async (_a) => {
2249
2266
  }
2250
2267
  }
2251
2268
  await fs.writeFile(tablePath, changes.apply());
2269
+ logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
2252
2270
  };
2253
2271
 
2254
2272
  var __defProp$1 = Object.defineProperty;
@@ -2283,7 +2301,8 @@ const updateTableFile = async (params) => {
2283
2301
 
2284
2302
  const createBaseTableFile = async ({
2285
2303
  baseTableName,
2286
- baseTablePath
2304
+ baseTablePath,
2305
+ logger
2287
2306
  }) => {
2288
2307
  await fs.mkdir(path__default.dirname(baseTablePath), { recursive: true });
2289
2308
  await fs.writeFile(
@@ -2295,7 +2314,9 @@ export const ${baseTableName} = createBaseTable();
2295
2314
  {
2296
2315
  flag: "wx"
2297
2316
  }
2298
- ).catch((err) => {
2317
+ ).then(() => {
2318
+ logger == null ? void 0 : logger.log(`Created ${pathToLog(baseTablePath)}`);
2319
+ }).catch((err) => {
2299
2320
  if (err.code === "EEXIST")
2300
2321
  return;
2301
2322
  throw err;
@@ -2329,7 +2350,7 @@ const appCodeUpdater = ({
2329
2350
  baseTableName,
2330
2351
  mainFilePath
2331
2352
  }) => {
2332
- return async ({ ast, options, basePath, cache: cacheObject }) => {
2353
+ return async ({ ast, options, basePath, cache: cacheObject, logger }) => {
2333
2354
  const params = {
2334
2355
  tablePath(name) {
2335
2356
  const file = tablePath(name);
@@ -2337,10 +2358,17 @@ const appCodeUpdater = ({
2337
2358
  },
2338
2359
  baseTablePath: resolvePath(basePath, baseTablePath),
2339
2360
  baseTableName: baseTableName || "BaseTable",
2340
- mainFilePath: resolvePath(basePath, mainFilePath)
2361
+ mainFilePath: resolvePath(basePath, mainFilePath),
2362
+ logger
2341
2363
  };
2342
2364
  const promises = [
2343
- updateMainFile(params.mainFilePath, params.tablePath, ast, options),
2365
+ updateMainFile(
2366
+ params.mainFilePath,
2367
+ params.tablePath,
2368
+ ast,
2369
+ options,
2370
+ logger
2371
+ ),
2344
2372
  updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2345
2373
  ];
2346
2374
  const cache = cacheObject;