orchid-orm 1.4.20 → 1.4.21

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
@@ -1594,8 +1594,38 @@ const getImportPath = (from, to) => {
1594
1594
 
1595
1595
  const libraryName = "orchid-orm";
1596
1596
  const importKey = "orchidORM";
1597
- const updateMainFile = async (path, tablePath, ast) => {
1598
- const content = await fs__default["default"].readFile(path, "utf-8");
1597
+ const newFile = (options) => `import { orchidORM } from 'orchid-orm';
1598
+
1599
+ export const db = orchidORM(
1600
+ {
1601
+ ${optionsToString(options)}
1602
+ },
1603
+ {
1604
+ }
1605
+ );
1606
+ `;
1607
+ const optionsToString = (options) => {
1608
+ const lines = [];
1609
+ for (const key in options) {
1610
+ const value = options[key];
1611
+ if (typeof value !== "object" && typeof value !== "function") {
1612
+ lines.push(
1613
+ `${key}: ${typeof value === "string" ? pqb.singleQuote(value) : value},`
1614
+ );
1615
+ }
1616
+ }
1617
+ return lines.join("\n ");
1618
+ };
1619
+ const updateMainFile = async (filePath, tablePath, ast, options) => {
1620
+ const result = await fs__default["default"].readFile(filePath, "utf-8").then(
1621
+ (content2) => ({ error: void 0, content: content2 }),
1622
+ (error) => {
1623
+ return { error, content: void 0 };
1624
+ }
1625
+ );
1626
+ if (result.error && result.error.code !== "ENOENT")
1627
+ throw result.error;
1628
+ const content = result.content || newFile(options);
1599
1629
  const statements = ts.getStatements(content);
1600
1630
  const importName = ts.import.getStatementsImportedName(
1601
1631
  statements,
@@ -1613,26 +1643,33 @@ const updateMainFile = async (path, tablePath, ast) => {
1613
1643
  }
1614
1644
  const spaces = ts.spaces.getAtLine(content, object.end);
1615
1645
  const context = {
1616
- path,
1646
+ filePath,
1617
1647
  tablePath,
1618
1648
  statements,
1619
1649
  object,
1620
1650
  content,
1621
1651
  spaces
1622
1652
  };
1653
+ let write;
1623
1654
  if (ast.type === "table") {
1624
1655
  if (ast.action === "create") {
1625
- return fs__default["default"].writeFile(path, createTable$1(context, ast));
1656
+ write = createTable$1(context, ast);
1626
1657
  } else {
1627
- return fs__default["default"].writeFile(path, dropTable(context, ast));
1658
+ write = dropTable(context, ast);
1628
1659
  }
1629
1660
  }
1661
+ if (write) {
1662
+ if (result.error) {
1663
+ await fs__default["default"].mkdir(path__default["default"].dirname(filePath), { recursive: true });
1664
+ }
1665
+ await fs__default["default"].writeFile(filePath, write);
1666
+ }
1630
1667
  };
1631
- const createTable$1 = ({ path, tablePath, statements, object, content, spaces }, ast) => {
1668
+ const createTable$1 = ({ filePath, tablePath, statements, object, content, spaces }, ast) => {
1632
1669
  const key = toCamelCase(ast.name);
1633
1670
  const value = toPascalCase(ast.name);
1634
1671
  const changes = new FileChanges(content);
1635
- const importPath = getImportPath(path, tablePath(ast.name));
1672
+ const importPath = getImportPath(filePath, tablePath(ast.name));
1636
1673
  const importPos = ts.import.getEndPos(statements);
1637
1674
  changes.add(
1638
1675
  importPos,
@@ -1650,9 +1687,9 @@ ${spaces}`;
1650
1687
  changes.add(object.properties.end, insert);
1651
1688
  return changes.apply();
1652
1689
  };
1653
- const dropTable = ({ path, tablePath, statements, object, content }, ast) => {
1690
+ const dropTable = ({ filePath, tablePath, statements, object, content }, ast) => {
1654
1691
  const changes = new FileChanges(content);
1655
- const importPath = getImportPath(path, tablePath(ast.name));
1692
+ const importPath = getImportPath(filePath, tablePath(ast.name));
1656
1693
  const tableClassName = toPascalCase(ast.name);
1657
1694
  const importNames = [];
1658
1695
  for (const node of ts.import.iterateWithSource(statements, importPath)) {
@@ -1731,8 +1768,9 @@ const createTable = async (_a) => {
1731
1768
  `,
1732
1769
  `export class ${toPascalCase(ast.name)} extends ${params.baseTableName} {`,
1733
1770
  props,
1734
- "}"
1771
+ "}\n"
1735
1772
  ];
1773
+ await fs__default["default"].mkdir(path__default["default"].dirname(tablePath), { recursive: true });
1736
1774
  await fs__default["default"].writeFile(tablePath, pqb.codeToString(code, "", " "));
1737
1775
  };
1738
1776
 
@@ -2205,6 +2243,32 @@ const updateTableFile = async (params) => {
2205
2243
  }
2206
2244
  };
2207
2245
 
2246
+ const createBaseTableFile = async ({
2247
+ baseTableName,
2248
+ baseTablePath
2249
+ }) => {
2250
+ await fs__default["default"].mkdir(path__default["default"].dirname(baseTablePath), { recursive: true });
2251
+ await fs__default["default"].writeFile(
2252
+ baseTablePath,
2253
+ `import { createBaseTable } from 'orchid-orm';
2254
+ import { columnTypes } from 'pqb';
2255
+
2256
+ export const ${baseTableName} = createBaseTable({
2257
+ columnTypes: {
2258
+ ...columnTypes,
2259
+ },
2260
+ });
2261
+ `,
2262
+ {
2263
+ flag: "wx"
2264
+ }
2265
+ ).catch((err) => {
2266
+ if (err.code === "EEXIST")
2267
+ return;
2268
+ throw err;
2269
+ });
2270
+ };
2271
+
2208
2272
  var __defProp = Object.defineProperty;
2209
2273
  var __defProps = Object.defineProperties;
2210
2274
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -2231,11 +2295,20 @@ const appCodeUpdater = (config) => {
2231
2295
  tablePath: (name) => path__namespace.resolve(config.tablePath(name)),
2232
2296
  mainFilePath: path__namespace.resolve(config.mainFilePath)
2233
2297
  });
2234
- return async (ast) => {
2235
- await Promise.all([
2236
- updateMainFile(params.mainFilePath, params.tablePath, ast),
2298
+ return async ({ ast, options, cache: cacheObject }) => {
2299
+ const promises = [
2300
+ updateMainFile(params.mainFilePath, params.tablePath, ast, options),
2237
2301
  updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2238
- ]);
2302
+ ];
2303
+ const cache = cacheObject;
2304
+ if (!cache.createdBaseTable) {
2305
+ promises.push(
2306
+ createBaseTableFile(params).then(() => {
2307
+ cache.createdBaseTable = true;
2308
+ })
2309
+ );
2310
+ }
2311
+ await Promise.all(promises);
2239
2312
  };
2240
2313
  };
2241
2314