orchid-orm 1.60.5 → 1.61.1
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 +5 -4
- package/dist/index.js +358 -117
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +359 -118
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/index.js +14 -6
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +16 -8
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/migrations/node-postgres.js +14 -6
- package/dist/migrations/node-postgres.js.map +1 -1
- package/dist/migrations/node-postgres.mjs +16 -8
- package/dist/migrations/node-postgres.mjs.map +1 -1
- package/dist/migrations/postgres-js.js +14 -6
- package/dist/migrations/postgres-js.js.map +1 -1
- package/dist/migrations/postgres-js.mjs +16 -8
- package/dist/migrations/postgres-js.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Column, TableData, ColumnsShape, Query, SelectableFromShape, CreateManyMethodsNames, QueryHasWhere, RelationConfigBase, QueryManyTake, QueryManyTakeOptional, EmptyObject, CreateData, UpdateData, WhereArg, CreateMethodsNames, MaybeArray, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, AfterCommitStandaloneHook, AdapterBase, FromArg, FromResult, DbSharedOptions, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, RecordUnknown, ShallowSimplify, ComputedOptionsConfig, QueryOrExpression, QueryData, IsQuery, TableDataFn, DbTableOptionScopes, QueryScopes, ColumnSchemaConfig, DbSqlMethod, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryBeforeActionHook, QueryAfterHook, AfterHook, MergeQuery, QueryReturnType } from 'pqb';
|
|
1
|
+
import { Column, TableData, ColumnsShape, Query, SelectableFromShape, CreateManyMethodsNames, QueryHasWhere, RelationConfigBase, QueryManyTake, QueryManyTakeOptional, EmptyObject, CreateData, UpdateData, WhereArg, CreateMethodsNames, MaybeArray, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, AfterCommitStandaloneHook, AdapterBase, FromArg, FromResult, DbSharedOptions, QuerySchema, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, RecordUnknown, ShallowSimplify, ComputedOptionsConfig, QueryOrExpression, QueryData, IsQuery, TableDataFn, DbTableOptionScopes, QueryScopes, ColumnSchemaConfig, DbSqlMethod, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryBeforeActionHook, QueryAfterHook, AfterHook, MergeQuery, QueryReturnType } from 'pqb';
|
|
2
2
|
export * from 'pqb';
|
|
3
3
|
|
|
4
4
|
interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
|
|
@@ -292,7 +292,7 @@ interface OrchidORMMethods {
|
|
|
292
292
|
$close(): Promise<void>;
|
|
293
293
|
}
|
|
294
294
|
type OrchidOrmParam<Options> = true | null extends true ? 'Set strict: true to tsconfig' : Options;
|
|
295
|
-
declare const orchidORMWithAdapter: <T extends TableClasses>({ log, logger, autoPreparedStatements, noPrimaryKey, ...options }: OrchidOrmParam<({
|
|
295
|
+
declare const orchidORMWithAdapter: <T extends TableClasses>({ log, logger, autoPreparedStatements, noPrimaryKey, schema, ...options }: OrchidOrmParam<({
|
|
296
296
|
db: Query;
|
|
297
297
|
} | {
|
|
298
298
|
adapter: AdapterBase;
|
|
@@ -308,6 +308,7 @@ interface HasAndBelongsToManyOptions<Columns extends Column.Shape.QueryInit = Co
|
|
|
308
308
|
references: string[];
|
|
309
309
|
foreignKey?: boolean | TableData.References.Options;
|
|
310
310
|
through: {
|
|
311
|
+
schema?: QuerySchema;
|
|
311
312
|
table: string;
|
|
312
313
|
columns: string[];
|
|
313
314
|
references: (keyof Related['columns']['shape'])[];
|
|
@@ -442,7 +443,7 @@ interface ORMTableInput {
|
|
|
442
443
|
shape: Column.Shape.QueryInit;
|
|
443
444
|
data: MaybeArray<TableDataItem>;
|
|
444
445
|
};
|
|
445
|
-
schema?:
|
|
446
|
+
schema?: QuerySchema;
|
|
446
447
|
types: unknown;
|
|
447
448
|
noPrimaryKey?: boolean;
|
|
448
449
|
filePath: string;
|
|
@@ -487,7 +488,7 @@ interface BaseTableInstance<ColumnTypes> {
|
|
|
487
488
|
shape: Column.Shape.QueryInit;
|
|
488
489
|
data: MaybeArray<TableDataItem>;
|
|
489
490
|
};
|
|
490
|
-
schema?:
|
|
491
|
+
schema?: QuerySchema;
|
|
491
492
|
noPrimaryKey?: boolean;
|
|
492
493
|
snakeCase?: boolean;
|
|
493
494
|
types: ColumnTypes;
|
package/dist/index.js
CHANGED
|
@@ -147,31 +147,33 @@ const getThroughRelation = (table, through) => {
|
|
|
147
147
|
const getSourceRelation = (throughRelation, source) => {
|
|
148
148
|
return throughRelation.query.relations[source];
|
|
149
149
|
};
|
|
150
|
-
const hasRelationHandleCreate = (q, ctx,
|
|
151
|
-
const value = item[key];
|
|
152
|
-
if ((!value.create || Array.isArray(value.create) && value.create.length === 0) && (!value.connect || Array.isArray(value.connect) && value.connect.length === 0) && (!value.connectOrCreate || Array.isArray(value.connectOrCreate) && value.connectOrCreate.length === 0))
|
|
153
|
-
return;
|
|
154
|
-
const store = ctx;
|
|
155
|
-
if (!store.hasRelation) store.hasRelation = {};
|
|
156
|
-
const values = [rowIndex, value];
|
|
157
|
-
if (store.hasRelation[key]) {
|
|
158
|
-
store.hasRelation[key].push(values);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
150
|
+
const hasRelationHandleCreate = (q, ctx, items, rowIndexes, key, primaryKeys, nestedInsert) => {
|
|
161
151
|
q.q.wrapInTransaction = true;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
152
|
+
items.forEach((item, i) => {
|
|
153
|
+
const value = item[key];
|
|
154
|
+
if ((!value.create || Array.isArray(value.create) && value.create.length === 0) && (!value.connect || Array.isArray(value.connect) && value.connect.length === 0) && (!value.connectOrCreate || Array.isArray(value.connectOrCreate) && value.connectOrCreate.length === 0))
|
|
155
|
+
return;
|
|
156
|
+
const store = ctx;
|
|
157
|
+
if (!store.hasRelation) store.hasRelation = {};
|
|
158
|
+
const values = [rowIndexes[i], value];
|
|
159
|
+
if (store.hasRelation[key]) {
|
|
160
|
+
store.hasRelation[key].push(values);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const relationData = [values];
|
|
164
|
+
store.hasRelation[key] = relationData;
|
|
165
|
+
pqb._queryHookAfterCreate(
|
|
166
|
+
q,
|
|
167
|
+
primaryKeys,
|
|
168
|
+
(rows, q2) => nestedInsert(
|
|
169
|
+
q2,
|
|
170
|
+
relationData.map(([rowIndex, data]) => [
|
|
171
|
+
rows[rowIndex],
|
|
172
|
+
data
|
|
173
|
+
])
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
});
|
|
175
177
|
};
|
|
176
178
|
const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
177
179
|
const value = set[key];
|
|
@@ -186,17 +188,6 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
186
188
|
);
|
|
187
189
|
});
|
|
188
190
|
};
|
|
189
|
-
const _selectIfNotSelected = (q, columns) => {
|
|
190
|
-
const select = q.q.select || [];
|
|
191
|
-
if (!select.includes("*")) {
|
|
192
|
-
for (const column of columns) {
|
|
193
|
-
if (!select.includes(column)) {
|
|
194
|
-
select.push(column);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
q.q.select = select;
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
191
|
function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
|
|
201
192
|
return q.whereExists(
|
|
202
193
|
throughRelation.joinQuery(
|
|
@@ -284,6 +275,14 @@ const getColumnKeyFromDbName = (query, name) => {
|
|
|
284
275
|
};
|
|
285
276
|
const selectCteColumnsSql = (cteAs, columns) => `(SELECT ${columns.map((c) => `"${cteAs}"."${c}"`).join(", ")} FROM "${cteAs}")`;
|
|
286
277
|
const selectCteColumnSql = (cteAs, column) => `(SELECT "${cteAs}"."${column}" FROM "${cteAs}")`;
|
|
278
|
+
const selectCteColumnFromManySql = (cteAs, column, rowIndex, count) => {
|
|
279
|
+
let sql = `(SELECT "${cteAs}"."${column}" FROM "${cteAs}"`;
|
|
280
|
+
if (count > 1) {
|
|
281
|
+
sql += ` LIMIT 1`;
|
|
282
|
+
if (rowIndex) sql += ` OFFSET ${rowIndex}`;
|
|
283
|
+
}
|
|
284
|
+
return sql + ")";
|
|
285
|
+
};
|
|
287
286
|
|
|
288
287
|
const joinQueryChainHOF = (relPKeys, reverseJoin, joinQuery) => (joiningQuery, baseQuery) => {
|
|
289
288
|
const jq = joiningQuery;
|
|
@@ -401,40 +400,88 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
401
400
|
this.state = state;
|
|
402
401
|
this.nestedUpdate = nestedUpdate$2(this.state);
|
|
403
402
|
}
|
|
404
|
-
create(q, ctx,
|
|
403
|
+
create(q, ctx, items) {
|
|
405
404
|
const {
|
|
406
405
|
key,
|
|
407
406
|
state: { query, primaryKeys, foreignKeys }
|
|
408
407
|
} = this;
|
|
408
|
+
let nestedCreateItems;
|
|
409
|
+
items.forEach((item) => {
|
|
410
|
+
var _a;
|
|
411
|
+
const value = item[key];
|
|
412
|
+
const kind = value.create ? "create" : value.connect ? "connect" : "connectOrCreate";
|
|
413
|
+
if (kind) {
|
|
414
|
+
const nestedCreateItem = (_a = nestedCreateItems ?? (nestedCreateItems = {}))[kind] ?? (_a[kind] = {
|
|
415
|
+
items: [],
|
|
416
|
+
values: []
|
|
417
|
+
});
|
|
418
|
+
nestedCreateItem.items.push(item);
|
|
419
|
+
nestedCreateItem.values.push(value[kind]);
|
|
420
|
+
if (kind !== "connect") {
|
|
421
|
+
for (const key2 of foreignKeys) {
|
|
422
|
+
item[key2] = new pqb.RawSql("");
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
if (!nestedCreateItems) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
409
430
|
for (const key2 of foreignKeys) {
|
|
410
431
|
if (!ctx.columns.has(key2)) {
|
|
411
432
|
ctx.columns.set(key2, ctx.columns.size);
|
|
412
433
|
}
|
|
413
434
|
}
|
|
414
|
-
const
|
|
415
|
-
if (
|
|
416
|
-
const asFn = setForeignKeysFromCte(item, primaryKeys, foreignKeys);
|
|
435
|
+
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
436
|
+
if (create) {
|
|
417
437
|
const selectPKeys = query.select(...primaryKeys);
|
|
418
438
|
pqb._prependWith(
|
|
419
439
|
q,
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
440
|
+
(as) => {
|
|
441
|
+
const count = create.items.length;
|
|
442
|
+
foreignKeys.forEach((foreignKey, i) => {
|
|
443
|
+
const primaryKey = primaryKeys[i];
|
|
444
|
+
create.items.forEach((item, i2) => {
|
|
445
|
+
item[foreignKey]._sql = selectCteColumnFromManySql(
|
|
446
|
+
as,
|
|
447
|
+
primaryKey,
|
|
448
|
+
i2,
|
|
449
|
+
count
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
},
|
|
454
|
+
pqb._queryInsertMany(selectPKeys, create.values)
|
|
427
455
|
);
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
456
|
+
}
|
|
457
|
+
if (connect) {
|
|
458
|
+
connect.values.forEach((value, itemI) => {
|
|
459
|
+
const as = pqb.getFreeAlias(q.q.withShapes, "q");
|
|
460
|
+
pqb._prependWith(q, as, query.select(...primaryKeys).findBy(value));
|
|
461
|
+
foreignKeys.map((foreignKey, i) => {
|
|
462
|
+
connect.items[itemI][foreignKey] = new pqb.RawSql(
|
|
463
|
+
selectCteColumnMustExistSql(i, as, primaryKeys[i])
|
|
464
|
+
);
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
if (connectOrCreate) {
|
|
469
|
+
connectOrCreate.values.forEach((value, itemI) => {
|
|
470
|
+
const asFn = setForeignKeysFromCte(
|
|
471
|
+
connectOrCreate.items[itemI],
|
|
472
|
+
primaryKeys,
|
|
473
|
+
foreignKeys
|
|
474
|
+
);
|
|
475
|
+
const selectPKeys = query.select(...primaryKeys);
|
|
476
|
+
pqb._prependWith(
|
|
477
|
+
q,
|
|
478
|
+
asFn,
|
|
479
|
+
pqb._orCreate(
|
|
480
|
+
pqb._queryWhere(selectPKeys, [value.where]),
|
|
481
|
+
value.create
|
|
482
|
+
)
|
|
435
483
|
);
|
|
436
484
|
});
|
|
437
|
-
return;
|
|
438
485
|
}
|
|
439
486
|
}
|
|
440
487
|
update(q, set) {
|
|
@@ -521,16 +568,20 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
521
568
|
const asFn = setForeignKeysFromCte(update, primaryKeys, foreignKeys);
|
|
522
569
|
pqb._prependWith(self, asFn, createQuery);
|
|
523
570
|
} else if (params.update) {
|
|
524
|
-
|
|
571
|
+
let appendedAs;
|
|
572
|
+
pqb._hookSelectColumns(self, foreignKeys, (aliasedForeignKeys) => {
|
|
573
|
+
selectIdsSql._sql = selectCteColumnsSql(
|
|
574
|
+
appendedAs,
|
|
575
|
+
aliasedForeignKeys
|
|
576
|
+
);
|
|
577
|
+
});
|
|
525
578
|
const selectIdsSql = new pqb.RawSql("");
|
|
526
579
|
const updateQuery = pqb._queryUpdate(
|
|
527
580
|
pqb._queryWhereIn(query.clone(), true, primaryKeys, selectIdsSql),
|
|
528
581
|
params.update
|
|
529
582
|
);
|
|
530
583
|
updateQuery.q.returnType = "value";
|
|
531
|
-
pqb._appendQuery(self, updateQuery, (as) =>
|
|
532
|
-
selectIdsSql._sql = selectCteColumnsSql(as, foreignKeys);
|
|
533
|
-
});
|
|
584
|
+
pqb._appendQuery(self, updateQuery, (as) => appendedAs = as);
|
|
534
585
|
} else if (params.upsert) {
|
|
535
586
|
if (pqb.isQueryReturnsAll(self)) {
|
|
536
587
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
@@ -551,7 +602,9 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
551
602
|
const asFn = setForeignKeysFromCte(update, primaryKeys, foreignKeys);
|
|
552
603
|
pqb._prependWith(self, asFn, upsertQuery);
|
|
553
604
|
} else if (params.delete) {
|
|
554
|
-
|
|
605
|
+
pqb._hookSelectColumns(self, foreignKeys, (a) => {
|
|
606
|
+
console.log(a);
|
|
607
|
+
});
|
|
555
608
|
disconnect(update, foreignKeys);
|
|
556
609
|
const { selectIdsSql, relQuery } = relWithSelectIds(
|
|
557
610
|
self,
|
|
@@ -659,39 +712,103 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
659
712
|
this.setNulls[foreignKey] = null;
|
|
660
713
|
}
|
|
661
714
|
}
|
|
662
|
-
create(self, ctx,
|
|
663
|
-
if (
|
|
664
|
-
const value = item[this.key];
|
|
665
|
-
if (!value.create && !value.connect && !value.connectOrCreate) {
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
715
|
+
create(self, ctx, items, rowIndexes, count) {
|
|
716
|
+
if (count <= self.qb.internal.nestedCreateBatchMax) {
|
|
668
717
|
const { query: rel, primaryKeys, foreignKeys } = this.state;
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
718
|
+
let nestedCreateItems;
|
|
719
|
+
items.forEach((item, i) => {
|
|
720
|
+
var _a;
|
|
721
|
+
const value = item[this.key];
|
|
722
|
+
const kind = value.create ? "create" : value.connect ? "connect" : "connectOrCreate";
|
|
723
|
+
if (kind) {
|
|
724
|
+
const nestedCreateItem = (_a = nestedCreateItems ?? (nestedCreateItems = {}))[kind] ?? (_a[kind] = {
|
|
725
|
+
indexes: [],
|
|
726
|
+
items: [],
|
|
727
|
+
values: []
|
|
728
|
+
});
|
|
729
|
+
nestedCreateItem.indexes.push(rowIndexes[i]);
|
|
730
|
+
nestedCreateItem.values.push(value[kind]);
|
|
731
|
+
const data = value.create ? { ...value.create } : {};
|
|
732
|
+
for (const key of foreignKeys) {
|
|
733
|
+
data[key] = new pqb.RawSql("");
|
|
734
|
+
}
|
|
735
|
+
nestedCreateItem.items.push(data);
|
|
682
736
|
}
|
|
683
|
-
})
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
737
|
+
});
|
|
738
|
+
if (!nestedCreateItems) {
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
let createAs;
|
|
742
|
+
let connectAs;
|
|
743
|
+
let connectOrCreateAs;
|
|
744
|
+
pqb._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
745
|
+
foreignKeys.forEach((key, keyI) => {
|
|
746
|
+
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
747
|
+
if (create && createAs) {
|
|
748
|
+
for (let i = 0; i < create.items.length; i++) {
|
|
749
|
+
create.items[i][key]._sql = selectCteColumnFromManySql(
|
|
750
|
+
createAs,
|
|
751
|
+
primaryKey,
|
|
752
|
+
create.indexes[i],
|
|
753
|
+
count
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (connect && connectAs) {
|
|
758
|
+
for (let i = 0; i < connect.items.length; i++) {
|
|
759
|
+
connect.items[i][key]._sql = selectCteColumnFromManySql(
|
|
760
|
+
connectAs,
|
|
761
|
+
primaryKey,
|
|
762
|
+
connect.indexes[i],
|
|
763
|
+
count
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (connectOrCreate && connectOrCreateAs) {
|
|
768
|
+
for (let i = 0; i < connectOrCreate.items.length; i++) {
|
|
769
|
+
connectOrCreate.items[i][key]._sql = selectCteColumnFromManySql(
|
|
770
|
+
connectOrCreateAs,
|
|
771
|
+
primaryKey,
|
|
772
|
+
connectOrCreate.indexes[i],
|
|
773
|
+
count
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
687
777
|
});
|
|
688
778
|
});
|
|
779
|
+
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
780
|
+
if (create) {
|
|
781
|
+
const query = pqb._queryInsertMany(pqb._clone(rel), create.items);
|
|
782
|
+
pqb._appendQuery(self, query, (as) => createAs = as);
|
|
783
|
+
}
|
|
784
|
+
if (connect) {
|
|
785
|
+
connect.values.forEach((value, i) => {
|
|
786
|
+
const query = pqb._queryUpdateOrThrow(
|
|
787
|
+
rel.where(value),
|
|
788
|
+
connect.items[i]
|
|
789
|
+
);
|
|
790
|
+
query.q.ensureCount = 1;
|
|
791
|
+
pqb._appendQuery(self, query, (as) => connectAs = as);
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
if (connectOrCreate) {
|
|
795
|
+
connectOrCreate.values.forEach((value, i) => {
|
|
796
|
+
const query = pqb._queryUpsert(rel.where(value.where), {
|
|
797
|
+
update: connectOrCreate.items[i],
|
|
798
|
+
create: {
|
|
799
|
+
...value.create,
|
|
800
|
+
...connectOrCreate.items[i]
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
pqb._appendQuery(self, query, (as) => connectOrCreateAs = as);
|
|
804
|
+
});
|
|
805
|
+
}
|
|
689
806
|
} else {
|
|
690
807
|
hasRelationHandleCreate(
|
|
691
808
|
self,
|
|
692
809
|
ctx,
|
|
693
|
-
|
|
694
|
-
|
|
810
|
+
items,
|
|
811
|
+
rowIndexes,
|
|
695
812
|
this.key,
|
|
696
813
|
this.state.primaryKeys,
|
|
697
814
|
this.nestedInsert
|
|
@@ -706,7 +823,21 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
706
823
|
}
|
|
707
824
|
const { primaryKeys, foreignKeys, query: relQuery } = this.state;
|
|
708
825
|
if (params.create || params.update || params.upsert || params.disconnect || params.set || params.delete) {
|
|
709
|
-
|
|
826
|
+
let appendedAs;
|
|
827
|
+
pqb._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
828
|
+
selectIdsSql._sql = selectCteColumnsSql(
|
|
829
|
+
appendedAs,
|
|
830
|
+
aliasedPrimaryKeys
|
|
831
|
+
);
|
|
832
|
+
if (params.create || params.set || params.upsert) {
|
|
833
|
+
foreignKeys.forEach((foreignKey, i) => {
|
|
834
|
+
setIds[foreignKey]._sql = selectCteColumnSql(
|
|
835
|
+
appendedAs,
|
|
836
|
+
aliasedPrimaryKeys[i]
|
|
837
|
+
);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
});
|
|
710
841
|
const selectIdsSql = new pqb.RawSql("");
|
|
711
842
|
const existingRelQuery = pqb._queryWhereIn(
|
|
712
843
|
pqb._clone(relQuery),
|
|
@@ -729,17 +860,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
729
860
|
}
|
|
730
861
|
}) : params.delete ? pqb._queryDelete(existingRelQuery) : pqb._queryUpdate(existingRelQuery, this.setNulls);
|
|
731
862
|
nullifyOrDeleteQuery.q.returnType = "void";
|
|
732
|
-
pqb._appendQuery(self, nullifyOrDeleteQuery, (as) =>
|
|
733
|
-
selectIdsSql._sql = selectCteColumnsSql(as, primaryKeys);
|
|
734
|
-
if (params.create || params.set || params.upsert) {
|
|
735
|
-
foreignKeys.forEach((foreignKey, i) => {
|
|
736
|
-
setIds[foreignKey]._sql = selectCteColumnSql(
|
|
737
|
-
as,
|
|
738
|
-
primaryKeys[i]
|
|
739
|
-
);
|
|
740
|
-
});
|
|
741
|
-
}
|
|
742
|
-
});
|
|
863
|
+
pqb._appendQuery(self, nullifyOrDeleteQuery, (as) => appendedAs = as);
|
|
743
864
|
if (params.create) {
|
|
744
865
|
const createQuery = pqb._queryInsert(pqb._clone(relQuery), {
|
|
745
866
|
...params.create,
|
|
@@ -934,16 +1055,133 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
934
1055
|
this.nestedInsert = nestedInsert$1(state);
|
|
935
1056
|
this.nestedUpdate = nestedUpdate$1(state);
|
|
936
1057
|
}
|
|
937
|
-
create(
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
item,
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1058
|
+
create(self, ctx, items, rowIndexes, count) {
|
|
1059
|
+
if (count <= self.qb.internal.nestedCreateBatchMax) {
|
|
1060
|
+
const { query: rel, primaryKeys, foreignKeys } = this.state;
|
|
1061
|
+
let nestedCreateItems;
|
|
1062
|
+
items.forEach((item, i) => {
|
|
1063
|
+
var _a, _b;
|
|
1064
|
+
const value = item[this.key];
|
|
1065
|
+
if (value.create?.length) {
|
|
1066
|
+
const nestedCreateItem = (_a = nestedCreateItems ?? (nestedCreateItems = {})).create ?? (_a.create = {
|
|
1067
|
+
indexes: [],
|
|
1068
|
+
items: []
|
|
1069
|
+
});
|
|
1070
|
+
nestedCreateItem.indexes.push(rowIndexes[i]);
|
|
1071
|
+
const data = value.create.map((obj) => {
|
|
1072
|
+
const data2 = { ...obj };
|
|
1073
|
+
for (const key of foreignKeys) {
|
|
1074
|
+
data2[key] = new pqb.RawSql("");
|
|
1075
|
+
}
|
|
1076
|
+
return data2;
|
|
1077
|
+
});
|
|
1078
|
+
nestedCreateItem.items.push(data);
|
|
1079
|
+
} else {
|
|
1080
|
+
const kind = value.connect?.length ? "connect" : value.connectOrCreate?.length ? "connectOrCreate" : void 0;
|
|
1081
|
+
if (kind) {
|
|
1082
|
+
const nestedCreateItem = (_b = nestedCreateItems ?? (nestedCreateItems = {}))[kind] ?? (_b[kind] = {
|
|
1083
|
+
indexes: [],
|
|
1084
|
+
items: [],
|
|
1085
|
+
values: []
|
|
1086
|
+
});
|
|
1087
|
+
nestedCreateItem.indexes.push(rowIndexes[i]);
|
|
1088
|
+
nestedCreateItem.values.push(value[kind]);
|
|
1089
|
+
const data = {};
|
|
1090
|
+
for (const key of foreignKeys) {
|
|
1091
|
+
data[key] = new pqb.RawSql("");
|
|
1092
|
+
}
|
|
1093
|
+
nestedCreateItem.items.push(data);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
if (!nestedCreateItems) {
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
let createAs;
|
|
1101
|
+
let connectAs;
|
|
1102
|
+
let connectOrCreateAs;
|
|
1103
|
+
pqb._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
1104
|
+
foreignKeys.forEach((key, keyI) => {
|
|
1105
|
+
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
1106
|
+
if (create && createAs) {
|
|
1107
|
+
for (let i = 0; i < create.items.length; i++) {
|
|
1108
|
+
const sql = selectCteColumnFromManySql(
|
|
1109
|
+
createAs,
|
|
1110
|
+
primaryKey,
|
|
1111
|
+
create.indexes[i],
|
|
1112
|
+
count
|
|
1113
|
+
);
|
|
1114
|
+
for (const item of create.items[i]) {
|
|
1115
|
+
item[key]._sql = sql;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
if (connect && connectAs) {
|
|
1120
|
+
for (let i = 0; i < connect.items.length; i++) {
|
|
1121
|
+
connect.items[i][key]._sql = selectCteColumnFromManySql(
|
|
1122
|
+
connectAs,
|
|
1123
|
+
primaryKey,
|
|
1124
|
+
connect.indexes[i],
|
|
1125
|
+
count
|
|
1126
|
+
);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
if (connectOrCreate && connectOrCreateAs) {
|
|
1130
|
+
for (let i = 0; i < connectOrCreate.items.length; i++) {
|
|
1131
|
+
connectOrCreate.items[i][key]._sql = selectCteColumnFromManySql(
|
|
1132
|
+
connectOrCreateAs,
|
|
1133
|
+
primaryKey,
|
|
1134
|
+
connectOrCreate.indexes[i],
|
|
1135
|
+
count
|
|
1136
|
+
);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
});
|
|
1140
|
+
});
|
|
1141
|
+
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
1142
|
+
if (create) {
|
|
1143
|
+
const query = pqb._queryInsertMany(
|
|
1144
|
+
pqb._clone(rel),
|
|
1145
|
+
create.items.flat()
|
|
1146
|
+
);
|
|
1147
|
+
pqb._appendQuery(self, query, (as) => createAs = as);
|
|
1148
|
+
}
|
|
1149
|
+
if (connect) {
|
|
1150
|
+
connect.values.forEach((value, i) => {
|
|
1151
|
+
const query = pqb._queryUpdateOrThrow(
|
|
1152
|
+
rel.whereOneOf(...value),
|
|
1153
|
+
connect.items[i]
|
|
1154
|
+
);
|
|
1155
|
+
query.q.ensureCount = value.length;
|
|
1156
|
+
pqb._appendQuery(self, query, (as) => connectAs = as);
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
if (connectOrCreate) {
|
|
1160
|
+
connectOrCreate.values.forEach((array, i) => {
|
|
1161
|
+
const foreignKeyValues = connectOrCreate.items[i];
|
|
1162
|
+
for (const value of array) {
|
|
1163
|
+
const query = pqb._queryUpsert(rel.where(value.where), {
|
|
1164
|
+
update: foreignKeyValues,
|
|
1165
|
+
create: {
|
|
1166
|
+
...value.create,
|
|
1167
|
+
...foreignKeyValues
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
pqb._appendQuery(self, query, (as) => connectOrCreateAs = as);
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
} else {
|
|
1175
|
+
hasRelationHandleCreate(
|
|
1176
|
+
self,
|
|
1177
|
+
ctx,
|
|
1178
|
+
items,
|
|
1179
|
+
rowIndexes,
|
|
1180
|
+
this.key,
|
|
1181
|
+
this.state.primaryKeys,
|
|
1182
|
+
this.nestedInsert
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
947
1185
|
}
|
|
948
1186
|
update(q, set) {
|
|
949
1187
|
const params = set[this.key];
|
|
@@ -1277,12 +1515,12 @@ class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1277
1515
|
this.nestedInsert = nestedInsert(state);
|
|
1278
1516
|
this.nestedUpdate = nestedUpdate(state);
|
|
1279
1517
|
}
|
|
1280
|
-
create(q, ctx,
|
|
1518
|
+
create(q, ctx, items, rowIndexes) {
|
|
1281
1519
|
hasRelationHandleCreate(
|
|
1282
1520
|
q,
|
|
1283
1521
|
ctx,
|
|
1284
|
-
|
|
1285
|
-
|
|
1522
|
+
items,
|
|
1523
|
+
rowIndexes,
|
|
1286
1524
|
this.key,
|
|
1287
1525
|
this.state.primaryKeys,
|
|
1288
1526
|
this.nestedInsert
|
|
@@ -1304,7 +1542,7 @@ const removeColumnName = (column) => {
|
|
|
1304
1542
|
cloned.data = { ...column.data, name: void 0 };
|
|
1305
1543
|
return cloned;
|
|
1306
1544
|
};
|
|
1307
|
-
const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relationName, query) => {
|
|
1545
|
+
const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relationName, query, schema) => {
|
|
1308
1546
|
const { options } = relation;
|
|
1309
1547
|
const { snakeCase } = table.internal;
|
|
1310
1548
|
const primaryKeys = options.columns;
|
|
@@ -1353,6 +1591,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1353
1591
|
baseQuery.shape = shape;
|
|
1354
1592
|
baseQuery.q = {
|
|
1355
1593
|
...baseQuery.q,
|
|
1594
|
+
schema: options.through.schema || schema,
|
|
1356
1595
|
shape: baseQuery.shape
|
|
1357
1596
|
};
|
|
1358
1597
|
const subQuery = Object.create(baseQuery);
|
|
@@ -1833,7 +2072,7 @@ const nestedUpdate = (state) => {
|
|
|
1833
2072
|
};
|
|
1834
2073
|
};
|
|
1835
2074
|
|
|
1836
|
-
const applyRelations = (qb, tables, result) => {
|
|
2075
|
+
const applyRelations = (qb, tables, result, schema) => {
|
|
1837
2076
|
const tableEntries = Object.entries(tables);
|
|
1838
2077
|
const delayedRelations = /* @__PURE__ */ new Map();
|
|
1839
2078
|
for (const name in tables) {
|
|
@@ -1883,7 +2122,7 @@ const applyRelations = (qb, tables, result) => {
|
|
|
1883
2122
|
continue;
|
|
1884
2123
|
}
|
|
1885
2124
|
}
|
|
1886
|
-
applyRelation(table, qb, data, delayedRelations);
|
|
2125
|
+
applyRelation(table, qb, data, delayedRelations, schema);
|
|
1887
2126
|
}
|
|
1888
2127
|
}
|
|
1889
2128
|
if (delayedRelations.size) {
|
|
@@ -1919,7 +2158,7 @@ const delayRelation = (delayedRelations, table, relationName, data) => {
|
|
|
1919
2158
|
tableRelations[relationName] = [data];
|
|
1920
2159
|
}
|
|
1921
2160
|
};
|
|
1922
|
-
const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations) => {
|
|
2161
|
+
const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations, schema) => {
|
|
1923
2162
|
const baseQuery = Object.create(otherDbTable);
|
|
1924
2163
|
baseQuery.baseQuery = baseQuery;
|
|
1925
2164
|
const query = baseQuery.as(relationName);
|
|
@@ -1944,7 +2183,8 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
|
|
|
1944
2183
|
qb,
|
|
1945
2184
|
relation,
|
|
1946
2185
|
relationName,
|
|
1947
|
-
query
|
|
2186
|
+
query,
|
|
2187
|
+
schema
|
|
1948
2188
|
);
|
|
1949
2189
|
} else {
|
|
1950
2190
|
throw new Error(`Unknown relation type ${type}`);
|
|
@@ -1983,7 +2223,7 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
|
|
|
1983
2223
|
const tableRelations = delayedRelations.get(dbTable);
|
|
1984
2224
|
if (!tableRelations) return;
|
|
1985
2225
|
tableRelations[relationName]?.forEach((data2) => {
|
|
1986
|
-
applyRelation(table, qb, data2, delayedRelations);
|
|
2226
|
+
applyRelation(table, qb, data2, delayedRelations, schema);
|
|
1987
2227
|
});
|
|
1988
2228
|
};
|
|
1989
2229
|
|
|
@@ -2008,6 +2248,7 @@ const orchidORMWithAdapter = ({
|
|
|
2008
2248
|
logger,
|
|
2009
2249
|
autoPreparedStatements,
|
|
2010
2250
|
noPrimaryKey = "error",
|
|
2251
|
+
schema,
|
|
2011
2252
|
...options
|
|
2012
2253
|
}, tables) => {
|
|
2013
2254
|
const commonOptions = {
|
|
@@ -2061,7 +2302,7 @@ const orchidORMWithAdapter = ({
|
|
|
2061
2302
|
tableInstances[key] = table;
|
|
2062
2303
|
const options2 = {
|
|
2063
2304
|
...commonOptions,
|
|
2064
|
-
schema: table.schema,
|
|
2305
|
+
schema: table.schema || schema,
|
|
2065
2306
|
language: table.language,
|
|
2066
2307
|
scopes: table.scopes,
|
|
2067
2308
|
softDelete: table.softDelete,
|
|
@@ -2088,7 +2329,7 @@ const orchidORMWithAdapter = ({
|
|
|
2088
2329
|
dbTable.name = table.constructor.name;
|
|
2089
2330
|
result[key] = dbTable;
|
|
2090
2331
|
}
|
|
2091
|
-
applyRelations(qb, tableInstances, result);
|
|
2332
|
+
applyRelations(qb, tableInstances, result, schema);
|
|
2092
2333
|
for (const key in tables) {
|
|
2093
2334
|
const table = tableInstances[key];
|
|
2094
2335
|
if (table.init) {
|