orchid-orm 1.10.5 → 1.11.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/LICENSE +1 -1
- package/dist/index.d.ts +31 -20
- package/dist/index.js +63 -68
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -70
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/LICENSE
CHANGED
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import { BelongsToRelation, HasManyRelation, HasOneRelation, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, Query, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, HasAndBelongsToManyRelation, SetQueryTableAlias, BaseRelation, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll,
|
|
2
|
+
import { BelongsToRelation, HasManyRelation, HasOneRelation, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, Query, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, HasAndBelongsToManyRelation, SetQueryTableAlias, BaseRelation, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, DefaultColumnTypes, QueryData, QueryBase, ColumnsShape, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
|
|
3
3
|
export { OrchidOrmError, OrchidOrmInternalError, columnTypes, testTransaction } from 'pqb';
|
|
4
4
|
import * as orchid_core from 'orchid-core';
|
|
5
|
-
import { StringKey, EmptyObject, ColumnTypesBase, ColumnShapeOutput } from 'orchid-core';
|
|
5
|
+
import { StringKey, EmptyObject, ColumnTypesBase, ColumnsShapeBase, ColumnShapeOutput } from 'orchid-core';
|
|
6
6
|
|
|
7
7
|
interface BelongsTo extends RelationThunkBase {
|
|
8
8
|
type: 'belongsTo';
|
|
@@ -148,19 +148,12 @@ type ScopeFn<Related extends TableClass, Scope extends Query> = (q: DbTable<Rela
|
|
|
148
148
|
type Table = {
|
|
149
149
|
table: string;
|
|
150
150
|
columns: ColumnsConfig;
|
|
151
|
-
hooks?: TableHooks;
|
|
152
151
|
schema?: string;
|
|
153
152
|
columnTypes: ColumnTypesBase;
|
|
154
153
|
noPrimaryKey?: boolean;
|
|
155
154
|
filePath: string;
|
|
156
155
|
};
|
|
157
156
|
type TableType<T extends Pick<Table, 'columns'>> = T['columns']['type'];
|
|
158
|
-
type TableHooks = {
|
|
159
|
-
[K in BeforeHookKey]?: BeforeHook;
|
|
160
|
-
} & {
|
|
161
|
-
[K in AfterHookKey]?: AfterHook;
|
|
162
|
-
};
|
|
163
|
-
declare const addTableHooks: (table: Query, hooks: TableHooks) => void;
|
|
164
157
|
declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>({ columnTypes, snakeCase, filePath, nowSQL, exportAs, }?: {
|
|
165
158
|
columnTypes?: CT | ((t: DefaultColumnTypes) => CT) | undefined;
|
|
166
159
|
snakeCase?: boolean | undefined;
|
|
@@ -224,7 +217,7 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
224
217
|
(sql: TemplateStringsArray, ...values: unknown[]): orchid_core.RawExpression;
|
|
225
218
|
(sql: string): orchid_core.RawExpression;
|
|
226
219
|
(values: Record<string, unknown>, sql: string): orchid_core.RawExpression;
|
|
227
|
-
(values: Record<string, unknown>): (
|
|
220
|
+
(values: Record<string, unknown>): (strings: TemplateStringsArray, ...values: unknown[]) => orchid_core.RawExpression;
|
|
228
221
|
};
|
|
229
222
|
smallint(): pqb.SmallIntColumn;
|
|
230
223
|
integer(): pqb.IntegerColumn;
|
|
@@ -322,10 +315,11 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
322
315
|
}) | undefined): {};
|
|
323
316
|
check(check: orchid_core.RawExpression): {};
|
|
324
317
|
} : CT;
|
|
318
|
+
query: QueryData;
|
|
325
319
|
filePath: string;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
setColumns<
|
|
320
|
+
result: ColumnsShapeBase;
|
|
321
|
+
clone<T_20 extends QueryBase>(this: T_20): T_20;
|
|
322
|
+
setColumns<T_21 extends ColumnsShape>(fn: (t: Record<string, orchid_core.AnyColumnTypeCreator> extends CT ? {
|
|
329
323
|
timestamps<T extends orchid_core.ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, orchid_core.ColumnDataBase>>(this: {
|
|
330
324
|
name(name: string): {
|
|
331
325
|
timestamp(): T;
|
|
@@ -375,7 +369,7 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
375
369
|
(sql: TemplateStringsArray, ...values: unknown[]): orchid_core.RawExpression;
|
|
376
370
|
(sql: string): orchid_core.RawExpression;
|
|
377
371
|
(values: Record<string, unknown>, sql: string): orchid_core.RawExpression;
|
|
378
|
-
(values: Record<string, unknown>): (
|
|
372
|
+
(values: Record<string, unknown>): (strings: TemplateStringsArray, ...values: unknown[]) => orchid_core.RawExpression;
|
|
379
373
|
};
|
|
380
374
|
smallint(): pqb.SmallIntColumn;
|
|
381
375
|
integer(): pqb.IntegerColumn;
|
|
@@ -444,9 +438,9 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
444
438
|
nativeEnum: <T_11 extends orchid_core.EnumLike>(givenEnum: T_11) => orchid_core.JSONNativeEnum<T_11>;
|
|
445
439
|
nullable: <T_12 extends orchid_core.JSONTypeAny>(type: T_12) => orchid_core.JSONNullable<T_12>;
|
|
446
440
|
nullish: <T_13 extends orchid_core.JSONTypeAny>(type: T_13) => orchid_core.JSONNullish<T_13>;
|
|
447
|
-
object: <T_14 extends orchid_core.JSONObjectShape, UnknownKeys extends orchid_core.UnknownKeysParam = "strip", Catchall extends orchid_core.JSONTypeAny = orchid_core.JSONTypeAny>(shape: T_14) => orchid_core.JSONObject<T_14, UnknownKeys, Catchall, orchid_core.JSONTypeAny extends Catchall ? orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer
|
|
441
|
+
object: <T_14 extends orchid_core.JSONObjectShape, UnknownKeys extends orchid_core.UnknownKeysParam = "strip", Catchall extends orchid_core.JSONTypeAny = orchid_core.JSONTypeAny>(shape: T_14) => orchid_core.JSONObject<T_14, UnknownKeys, Catchall, orchid_core.JSONTypeAny extends Catchall ? orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_22 extends object ? { [k in keyof T_22]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never : (orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_25 extends object ? { [k in keyof T_25]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never) & {
|
|
448
442
|
[k: string]: Catchall["type"];
|
|
449
|
-
} extends infer
|
|
443
|
+
} extends infer T_23 extends object ? { [k_2 in keyof T_23]: ((orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_24 extends object ? { [k in keyof T_24]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never) & {
|
|
450
444
|
[k: string]: Catchall["type"];
|
|
451
445
|
})[k_2]; } : never>;
|
|
452
446
|
optional: <T_19 extends orchid_core.JSONTypeAny>(type: T_19) => orchid_core.JSONOptional<T_19>;
|
|
@@ -472,9 +466,9 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
472
466
|
dropMode?: pqb.DropMode | undefined;
|
|
473
467
|
}) | undefined): {};
|
|
474
468
|
check(check: orchid_core.RawExpression): {};
|
|
475
|
-
} : CT) =>
|
|
476
|
-
shape:
|
|
477
|
-
type: ColumnShapeOutput<
|
|
469
|
+
} : CT) => T_21): {
|
|
470
|
+
shape: T_21;
|
|
471
|
+
type: ColumnShapeOutput<T_21>;
|
|
478
472
|
};
|
|
479
473
|
belongsTo<Self extends any, Related extends TableClass<Table>, Scope extends Query, Options extends {
|
|
480
474
|
primaryKey: keyof InstanceType<Related>["columns"]["shape"];
|
|
@@ -531,11 +525,28 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
|
|
|
531
525
|
fn: () => Related_3;
|
|
532
526
|
options: Options_3;
|
|
533
527
|
};
|
|
528
|
+
beforeQuery: BeforeHookMethod;
|
|
529
|
+
afterQuery: AfterHookMethod;
|
|
530
|
+
beforeCreate: BeforeHookMethod;
|
|
531
|
+
afterCreate: AfterSelectableHookMethod;
|
|
532
|
+
afterCreateCommit: AfterSelectableHookMethod;
|
|
533
|
+
beforeUpdate: BeforeHookMethod;
|
|
534
|
+
afterUpdate: AfterSelectableHookMethod;
|
|
535
|
+
afterUpdateCommit: AfterSelectableHookMethod;
|
|
536
|
+
beforeSave: BeforeHookMethod;
|
|
537
|
+
afterSave: AfterSelectableHookMethod;
|
|
538
|
+
afterSaveCommit: AfterSelectableHookMethod;
|
|
539
|
+
beforeDelete: BeforeHookMethod;
|
|
540
|
+
afterDelete: AfterSelectableHookMethod;
|
|
541
|
+
afterDeleteCommit: AfterSelectableHookMethod;
|
|
534
542
|
};
|
|
535
543
|
filePath: string;
|
|
536
544
|
nowSQL: string | undefined;
|
|
537
545
|
exportAs: string;
|
|
538
546
|
};
|
|
547
|
+
type BeforeHookMethod = <T extends Table>(cb: QueryBeforeHook) => T;
|
|
548
|
+
type AfterHookMethod = <T extends Table>(cb: QueryAfterHook) => T;
|
|
549
|
+
type AfterSelectableHookMethod = <T extends Table, S extends (keyof T['columns']['shape'])[]>(this: T, select: S, cb: AfterHook<S, T['columns']['shape']>) => T;
|
|
539
550
|
|
|
540
551
|
type QueryMethods<T extends Query> = Record<string, (q: T, ...args: any[]) => any>;
|
|
541
552
|
type QueryOne<T extends Query> = SetQueryReturns<T, Exclude<QueryReturnType, 'all'>>;
|
|
@@ -556,4 +567,4 @@ type Repo<T extends Query, Methods extends MethodsBase<T>, Mapped = MapMethods<T
|
|
|
556
567
|
}>(q: Q) => Q & Mapped) & T & Mapped;
|
|
557
568
|
declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(table: T, methods: Methods) => Repo<T, Methods, MapMethods<T, Methods>>;
|
|
558
569
|
|
|
559
|
-
export { DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses,
|
|
570
|
+
export { DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses, TableToDb, TableType, createBaseTable, createRepo, orchidORM };
|
package/dist/index.js
CHANGED
|
@@ -4,15 +4,6 @@ var pqb = require('pqb');
|
|
|
4
4
|
var orchidCore = require('orchid-core');
|
|
5
5
|
var node_async_hooks = require('node:async_hooks');
|
|
6
6
|
|
|
7
|
-
const addTableHooks = (table, hooks) => {
|
|
8
|
-
for (const key in hooks) {
|
|
9
|
-
pqb.addQueryHook(
|
|
10
|
-
table.baseQuery,
|
|
11
|
-
key,
|
|
12
|
-
hooks[key]
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
7
|
const createBaseTable = ({
|
|
17
8
|
columnTypes,
|
|
18
9
|
snakeCase,
|
|
@@ -40,10 +31,11 @@ const create = (columnTypes, filePath, snakeCase, nowSQL, exportAs = "BaseTable"
|
|
|
40
31
|
const base = (_a = class {
|
|
41
32
|
constructor() {
|
|
42
33
|
this.snakeCase = snakeCase;
|
|
34
|
+
this.query = {};
|
|
43
35
|
this.columnTypes = columnTypes;
|
|
44
36
|
}
|
|
45
|
-
|
|
46
|
-
return
|
|
37
|
+
clone() {
|
|
38
|
+
return this;
|
|
47
39
|
}
|
|
48
40
|
setColumns(fn) {
|
|
49
41
|
if (!this.filePath) {
|
|
@@ -106,6 +98,7 @@ const create = (columnTypes, filePath, snakeCase, nowSQL, exportAs = "BaseTable"
|
|
|
106
98
|
};
|
|
107
99
|
}
|
|
108
100
|
}, _a.filePath = filePath, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a);
|
|
101
|
+
orchidCore.applyMixins(base, [pqb.QueryHooks]);
|
|
109
102
|
base.prototype.columnTypes = columnTypes;
|
|
110
103
|
return base;
|
|
111
104
|
};
|
|
@@ -225,9 +218,9 @@ const nestedInsert$3 = ({ query, primaryKey }) => {
|
|
|
225
218
|
let createdI = 0;
|
|
226
219
|
let connectedI = 0;
|
|
227
220
|
connectOrCreatedI = 0;
|
|
228
|
-
return data.map(
|
|
229
|
-
|
|
230
|
-
);
|
|
221
|
+
return data.map((item) => {
|
|
222
|
+
return item.connectOrCreate ? connectOrCreated[connectOrCreatedI++] || created[createdI++] : item.connect ? connected[connectedI++] : created[createdI++];
|
|
223
|
+
});
|
|
231
224
|
};
|
|
232
225
|
};
|
|
233
226
|
const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
@@ -236,7 +229,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
236
229
|
if (params.upsert && pqb.isQueryReturnsAll(q)) {
|
|
237
230
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
238
231
|
}
|
|
239
|
-
let
|
|
232
|
+
let idsForDelete;
|
|
240
233
|
q._beforeUpdate(async (q2) => {
|
|
241
234
|
if (params.disconnect) {
|
|
242
235
|
update[foreignKey] = null;
|
|
@@ -251,7 +244,7 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
251
244
|
} else if (params.delete) {
|
|
252
245
|
const selectQuery = q2.clone();
|
|
253
246
|
selectQuery.query.type = void 0;
|
|
254
|
-
|
|
247
|
+
idsForDelete = await selectQuery._pluck(foreignKey);
|
|
255
248
|
update[foreignKey] = null;
|
|
256
249
|
}
|
|
257
250
|
});
|
|
@@ -274,12 +267,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
274
267
|
}
|
|
275
268
|
});
|
|
276
269
|
} else if (params.delete || params.update) {
|
|
277
|
-
q.
|
|
278
|
-
const id = params.delete ?
|
|
270
|
+
q._afterUpdate([], async (data) => {
|
|
271
|
+
const id = params.delete ? { in: idsForDelete } : Array.isArray(data) ? data.length === 0 ? null : {
|
|
279
272
|
in: data.map((item) => item[foreignKey]).filter((id2) => id2 !== null)
|
|
280
273
|
} : data[foreignKey];
|
|
281
274
|
if (id !== void 0 && id !== null) {
|
|
282
|
-
const t = query.
|
|
275
|
+
const t = query.where({
|
|
283
276
|
[primaryKey]: id
|
|
284
277
|
});
|
|
285
278
|
if (params.delete) {
|
|
@@ -313,22 +306,20 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nested
|
|
|
313
306
|
return;
|
|
314
307
|
}
|
|
315
308
|
q.query.wrapInTransaction = true;
|
|
316
|
-
ctx.returnTypeAll = true;
|
|
317
|
-
ctx.requiredReturning[primaryKey] = true;
|
|
318
309
|
const relationData = [values];
|
|
319
310
|
store.hasRelation[key] = relationData;
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
311
|
+
q._afterCreate(
|
|
312
|
+
[primaryKey],
|
|
313
|
+
(rows, q2) => nestedInsert(
|
|
323
314
|
q2,
|
|
324
315
|
relationData.map(([rowIndex2, data]) => [
|
|
325
|
-
|
|
316
|
+
rows[rowIndex2],
|
|
326
317
|
data
|
|
327
318
|
])
|
|
328
|
-
)
|
|
329
|
-
|
|
319
|
+
)
|
|
320
|
+
);
|
|
330
321
|
};
|
|
331
|
-
const hasRelationHandleUpdate = (q,
|
|
322
|
+
const hasRelationHandleUpdate = (q, set, key, primaryKey, nestedUpdate) => {
|
|
332
323
|
var _a, _b;
|
|
333
324
|
const value = set[key];
|
|
334
325
|
if (!value.set && !("upsert" in value) && (!value.disconnect || Array.isArray(value.disconnect) && value.disconnect.length === 0) && (!value.delete || Array.isArray(value.delete) && value.delete.length === 0) && (!value.update || Array.isArray(value.update.where) && value.update.where.length === 0) && (!value.create || Array.isArray(value.create) && value.create.length === 0))
|
|
@@ -337,11 +328,10 @@ const hasRelationHandleUpdate = (q, ctx, set, key, primaryKey, nestedUpdate) =>
|
|
|
337
328
|
q._select(primaryKey);
|
|
338
329
|
}
|
|
339
330
|
q.query.wrapInTransaction = true;
|
|
340
|
-
|
|
341
|
-
pqb.pushQueryValue(q, "afterUpdate", (q2) => {
|
|
331
|
+
q._afterUpdate(q.primaryKeys, (rows, q2) => {
|
|
342
332
|
return nestedUpdate(
|
|
343
333
|
q2,
|
|
344
|
-
|
|
334
|
+
rows,
|
|
345
335
|
value
|
|
346
336
|
);
|
|
347
337
|
});
|
|
@@ -385,10 +375,14 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
385
375
|
this.nestedInsert
|
|
386
376
|
);
|
|
387
377
|
}
|
|
388
|
-
update(q,
|
|
378
|
+
update(q, _, set) {
|
|
379
|
+
const params = set[this.key];
|
|
380
|
+
if ((params.set || params.create || params.upsert) && pqb.isQueryReturnsAll(q)) {
|
|
381
|
+
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
382
|
+
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
383
|
+
}
|
|
389
384
|
hasRelationHandleUpdate(
|
|
390
385
|
q,
|
|
391
|
-
ctx,
|
|
392
386
|
set,
|
|
393
387
|
this.key,
|
|
394
388
|
this.state.primaryKey,
|
|
@@ -501,11 +495,7 @@ const nestedInsert$2 = ({ query, primaryKey, foreignKey }) => {
|
|
|
501
495
|
};
|
|
502
496
|
};
|
|
503
497
|
const nestedUpdate$2 = ({ query, primaryKey, foreignKey }) => {
|
|
504
|
-
return async (
|
|
505
|
-
if ((params.set || params.create || params.upsert) && pqb.isQueryReturnsAll(q)) {
|
|
506
|
-
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
507
|
-
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
508
|
-
}
|
|
498
|
+
return async (_, data, params) => {
|
|
509
499
|
const t = query.clone();
|
|
510
500
|
const ids = data.map((item) => item[primaryKey]);
|
|
511
501
|
const currentRelationsQuery = t.where({
|
|
@@ -578,10 +568,14 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
578
568
|
this.nestedInsert
|
|
579
569
|
);
|
|
580
570
|
}
|
|
581
|
-
update(q,
|
|
571
|
+
update(q, _, set) {
|
|
572
|
+
const params = set[this.key];
|
|
573
|
+
if ((params.set || params.create) && pqb.isQueryReturnsAll(q)) {
|
|
574
|
+
const key = params.set ? "set" : "create";
|
|
575
|
+
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
576
|
+
}
|
|
582
577
|
hasRelationHandleUpdate(
|
|
583
578
|
q,
|
|
584
|
-
ctx,
|
|
585
579
|
set,
|
|
586
580
|
this.key,
|
|
587
581
|
this.state.primaryKey,
|
|
@@ -737,12 +731,8 @@ const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
737
731
|
};
|
|
738
732
|
};
|
|
739
733
|
const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
740
|
-
return async (
|
|
734
|
+
return async (_, data, params) => {
|
|
741
735
|
var _a;
|
|
742
|
-
if ((params.set || params.create) && pqb.isQueryReturnsAll(q)) {
|
|
743
|
-
const key = params.set ? "set" : "create";
|
|
744
|
-
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
745
|
-
}
|
|
746
736
|
const t = query.clone();
|
|
747
737
|
if (params.create) {
|
|
748
738
|
await t._count()._createMany(
|
|
@@ -772,7 +762,7 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
772
762
|
}
|
|
773
763
|
if (params.delete || params.update) {
|
|
774
764
|
delete t.query[pqb.toSqlCacheKey];
|
|
775
|
-
const
|
|
765
|
+
const q = t._where(
|
|
776
766
|
getWhereForNestedUpdate(
|
|
777
767
|
data,
|
|
778
768
|
params.delete || ((_a = params.update) == null ? void 0 : _a.where),
|
|
@@ -781,9 +771,9 @@ const nestedUpdate$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
781
771
|
)
|
|
782
772
|
);
|
|
783
773
|
if (params.delete) {
|
|
784
|
-
await
|
|
774
|
+
await q._delete();
|
|
785
775
|
} else if (params.update) {
|
|
786
|
-
await
|
|
776
|
+
await q._update(params.update.data);
|
|
787
777
|
}
|
|
788
778
|
}
|
|
789
779
|
};
|
|
@@ -827,10 +817,9 @@ class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
827
817
|
this.nestedInsert
|
|
828
818
|
);
|
|
829
819
|
}
|
|
830
|
-
update(q,
|
|
820
|
+
update(q, _, set) {
|
|
831
821
|
hasRelationHandleUpdate(
|
|
832
822
|
q,
|
|
833
|
-
ctx,
|
|
834
823
|
set,
|
|
835
824
|
this.key,
|
|
836
825
|
this.state.primaryKey,
|
|
@@ -910,24 +899,26 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
910
899
|
primaryKey: pk,
|
|
911
900
|
modifyRelatedQuery(relationQuery) {
|
|
912
901
|
const ref = {};
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
|
|
919
|
-
const createdCount = await subQuery.count()._createFrom(
|
|
920
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
921
|
-
fromQuery,
|
|
922
|
-
{
|
|
923
|
-
[afk]: result[apk]
|
|
924
|
-
}
|
|
902
|
+
relationQuery._afterCreate([], async (result) => {
|
|
903
|
+
if (result.length > 1) {
|
|
904
|
+
throw new pqb.OrchidOrmInternalError(
|
|
905
|
+
relationQuery,
|
|
906
|
+
"Creating multiple `hasAndBelongsToMany` records is not yet supported"
|
|
925
907
|
);
|
|
926
|
-
|
|
927
|
-
|
|
908
|
+
}
|
|
909
|
+
const fromQuery = ref.query.clone();
|
|
910
|
+
fromQuery.query.select = [{ selectAs: { [fk]: pk } }];
|
|
911
|
+
const createdCount = await subQuery.count()._createFrom(
|
|
912
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
913
|
+
fromQuery,
|
|
914
|
+
{
|
|
915
|
+
[afk]: result[0][apk]
|
|
928
916
|
}
|
|
917
|
+
);
|
|
918
|
+
if (createdCount === 0) {
|
|
919
|
+
throw new pqb.NotFoundError(fromQuery);
|
|
929
920
|
}
|
|
930
|
-
);
|
|
921
|
+
});
|
|
931
922
|
return (q) => {
|
|
932
923
|
ref.query = q;
|
|
933
924
|
};
|
|
@@ -1438,11 +1429,16 @@ const orchidORM = (_a, tables) => {
|
|
|
1438
1429
|
dbTable.db = result;
|
|
1439
1430
|
dbTable.filePath = table.filePath;
|
|
1440
1431
|
dbTable.name = table.constructor.name;
|
|
1441
|
-
if (table.hooks)
|
|
1442
|
-
addTableHooks(dbTable, table.hooks);
|
|
1443
1432
|
result[key] = dbTable;
|
|
1444
1433
|
}
|
|
1445
1434
|
applyRelations(qb, tableInstances, result);
|
|
1435
|
+
for (const key in tables) {
|
|
1436
|
+
const table = tableInstances[key];
|
|
1437
|
+
if (table.init) {
|
|
1438
|
+
table.init(result);
|
|
1439
|
+
Object.assign(result[key].baseQuery.query, table.query);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1446
1442
|
return result;
|
|
1447
1443
|
};
|
|
1448
1444
|
|
|
@@ -1505,7 +1501,6 @@ Object.defineProperty(exports, 'testTransaction', {
|
|
|
1505
1501
|
enumerable: true,
|
|
1506
1502
|
get: function () { return pqb.testTransaction; }
|
|
1507
1503
|
});
|
|
1508
|
-
exports.addTableHooks = addTableHooks;
|
|
1509
1504
|
exports.createBaseTable = createBaseTable;
|
|
1510
1505
|
exports.createRepo = createRepo;
|
|
1511
1506
|
exports.orchidORM = orchidORM;
|