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 +18 -0
- package/dist/index.esm.js +115 -17
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +115 -17
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/{appCodeUpdater → codegen}/appCodeUpdater.test.ts +33 -9
- package/src/{appCodeUpdater → codegen}/appCodeUpdater.ts +16 -4
- package/src/codegen/createBaseTableFile.test.ts +58 -0
- package/src/codegen/createBaseTableFile.ts +36 -0
- package/src/{appCodeUpdater → codegen}/fileChanges.ts +0 -0
- package/src/{appCodeUpdater → codegen}/testUtils.ts +0 -0
- package/src/{appCodeUpdater → codegen}/tsUtils.ts +0 -0
- package/src/{appCodeUpdater → codegen}/updateMainFile.test.ts +34 -15
- package/src/{appCodeUpdater → codegen}/updateMainFile.ts +57 -11
- package/src/{appCodeUpdater → codegen}/updateTableFile/changeTable.test.ts +1 -0
- package/src/{appCodeUpdater → codegen}/updateTableFile/changeTable.ts +0 -0
- package/src/{appCodeUpdater → codegen}/updateTableFile/createTable.test.ts +58 -16
- package/src/{appCodeUpdater → codegen}/updateTableFile/createTable.ts +11 -2
- package/src/codegen/updateTableFile/renameTable.test.ts +124 -0
- package/src/{appCodeUpdater → codegen}/updateTableFile/renameTable.ts +29 -2
- package/src/{appCodeUpdater → codegen}/updateTableFile/updateTableFile.ts +0 -0
- package/src/{appCodeUpdater → codegen}/utils.ts +0 -0
- package/src/index.ts +1 -1
- package/src/appCodeUpdater/updateTableFile/renameTable.test.ts +0 -43
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
|
|
1598
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1656
|
+
write = createTable$1(context, ast);
|
|
1626
1657
|
} else {
|
|
1627
|
-
|
|
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 = ({
|
|
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(
|
|
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 = ({
|
|
1690
|
+
const dropTable = ({ filePath, tablePath, statements, object, content }, ast) => {
|
|
1654
1691
|
const changes = new FileChanges(content);
|
|
1655
|
-
const importPath = getImportPath(
|
|
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)) {
|
|
@@ -1717,7 +1754,11 @@ const createTable = async (_a) => {
|
|
|
1717
1754
|
]);
|
|
1718
1755
|
const tablePath = params.tablePath(ast.name);
|
|
1719
1756
|
const baseTablePath = getImportPath(tablePath, params.baseTablePath);
|
|
1720
|
-
const props = [
|
|
1757
|
+
const props = [];
|
|
1758
|
+
if (ast.schema) {
|
|
1759
|
+
props.push(`schema = ${pqb.singleQuote(ast.schema)};`);
|
|
1760
|
+
}
|
|
1761
|
+
props.push(`table = ${pqb.singleQuote(ast.name)};`);
|
|
1721
1762
|
if (ast.noPrimaryKey === "ignore") {
|
|
1722
1763
|
props.push("noPrimaryKey = true;");
|
|
1723
1764
|
}
|
|
@@ -1731,8 +1772,9 @@ const createTable = async (_a) => {
|
|
|
1731
1772
|
`,
|
|
1732
1773
|
`export class ${toPascalCase(ast.name)} extends ${params.baseTableName} {`,
|
|
1733
1774
|
props,
|
|
1734
|
-
"}"
|
|
1775
|
+
"}\n"
|
|
1735
1776
|
];
|
|
1777
|
+
await fs__default["default"].mkdir(path__default["default"].dirname(tablePath), { recursive: true });
|
|
1736
1778
|
await fs__default["default"].writeFile(tablePath, pqb.codeToString(code, "", " "));
|
|
1737
1779
|
};
|
|
1738
1780
|
|
|
@@ -2159,17 +2201,38 @@ const renameTable = async (_a) => {
|
|
|
2159
2201
|
const changes = new FileChanges(content);
|
|
2160
2202
|
const statements = ts.getStatements(content);
|
|
2161
2203
|
const className = toPascalCase(ast.from);
|
|
2204
|
+
const changeSchema = ast.fromSchema !== ast.toSchema;
|
|
2162
2205
|
for (const node of ts.class.iterate(statements)) {
|
|
2163
2206
|
if (((_a2 = node.name) == null ? void 0 : _a2.escapedText) !== className)
|
|
2164
2207
|
continue;
|
|
2208
|
+
const addSchema = changeSchema && ast.toSchema && !node.members.some((member) => ts.prop.getName(member) === "schema");
|
|
2209
|
+
if (addSchema && ast.toSchema) {
|
|
2210
|
+
changes.add(
|
|
2211
|
+
node.members.pos,
|
|
2212
|
+
`
|
|
2213
|
+
schema = ${pqb.singleQuote(ast.toSchema)};`
|
|
2214
|
+
);
|
|
2215
|
+
}
|
|
2165
2216
|
for (const member of node.members) {
|
|
2166
2217
|
const name = ts.prop.getName(member);
|
|
2167
|
-
if (name !== "table")
|
|
2218
|
+
if (name !== "table" && !(changeSchema && name === "schema"))
|
|
2168
2219
|
continue;
|
|
2169
2220
|
const { initializer: value } = member;
|
|
2170
2221
|
if (!value)
|
|
2171
2222
|
continue;
|
|
2172
|
-
|
|
2223
|
+
if (name === "schema") {
|
|
2224
|
+
if (ast.toSchema) {
|
|
2225
|
+
changes.replace(
|
|
2226
|
+
value.pos,
|
|
2227
|
+
value.end,
|
|
2228
|
+
` ${pqb.singleQuote(ast.toSchema)}`
|
|
2229
|
+
);
|
|
2230
|
+
} else {
|
|
2231
|
+
changes.remove(member.pos, member.end);
|
|
2232
|
+
}
|
|
2233
|
+
} else {
|
|
2234
|
+
changes.replace(value.pos, value.end, ` ${pqb.singleQuote(ast.to)}`);
|
|
2235
|
+
}
|
|
2173
2236
|
}
|
|
2174
2237
|
}
|
|
2175
2238
|
await fs__default["default"].writeFile(params.tablePath(ast.to), changes.apply());
|
|
@@ -2205,6 +2268,32 @@ const updateTableFile = async (params) => {
|
|
|
2205
2268
|
}
|
|
2206
2269
|
};
|
|
2207
2270
|
|
|
2271
|
+
const createBaseTableFile = async ({
|
|
2272
|
+
baseTableName,
|
|
2273
|
+
baseTablePath
|
|
2274
|
+
}) => {
|
|
2275
|
+
await fs__default["default"].mkdir(path__default["default"].dirname(baseTablePath), { recursive: true });
|
|
2276
|
+
await fs__default["default"].writeFile(
|
|
2277
|
+
baseTablePath,
|
|
2278
|
+
`import { createBaseTable } from 'orchid-orm';
|
|
2279
|
+
import { columnTypes } from 'pqb';
|
|
2280
|
+
|
|
2281
|
+
export const ${baseTableName} = createBaseTable({
|
|
2282
|
+
columnTypes: {
|
|
2283
|
+
...columnTypes,
|
|
2284
|
+
},
|
|
2285
|
+
});
|
|
2286
|
+
`,
|
|
2287
|
+
{
|
|
2288
|
+
flag: "wx"
|
|
2289
|
+
}
|
|
2290
|
+
).catch((err) => {
|
|
2291
|
+
if (err.code === "EEXIST")
|
|
2292
|
+
return;
|
|
2293
|
+
throw err;
|
|
2294
|
+
});
|
|
2295
|
+
};
|
|
2296
|
+
|
|
2208
2297
|
var __defProp = Object.defineProperty;
|
|
2209
2298
|
var __defProps = Object.defineProperties;
|
|
2210
2299
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
@@ -2231,11 +2320,20 @@ const appCodeUpdater = (config) => {
|
|
|
2231
2320
|
tablePath: (name) => path__namespace.resolve(config.tablePath(name)),
|
|
2232
2321
|
mainFilePath: path__namespace.resolve(config.mainFilePath)
|
|
2233
2322
|
});
|
|
2234
|
-
return async (ast) => {
|
|
2235
|
-
|
|
2236
|
-
updateMainFile(params.mainFilePath, params.tablePath, ast),
|
|
2323
|
+
return async ({ ast, options, cache: cacheObject }) => {
|
|
2324
|
+
const promises = [
|
|
2325
|
+
updateMainFile(params.mainFilePath, params.tablePath, ast, options),
|
|
2237
2326
|
updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
|
|
2238
|
-
]
|
|
2327
|
+
];
|
|
2328
|
+
const cache = cacheObject;
|
|
2329
|
+
if (!cache.createdBaseTable) {
|
|
2330
|
+
promises.push(
|
|
2331
|
+
createBaseTableFile(params).then(() => {
|
|
2332
|
+
cache.createdBaseTable = true;
|
|
2333
|
+
})
|
|
2334
|
+
);
|
|
2335
|
+
}
|
|
2336
|
+
await Promise.all(promises);
|
|
2239
2337
|
};
|
|
2240
2338
|
};
|
|
2241
2339
|
|