orchid-orm 1.6.2 → 1.6.4
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/bin.js +2 -2
- package/dist/bin.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +99 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +100 -60
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { columnTypes, getColumnTypes, addQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, getQueryAs, toSqlCacheKey, NotFoundError, relationQueryKey, Db, Adapter, anyShape, getClonedQueryData, columnsShapeToCode, codeToString, primaryKeyToCode, indexToCode, foreignKeyToCode, columnIndexesToCode, columnForeignKeysToCode, columnDefaultArgumentToCode } from 'pqb';
|
|
2
2
|
export { columnTypes } from 'pqb';
|
|
3
|
-
import { snakeCaseKey, emptyObject, singleQuote, quoteObjectKey, addCode } from 'orchid-core';
|
|
3
|
+
import { snakeCaseKey, emptyObject, pathToLog, singleQuote, quoteObjectKey, addCode } from 'orchid-core';
|
|
4
4
|
import * as path from 'path';
|
|
5
5
|
import path__default from 'path';
|
|
6
6
|
import fs from 'fs/promises';
|
|
@@ -363,36 +363,41 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
363
363
|
if ("through" in relation.options) {
|
|
364
364
|
const { through, source } = relation.options;
|
|
365
365
|
const throughRelation = getThroughRelation(table, through);
|
|
366
|
-
const throughTable = throughRelation.table;
|
|
367
366
|
const sourceRelation = getSourceRelation(throughRelation, source);
|
|
368
367
|
const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
|
|
368
|
+
const whereExistsCallback = () => sourceQuery;
|
|
369
369
|
return {
|
|
370
370
|
returns: "one",
|
|
371
371
|
method: (params) => {
|
|
372
372
|
const throughQuery = table[through](params);
|
|
373
|
-
return query.whereExists(
|
|
373
|
+
return query.whereExists(
|
|
374
|
+
throughQuery,
|
|
375
|
+
whereExistsCallback
|
|
376
|
+
);
|
|
374
377
|
},
|
|
375
378
|
joinQuery(fromQuery, toQuery) {
|
|
376
|
-
return toQuery.whereExists(
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
379
|
+
return toQuery.whereExists(
|
|
380
|
+
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
381
|
+
() => {
|
|
382
|
+
const as = getQueryAs(toQuery);
|
|
383
|
+
return sourceRelation.joinQuery(
|
|
380
384
|
throughRelation.query,
|
|
381
385
|
sourceRelation.query.as(as)
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
);
|
|
385
389
|
},
|
|
386
390
|
reverseJoin(fromQuery, toQuery) {
|
|
387
|
-
return fromQuery.whereExists(
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
+
return fromQuery.whereExists(
|
|
392
|
+
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
393
|
+
() => {
|
|
394
|
+
const as = getQueryAs(toQuery);
|
|
395
|
+
return sourceRelation.joinQuery(
|
|
391
396
|
throughRelation.query,
|
|
392
397
|
sourceRelation.query.as(as)
|
|
393
|
-
)
|
|
394
|
-
|
|
395
|
-
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
);
|
|
396
401
|
},
|
|
397
402
|
primaryKey: sourceRelation.primaryKey
|
|
398
403
|
};
|
|
@@ -550,40 +555,45 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
550
555
|
if ("through" in relation.options) {
|
|
551
556
|
const { through, source } = relation.options;
|
|
552
557
|
const throughRelation = getThroughRelation(table, through);
|
|
553
|
-
const throughTable = throughRelation.table.as(through);
|
|
554
558
|
const sourceRelation = getSourceRelation(throughRelation, source);
|
|
555
559
|
const sourceRelationQuery = sourceRelation.query.as(relationName);
|
|
556
560
|
const sourceQuery = sourceRelation.joinQuery(
|
|
557
561
|
throughRelation.query,
|
|
558
562
|
sourceRelationQuery
|
|
559
563
|
);
|
|
564
|
+
const whereExistsCallback = () => sourceQuery;
|
|
560
565
|
return {
|
|
561
566
|
returns: "many",
|
|
562
567
|
method: (params) => {
|
|
563
568
|
const throughQuery = table[through](params);
|
|
564
|
-
return query.whereExists(
|
|
569
|
+
return query.whereExists(
|
|
570
|
+
throughQuery,
|
|
571
|
+
whereExistsCallback
|
|
572
|
+
);
|
|
565
573
|
},
|
|
566
574
|
joinQuery(fromQuery, toQuery) {
|
|
567
|
-
return toQuery.whereExists(
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
575
|
+
return toQuery.whereExists(
|
|
576
|
+
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
577
|
+
() => {
|
|
578
|
+
const as = getQueryAs(toQuery);
|
|
579
|
+
return sourceRelation.joinQuery(
|
|
571
580
|
throughRelation.query,
|
|
572
581
|
sourceRelation.query.as(as)
|
|
573
|
-
)
|
|
574
|
-
|
|
575
|
-
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
);
|
|
576
585
|
},
|
|
577
586
|
reverseJoin(fromQuery, toQuery) {
|
|
578
|
-
return fromQuery.whereExists(
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
587
|
+
return fromQuery.whereExists(
|
|
588
|
+
throughRelation.joinQuery(fromQuery, throughRelation.query),
|
|
589
|
+
() => {
|
|
590
|
+
const as = getQueryAs(toQuery);
|
|
591
|
+
return sourceRelation.joinQuery(
|
|
582
592
|
throughRelation.query,
|
|
583
593
|
sourceRelation.query.as(as)
|
|
584
|
-
)
|
|
585
|
-
|
|
586
|
-
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
);
|
|
587
597
|
},
|
|
588
598
|
primaryKey: sourceRelation.primaryKey
|
|
589
599
|
};
|
|
@@ -790,6 +800,13 @@ class HasAndBelongsToManyVirtualColumn extends VirtualColumn {
|
|
|
790
800
|
);
|
|
791
801
|
}
|
|
792
802
|
}
|
|
803
|
+
const removeColumnName = (column) => {
|
|
804
|
+
if (!column.data.name)
|
|
805
|
+
return column;
|
|
806
|
+
const cloned = Object.create(column);
|
|
807
|
+
cloned.data = __spreadProps$3(__spreadValues$4({}, column.data), { name: void 0 });
|
|
808
|
+
return cloned;
|
|
809
|
+
};
|
|
793
810
|
const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query) => {
|
|
794
811
|
const {
|
|
795
812
|
primaryKey: pk,
|
|
@@ -805,8 +822,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
805
822
|
baseQuery.baseQuery = baseQuery;
|
|
806
823
|
baseQuery.table = joinTable;
|
|
807
824
|
baseQuery.shape = {
|
|
808
|
-
[fk]: table.shape[pk],
|
|
809
|
-
[afk]: query.shape[apk]
|
|
825
|
+
[fk]: removeColumnName(table.shape[pk]),
|
|
826
|
+
[afk]: removeColumnName(query.shape[apk])
|
|
810
827
|
};
|
|
811
828
|
baseQuery.query = __spreadProps$3(__spreadValues$4({}, baseQuery.query), {
|
|
812
829
|
shape: baseQuery.shape
|
|
@@ -839,15 +856,19 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
839
856
|
state
|
|
840
857
|
),
|
|
841
858
|
joinQuery(fromQuery, toQuery) {
|
|
842
|
-
|
|
859
|
+
const join = toQuery.whereExists(
|
|
843
860
|
subQuery,
|
|
844
|
-
(q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${
|
|
861
|
+
(q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
|
|
845
862
|
);
|
|
863
|
+
join.query.joinedShapes = __spreadProps$3(__spreadValues$4({}, join.query.joinedShapes), {
|
|
864
|
+
[fromQuery.query.as || fromQuery.table]: fromQuery.query.shape
|
|
865
|
+
});
|
|
866
|
+
return join;
|
|
846
867
|
},
|
|
847
868
|
reverseJoin(fromQuery, toQuery) {
|
|
848
869
|
return fromQuery.whereExists(
|
|
849
870
|
subQuery,
|
|
850
|
-
(q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${
|
|
871
|
+
(q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
|
|
851
872
|
);
|
|
852
873
|
},
|
|
853
874
|
primaryKey: pk,
|
|
@@ -1211,11 +1232,8 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
|
|
|
1211
1232
|
toTable._take();
|
|
1212
1233
|
}
|
|
1213
1234
|
const query = this.isSubQuery ? toTable : toTable._whereExists(
|
|
1214
|
-
this,
|
|
1215
|
-
(q) => data.reverseJoin(
|
|
1216
|
-
q,
|
|
1217
|
-
toTable
|
|
1218
|
-
)
|
|
1235
|
+
this.baseQuery,
|
|
1236
|
+
(q) => data.reverseJoin(this, toTable)
|
|
1219
1237
|
);
|
|
1220
1238
|
query.query[relationQueryKey] = {
|
|
1221
1239
|
relationName,
|
|
@@ -1603,7 +1621,7 @@ const optionsToString = (options) => {
|
|
|
1603
1621
|
}
|
|
1604
1622
|
return lines.join("\n ");
|
|
1605
1623
|
};
|
|
1606
|
-
const updateMainFile = async (filePath, tablePath, ast, options) => {
|
|
1624
|
+
const updateMainFile = async (filePath, tablePath, ast, options, logger) => {
|
|
1607
1625
|
const result = await fs.readFile(filePath, "utf-8").then(
|
|
1608
1626
|
(content2) => ({ error: void 0, content: content2 }),
|
|
1609
1627
|
(error) => {
|
|
@@ -1650,6 +1668,9 @@ const updateMainFile = async (filePath, tablePath, ast, options) => {
|
|
|
1650
1668
|
await fs.mkdir(path__default.dirname(filePath), { recursive: true });
|
|
1651
1669
|
}
|
|
1652
1670
|
await fs.writeFile(filePath, write);
|
|
1671
|
+
logger == null ? void 0 : logger.log(
|
|
1672
|
+
`${result.content ? "Updated" : "Created"} ${pathToLog(filePath)}`
|
|
1673
|
+
);
|
|
1653
1674
|
}
|
|
1654
1675
|
};
|
|
1655
1676
|
const createTable$1 = ({ filePath, tablePath, statements, object, content, spaces }, ast) => {
|
|
@@ -1745,9 +1766,11 @@ var __objRest$2 = (source, exclude) => {
|
|
|
1745
1766
|
};
|
|
1746
1767
|
const createTable = async (_a) => {
|
|
1747
1768
|
var _b = _a, {
|
|
1748
|
-
ast
|
|
1769
|
+
ast,
|
|
1770
|
+
logger
|
|
1749
1771
|
} = _b, params = __objRest$2(_b, [
|
|
1750
|
-
"ast"
|
|
1772
|
+
"ast",
|
|
1773
|
+
"logger"
|
|
1751
1774
|
]);
|
|
1752
1775
|
const tablePath = params.tablePath(ast.name);
|
|
1753
1776
|
const baseTablePath = getImportPath(tablePath, params.baseTablePath);
|
|
@@ -1755,7 +1778,7 @@ const createTable = async (_a) => {
|
|
|
1755
1778
|
if (ast.schema) {
|
|
1756
1779
|
props.push(`schema = ${singleQuote(ast.schema)};`);
|
|
1757
1780
|
}
|
|
1758
|
-
props.push(`table = ${singleQuote(ast.name)};`);
|
|
1781
|
+
props.push(`readonly table = ${singleQuote(ast.name)};`);
|
|
1759
1782
|
if (ast.noPrimaryKey === "ignore") {
|
|
1760
1783
|
props.push("noPrimaryKey = true;");
|
|
1761
1784
|
}
|
|
@@ -1774,6 +1797,7 @@ const createTable = async (_a) => {
|
|
|
1774
1797
|
await fs.mkdir(path__default.dirname(tablePath), { recursive: true });
|
|
1775
1798
|
try {
|
|
1776
1799
|
await fs.writeFile(tablePath, codeToString(code, "", " "), { flag: "wx" });
|
|
1800
|
+
logger == null ? void 0 : logger.log(`Created ${pathToLog(tablePath)}`);
|
|
1777
1801
|
} catch (err) {
|
|
1778
1802
|
if (err.code !== "EEXIST") {
|
|
1779
1803
|
throw err;
|
|
@@ -1798,9 +1822,11 @@ var __objRest$1 = (source, exclude) => {
|
|
|
1798
1822
|
};
|
|
1799
1823
|
const changeTable = async (_a) => {
|
|
1800
1824
|
var _b = _a, {
|
|
1801
|
-
ast
|
|
1825
|
+
ast,
|
|
1826
|
+
logger
|
|
1802
1827
|
} = _b, params = __objRest$1(_b, [
|
|
1803
|
-
"ast"
|
|
1828
|
+
"ast",
|
|
1829
|
+
"logger"
|
|
1804
1830
|
]);
|
|
1805
1831
|
const tablePath = params.tablePath(ast.name);
|
|
1806
1832
|
const content = await fs.readFile(tablePath, "utf-8").catch(() => void 0);
|
|
@@ -1818,6 +1844,7 @@ const changeTable = async (_a) => {
|
|
|
1818
1844
|
addTableData(context);
|
|
1819
1845
|
}
|
|
1820
1846
|
await fs.writeFile(tablePath, changes.apply());
|
|
1847
|
+
logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
|
|
1821
1848
|
};
|
|
1822
1849
|
function* iterateColumnsShapes(statements, className) {
|
|
1823
1850
|
var _a, _b;
|
|
@@ -1988,7 +2015,7 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
|
|
|
1988
2015
|
continue;
|
|
1989
2016
|
let remove = true;
|
|
1990
2017
|
if (!replaced[key]) {
|
|
1991
|
-
const code = getColumnMethodArgs(to, key);
|
|
2018
|
+
const code = getColumnMethodArgs(t, to, key);
|
|
1992
2019
|
if (code) {
|
|
1993
2020
|
changes.replace(
|
|
1994
2021
|
item.expression.expression.end,
|
|
@@ -2008,7 +2035,7 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
|
|
|
2008
2035
|
for (const key in propsToChange) {
|
|
2009
2036
|
if (changedProps[key])
|
|
2010
2037
|
continue;
|
|
2011
|
-
const code = getColumnMethodArgs(to, key);
|
|
2038
|
+
const code = getColumnMethodArgs(t, to, key);
|
|
2012
2039
|
if (code) {
|
|
2013
2040
|
append += codeToString(code, spaces + " ", " ").trim();
|
|
2014
2041
|
}
|
|
@@ -2061,7 +2088,7 @@ ${spaces}`);
|
|
|
2061
2088
|
${spaces}`);
|
|
2062
2089
|
}
|
|
2063
2090
|
};
|
|
2064
|
-
const getColumnMethodArgs = (to, key) => {
|
|
2091
|
+
const getColumnMethodArgs = (t, to, key) => {
|
|
2065
2092
|
const value = to[key];
|
|
2066
2093
|
if (!value)
|
|
2067
2094
|
return;
|
|
@@ -2075,7 +2102,7 @@ const getColumnMethodArgs = (to, key) => {
|
|
|
2075
2102
|
if (key === "collate" || key === "compression") {
|
|
2076
2103
|
addCode(code, singleQuote(value));
|
|
2077
2104
|
} else if (key === "default") {
|
|
2078
|
-
addCode(code, columnDefaultArgumentToCode(value));
|
|
2105
|
+
addCode(code, columnDefaultArgumentToCode(t, value));
|
|
2079
2106
|
} else if (key !== "nullable" && key !== "primaryKey") {
|
|
2080
2107
|
return;
|
|
2081
2108
|
}
|
|
@@ -2208,9 +2235,11 @@ var __objRest = (source, exclude) => {
|
|
|
2208
2235
|
};
|
|
2209
2236
|
const renameTable = async (_a) => {
|
|
2210
2237
|
var _b = _a, {
|
|
2211
|
-
ast
|
|
2238
|
+
ast,
|
|
2239
|
+
logger
|
|
2212
2240
|
} = _b, params = __objRest(_b, [
|
|
2213
|
-
"ast"
|
|
2241
|
+
"ast",
|
|
2242
|
+
"logger"
|
|
2214
2243
|
]);
|
|
2215
2244
|
var _a2;
|
|
2216
2245
|
const tablePath = params.tablePath(ast.from);
|
|
@@ -2255,6 +2284,7 @@ const renameTable = async (_a) => {
|
|
|
2255
2284
|
}
|
|
2256
2285
|
}
|
|
2257
2286
|
await fs.writeFile(tablePath, changes.apply());
|
|
2287
|
+
logger == null ? void 0 : logger.log(`Updated ${pathToLog(tablePath)}`);
|
|
2258
2288
|
};
|
|
2259
2289
|
|
|
2260
2290
|
var __defProp$1 = Object.defineProperty;
|
|
@@ -2289,7 +2319,8 @@ const updateTableFile = async (params) => {
|
|
|
2289
2319
|
|
|
2290
2320
|
const createBaseTableFile = async ({
|
|
2291
2321
|
baseTableName,
|
|
2292
|
-
baseTablePath
|
|
2322
|
+
baseTablePath,
|
|
2323
|
+
logger
|
|
2293
2324
|
}) => {
|
|
2294
2325
|
await fs.mkdir(path__default.dirname(baseTablePath), { recursive: true });
|
|
2295
2326
|
await fs.writeFile(
|
|
@@ -2301,7 +2332,9 @@ export const ${baseTableName} = createBaseTable();
|
|
|
2301
2332
|
{
|
|
2302
2333
|
flag: "wx"
|
|
2303
2334
|
}
|
|
2304
|
-
).
|
|
2335
|
+
).then(() => {
|
|
2336
|
+
logger == null ? void 0 : logger.log(`Created ${pathToLog(baseTablePath)}`);
|
|
2337
|
+
}).catch((err) => {
|
|
2305
2338
|
if (err.code === "EEXIST")
|
|
2306
2339
|
return;
|
|
2307
2340
|
throw err;
|
|
@@ -2335,7 +2368,7 @@ const appCodeUpdater = ({
|
|
|
2335
2368
|
baseTableName,
|
|
2336
2369
|
mainFilePath
|
|
2337
2370
|
}) => {
|
|
2338
|
-
return async ({ ast, options, basePath, cache: cacheObject }) => {
|
|
2371
|
+
return async ({ ast, options, basePath, cache: cacheObject, logger }) => {
|
|
2339
2372
|
const params = {
|
|
2340
2373
|
tablePath(name) {
|
|
2341
2374
|
const file = tablePath(name);
|
|
@@ -2343,10 +2376,17 @@ const appCodeUpdater = ({
|
|
|
2343
2376
|
},
|
|
2344
2377
|
baseTablePath: resolvePath(basePath, baseTablePath),
|
|
2345
2378
|
baseTableName: baseTableName || "BaseTable",
|
|
2346
|
-
mainFilePath: resolvePath(basePath, mainFilePath)
|
|
2379
|
+
mainFilePath: resolvePath(basePath, mainFilePath),
|
|
2380
|
+
logger
|
|
2347
2381
|
};
|
|
2348
2382
|
const promises = [
|
|
2349
|
-
updateMainFile(
|
|
2383
|
+
updateMainFile(
|
|
2384
|
+
params.mainFilePath,
|
|
2385
|
+
params.tablePath,
|
|
2386
|
+
ast,
|
|
2387
|
+
options,
|
|
2388
|
+
logger
|
|
2389
|
+
),
|
|
2350
2390
|
updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
|
|
2351
2391
|
];
|
|
2352
2392
|
const cache = cacheObject;
|