orchid-orm 1.58.10 → 1.59.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 +30 -30
- package/dist/index.js +39 -108
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -109
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/node-postgres.js.map +1 -1
- package/dist/migrations/node-postgres.mjs.map +1 -1
- package/dist/migrations/postgres-js.js.map +1 -1
- package/dist/migrations/postgres-js.mjs.map +1 -1
- package/dist/node-postgres.d.ts +1 -1
- package/dist/postgres-js.d.ts +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { makeColumnTypes, getStackTrace, applyMixins, QueryHooks, defaultSchemaConfig, raw, getCallerFilePath, snakeCaseKey, getColumnTypes, parseTableData, toSnakeCase, emptyObject, emptyArray, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValueImmutable, pushQueryOnForOuter, isExpression, cloneQueryBaseUnscoped, DynamicRawSQL, RawSQL, getShapeFromSelect, _queryWhere, _queryDefaults, getPrimaryKeys, VirtualColumn,
|
|
1
|
+
import { makeColumnTypes, getStackTrace, applyMixins, QueryHooks, defaultSchemaConfig, raw, getCallerFilePath, snakeCaseKey, getColumnTypes, parseTableData, toSnakeCase, emptyObject, emptyArray, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValueImmutable, pushQueryOnForOuter, isExpression, cloneQueryBaseUnscoped, DynamicRawSQL, RawSQL, getShapeFromSelect, _queryWhere, _queryDefaults, getPrimaryKeys, VirtualColumn, _with, _queryCreate, _orCreate, getFreeAlias, isQueryReturnsAll, _queryHookBeforeUpdate, _queryFindBy, _queryRows, _queryUpdate, _queryDelete, prepareSubQueryForSql, _queryUpdateOrThrow, _queryCreateMany, OrchidOrmInternalError, toArray, objectHasValues, _queryJoinOn, _queryCreateManyFrom, NotFoundError, _queryFindByOptional, _querySelect, pick, _queryWhereExists, _queryTake, _queryTakeOptional, _initQueryBuilder, Db, getClonedQueryData } from 'pqb';
|
|
2
2
|
export * from 'pqb';
|
|
3
3
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
4
|
|
|
@@ -401,45 +401,48 @@ class BelongsToVirtualColumn extends VirtualColumn {
|
|
|
401
401
|
super(schema);
|
|
402
402
|
this.key = key;
|
|
403
403
|
this.state = state;
|
|
404
|
-
this.nestedInsert = nestedInsert$3(this.state);
|
|
405
404
|
this.nestedUpdate = nestedUpdate$3(this.state);
|
|
406
405
|
}
|
|
407
|
-
create(q, ctx, item
|
|
406
|
+
create(q, ctx, item) {
|
|
408
407
|
const {
|
|
409
408
|
key,
|
|
410
|
-
state: { primaryKeys, foreignKeys }
|
|
409
|
+
state: { query, primaryKeys, foreignKeys }
|
|
411
410
|
} = this;
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
ctx.columns.set(key2, index = ctx.columns.size);
|
|
411
|
+
for (const key2 of foreignKeys) {
|
|
412
|
+
if (!ctx.columns.has(key2)) {
|
|
413
|
+
ctx.columns.set(key2, ctx.columns.size);
|
|
416
414
|
}
|
|
417
|
-
return index;
|
|
418
|
-
});
|
|
419
|
-
const store = ctx;
|
|
420
|
-
if (!store.belongsTo) store.belongsTo = {};
|
|
421
|
-
const values = [rowIndex, columnIndexes, item[key]];
|
|
422
|
-
if (store.belongsTo[key]) {
|
|
423
|
-
store.belongsTo[key].push(values);
|
|
424
|
-
return;
|
|
425
415
|
}
|
|
426
|
-
const
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
416
|
+
const value = item[key];
|
|
417
|
+
if ("create" in value || "connectOrCreate" in value) {
|
|
418
|
+
foreignKeys.forEach((foreignKey) => item[foreignKey] = new RawSQL(""));
|
|
419
|
+
const selectPKeys = query.select(...primaryKeys);
|
|
420
|
+
_with(
|
|
421
|
+
q,
|
|
422
|
+
(as) => {
|
|
423
|
+
foreignKeys.forEach((foreignKey, i) => {
|
|
424
|
+
item[foreignKey]._sql = `(SELECT "${as}"."${primaryKeys[i]}" FROM "${as}")`;
|
|
425
|
+
});
|
|
426
|
+
},
|
|
427
|
+
"create" in value ? _queryCreate(selectPKeys, value.create) : _orCreate(
|
|
428
|
+
_queryWhere(selectPKeys, [
|
|
429
|
+
value.connectOrCreate.where
|
|
430
|
+
]),
|
|
431
|
+
value.connectOrCreate.create
|
|
432
|
+
)
|
|
433
433
|
);
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
434
|
+
return;
|
|
435
|
+
} else if ("connect" in value) {
|
|
436
|
+
const as = getFreeAlias(q.q.withShapes, "q");
|
|
437
|
+
_with(q, as, query.select(...primaryKeys).findBy(value.connect));
|
|
438
|
+
foreignKeys.map((foreignKey, i) => {
|
|
439
|
+
const selectColumn = `(SELECT "${as}"."${primaryKeys[i]}" FROM "${as}")`;
|
|
440
|
+
item[foreignKey] = new RawSQL(
|
|
441
|
+
i === 0 ? `CASE WHEN (SELECT count(*) FROM "${as}") = 0 AND (SELECT 'not-found')::int = 0 THEN NULL ELSE ${selectColumn} END` : selectColumn
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
443
446
|
}
|
|
444
447
|
update(q, ctx, set) {
|
|
445
448
|
q.q.wrapInTransaction = true;
|
|
@@ -515,80 +518,6 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
515
518
|
reverseJoin
|
|
516
519
|
};
|
|
517
520
|
};
|
|
518
|
-
const nestedInsert$3 = ({ query, primaryKeys, on }) => {
|
|
519
|
-
return async (_, data) => {
|
|
520
|
-
const t = query.clone();
|
|
521
|
-
const items = [];
|
|
522
|
-
for (const item of data) {
|
|
523
|
-
if (item.connectOrCreate) {
|
|
524
|
-
items.push(
|
|
525
|
-
on ? {
|
|
526
|
-
...item,
|
|
527
|
-
connectOrCreate: {
|
|
528
|
-
...item.connectOrCreate,
|
|
529
|
-
where: { ...item.connectOrCreate.where, ...on }
|
|
530
|
-
}
|
|
531
|
-
} : item
|
|
532
|
-
);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
let connectOrCreated;
|
|
536
|
-
if (items.length) {
|
|
537
|
-
for (let i = 0, len = items.length; i < len; i++) {
|
|
538
|
-
items[i] = t.findByOptional(
|
|
539
|
-
items[i].connectOrCreate.where
|
|
540
|
-
);
|
|
541
|
-
}
|
|
542
|
-
connectOrCreated = await Promise.all(items);
|
|
543
|
-
} else {
|
|
544
|
-
connectOrCreated = emptyArray;
|
|
545
|
-
}
|
|
546
|
-
let connectOrCreatedI = 0;
|
|
547
|
-
items.length = 0;
|
|
548
|
-
for (const item of data) {
|
|
549
|
-
if (item.connectOrCreate) {
|
|
550
|
-
if (!connectOrCreated[connectOrCreatedI++]) items.push(item);
|
|
551
|
-
} else if (item.create) {
|
|
552
|
-
items.push(item);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
let created;
|
|
556
|
-
if (items.length) {
|
|
557
|
-
for (let i = 0, len = items.length; i < len; i++) {
|
|
558
|
-
items[i] = "create" in items[i] ? items[i].create : items[i].connectOrCreate.create;
|
|
559
|
-
}
|
|
560
|
-
created = await _queryCreateMany(
|
|
561
|
-
t.select(...primaryKeys),
|
|
562
|
-
items
|
|
563
|
-
);
|
|
564
|
-
} else {
|
|
565
|
-
created = emptyArray;
|
|
566
|
-
}
|
|
567
|
-
items.length = 0;
|
|
568
|
-
for (const item of data) {
|
|
569
|
-
if (item.connect) {
|
|
570
|
-
items.push(
|
|
571
|
-
on ? { ...item, connect: { ...item.connect, ...on } } : item
|
|
572
|
-
);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
let connected;
|
|
576
|
-
if (items.length) {
|
|
577
|
-
for (let i = 0, len = items.length; i < len; i++) {
|
|
578
|
-
items[i] = t.findBy(items[i].connect);
|
|
579
|
-
}
|
|
580
|
-
connected = await Promise.all(items);
|
|
581
|
-
} else {
|
|
582
|
-
connected = emptyArray;
|
|
583
|
-
}
|
|
584
|
-
let createdI = 0;
|
|
585
|
-
let connectedI = 0;
|
|
586
|
-
connectOrCreatedI = 0;
|
|
587
|
-
return data.map((item) => {
|
|
588
|
-
return item.connectOrCreate ? connectOrCreated[connectOrCreatedI++] || created[createdI++] : item.connect ? connected[connectedI++] : created[createdI++];
|
|
589
|
-
});
|
|
590
|
-
};
|
|
591
|
-
};
|
|
592
521
|
const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
593
522
|
return (q, update, params, state) => {
|
|
594
523
|
if (params.upsert && isQueryReturnsAll(q)) {
|
|
@@ -856,7 +785,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
856
785
|
const baseQuery = query2.clone();
|
|
857
786
|
baseQuery.q.select = fromQuerySelect;
|
|
858
787
|
const q = relationQuery.q;
|
|
859
|
-
q.insertFrom = baseQuery;
|
|
788
|
+
q.insertFrom = prepareSubQueryForSql(q, baseQuery);
|
|
860
789
|
q.values = [];
|
|
861
790
|
};
|
|
862
791
|
}
|
|
@@ -1134,7 +1063,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
1134
1063
|
const baseQuery = query2.clone();
|
|
1135
1064
|
baseQuery.q.select = fromQuerySelect;
|
|
1136
1065
|
const q = relationQuery.q;
|
|
1137
|
-
q.insertFrom = baseQuery;
|
|
1066
|
+
q.insertFrom = prepareSubQueryForSql(q, baseQuery);
|
|
1138
1067
|
q.values = [];
|
|
1139
1068
|
};
|
|
1140
1069
|
}
|
|
@@ -1406,7 +1335,9 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1406
1335
|
const primaryKeysShape = {};
|
|
1407
1336
|
for (let i = 0; i < len; i++) {
|
|
1408
1337
|
const pk = primaryKeys[i];
|
|
1409
|
-
shape[foreignKeys[i]] = removeColumnName(
|
|
1338
|
+
shape[foreignKeys[i]] = removeColumnName(
|
|
1339
|
+
table.shape[pk]
|
|
1340
|
+
);
|
|
1410
1341
|
primaryKeysShape[pk] = table.shape[pk];
|
|
1411
1342
|
}
|
|
1412
1343
|
for (let i = 0; i < throughLen; i++) {
|