orchid-orm 1.8.0 → 1.9.0
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/index.d.ts +10 -6
- package/dist/index.js +31 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as pqb from 'pqb';
|
|
|
2
2
|
import { BelongsToRelation, HasManyRelation, HasOneRelation, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, Query, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, HasAndBelongsToManyRelation, SetQueryTableAlias, defaultsKey, BaseRelation, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, DefaultColumnTypes, ColumnsShape, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
|
|
3
3
|
export { OrchidOrmError, OrchidOrmInternalError, columnTypes } from 'pqb';
|
|
4
4
|
import * as orchid_core from 'orchid-core';
|
|
5
|
-
import { StringKey, EmptyObject, ColumnTypesBase, ColumnShapeOutput
|
|
5
|
+
import { StringKey, EmptyObject, ColumnTypesBase, ColumnShapeOutput } from 'orchid-core';
|
|
6
6
|
import { AppCodeUpdater } from 'rake-db';
|
|
7
7
|
|
|
8
8
|
interface BelongsTo extends RelationThunkBase {
|
|
@@ -147,9 +147,10 @@ type Table = {
|
|
|
147
147
|
columnTypes: ColumnTypesBase;
|
|
148
148
|
noPrimaryKey?: boolean;
|
|
149
149
|
};
|
|
150
|
-
declare const createBaseTable: <CT extends ColumnTypesBase>({ columnTypes, snakeCase, }?: {
|
|
150
|
+
declare const createBaseTable: <CT extends ColumnTypesBase>({ columnTypes, snakeCase, filePath, }?: {
|
|
151
151
|
columnTypes?: CT | ((t: DefaultColumnTypes) => CT) | undefined;
|
|
152
152
|
snakeCase?: boolean | undefined;
|
|
153
|
+
filePath?: string | undefined;
|
|
153
154
|
}) => {
|
|
154
155
|
new (): {
|
|
155
156
|
table: string;
|
|
@@ -450,6 +451,7 @@ declare const createBaseTable: <CT extends ColumnTypesBase>({ columnTypes, snake
|
|
|
450
451
|
options: Options_3;
|
|
451
452
|
};
|
|
452
453
|
};
|
|
454
|
+
filePath: string;
|
|
453
455
|
};
|
|
454
456
|
|
|
455
457
|
type QueryMethods<T extends Query> = Record<string, (q: T, ...args: any[]) => any>;
|
|
@@ -475,11 +477,13 @@ declare class AppCodeUpdaterError extends Error {
|
|
|
475
477
|
}
|
|
476
478
|
type AppCodeUpdaterConfig = {
|
|
477
479
|
tablePath(tableName: string): string;
|
|
478
|
-
baseTablePath: string;
|
|
479
|
-
baseTableName: string;
|
|
480
480
|
mainFilePath: string;
|
|
481
481
|
logger?: QueryLogOptions['logger'];
|
|
482
482
|
};
|
|
483
|
-
|
|
483
|
+
type BaseTableParam = {
|
|
484
|
+
filePath: string;
|
|
485
|
+
name: string;
|
|
486
|
+
};
|
|
487
|
+
declare const appCodeUpdater: ({ tablePath, mainFilePath, }: AppCodeUpdaterConfig) => AppCodeUpdater;
|
|
484
488
|
|
|
485
|
-
export { AppCodeUpdaterConfig, AppCodeUpdaterError, DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses, TableToDb, appCodeUpdater, createBaseTable, createRepo, orchidORM };
|
|
489
|
+
export { AppCodeUpdaterConfig, AppCodeUpdaterError, BaseTableParam, DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses, TableToDb, appCodeUpdater, createBaseTable, createRepo, orchidORM };
|
package/dist/index.js
CHANGED
|
@@ -28,16 +28,25 @@ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
|
28
28
|
|
|
29
29
|
const createBaseTable = ({
|
|
30
30
|
columnTypes,
|
|
31
|
-
snakeCase
|
|
31
|
+
snakeCase,
|
|
32
|
+
filePath
|
|
32
33
|
} = { columnTypes: pqb.columnTypes }) => {
|
|
33
34
|
const ct = typeof columnTypes === "function" ? columnTypes(pqb.columnTypes) : columnTypes || pqb.columnTypes;
|
|
35
|
+
filePath != null ? filePath : filePath = orchidCore.getCallerFilePath();
|
|
36
|
+
if (!filePath) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
34
41
|
return create(
|
|
35
42
|
ct,
|
|
43
|
+
filePath,
|
|
36
44
|
snakeCase
|
|
37
45
|
);
|
|
38
46
|
};
|
|
39
|
-
const create = (columnTypes, snakeCase) => {
|
|
40
|
-
|
|
47
|
+
const create = (columnTypes, filePath, snakeCase) => {
|
|
48
|
+
var _a;
|
|
49
|
+
const base = (_a = class {
|
|
41
50
|
constructor() {
|
|
42
51
|
this.snakeCase = snakeCase;
|
|
43
52
|
this.setColumns = (fn) => {
|
|
@@ -93,7 +102,7 @@ const create = (columnTypes, snakeCase) => {
|
|
|
93
102
|
options
|
|
94
103
|
};
|
|
95
104
|
}
|
|
96
|
-
};
|
|
105
|
+
}, _a.filePath = filePath, _a);
|
|
97
106
|
base.prototype.columnTypes = columnTypes;
|
|
98
107
|
return base;
|
|
99
108
|
};
|
|
@@ -1793,7 +1802,7 @@ const createTable = async (_a) => {
|
|
|
1793
1802
|
"logger"
|
|
1794
1803
|
]);
|
|
1795
1804
|
const tablePath = params.tablePath(orchidCore.toCamelCase(ast.name));
|
|
1796
|
-
const baseTablePath = orchidCore.getImportPath(tablePath, params.
|
|
1805
|
+
const baseTablePath = orchidCore.getImportPath(tablePath, params.baseTable.filePath);
|
|
1797
1806
|
const props = [];
|
|
1798
1807
|
if (ast.schema) {
|
|
1799
1808
|
props.push(`schema = ${orchidCore.singleQuote(ast.schema)};`);
|
|
@@ -1808,9 +1817,9 @@ const createTable = async (_a) => {
|
|
|
1808
1817
|
"}));"
|
|
1809
1818
|
);
|
|
1810
1819
|
const code = [
|
|
1811
|
-
`import { ${params.
|
|
1820
|
+
`import { ${params.baseTable.name} } from '${baseTablePath}';
|
|
1812
1821
|
`,
|
|
1813
|
-
`export class ${orchidCore.toPascalCase(ast.name)}Table extends ${params.
|
|
1822
|
+
`export class ${orchidCore.toPascalCase(ast.name)}Table extends ${params.baseTable.name} {`,
|
|
1814
1823
|
props,
|
|
1815
1824
|
"}\n"
|
|
1816
1825
|
];
|
|
@@ -2422,22 +2431,21 @@ const updateTableFile = async (params) => {
|
|
|
2422
2431
|
};
|
|
2423
2432
|
|
|
2424
2433
|
const createBaseTableFile = async ({
|
|
2425
|
-
|
|
2426
|
-
baseTablePath,
|
|
2434
|
+
baseTable,
|
|
2427
2435
|
logger
|
|
2428
2436
|
}) => {
|
|
2429
|
-
await fs.mkdir(path.dirname(
|
|
2437
|
+
await fs.mkdir(path.dirname(baseTable.filePath), { recursive: true });
|
|
2430
2438
|
await fs.writeFile(
|
|
2431
|
-
|
|
2439
|
+
baseTable.filePath,
|
|
2432
2440
|
`import { createBaseTable } from 'orchid-orm';
|
|
2433
2441
|
|
|
2434
|
-
export const ${
|
|
2442
|
+
export const ${baseTable.name} = createBaseTable();
|
|
2435
2443
|
`,
|
|
2436
2444
|
{
|
|
2437
2445
|
flag: "wx"
|
|
2438
2446
|
}
|
|
2439
2447
|
).then(() => {
|
|
2440
|
-
logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(
|
|
2448
|
+
logger == null ? void 0 : logger.log(`Created ${orchidCore.pathToLog(baseTable.filePath)}`);
|
|
2441
2449
|
}).catch((err) => {
|
|
2442
2450
|
if (err.code === "EEXIST")
|
|
2443
2451
|
return;
|
|
@@ -2468,18 +2476,21 @@ class AppCodeUpdaterError extends Error {
|
|
|
2468
2476
|
}
|
|
2469
2477
|
const appCodeUpdater = ({
|
|
2470
2478
|
tablePath,
|
|
2471
|
-
baseTablePath,
|
|
2472
|
-
baseTableName,
|
|
2473
2479
|
mainFilePath
|
|
2474
2480
|
}) => {
|
|
2475
|
-
return async ({
|
|
2481
|
+
return async ({
|
|
2482
|
+
ast,
|
|
2483
|
+
options,
|
|
2484
|
+
basePath,
|
|
2485
|
+
cache: cacheObject,
|
|
2486
|
+
logger,
|
|
2487
|
+
baseTable
|
|
2488
|
+
}) => {
|
|
2476
2489
|
const params = {
|
|
2477
2490
|
tablePath(name) {
|
|
2478
2491
|
const file = tablePath(name);
|
|
2479
2492
|
return resolvePath(basePath, file);
|
|
2480
2493
|
},
|
|
2481
|
-
baseTablePath: resolvePath(basePath, baseTablePath),
|
|
2482
|
-
baseTableName: baseTableName || "BaseTable",
|
|
2483
2494
|
mainFilePath: resolvePath(basePath, mainFilePath),
|
|
2484
2495
|
logger
|
|
2485
2496
|
};
|
|
@@ -2491,12 +2502,12 @@ const appCodeUpdater = ({
|
|
|
2491
2502
|
options,
|
|
2492
2503
|
logger
|
|
2493
2504
|
),
|
|
2494
|
-
updateTableFile(__spreadProps(__spreadValues({}, params), { ast }))
|
|
2505
|
+
updateTableFile(__spreadProps(__spreadValues({}, params), { ast, baseTable }))
|
|
2495
2506
|
];
|
|
2496
2507
|
const cache = cacheObject;
|
|
2497
2508
|
if (!cache.createdBaseTable) {
|
|
2498
2509
|
promises.push(
|
|
2499
|
-
createBaseTableFile(params).then(() => {
|
|
2510
|
+
createBaseTableFile({ logger: params.logger, baseTable }).then(() => {
|
|
2500
2511
|
cache.createdBaseTable = true;
|
|
2501
2512
|
})
|
|
2502
2513
|
);
|