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 +10 -0
- package/dist/index.esm.js +87 -14
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +87 -14
- 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 +11 -3
- package/src/{appCodeUpdater → codegen}/updateTableFile/createTable.ts +3 -1
- package/src/{appCodeUpdater → codegen}/updateTableFile/renameTable.test.ts +1 -0
- package/src/{appCodeUpdater → codegen}/updateTableFile/renameTable.ts +0 -0
- package/src/{appCodeUpdater → codegen}/updateTableFile/updateTableFile.ts +0 -0
- package/src/{appCodeUpdater → codegen}/utils.ts +0 -0
- package/src/index.ts +1 -1
package/CHANGELOG.md
CHANGED
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
|
|
1571
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1629
|
+
write = createTable$1(context, ast);
|
|
1599
1630
|
} else {
|
|
1600
|
-
|
|
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 = ({
|
|
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(
|
|
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 = ({
|
|
1663
|
+
const dropTable = ({ filePath, tablePath, statements, object, content }, ast) => {
|
|
1627
1664
|
const changes = new FileChanges(content);
|
|
1628
|
-
const importPath = getImportPath(
|
|
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
|
-
|
|
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
|
|