orchid-orm 1.72.3 → 1.72.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/index.d.ts +38 -26
- package/dist/index.js +32 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Adapter, AfterCommitStandaloneHook, AfterHook, Column, ColumnSchemaConfig, ColumnsShape, ComputedColumnsFromOptions, ComputedOptionsConfig, ComputedOptionsFactory, CreateData, CreateManyMethodsNames, CreateMethodsNames, DbSharedOptions, DbSqlMethod, DbTableOptionScopes, DefaultColumnTypes, DefaultSchemaConfig, DeleteMethodsNames, EmptyObject, FromArg, FromResult, Grant, IsQuery, IsolationLevel, JoinQueryMethod, MapTableScopesOption, MaybeArray, MergeQuery, QueryAfterHook, QueryBeforeActionHook, QueryBeforeHook, QueryData, QueryHasWhere, QueryManyTake, QueryManyTakeOptional, QueryOrExpression, QueryReturnType, QuerySchema, QueryScopes, RecordUnknown, RelationConfigBase, Rls, SelectableFromShape, ShallowSimplify, ShapeColumnPrimaryKeys, ShapeUniqueColumns, StorageOptions, TableData, TableDataFn, TableDataItem, TableDataItemsUniqueColumnTuples, TableDataItemsUniqueColumns, TableDataItemsUniqueConstraints, TransactionOptions, UniqueConstraints, UpdateData, WhereArg } from "pqb/internal";
|
|
1
|
+
import { Adapter, AfterCommitStandaloneHook, AfterHook, Column, ColumnSchemaConfig, ColumnsShape, ComputedColumnsFromOptions, ComputedOptionsConfig, ComputedOptionsFactory, CreateData, CreateManyMethodsNames, CreateMethodsNames, CreateSelf, DbSharedOptions, DbSqlMethod, DbTableOptionScopes, DefaultColumnTypes, DefaultSchemaConfig, DeleteMethodsNames, EmptyObject, FromArg, FromResult, Grant, IsQuery, IsolationLevel, JoinQueryMethod, MapTableScopesOption, MaybeArray, MergeQuery, PickQuerySelectableRelations, QueryAfterHook, QueryBeforeActionHook, QueryBeforeHook, QueryData, QueryHasWhere, QueryManyTake, QueryManyTakeOptional, QueryOrExpression, QueryReturnType, QuerySchema, QueryScopes, RecordUnknown, RelationConfigBase, Rls, SelectableFromShape, ShallowSimplify, ShapeColumnPrimaryKeys, ShapeUniqueColumns, StorageOptions, TableData, TableDataFn, TableDataItem, TableDataItemsUniqueColumnTuples, TableDataItemsUniqueColumns, TableDataItemsUniqueConstraints, TransactionOptions, UniqueConstraints, UpdateData, WhereArg } from "pqb/internal";
|
|
2
2
|
import { Db, Query } from "pqb";
|
|
3
3
|
export * from "pqb";
|
|
4
4
|
interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
|
|
@@ -29,19 +29,19 @@ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel exte
|
|
|
29
29
|
params: HasOneParams<T, Rel['options']>;
|
|
30
30
|
maybeSingle: T['relations'][Name]['options']['required'] extends true ? QueryManyTake<Q> : QueryManyTakeOptional<Q>;
|
|
31
31
|
omitForeignKeyInCreate: never;
|
|
32
|
-
optionalDataForCreate: T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : { [P in Name]?: RelationToOneDataForCreate<{
|
|
32
|
+
optionalDataForCreate: Q extends Query.Pick.IsNotReadOnly ? T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : { [P in Name]?: RelationToOneDataForCreate<{
|
|
33
33
|
nestedCreateQuery: CreateData<Q>;
|
|
34
34
|
table: Q;
|
|
35
|
-
}> };
|
|
36
|
-
dataForCreate:
|
|
37
|
-
dataForUpdate: {
|
|
35
|
+
}> } : EmptyObject;
|
|
36
|
+
dataForCreate: undefined;
|
|
37
|
+
dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
|
|
38
38
|
disconnect: boolean;
|
|
39
39
|
} | {
|
|
40
40
|
delete: boolean;
|
|
41
41
|
} | {
|
|
42
42
|
update: UpdateData<Q>;
|
|
43
|
-
};
|
|
44
|
-
dataForUpdateOne: {
|
|
43
|
+
} : never;
|
|
44
|
+
dataForUpdateOne: Q extends Query.Pick.IsNotReadOnly ? {
|
|
45
45
|
disconnect: boolean;
|
|
46
46
|
} | {
|
|
47
47
|
set: WhereArg<Q>;
|
|
@@ -56,7 +56,7 @@ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel exte
|
|
|
56
56
|
};
|
|
57
57
|
} | {
|
|
58
58
|
create: CreateData<Q>;
|
|
59
|
-
};
|
|
59
|
+
} : never;
|
|
60
60
|
}
|
|
61
61
|
interface HasMany extends RelationThunkBase {
|
|
62
62
|
type: 'hasMany';
|
|
@@ -69,24 +69,24 @@ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel ext
|
|
|
69
69
|
params: HasOneParams<T, Rel['options']>;
|
|
70
70
|
maybeSingle: Q;
|
|
71
71
|
omitForeignKeyInCreate: never;
|
|
72
|
-
optionalDataForCreate: { [P in Name]?: T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : {
|
|
72
|
+
optionalDataForCreate: Q extends Query.Pick.IsNotReadOnly ? { [P in Name]?: T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : {
|
|
73
73
|
create?: CreateData<Q>[];
|
|
74
74
|
connect?: WhereArg<Q>[];
|
|
75
75
|
connectOrCreate?: {
|
|
76
76
|
where: WhereArg<Q>;
|
|
77
77
|
create: CreateData<Q>;
|
|
78
78
|
}[];
|
|
79
|
-
} };
|
|
80
|
-
dataForCreate:
|
|
81
|
-
dataForUpdate: {
|
|
79
|
+
} } : EmptyObject;
|
|
80
|
+
dataForCreate: undefined;
|
|
81
|
+
dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
|
|
82
82
|
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
83
83
|
delete?: MaybeArray<WhereArg<Q>>;
|
|
84
84
|
update?: {
|
|
85
85
|
where: MaybeArray<WhereArg<Q>>;
|
|
86
86
|
data: UpdateData<Q>;
|
|
87
87
|
};
|
|
88
|
-
};
|
|
89
|
-
dataForUpdateOne: {
|
|
88
|
+
} : never;
|
|
89
|
+
dataForUpdateOne: Q extends Query.Pick.IsNotReadOnly ? {
|
|
90
90
|
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
91
91
|
delete?: MaybeArray<WhereArg<Q>>;
|
|
92
92
|
update?: {
|
|
@@ -96,7 +96,7 @@ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel ext
|
|
|
96
96
|
set?: MaybeArray<WhereArg<Q>>;
|
|
97
97
|
add?: MaybeArray<WhereArg<Q>>;
|
|
98
98
|
create?: CreateData<Q>[];
|
|
99
|
-
};
|
|
99
|
+
} : never;
|
|
100
100
|
}
|
|
101
101
|
interface RelJoin extends JoinQueryMethod {
|
|
102
102
|
<T extends Query>(this: T): T;
|
|
@@ -125,25 +125,25 @@ interface BelongsToInfo<T extends RelationConfigSelf, Name extends string, FK ex
|
|
|
125
125
|
omitForeignKeyInCreate: FK;
|
|
126
126
|
dataForCreate: {
|
|
127
127
|
columns: FK;
|
|
128
|
-
nested: Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> };
|
|
128
|
+
nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : never;
|
|
129
129
|
};
|
|
130
130
|
optionalDataForCreate: EmptyObject;
|
|
131
131
|
dataForUpdate: {
|
|
132
132
|
disconnect: boolean;
|
|
133
133
|
} | {
|
|
134
134
|
set: WhereArg<Q>;
|
|
135
|
-
} | {
|
|
135
|
+
} | (Q extends Query.Pick.IsNotReadOnly ? {
|
|
136
136
|
delete: boolean;
|
|
137
137
|
} | {
|
|
138
138
|
update: UpdateData<Q>;
|
|
139
139
|
} | {
|
|
140
140
|
create: CreateData<Q>;
|
|
141
|
-
};
|
|
141
|
+
} : never);
|
|
142
142
|
dataForUpdateOne: {
|
|
143
143
|
disconnect: boolean;
|
|
144
144
|
} | {
|
|
145
145
|
set: WhereArg<Q>;
|
|
146
|
-
} | {
|
|
146
|
+
} | (Q extends Query.Pick.IsNotReadOnly ? {
|
|
147
147
|
delete: boolean;
|
|
148
148
|
} | {
|
|
149
149
|
update: UpdateData<Q>;
|
|
@@ -154,7 +154,7 @@ interface BelongsToInfo<T extends RelationConfigSelf, Name extends string, FK ex
|
|
|
154
154
|
update: UpdateData<Q>;
|
|
155
155
|
create: CreateData<Q> | (() => CreateData<Q>);
|
|
156
156
|
};
|
|
157
|
-
};
|
|
157
|
+
} : never);
|
|
158
158
|
}
|
|
159
159
|
declare function transaction<Result>(this: {
|
|
160
160
|
$qb: Db;
|
|
@@ -392,16 +392,18 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
|
|
|
392
392
|
params: HasAndBelongsToManyParams<T, FK>;
|
|
393
393
|
maybeSingle: Q;
|
|
394
394
|
omitForeignKeyInCreate: never;
|
|
395
|
-
optionalDataForCreate: { [P in Name]?: {
|
|
395
|
+
optionalDataForCreate: { [P in Name]?: Q extends Query.Pick.IsNotReadOnly ? {
|
|
396
396
|
create?: CreateData<Q>[];
|
|
397
397
|
connect?: WhereArg<Q>[];
|
|
398
398
|
connectOrCreate?: {
|
|
399
399
|
where: WhereArg<Q>;
|
|
400
400
|
create: CreateData<Q>;
|
|
401
401
|
}[];
|
|
402
|
+
} : {
|
|
403
|
+
connect?: WhereArg<Q>[];
|
|
402
404
|
} };
|
|
403
|
-
dataForCreate:
|
|
404
|
-
dataForUpdate: {
|
|
405
|
+
dataForCreate: undefined;
|
|
406
|
+
dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
|
|
405
407
|
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
406
408
|
set?: MaybeArray<WhereArg<Q>>;
|
|
407
409
|
add?: MaybeArray<WhereArg<Q>>;
|
|
@@ -411,8 +413,12 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
|
|
|
411
413
|
data: UpdateData<Q>;
|
|
412
414
|
};
|
|
413
415
|
create?: CreateData<Q>[];
|
|
416
|
+
} : {
|
|
417
|
+
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
418
|
+
set?: MaybeArray<WhereArg<Q>>;
|
|
419
|
+
add?: MaybeArray<WhereArg<Q>>;
|
|
414
420
|
};
|
|
415
|
-
dataForUpdateOne: {
|
|
421
|
+
dataForUpdateOne: Q extends Query.Pick.IsNotReadOnly ? {
|
|
416
422
|
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
417
423
|
set?: MaybeArray<WhereArg<Q>>;
|
|
418
424
|
add?: MaybeArray<WhereArg<Q>>;
|
|
@@ -422,6 +428,10 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
|
|
|
422
428
|
data: UpdateData<Q>;
|
|
423
429
|
};
|
|
424
430
|
create?: CreateData<Q>[];
|
|
431
|
+
} : {
|
|
432
|
+
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
433
|
+
set?: MaybeArray<WhereArg<Q>>;
|
|
434
|
+
add?: MaybeArray<WhereArg<Q>>;
|
|
425
435
|
};
|
|
426
436
|
}
|
|
427
437
|
type RelationToOneDataForCreate<Rel extends {
|
|
@@ -443,7 +453,8 @@ type RelationToOneDataForCreate<Rel extends {
|
|
|
443
453
|
create: Rel['nestedCreateQuery'];
|
|
444
454
|
};
|
|
445
455
|
};
|
|
446
|
-
|
|
456
|
+
interface RelationToOneDataForCreateSameQuerySelf extends CreateSelf, PickQuerySelectableRelations {}
|
|
457
|
+
type RelationToOneDataForCreateSameQuery<Q extends RelationToOneDataForCreateSameQuerySelf> = {
|
|
447
458
|
create: CreateData<Q>;
|
|
448
459
|
connect?: never;
|
|
449
460
|
connectOrCreate?: never;
|
|
@@ -491,7 +502,7 @@ interface TableInfo {
|
|
|
491
502
|
name: string;
|
|
492
503
|
}
|
|
493
504
|
interface Table extends Query, TableInfo {}
|
|
494
|
-
interface TableToDb<T extends ORMTableInput> extends TableInfo, Db<T['table'], T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>, ColumnsShape.DefaultSelectKeys<T['columns']['shape']
|
|
505
|
+
interface TableToDb<T extends ORMTableInput> extends TableInfo, Db<T['table'], T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>, ColumnsShape.DefaultSelectKeys<T['columns']['shape']>, T['readOnly'] extends true ? true : undefined> {
|
|
495
506
|
relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? BelongsToInfo<T, K, T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<RelationTableToQuery<T['relations'][K]>, K>> : T['relations'][K] extends HasOne ? HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasMany ? HasManyInfo<T, K, T['relations'][K], HasManyQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, K, T['relations'][K]['options']['columns'][number] & string, HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>> : never } : EmptyObject;
|
|
496
507
|
}
|
|
497
508
|
interface ORMTableInput {
|
|
@@ -517,6 +528,7 @@ interface ORMTableInput {
|
|
|
517
528
|
* Table-local grants used by migration generation.
|
|
518
529
|
*/
|
|
519
530
|
grants?: readonly Grant.TableClassGrant[];
|
|
531
|
+
readonly readOnly?: boolean;
|
|
520
532
|
autoForeignKeys?: TableData.References.BaseOptions | boolean;
|
|
521
533
|
}
|
|
522
534
|
type Queryable<T extends ORMTableInput> = ShallowSimplify<{ [K in keyof T['columns']['shape']]?: T['columns']['shape'][K]['queryType'] }>;
|
package/dist/index.js
CHANGED
|
@@ -313,6 +313,7 @@ var BelongsToVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
313
313
|
this.nestedUpdate = nestedUpdate$2(this.state);
|
|
314
314
|
}
|
|
315
315
|
create(q, ctx, items) {
|
|
316
|
+
const queryForCreate = q;
|
|
316
317
|
const { key, state: { query, primaryKeys, foreignKeys } } = this;
|
|
317
318
|
let nestedCreateItems;
|
|
318
319
|
items.forEach((item) => {
|
|
@@ -331,7 +332,7 @@ var BelongsToVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
331
332
|
if (!nestedCreateItems) return;
|
|
332
333
|
for (const key of foreignKeys) if (!ctx.columns.has(key)) ctx.columns.set(key, ctx.columns.size);
|
|
333
334
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
334
|
-
if (create) (0, pqb_internal._prependWith)(
|
|
335
|
+
if (create) (0, pqb_internal._prependWith)(queryForCreate, (as) => {
|
|
335
336
|
const count = create.items.length;
|
|
336
337
|
foreignKeys.forEach((foreignKey, i) => {
|
|
337
338
|
const primaryKey = primaryKeys[i];
|
|
@@ -341,20 +342,21 @@ var BelongsToVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
341
342
|
});
|
|
342
343
|
}, (0, pqb_internal._queryInsertMany)(query.select(...primaryKeys), create.values));
|
|
343
344
|
if (connect) connect.values.forEach((value, itemI) => {
|
|
344
|
-
const as = (0, pqb_internal.getFreeAlias)(
|
|
345
|
-
(0, pqb_internal._prependWith)(
|
|
345
|
+
const as = (0, pqb_internal.getFreeAlias)(queryForCreate.q.withShapes, "q");
|
|
346
|
+
(0, pqb_internal._prependWith)(queryForCreate, as, query.select(...primaryKeys).findBy(value));
|
|
346
347
|
foreignKeys.map((foreignKey, i) => {
|
|
347
348
|
connect.items[itemI][foreignKey] = new pqb_internal.RawSql(selectCteColumnMustExistSql(i, as, primaryKeys[i]));
|
|
348
349
|
});
|
|
349
350
|
});
|
|
350
351
|
if (connectOrCreate) connectOrCreate.values.forEach((value, itemI) => {
|
|
351
|
-
(0, pqb_internal._prependWith)(
|
|
352
|
+
(0, pqb_internal._prependWith)(queryForCreate, setForeignKeysFromCte(connectOrCreate.items[itemI], primaryKeys, foreignKeys), (0, pqb_internal._orCreate)((0, pqb_internal._queryWhere)(query.select(...primaryKeys), [value.where]), value.create));
|
|
352
353
|
});
|
|
353
354
|
}
|
|
354
355
|
update(q, set) {
|
|
355
|
-
|
|
356
|
+
const queryForUpdate = q;
|
|
357
|
+
queryForUpdate.q.wrapInTransaction = true;
|
|
356
358
|
const data = set[this.key];
|
|
357
|
-
this.nestedUpdate(
|
|
359
|
+
this.nestedUpdate(queryForUpdate, set, data);
|
|
358
360
|
}
|
|
359
361
|
};
|
|
360
362
|
const makeBelongsToMethod = (tableConfig, table, relation, relationName, query) => {
|
|
@@ -486,7 +488,8 @@ var HasOneVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
486
488
|
for (const foreignKey of state.foreignKeys) this.setNulls[foreignKey] = null;
|
|
487
489
|
}
|
|
488
490
|
create(self, ctx, items, rowIndexes, count) {
|
|
489
|
-
|
|
491
|
+
const querySelf = self;
|
|
492
|
+
if (count <= querySelf.qb.internal.nestedCreateBatchMax) {
|
|
490
493
|
const { query: rel, primaryKeys, foreignKeys } = this.state;
|
|
491
494
|
let nestedCreateItems;
|
|
492
495
|
items.forEach((item, i) => {
|
|
@@ -509,7 +512,7 @@ var HasOneVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
509
512
|
let createAs;
|
|
510
513
|
let connectAs;
|
|
511
514
|
let connectOrCreateAs;
|
|
512
|
-
(0, pqb_internal._hookSelectColumns)(
|
|
515
|
+
(0, pqb_internal._hookSelectColumns)(querySelf, primaryKeys, (aliasedPrimaryKeys) => {
|
|
513
516
|
foreignKeys.forEach((key, keyI) => {
|
|
514
517
|
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
515
518
|
if (create && createAs) for (let i = 0; i < create.items.length; i++) create.items[i][key]._sql = selectCteColumnFromManySql(createAs, primaryKey, create.indexes[i], count);
|
|
@@ -518,14 +521,14 @@ var HasOneVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
518
521
|
});
|
|
519
522
|
});
|
|
520
523
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
521
|
-
if (create) (0, pqb_internal._appendQuery)(
|
|
524
|
+
if (create) (0, pqb_internal._appendQuery)(querySelf, (0, pqb_internal._queryInsertMany)((0, pqb_internal._clone)(rel), create.items), (as) => createAs = as);
|
|
522
525
|
if (connect) connect.values.forEach((value, i) => {
|
|
523
526
|
const query = (0, pqb_internal._queryUpdateOrThrow)(rel.where(value), connect.items[i]);
|
|
524
527
|
query.q.ensureCount = 1;
|
|
525
|
-
(0, pqb_internal._appendQuery)(
|
|
528
|
+
(0, pqb_internal._appendQuery)(querySelf, query, (as) => connectAs = as);
|
|
526
529
|
});
|
|
527
530
|
if (connectOrCreate) connectOrCreate.values.forEach((value, i) => {
|
|
528
|
-
(0, pqb_internal._appendQuery)(
|
|
531
|
+
(0, pqb_internal._appendQuery)(querySelf, (0, pqb_internal._queryUpsert)(rel.where(value.where), {
|
|
529
532
|
update: connectOrCreate.items[i],
|
|
530
533
|
create: {
|
|
531
534
|
...value.create,
|
|
@@ -533,18 +536,19 @@ var HasOneVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
533
536
|
}
|
|
534
537
|
}), (as) => connectOrCreateAs = as);
|
|
535
538
|
});
|
|
536
|
-
} else hasRelationHandleCreate(
|
|
539
|
+
} else hasRelationHandleCreate(querySelf, ctx, items, rowIndexes, this.key, this.state.primaryKeys, this.nestedInsert);
|
|
537
540
|
}
|
|
538
541
|
update(self, set) {
|
|
542
|
+
const querySelf = self;
|
|
539
543
|
const params = set[this.key];
|
|
540
|
-
if ((params.set || params.create || params.upsert) && (0, pqb_internal.isQueryReturnsAll)(
|
|
544
|
+
if ((params.set || params.create || params.upsert) && (0, pqb_internal.isQueryReturnsAll)(querySelf)) {
|
|
541
545
|
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
542
546
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
543
547
|
}
|
|
544
548
|
const { primaryKeys, foreignKeys, query: relQuery } = this.state;
|
|
545
549
|
if (params.create || params.update || params.upsert || params.disconnect || params.set || params.delete) {
|
|
546
550
|
let appendedAs;
|
|
547
|
-
(0, pqb_internal._hookSelectColumns)(
|
|
551
|
+
(0, pqb_internal._hookSelectColumns)(querySelf, primaryKeys, (aliasedPrimaryKeys) => {
|
|
548
552
|
selectIdsSql._sql = selectCteColumnsSql(appendedAs, aliasedPrimaryKeys);
|
|
549
553
|
if (params.create || params.set || params.upsert) foreignKeys.forEach((foreignKey, i) => {
|
|
550
554
|
setIds[foreignKey]._sql = selectCteColumnSql(appendedAs, aliasedPrimaryKeys[i]);
|
|
@@ -567,15 +571,15 @@ var HasOneVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
567
571
|
}
|
|
568
572
|
}) : params.delete ? (0, pqb_internal._queryDelete)(existingRelQuery) : (0, pqb_internal._queryUpdate)(existingRelQuery, this.setNulls);
|
|
569
573
|
nullifyOrDeleteQuery.q.returnType = "void";
|
|
570
|
-
(0, pqb_internal._appendQuery)(
|
|
571
|
-
if (params.create) (0, pqb_internal._appendQuery)(
|
|
574
|
+
(0, pqb_internal._appendQuery)(querySelf, nullifyOrDeleteQuery, (as) => appendedAs = as);
|
|
575
|
+
if (params.create) (0, pqb_internal._appendQuery)(querySelf, (0, pqb_internal._queryInsert)((0, pqb_internal._clone)(relQuery), {
|
|
572
576
|
...params.create,
|
|
573
577
|
...setIds
|
|
574
578
|
}), pqb_internal.noop);
|
|
575
579
|
else if (params.set) {
|
|
576
580
|
const setQuery = (0, pqb_internal._queryUpdate)((0, pqb_internal._queryWhere)((0, pqb_internal._clone)(relQuery), [params.set]), setIds);
|
|
577
581
|
setQuery.q.returnType = "void";
|
|
578
|
-
(0, pqb_internal._appendQuery)(
|
|
582
|
+
(0, pqb_internal._appendQuery)(querySelf, setQuery, pqb_internal.noop);
|
|
579
583
|
}
|
|
580
584
|
}
|
|
581
585
|
}
|
|
@@ -689,7 +693,8 @@ var HasManyVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
689
693
|
this.nestedUpdate = nestedUpdate$1(state);
|
|
690
694
|
}
|
|
691
695
|
create(self, ctx, items, rowIndexes, count) {
|
|
692
|
-
|
|
696
|
+
const querySelf = self;
|
|
697
|
+
if (count <= querySelf.qb.internal.nestedCreateBatchMax) {
|
|
693
698
|
const { query: rel, primaryKeys, foreignKeys } = this.state;
|
|
694
699
|
let nestedCreateItems;
|
|
695
700
|
items.forEach((item, i) => {
|
|
@@ -726,7 +731,7 @@ var HasManyVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
726
731
|
let createAs;
|
|
727
732
|
let connectAs;
|
|
728
733
|
let connectOrCreateAs;
|
|
729
|
-
(0, pqb_internal._hookSelectColumns)(
|
|
734
|
+
(0, pqb_internal._hookSelectColumns)(querySelf, primaryKeys, (aliasedPrimaryKeys) => {
|
|
730
735
|
foreignKeys.forEach((key, keyI) => {
|
|
731
736
|
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
732
737
|
if (create && createAs) for (let i = 0; i < create.items.length; i++) {
|
|
@@ -738,15 +743,15 @@ var HasManyVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
738
743
|
});
|
|
739
744
|
});
|
|
740
745
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
741
|
-
if (create) (0, pqb_internal._appendQuery)(
|
|
746
|
+
if (create) (0, pqb_internal._appendQuery)(querySelf, (0, pqb_internal._queryInsertMany)((0, pqb_internal._clone)(rel), create.items.flat()), (as) => createAs = as);
|
|
742
747
|
if (connect) connect.values.forEach((value, i) => {
|
|
743
748
|
const query = (0, pqb_internal._queryUpdateOrThrow)(rel.whereOneOf(...value), connect.items[i]);
|
|
744
749
|
query.q.ensureCount = value.length;
|
|
745
|
-
(0, pqb_internal._appendQuery)(
|
|
750
|
+
(0, pqb_internal._appendQuery)(querySelf, query, (as) => connectAs = as);
|
|
746
751
|
});
|
|
747
752
|
if (connectOrCreate) connectOrCreate.values.forEach((array, i) => {
|
|
748
753
|
const foreignKeyValues = connectOrCreate.items[i];
|
|
749
|
-
for (const value of array) (0, pqb_internal._appendQuery)(
|
|
754
|
+
for (const value of array) (0, pqb_internal._appendQuery)(querySelf, (0, pqb_internal._queryUpsert)(rel.where(value.where), {
|
|
750
755
|
update: foreignKeyValues,
|
|
751
756
|
create: {
|
|
752
757
|
...value.create,
|
|
@@ -754,15 +759,16 @@ var HasManyVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
754
759
|
}
|
|
755
760
|
}), (as) => connectOrCreateAs = as);
|
|
756
761
|
});
|
|
757
|
-
} else hasRelationHandleCreate(
|
|
762
|
+
} else hasRelationHandleCreate(querySelf, ctx, items, rowIndexes, this.key, this.state.primaryKeys, this.nestedInsert);
|
|
758
763
|
}
|
|
759
764
|
update(q, set) {
|
|
765
|
+
const query = q;
|
|
760
766
|
const params = set[this.key];
|
|
761
|
-
if ((params.set || params.create) && (0, pqb_internal.isQueryReturnsAll)(
|
|
767
|
+
if ((params.set || params.create) && (0, pqb_internal.isQueryReturnsAll)(query)) {
|
|
762
768
|
const key = params.set ? "set" : "create";
|
|
763
769
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
764
770
|
}
|
|
765
|
-
hasRelationHandleUpdate(
|
|
771
|
+
hasRelationHandleUpdate(query, set, this.key, this.state.primaryKeys, this.nestedUpdate);
|
|
766
772
|
}
|
|
767
773
|
};
|
|
768
774
|
const makeHasManyMethod = (tableConfig, table, relation, relationName, query) => {
|
|
@@ -1401,6 +1407,7 @@ const assignTablesToOrm = (tables, result, adapter, qb, asyncStorage, commonOpti
|
|
|
1401
1407
|
softDelete: table.softDelete,
|
|
1402
1408
|
snakeCase: table.snakeCase,
|
|
1403
1409
|
comment: table.comment,
|
|
1410
|
+
readOnly: table.readOnly,
|
|
1404
1411
|
noPrimaryKey: table.noPrimaryKey ? "ignore" : void 0,
|
|
1405
1412
|
computed: table.computed,
|
|
1406
1413
|
nowSQL: tableClass.nowSQL
|