orchid-orm 1.54.1 → 1.55.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 +9 -3
- package/dist/index.js +45 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +37 -34
- package/dist/index.mjs.map +1 -1
- package/dist/migrations.js +12 -12
- package/dist/migrations.js.map +1 -1
- package/dist/migrations.mjs +4 -4
- package/dist/migrations.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { TableData, Query, SelectableFromShape, CreateMethodsNames, CreateData, CreateBelongsToData, AddQueryDefaults,
|
|
1
|
+
import { TableData, Query, SelectableFromShape, CreateMethodsNames, CreateData, CreateBelongsToData, AddQueryDefaults, QueryTake, QueryTakeOptional, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, ComputedOptionsConfig, QueryData, TableDataFn, DbTableOptionScopes, RawSQL, DynamicRawSQL, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHookInternal, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
|
|
2
2
|
export * from 'pqb';
|
|
3
|
-
import { ColumnsShapeBase, ColumnShapeInputPartial, EmptyObject, MaybeArray, AfterCommitStandaloneHook, RecordUnknown, ShallowSimplify, ColumnShapeOutput, DefaultSelectColumns, QueryOrExpression, QueryColumn, ColumnShapeInput, IsQuery, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
|
|
3
|
+
import { ColumnsShapeBase, ColumnShapeInputPartial, RelationConfigBase, EmptyObject, MaybeArray, AfterCommitStandaloneHook, RelationsBase, RecordUnknown, ShallowSimplify, ColumnShapeOutput, DefaultSelectColumns, QueryOrExpression, QueryColumn, ColumnShapeInput, IsQuery, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
|
|
4
4
|
export * from 'orchid-core';
|
|
5
5
|
|
|
6
6
|
interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends ColumnsShapeBase = ColumnsShapeBase> {
|
|
@@ -130,7 +130,13 @@ interface BelongsTo extends RelationThunkBase {
|
|
|
130
130
|
type: 'belongsTo';
|
|
131
131
|
options: BelongsToOptions;
|
|
132
132
|
}
|
|
133
|
-
|
|
133
|
+
interface BelongsToOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass> {
|
|
134
|
+
required?: boolean;
|
|
135
|
+
columns: (keyof Columns)[];
|
|
136
|
+
references: (keyof InstanceType<Related>['columns']['shape'])[];
|
|
137
|
+
foreignKey?: boolean | TableData.References.Options;
|
|
138
|
+
on?: ColumnShapeInputPartial<InstanceType<Related>['columns']['shape']>;
|
|
139
|
+
}
|
|
134
140
|
type BelongsToFKey<Relation extends RelationThunkBase> = Relation['options'] extends RelationRefsOptions ? Relation['options']['columns'][number] : never;
|
|
135
141
|
type BelongsToParams<T extends RelationConfigSelf, Relation extends BelongsTo> = {
|
|
136
142
|
[Name in BelongsToFKey<Relation>]: T['columns']['shape'][Name]['type'];
|
package/dist/index.js
CHANGED
|
@@ -183,7 +183,6 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
183
183
|
const value = set[key];
|
|
184
184
|
if (!value.set && !("upsert" in value) && (!value.add || Array.isArray(value.add) && value.add.length === 0) && (!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))
|
|
185
185
|
return;
|
|
186
|
-
selectIfNotSelected(q, primaryKeys);
|
|
187
186
|
q.q.wrapInTransaction = true;
|
|
188
187
|
pqb._queryHookAfterUpdate(q, primaryKeys, (rows, q2) => {
|
|
189
188
|
return nestedUpdate(
|
|
@@ -506,7 +505,7 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
506
505
|
state
|
|
507
506
|
),
|
|
508
507
|
joinQuery: joinQueryChainHOF(
|
|
509
|
-
|
|
508
|
+
orchidCore.getPrimaryKeys(query),
|
|
510
509
|
reverseJoin,
|
|
511
510
|
(joiningQuery, baseQuery) => join(
|
|
512
511
|
baseQuery,
|
|
@@ -637,6 +636,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
637
636
|
const selectQuery = q2.clone();
|
|
638
637
|
selectQuery.q.type = void 0;
|
|
639
638
|
selectQuery.q.distinct = orchidCore.emptyArray;
|
|
639
|
+
selectIfNotSelected(selectQuery, foreignKeys);
|
|
640
640
|
idsForDelete = await pqb._queryRows(selectQuery);
|
|
641
641
|
for (const foreignKey of foreignKeys) {
|
|
642
642
|
update[foreignKey] = null;
|
|
@@ -644,9 +644,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
644
644
|
}
|
|
645
645
|
});
|
|
646
646
|
const { upsert } = params;
|
|
647
|
-
if (upsert || params.update || params.delete)
|
|
648
|
-
selectIfNotSelected(q, foreignKeys);
|
|
649
|
-
}
|
|
647
|
+
if (upsert || params.update || params.delete) ;
|
|
650
648
|
if (upsert) {
|
|
651
649
|
(state.queries ?? (state.queries = [])).push(async (queryResult) => {
|
|
652
650
|
const row = queryResult.rows[0];
|
|
@@ -671,7 +669,6 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
671
669
|
);
|
|
672
670
|
const collectData = {};
|
|
673
671
|
state.collect = {
|
|
674
|
-
keys: primaryKeys,
|
|
675
672
|
data: collectData
|
|
676
673
|
};
|
|
677
674
|
for (let i = 0; i < len; i++) {
|
|
@@ -680,37 +677,41 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
680
677
|
}
|
|
681
678
|
});
|
|
682
679
|
} else if (params.delete || params.update) {
|
|
683
|
-
pqb._queryHookAfterUpdate(
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
680
|
+
pqb._queryHookAfterUpdate(
|
|
681
|
+
q,
|
|
682
|
+
params.update ? foreignKeys : orchidCore.emptyArray,
|
|
683
|
+
async (data) => {
|
|
684
|
+
let ids;
|
|
685
|
+
if (params.delete) {
|
|
686
|
+
ids = idsForDelete;
|
|
687
|
+
} else {
|
|
688
|
+
ids = [];
|
|
689
|
+
for (const item of data) {
|
|
690
|
+
let row;
|
|
691
|
+
for (const foreignKey of foreignKeys) {
|
|
692
|
+
const id = item[foreignKey];
|
|
693
|
+
if (id === null) {
|
|
694
|
+
row = void 0;
|
|
695
|
+
break;
|
|
696
|
+
} else {
|
|
697
|
+
(row ?? (row = [])).push(id);
|
|
698
|
+
}
|
|
698
699
|
}
|
|
700
|
+
if (row) ids.push(row);
|
|
699
701
|
}
|
|
700
|
-
|
|
702
|
+
}
|
|
703
|
+
if (!ids?.length) return;
|
|
704
|
+
const t = query.whereIn(
|
|
705
|
+
primaryKeys,
|
|
706
|
+
ids
|
|
707
|
+
);
|
|
708
|
+
if (params.delete) {
|
|
709
|
+
await pqb._queryDelete(t);
|
|
710
|
+
} else {
|
|
711
|
+
await pqb._queryUpdate(t, params.update);
|
|
701
712
|
}
|
|
702
713
|
}
|
|
703
|
-
|
|
704
|
-
const t = query.whereIn(
|
|
705
|
-
primaryKeys,
|
|
706
|
-
ids
|
|
707
|
-
);
|
|
708
|
-
if (params.delete) {
|
|
709
|
-
await pqb._queryDelete(t);
|
|
710
|
-
} else {
|
|
711
|
-
await pqb._queryUpdate(t, params.update);
|
|
712
|
-
}
|
|
713
|
-
});
|
|
714
|
+
);
|
|
714
715
|
}
|
|
715
716
|
};
|
|
716
717
|
};
|
|
@@ -750,7 +751,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
750
751
|
}
|
|
751
752
|
}
|
|
752
753
|
const makeHasOneMethod = (tableConfig, table, relation, relationName, query) => {
|
|
753
|
-
const relPKeys =
|
|
754
|
+
const relPKeys = orchidCore.getPrimaryKeys(query);
|
|
754
755
|
if ("through" in relation.options) {
|
|
755
756
|
const { through, source } = relation.options;
|
|
756
757
|
const throughRelation = getThroughRelation(table, through);
|
|
@@ -1024,7 +1025,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1024
1025
|
}
|
|
1025
1026
|
}
|
|
1026
1027
|
const makeHasManyMethod = (tableConfig, table, relation, relationName, query) => {
|
|
1027
|
-
const relPKeys =
|
|
1028
|
+
const relPKeys = orchidCore.getPrimaryKeys(query);
|
|
1028
1029
|
if ("through" in relation.options) {
|
|
1029
1030
|
const { through, source } = relation.options;
|
|
1030
1031
|
const throughRelation = getThroughRelation(table, through);
|
|
@@ -1262,7 +1263,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1262
1263
|
}
|
|
1263
1264
|
if (params.add) {
|
|
1264
1265
|
if (data.length > 1) {
|
|
1265
|
-
throw new
|
|
1266
|
+
throw new orchidCore.OrchidOrmInternalError(
|
|
1266
1267
|
query,
|
|
1267
1268
|
"`connect` is not available when updating multiple records, it is only applicable for a single record update"
|
|
1268
1269
|
);
|
|
@@ -1277,7 +1278,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1277
1278
|
obj
|
|
1278
1279
|
);
|
|
1279
1280
|
if (count < relatedWheres.length) {
|
|
1280
|
-
throw new
|
|
1281
|
+
throw new orchidCore.OrchidOrmInternalError(
|
|
1281
1282
|
query,
|
|
1282
1283
|
`Expected to find at least ${relatedWheres.length} record(s) based on \`add\` conditions, but found ${count}`
|
|
1283
1284
|
);
|
|
@@ -1508,7 +1509,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1508
1509
|
state
|
|
1509
1510
|
),
|
|
1510
1511
|
joinQuery: joinQueryChainHOF(
|
|
1511
|
-
|
|
1512
|
+
orchidCore.getPrimaryKeys(query),
|
|
1512
1513
|
reverseJoin,
|
|
1513
1514
|
(joiningQuery, baseQuery2) => joinQuery(
|
|
1514
1515
|
joiningQuery,
|
|
@@ -1528,7 +1529,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1528
1529
|
[],
|
|
1529
1530
|
async (result) => {
|
|
1530
1531
|
if (result.length > 1) {
|
|
1531
|
-
throw new
|
|
1532
|
+
throw new orchidCore.OrchidOrmInternalError(
|
|
1532
1533
|
relationQuery,
|
|
1533
1534
|
"Creating multiple `hasAndBelongsToMany` records is not yet supported"
|
|
1534
1535
|
);
|
|
@@ -1545,7 +1546,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1545
1546
|
data
|
|
1546
1547
|
);
|
|
1547
1548
|
if (createdCount === 0) {
|
|
1548
|
-
throw new
|
|
1549
|
+
throw new orchidCore.NotFoundError(baseQuery2);
|
|
1549
1550
|
}
|
|
1550
1551
|
}
|
|
1551
1552
|
);
|
|
@@ -1840,14 +1841,14 @@ const nestedUpdate = (state) => {
|
|
|
1840
1841
|
)
|
|
1841
1842
|
).onConflict(joinTableColumns).merge([state.foreignKeys[0]]);
|
|
1842
1843
|
if (count < data.length * relatedWheres.length) {
|
|
1843
|
-
throw new
|
|
1844
|
+
throw new orchidCore.OrchidOrmInternalError(
|
|
1844
1845
|
query,
|
|
1845
1846
|
`Expected to find at least ${relatedWheres.length} record(s) based on \`add\` conditions, but found ${count / data.length}`
|
|
1846
1847
|
);
|
|
1847
1848
|
}
|
|
1848
1849
|
} catch (err) {
|
|
1849
1850
|
if (err.code === "42P10") {
|
|
1850
|
-
throw new
|
|
1851
|
+
throw new orchidCore.OrchidOrmInternalError(
|
|
1851
1852
|
query,
|
|
1852
1853
|
`"${state.joinTableQuery.table}" must have a primary key or a unique index on columns (${joinTableColumns.join(
|
|
1853
1854
|
", "
|
|
@@ -2157,6 +2158,8 @@ const orchidORM = ({
|
|
|
2157
2158
|
Object.assign(result[key].baseQuery.q, table.q);
|
|
2158
2159
|
}
|
|
2159
2160
|
}
|
|
2161
|
+
const db = result;
|
|
2162
|
+
db.$adapter;
|
|
2160
2163
|
return result;
|
|
2161
2164
|
};
|
|
2162
2165
|
|