orchid-orm 1.4.20 → 1.4.22

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,23 @@
1
1
  # orchid-orm
2
2
 
3
+ ## 1.4.22
4
+
5
+ ### Patch Changes
6
+
7
+ - Change inner aspects of columns
8
+ - Updated dependencies
9
+ - pqb@0.8.5
10
+
11
+ ## 1.4.21
12
+
13
+ ### Patch Changes
14
+
15
+ - Add --code cli argument to rake-db
16
+ - Improve codegen
17
+ - Updated dependencies
18
+ - Updated dependencies
19
+ - pqb@0.8.4
20
+
3
21
  ## 1.4.19
4
22
 
5
23
  ### 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)) {
@@ -1690,7 +1727,11 @@ const createTable = async (_a) => {
1690
1727
  ]);
1691
1728
  const tablePath = params.tablePath(ast.name);
1692
1729
  const baseTablePath = getImportPath(tablePath, params.baseTablePath);
1693
- const props = [`table = ${singleQuote(ast.name)};`];
1730
+ const props = [];
1731
+ if (ast.schema) {
1732
+ props.push(`schema = ${singleQuote(ast.schema)};`);
1733
+ }
1734
+ props.push(`table = ${singleQuote(ast.name)};`);
1694
1735
  if (ast.noPrimaryKey === "ignore") {
1695
1736
  props.push("noPrimaryKey = true;");
1696
1737
  }
@@ -1704,8 +1745,9 @@ const createTable = async (_a) => {
1704
1745
  `,
1705
1746
  `export class ${toPascalCase(ast.name)} extends ${params.baseTableName} {`,
1706
1747
  props,
1707
- "}"
1748
+ "}\n"
1708
1749
  ];
1750
+ await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
1709
1751
  await fs.writeFile(tablePath, codeToString(code, "", " "));
1710
1752
  };
1711
1753
 
@@ -2132,17 +2174,38 @@ const renameTable = async (_a) => {
2132
2174
  const changes = new FileChanges(content);
2133
2175
  const statements = ts.getStatements(content);
2134
2176
  const className = toPascalCase(ast.from);
2177
+ const changeSchema = ast.fromSchema !== ast.toSchema;
2135
2178
  for (const node of ts.class.iterate(statements)) {
2136
2179
  if (((_a2 = node.name) == null ? void 0 : _a2.escapedText) !== className)
2137
2180
  continue;
2181
+ const addSchema = changeSchema && ast.toSchema && !node.members.some((member) => ts.prop.getName(member) === "schema");
2182
+ if (addSchema && ast.toSchema) {
2183
+ changes.add(
2184
+ node.members.pos,
2185
+ `
2186
+ schema = ${singleQuote(ast.toSchema)};`
2187
+ );
2188
+ }
2138
2189
  for (const member of node.members) {
2139
2190
  const name = ts.prop.getName(member);
2140
- if (name !== "table")
2191
+ if (name !== "table" && !(changeSchema && name === "schema"))
2141
2192
  continue;
2142
2193
  const { initializer: value } = member;
2143
2194
  if (!value)
2144
2195
  continue;
2145
- changes.replace(value.pos, value.end, ` ${singleQuote(ast.to)}`);
2196
+ if (name === "schema") {
2197
+ if (ast.toSchema) {
2198
+ changes.replace(
2199
+ value.pos,
2200
+ value.end,
2201
+ ` ${singleQuote(ast.toSchema)}`
2202
+ );
2203
+ } else {
2204
+ changes.remove(member.pos, member.end);
2205
+ }
2206
+ } else {
2207
+ changes.replace(value.pos, value.end, ` ${singleQuote(ast.to)}`);
2208
+ }
2146
2209
  }
2147
2210
  }
2148
2211
  await fs.writeFile(params.tablePath(ast.to), changes.apply());
@@ -2178,6 +2241,32 @@ const updateTableFile = async (params) => {
2178
2241
  }
2179
2242
  };
2180
2243
 
2244
+ const createBaseTableFile = async ({
2245
+ baseTableName,
2246
+ baseTablePath
2247
+ }) => {
2248
+ await fs.mkdir(path__default.dirname(baseTablePath), { recursive: true });
2249
+ await fs.writeFile(
2250
+ baseTablePath,
2251
+ `import { createBaseTable } from 'orchid-orm';
2252
+ import { columnTypes } from 'pqb';
2253
+
2254
+ export const ${baseTableName} = createBaseTable({
2255
+ columnTypes: {
2256
+ ...columnTypes,
2257
+ },
2258
+ });
2259
+ `,
2260
+ {
2261
+ flag: "wx"
2262
+ }
2263
+ ).catch((err) => {
2264
+ if (err.code === "EEXIST")
2265
+ return;
2266
+ throw err;
2267
+ });
2268
+ };
2269
+
2181
2270
  var __defProp = Object.defineProperty;
2182
2271
  var __defProps = Object.defineProperties;
2183
2272
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -2204,11 +2293,20 @@ const appCodeUpdater = (config) => {
2204
2293
  tablePath: (name) => path.resolve(config.tablePath(name)),
2205
2294
  mainFilePath: path.resolve(config.mainFilePath)
2206
2295
  });
2207
- return async (ast) => {
2208
- await Promise.all([
2209
- updateMainFile(params.mainFilePath, params.tablePath, ast),
2296
+ return async ({ ast, options, cache: cacheObject }) => {
2297
+ const promises = [
2298
+ updateMainFile(params.mainFilePath, params.tablePath, ast, options),
2210
2299
  updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
2211
- ]);
2300
+ ];
2301
+ const cache = cacheObject;
2302
+ if (!cache.createdBaseTable) {
2303
+ promises.push(
2304
+ createBaseTableFile(params).then(() => {
2305
+ cache.createdBaseTable = true;
2306
+ })
2307
+ );
2308
+ }
2309
+ await Promise.all(promises);
2212
2310
  };
2213
2311
  };
2214
2312