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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # orchid-orm
2
2
 
3
+ ## 1.4.21
4
+
5
+ ### Patch Changes
6
+
7
+ - Add --code cli argument to rake-db
8
+ - Improve codegen
9
+ - Updated dependencies
10
+ - Updated dependencies
11
+ - pqb@0.8.4
12
+
3
13
  ## 1.4.19
4
14
 
5
15
  ### Patch Changes
package/dist/index.esm.js CHANGED
@@ -1567,8 +1567,38 @@ const getImportPath = (from, to) => {
1567
1567
 
1568
1568
  const libraryName = "orchid-orm";
1569
1569
  const importKey = "orchidORM";
1570
- const updateMainFile = async (path, tablePath, ast) => {
1571
- const content = await fs.readFile(path, "utf-8");
1570
+ const newFile = (options) => `import { orchidORM } from 'orchid-orm';
1571
+
1572
+ export const db = orchidORM(
1573
+ {
1574
+ ${optionsToString(options)}
1575
+ },
1576
+ {
1577
+ }
1578
+ );
1579
+ `;
1580
+ const optionsToString = (options) => {
1581
+ const lines = [];
1582
+ for (const key in options) {
1583
+ const value = options[key];
1584
+ if (typeof value !== "object" && typeof value !== "function") {
1585
+ lines.push(
1586
+ `${key}: ${typeof value === "string" ? singleQuote(value) : value},`
1587
+ );
1588
+ }
1589
+ }
1590
+ return lines.join("\n ");
1591
+ };
1592
+ const updateMainFile = async (filePath, tablePath, ast, options) => {
1593
+ const result = await fs.readFile(filePath, "utf-8").then(
1594
+ (content2) => ({ error: void 0, content: content2 }),
1595
+ (error) => {
1596
+ return { error, content: void 0 };
1597
+ }
1598
+ );
1599
+ if (result.error && result.error.code !== "ENOENT")
1600
+ throw result.error;
1601
+ const content = result.content || newFile(options);
1572
1602
  const statements = ts.getStatements(content);
1573
1603
  const importName = ts.import.getStatementsImportedName(
1574
1604
  statements,
@@ -1586,26 +1616,33 @@ const updateMainFile = async (path, tablePath, ast) => {
1586
1616
  }
1587
1617
  const spaces = ts.spaces.getAtLine(content, object.end);
1588
1618
  const context = {
1589
- path,
1619
+ filePath,
1590
1620
  tablePath,
1591
1621
  statements,
1592
1622
  object,
1593
1623
  content,
1594
1624
  spaces
1595
1625
  };
1626
+ let write;
1596
1627
  if (ast.type === "table") {
1597
1628
  if (ast.action === "create") {
1598
- return fs.writeFile(path, createTable$1(context, ast));
1629
+ write = createTable$1(context, ast);
1599
1630
  } else {
1600
- return fs.writeFile(path, dropTable(context, ast));
1631
+ write = dropTable(context, ast);
1601
1632
  }
1602
1633
  }
1634
+ if (write) {
1635
+ if (result.error) {
1636
+ await fs.mkdir(path__default.dirname(filePath), { recursive: true });
1637
+ }
1638
+ await fs.writeFile(filePath, write);
1639
+ }
1603
1640
  };
1604
- const createTable$1 = ({ path, tablePath, statements, object, content, spaces }, ast) => {
1641
+ const createTable$1 = ({ filePath, tablePath, statements, object, content, spaces }, ast) => {
1605
1642
  const key = toCamelCase(ast.name);
1606
1643
  const value = toPascalCase(ast.name);
1607
1644
  const changes = new FileChanges(content);
1608
- const importPath = getImportPath(path, tablePath(ast.name));
1645
+ const importPath = getImportPath(filePath, tablePath(ast.name));
1609
1646
  const importPos = ts.import.getEndPos(statements);
1610
1647
  changes.add(
1611
1648
  importPos,
@@ -1623,9 +1660,9 @@ ${spaces}`;
1623
1660
  changes.add(object.properties.end, insert);
1624
1661
  return changes.apply();
1625
1662
  };
1626
- const dropTable = ({ path, tablePath, statements, object, content }, ast) => {
1663
+ const dropTable = ({ filePath, tablePath, statements, object, content }, ast) => {
1627
1664
  const changes = new FileChanges(content);
1628
- const importPath = getImportPath(path, tablePath(ast.name));
1665
+ const importPath = getImportPath(filePath, tablePath(ast.name));
1629
1666
  const tableClassName = toPascalCase(ast.name);
1630
1667
  const importNames = [];
1631
1668
  for (const node of ts.import.iterateWithSource(statements, importPath)) {
@@ -1704,8 +1741,9 @@ const createTable = async (_a) => {
1704
1741
  `,
1705
1742
  `export class ${toPascalCase(ast.name)} extends ${params.baseTableName} {`,
1706
1743
  props,
1707
- "}"
1744
+ "}\n"
1708
1745
  ];
1746
+ await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
1709
1747
  await fs.writeFile(tablePath, codeToString(code, "", " "));
1710
1748
  };
1711
1749
 
@@ -2178,6 +2216,32 @@ const updateTableFile = async (params) => {
2178
2216
  }
2179
2217
  };
2180
2218
 
2219
+ const createBaseTableFile = async ({
2220
+ baseTableName,
2221
+ baseTablePath
2222
+ }) => {
2223
+ await fs.mkdir(path__default.dirname(baseTablePath), { recursive: true });
2224
+ await fs.writeFile(
2225
+ baseTablePath,
2226
+ `import { createBaseTable } from 'orchid-orm';
2227
+ import { columnTypes } from 'pqb';
2228
+
2229
+ export const ${baseTableName} = createBaseTable({
2230
+ columnTypes: {
2231
+ ...columnTypes,
2232
+ },
2233
+ });
2234
+ `,
2235
+ {
2236
+ flag: "wx"
2237
+ }
2238
+ ).catch((err) => {
2239
+ if (err.code === "EEXIST")
2240
+ return;
2241
+ throw err;
2242
+ });
2243
+ };
2244
+
2181
2245
  var __defProp = Object.defineProperty;
2182
2246
  var __defProps = Object.defineProperties;
2183
2247
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -2204,11 +2268,20 @@ const appCodeUpdater = (config) => {
2204
2268
  tablePath: (name) => path.resolve(config.tablePath(name)),
2205
2269
  mainFilePath: path.resolve(config.mainFilePath)
2206
2270
  });
2207
- return async (ast) => {
2208
- await Promise.all([
2209
- updateMainFile(params.mainFilePath, params.tablePath, ast),
2271
+ return async ({ ast, options, cache: cacheObject }) => {
2272
+ const promises = [
2273
+ updateMainFile(params.mainFilePath, params.tablePath, ast, options),
2210
2274
  updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2211
- ]);
2275
+ ];
2276
+ const cache = cacheObject;
2277
+ if (!cache.createdBaseTable) {
2278
+ promises.push(
2279
+ createBaseTableFile(params).then(() => {
2280
+ cache.createdBaseTable = true;
2281
+ })
2282
+ );
2283
+ }
2284
+ await Promise.all(promises);
2212
2285
  };
2213
2286
  };
2214
2287