orchid-orm 1.14.0 → 1.14.2
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 +193 -49
- package/dist/index.js +15 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +16 -27
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import {
|
|
2
|
+
import { QueryWithTable, SetQueryTableAlias, Query, WhereArg, UpdateData, CreateData, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, RelationConfigBase, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, RelationQueryBase, DefaultColumnTypes, QueryData, QueryBase, ColumnsShape, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
|
|
3
3
|
export { OrchidOrmError, OrchidOrmInternalError, columnTypes, raw, testTransaction } from 'pqb';
|
|
4
4
|
import * as orchid_core from 'orchid-core';
|
|
5
|
-
import {
|
|
5
|
+
import { EmptyObject, MaybeArray, ColumnTypesBase, ColumnsShapeBase, ColumnShapeOutput } from 'orchid-core';
|
|
6
6
|
|
|
7
7
|
interface BelongsTo extends RelationThunkBase {
|
|
8
8
|
type: 'belongsTo';
|
|
9
9
|
returns: 'one';
|
|
10
|
-
options:
|
|
10
|
+
options: RelationCommonOptions & {
|
|
11
|
+
primaryKey: string;
|
|
12
|
+
foreignKey: string;
|
|
13
|
+
};
|
|
11
14
|
}
|
|
12
|
-
type BelongsToInfo<T extends Table, Relation extends BelongsTo, FK extends string = Relation['options']['foreignKey']
|
|
15
|
+
type BelongsToInfo<T extends Table, Relation extends BelongsTo, K extends string, FK extends string = Relation['options']['foreignKey'], Q extends QueryWithTable = SetQueryTableAlias<DbTable<ReturnType<Relation['fn']>>, K>> = {
|
|
16
|
+
table: Q;
|
|
17
|
+
query: Q;
|
|
18
|
+
joinQuery(fromQuery: Query, toQuery: Query): Query;
|
|
19
|
+
one: true;
|
|
20
|
+
required: Relation['options']['required'] extends true ? true : false;
|
|
21
|
+
omitForeignKeyInCreate: FK;
|
|
22
|
+
dataForCreate: RelationToOneDataForCreate<{
|
|
23
|
+
nestedCreateQuery: Q;
|
|
24
|
+
table: Q;
|
|
25
|
+
}>;
|
|
26
|
+
dataForUpdate: {
|
|
27
|
+
disconnect: boolean;
|
|
28
|
+
} | {
|
|
29
|
+
set: WhereArg<Q>;
|
|
30
|
+
} | {
|
|
31
|
+
delete: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
update: UpdateData<Q>;
|
|
34
|
+
} | {
|
|
35
|
+
create: CreateData<Q>;
|
|
36
|
+
};
|
|
37
|
+
dataForUpdateOne: {
|
|
38
|
+
upsert: {
|
|
39
|
+
update: UpdateData<Q>;
|
|
40
|
+
create: CreateData<Q> | (() => CreateData<Q>);
|
|
41
|
+
};
|
|
42
|
+
};
|
|
13
43
|
params: Record<FK, T['columns']['shape'][FK]['type']>;
|
|
14
44
|
populate: never;
|
|
15
45
|
chainedCreate: false;
|
|
@@ -19,17 +49,51 @@ type BelongsToInfo<T extends Table, Relation extends BelongsTo, FK extends strin
|
|
|
19
49
|
interface HasMany extends RelationThunkBase {
|
|
20
50
|
type: 'hasMany';
|
|
21
51
|
returns: 'many';
|
|
22
|
-
options:
|
|
52
|
+
options: RelationCommonOptions & ({
|
|
53
|
+
primaryKey: string;
|
|
54
|
+
foreignKey: string;
|
|
55
|
+
} | {
|
|
56
|
+
through: string;
|
|
57
|
+
source: string;
|
|
58
|
+
});
|
|
23
59
|
}
|
|
24
|
-
type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany
|
|
60
|
+
type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany, K extends string, Populate extends string = Relation['options'] extends {
|
|
61
|
+
foreignKey: string;
|
|
62
|
+
} ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = [Populate] extends [never] ? Q : Q & {
|
|
63
|
+
meta: {
|
|
64
|
+
defaults: Record<Populate, true>;
|
|
65
|
+
};
|
|
66
|
+
}> = {
|
|
67
|
+
table: Q;
|
|
68
|
+
query: Q;
|
|
69
|
+
joinQuery(fromQuery: Query, toQuery: Query): Query;
|
|
70
|
+
one: false;
|
|
71
|
+
required: Relation['options']['required'] extends true ? true : false;
|
|
72
|
+
omitForeignKeyInCreate: never;
|
|
73
|
+
dataForCreate: Relation['options'] extends {
|
|
74
|
+
through: string;
|
|
75
|
+
} ? EmptyObject : RelationToManyDataForCreate<{
|
|
76
|
+
nestedCreateQuery: NestedCreateQuery;
|
|
77
|
+
table: Q;
|
|
78
|
+
}>;
|
|
79
|
+
dataForUpdate: {
|
|
80
|
+
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
81
|
+
delete?: MaybeArray<WhereArg<Q>>;
|
|
82
|
+
update?: {
|
|
83
|
+
where: MaybeArray<WhereArg<Q>>;
|
|
84
|
+
data: UpdateData<Q>;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
dataForUpdateOne: {
|
|
88
|
+
set?: MaybeArray<WhereArg<Q>>;
|
|
89
|
+
create?: CreateData<NestedCreateQuery>[];
|
|
90
|
+
};
|
|
25
91
|
params: Relation['options'] extends {
|
|
26
92
|
primaryKey: string;
|
|
27
93
|
} ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
|
|
28
94
|
through: string;
|
|
29
|
-
} ?
|
|
30
|
-
populate:
|
|
31
|
-
foreignKey: string;
|
|
32
|
-
} ? Relation['options']['foreignKey'] : never;
|
|
95
|
+
} ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
|
|
96
|
+
populate: Populate;
|
|
33
97
|
chainedCreate: Relation['options'] extends {
|
|
34
98
|
primaryKey: string;
|
|
35
99
|
} ? true : false;
|
|
@@ -39,17 +103,56 @@ type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation ext
|
|
|
39
103
|
interface HasOne extends RelationThunkBase {
|
|
40
104
|
type: 'hasOne';
|
|
41
105
|
returns: 'one';
|
|
42
|
-
options:
|
|
106
|
+
options: RelationCommonOptions & ({
|
|
107
|
+
primaryKey: string;
|
|
108
|
+
foreignKey: string;
|
|
109
|
+
} | {
|
|
110
|
+
through: string;
|
|
111
|
+
source: string;
|
|
112
|
+
});
|
|
43
113
|
}
|
|
44
|
-
type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne
|
|
114
|
+
type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne, K extends string, Populate extends string = Relation['options'] extends {
|
|
115
|
+
foreignKey: string;
|
|
116
|
+
} ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = [Populate] extends [never] ? Q : Q & {
|
|
117
|
+
meta: {
|
|
118
|
+
defaults: Record<Populate, true>;
|
|
119
|
+
};
|
|
120
|
+
}> = {
|
|
121
|
+
table: Q;
|
|
122
|
+
query: Q;
|
|
123
|
+
joinQuery(fromQuery: Query, toQuery: Query): Query;
|
|
124
|
+
one: true;
|
|
125
|
+
required: Relation['options']['required'] extends true ? true : false;
|
|
126
|
+
omitForeignKeyInCreate: never;
|
|
127
|
+
dataForCreate: Relation['options'] extends {
|
|
128
|
+
through: string;
|
|
129
|
+
} ? EmptyObject : RelationToOneDataForCreate<{
|
|
130
|
+
nestedCreateQuery: NestedCreateQuery;
|
|
131
|
+
table: Q;
|
|
132
|
+
}>;
|
|
133
|
+
dataForUpdate: {
|
|
134
|
+
disconnect: boolean;
|
|
135
|
+
} | {
|
|
136
|
+
delete: boolean;
|
|
137
|
+
} | {
|
|
138
|
+
update: UpdateData<Q>;
|
|
139
|
+
};
|
|
140
|
+
dataForUpdateOne: {
|
|
141
|
+
set: WhereArg<Q>;
|
|
142
|
+
} | {
|
|
143
|
+
upsert: {
|
|
144
|
+
update: UpdateData<Q>;
|
|
145
|
+
create: CreateData<NestedCreateQuery> | (() => CreateData<NestedCreateQuery>);
|
|
146
|
+
};
|
|
147
|
+
} | {
|
|
148
|
+
create: CreateData<NestedCreateQuery>;
|
|
149
|
+
};
|
|
45
150
|
params: Relation['options'] extends {
|
|
46
151
|
primaryKey: string;
|
|
47
152
|
} ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
|
|
48
153
|
through: string;
|
|
49
|
-
} ?
|
|
50
|
-
populate:
|
|
51
|
-
foreignKey: string;
|
|
52
|
-
} ? Relation['options']['foreignKey'] : never;
|
|
154
|
+
} ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
|
|
155
|
+
populate: Populate;
|
|
53
156
|
chainedCreate: Relation['options'] extends {
|
|
54
157
|
primaryKey: string;
|
|
55
158
|
} ? true : false;
|
|
@@ -141,62 +244,103 @@ declare const orchidORM: <T extends TableClasses>({ log, logger, autoPreparedSta
|
|
|
141
244
|
interface HasAndBelongsToMany extends RelationThunkBase {
|
|
142
245
|
type: 'hasAndBelongsToMany';
|
|
143
246
|
returns: 'many';
|
|
144
|
-
options:
|
|
247
|
+
options: RelationCommonOptions & {
|
|
248
|
+
primaryKey: string;
|
|
249
|
+
foreignKey: string;
|
|
250
|
+
associationPrimaryKey: string;
|
|
251
|
+
associationForeignKey: string;
|
|
252
|
+
joinTable: string;
|
|
253
|
+
};
|
|
145
254
|
}
|
|
146
|
-
type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMany
|
|
255
|
+
type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMany, K extends string, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>> = {
|
|
256
|
+
table: Q;
|
|
257
|
+
query: Q;
|
|
258
|
+
joinQuery(fromQuery: Query, toQuery: Query): Query;
|
|
259
|
+
one: false;
|
|
260
|
+
required: Relation['options']['required'] extends true ? true : false;
|
|
261
|
+
omitForeignKeyInCreate: never;
|
|
262
|
+
dataForCreate: RelationToManyDataForCreate<{
|
|
263
|
+
nestedCreateQuery: Q;
|
|
264
|
+
table: Q;
|
|
265
|
+
}>;
|
|
266
|
+
dataForUpdate: {
|
|
267
|
+
disconnect?: MaybeArray<WhereArg<Q>>;
|
|
268
|
+
set?: MaybeArray<WhereArg<Q>>;
|
|
269
|
+
delete?: MaybeArray<WhereArg<Q>>;
|
|
270
|
+
update?: {
|
|
271
|
+
where: MaybeArray<WhereArg<Q>>;
|
|
272
|
+
data: UpdateData<Q>;
|
|
273
|
+
};
|
|
274
|
+
create?: CreateData<Q>[];
|
|
275
|
+
};
|
|
276
|
+
dataForUpdateOne: EmptyObject;
|
|
147
277
|
params: Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']>;
|
|
148
278
|
populate: never;
|
|
149
279
|
chainedCreate: true;
|
|
150
280
|
chainedDelete: true;
|
|
151
281
|
};
|
|
152
282
|
|
|
283
|
+
type RelationCommonOptions = {
|
|
284
|
+
scope?(q: QueryWithTable): QueryWithTable;
|
|
285
|
+
required?: boolean;
|
|
286
|
+
};
|
|
287
|
+
type RelationToOneDataForCreate<Rel extends {
|
|
288
|
+
nestedCreateQuery: Query;
|
|
289
|
+
table: Query;
|
|
290
|
+
}> = {
|
|
291
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
292
|
+
connect?: never;
|
|
293
|
+
connectOrCreate?: never;
|
|
294
|
+
} | {
|
|
295
|
+
create?: never;
|
|
296
|
+
connect: WhereArg<Rel['table']>;
|
|
297
|
+
connectOrCreate?: never;
|
|
298
|
+
} | {
|
|
299
|
+
create?: never;
|
|
300
|
+
connect?: never;
|
|
301
|
+
connectOrCreate: {
|
|
302
|
+
where: WhereArg<Rel['table']>;
|
|
303
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
type RelationToManyDataForCreate<Rel extends {
|
|
307
|
+
nestedCreateQuery: Query;
|
|
308
|
+
table: Query;
|
|
309
|
+
}> = {
|
|
310
|
+
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
311
|
+
connect?: WhereArg<Rel['table']>[];
|
|
312
|
+
connectOrCreate?: {
|
|
313
|
+
where: WhereArg<Rel['table']>;
|
|
314
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
315
|
+
}[];
|
|
316
|
+
};
|
|
153
317
|
interface RelationThunkBase {
|
|
154
318
|
type: string;
|
|
155
319
|
returns: 'one' | 'many';
|
|
156
320
|
fn(): TableClass;
|
|
157
|
-
options:
|
|
321
|
+
options: RelationCommonOptions;
|
|
158
322
|
}
|
|
159
323
|
type RelationThunk = BelongsTo | HasOne | HasMany | HasAndBelongsToMany;
|
|
160
324
|
type RelationThunks = Record<string, RelationThunk>;
|
|
161
|
-
type Relation
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
defaults: Info['populate'];
|
|
169
|
-
nestedCreateQuery: [Info['populate']] extends [never] ? M : M & {
|
|
170
|
-
meta: {
|
|
171
|
-
defaults: Record<Info['populate'], true>;
|
|
172
|
-
};
|
|
173
|
-
};
|
|
174
|
-
primaryKey: string;
|
|
175
|
-
options: Relations[K]['options'];
|
|
176
|
-
};
|
|
177
|
-
type RelationScopeOrTable<Relation extends RelationThunkBase> = Relation['options']['scope'] extends (q: Query) => Query ? ReturnType<Relation['options']['scope']> : DbTable<ReturnType<Relation['fn']>>;
|
|
178
|
-
type RelationInfo<T extends Table = Table, Relations extends RelationThunks = RelationThunks, Relation extends RelationThunk = RelationThunk> = Relation extends BelongsTo ? BelongsToInfo<T, Relation> : Relation extends HasOne ? HasOneInfo<T, Relations, Relation> : Relation extends HasMany ? HasManyInfo<T, Relations, Relation> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, Relation> : never;
|
|
179
|
-
type MapRelation<T extends Table, Relations extends RelationThunks, RelationName extends keyof Relations, Relation extends RelationThunk = Relations[RelationName], RelatedQuery extends Query = RelationScopeOrTable<Relation>, Info extends {
|
|
180
|
-
params: Record<string, unknown>;
|
|
181
|
-
populate: string;
|
|
182
|
-
chainedCreate: boolean;
|
|
183
|
-
chainedDelete: boolean;
|
|
184
|
-
} = RelationInfo<T, Relations, Relation>> = RelationQuery<RelationName, Info['params'], Info['populate'], Relation['returns'] extends 'one' ? Relation['options']['required'] extends true ? SetQueryReturnsOne<RelatedQuery> : SetQueryReturnsOneOptional<RelatedQuery> : SetQueryReturnsAll<RelatedQuery>, Relation['options']['required'] extends boolean ? Relation['options']['required'] : false, Info['chainedCreate'], Info['chainedDelete']>;
|
|
185
|
-
type MapRelations<T extends Table> = 'relations' extends keyof T ? T['relations'] extends RelationThunks ? {
|
|
325
|
+
type RelationScopeOrTable<Relation extends RelationThunkBase> = Relation['options']['scope'] extends (q: Query) => Query ? ReturnType<Relation['options']['scope']> : RelationQueryFromFn<Relation>;
|
|
326
|
+
type RelationQueryFromFn<Relation extends RelationThunkBase, TC extends TableClass = ReturnType<Relation['fn']>, Q extends Query = DbTable<TC>> = Q;
|
|
327
|
+
type RelationConfig<T extends Table = Table, Relations extends RelationThunks = RelationThunks, Relation extends RelationThunk = RelationThunk, K extends string = string, Result extends RelationConfigBase = Relation extends BelongsTo ? BelongsToInfo<T, Relation, K> : Relation extends HasOne ? HasOneInfo<T, Relations, Relation, K> : Relation extends HasMany ? HasManyInfo<T, Relations, Relation, K> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, Relation, K> : never> = Result;
|
|
328
|
+
type MapRelation<T extends Table, Relations extends RelationThunks, RelationName extends keyof Relations, Relation extends RelationThunk = Relations[RelationName], RelatedQuery extends Query = RelationScopeOrTable<Relation>, Config extends RelationConfigBase = RelationConfig<T, Relations, Relation>> = RelationQuery<RelationName, Config, Config['one'] extends true ? Config['required'] extends true ? SetQueryReturnsOne<RelatedQuery> : SetQueryReturnsOneOptional<RelatedQuery> : SetQueryReturnsAll<RelatedQuery>>;
|
|
329
|
+
type MapRelations<T extends Table> = T extends {
|
|
330
|
+
relations: RelationThunks;
|
|
331
|
+
} ? {
|
|
186
332
|
[K in keyof T['relations']]: MapRelation<T, T['relations'], K>;
|
|
187
|
-
} : EmptyObject
|
|
333
|
+
} : EmptyObject;
|
|
188
334
|
|
|
189
335
|
type TableClass<T extends Table = Table> = new () => T;
|
|
190
336
|
type TableClasses = Record<string, TableClass>;
|
|
191
|
-
type TableToDb<T extends Table
|
|
192
|
-
[K in StringKey<keyof T['relations']>]: Relation<T, T['relations'], K>;
|
|
193
|
-
} : Query['relations'] : Query['relations'], T['columnTypes']> & {
|
|
337
|
+
type TableToDb<T extends Table, RelationQueries extends Record<string, RelationQueryBase>> = Db<T['table'], T['columns']['shape'], RelationQueries, T['columnTypes']> & {
|
|
194
338
|
definedAs: string;
|
|
195
339
|
db: OrchidORM;
|
|
196
340
|
getFilePath(): string;
|
|
197
341
|
name: string;
|
|
198
342
|
};
|
|
199
|
-
type DbTable<
|
|
343
|
+
type DbTable<TC extends TableClass, T extends Table = InstanceType<TC>, RelationQueries extends Record<string, RelationQueryBase> = MapRelations<T>, Q extends QueryWithTable = TableToDb<T, RelationQueries>, Result extends QueryWithTable = Q & RelationQueries> = Result;
|
|
200
344
|
type ColumnsConfig = {
|
|
201
345
|
shape: ColumnsShape;
|
|
202
346
|
type: unknown;
|
package/dist/index.js
CHANGED
|
@@ -178,8 +178,7 @@ const makeBelongsToMethod = (relation, relationName, query) => {
|
|
|
178
178
|
},
|
|
179
179
|
reverseJoin(fromQuery, toQuery) {
|
|
180
180
|
return pqb.addQueryOn(fromQuery, toQuery, fromQuery, foreignKey, primaryKey);
|
|
181
|
-
}
|
|
182
|
-
primaryKey
|
|
181
|
+
}
|
|
183
182
|
};
|
|
184
183
|
};
|
|
185
184
|
const nestedInsert$3 = ({ query, primaryKey }) => {
|
|
@@ -302,10 +301,12 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
|
|
|
302
301
|
};
|
|
303
302
|
|
|
304
303
|
const getThroughRelation = (table, through) => {
|
|
305
|
-
|
|
304
|
+
var _a;
|
|
305
|
+
return (_a = table.relations[through]) == null ? void 0 : _a.relationConfig;
|
|
306
306
|
};
|
|
307
307
|
const getSourceRelation = (throughRelation, source) => {
|
|
308
|
-
|
|
308
|
+
var _a;
|
|
309
|
+
return (_a = throughRelation.table.relations[source]) == null ? void 0 : _a.relationConfig;
|
|
309
310
|
};
|
|
310
311
|
const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nestedInsert) => {
|
|
311
312
|
const value = item[key];
|
|
@@ -443,8 +444,7 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
443
444
|
);
|
|
444
445
|
}
|
|
445
446
|
);
|
|
446
|
-
}
|
|
447
|
-
primaryKey: sourceRelation.primaryKey
|
|
447
|
+
}
|
|
448
448
|
};
|
|
449
449
|
}
|
|
450
450
|
const { primaryKey, foreignKey } = relation.options;
|
|
@@ -463,7 +463,6 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
463
463
|
reverseJoin(fromQuery, toQuery) {
|
|
464
464
|
return pqb.addQueryOn(fromQuery, toQuery, fromQuery, primaryKey, foreignKey);
|
|
465
465
|
},
|
|
466
|
-
primaryKey,
|
|
467
466
|
modifyRelatedQuery(relationQuery) {
|
|
468
467
|
return (query2) => {
|
|
469
468
|
const fromQuery = query2.clone();
|
|
@@ -642,8 +641,7 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
642
641
|
);
|
|
643
642
|
}
|
|
644
643
|
);
|
|
645
|
-
}
|
|
646
|
-
primaryKey: sourceRelation.primaryKey
|
|
644
|
+
}
|
|
647
645
|
};
|
|
648
646
|
}
|
|
649
647
|
const { primaryKey, foreignKey } = relation.options;
|
|
@@ -662,7 +660,6 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
662
660
|
reverseJoin(fromQuery, toQuery) {
|
|
663
661
|
return pqb.addQueryOn(fromQuery, toQuery, fromQuery, primaryKey, foreignKey);
|
|
664
662
|
},
|
|
665
|
-
primaryKey,
|
|
666
663
|
modifyRelatedQuery(relationQuery) {
|
|
667
664
|
return (query2) => {
|
|
668
665
|
const fromQuery = query2.clone();
|
|
@@ -696,7 +693,9 @@ const nestedInsert$1 = ({ query, primaryKey, foreignKey }) => {
|
|
|
696
693
|
if (connect.length) {
|
|
697
694
|
await Promise.all(
|
|
698
695
|
connect.flatMap(
|
|
699
|
-
([selfData, { connect: connect2 }]) => t.or(...connect2)._updateOrThrow({
|
|
696
|
+
([selfData, { connect: connect2 }]) => t.or(...connect2)._updateOrThrow({
|
|
697
|
+
[foreignKey]: selfData[primaryKey]
|
|
698
|
+
})
|
|
700
699
|
)
|
|
701
700
|
);
|
|
702
701
|
}
|
|
@@ -914,7 +913,6 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
914
913
|
(q) => q._on(associationForeignKeyFull, `${pqb.getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${pqb.getQueryAs(fromQuery)}.${pk}`)
|
|
915
914
|
);
|
|
916
915
|
},
|
|
917
|
-
primaryKey: pk,
|
|
918
916
|
modifyRelatedQuery(relationQuery) {
|
|
919
917
|
const ref = {};
|
|
920
918
|
relationQuery._afterCreate([], async (result) => {
|
|
@@ -1152,6 +1150,7 @@ var __spreadValues$2 = (a, b) => {
|
|
|
1152
1150
|
return a;
|
|
1153
1151
|
};
|
|
1154
1152
|
const applyRelations = (qb, tables, result) => {
|
|
1153
|
+
var _a;
|
|
1155
1154
|
const tableEntries = Object.entries(tables);
|
|
1156
1155
|
const delayedRelations = /* @__PURE__ */ new Map();
|
|
1157
1156
|
for (const name in tables) {
|
|
@@ -1215,7 +1214,7 @@ const applyRelations = (qb, tables, result) => {
|
|
|
1215
1214
|
let message = `Cannot define a \`${item.relationName}\` relation on \`${as}\``;
|
|
1216
1215
|
const table = result[as];
|
|
1217
1216
|
const { through, source } = relation.options;
|
|
1218
|
-
const throughRel = table.relations[through];
|
|
1217
|
+
const throughRel = (_a = table.relations[through]) == null ? void 0 : _a.relationConfig;
|
|
1219
1218
|
if (through && !throughRel) {
|
|
1220
1219
|
message += `: cannot find \`${through}\` relation required by the \`through\` option`;
|
|
1221
1220
|
} else if (source && throughRel && !throughRel.table.relations[source]) {
|
|
@@ -1291,16 +1290,12 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
|
|
|
1291
1290
|
return q;
|
|
1292
1291
|
}
|
|
1293
1292
|
};
|
|
1294
|
-
|
|
1295
|
-
type,
|
|
1296
|
-
key: relationName,
|
|
1293
|
+
baseQuery.relationConfig = {
|
|
1297
1294
|
table: otherDbTable,
|
|
1298
1295
|
query,
|
|
1299
|
-
joinQuery: data.joinQuery
|
|
1300
|
-
primaryKey: data.primaryKey,
|
|
1301
|
-
options: relation.options
|
|
1296
|
+
joinQuery: data.joinQuery
|
|
1302
1297
|
};
|
|
1303
|
-
dbTable.
|
|
1298
|
+
dbTable.relations[relationName] = query;
|
|
1304
1299
|
const tableRelations = delayedRelations.get(dbTable);
|
|
1305
1300
|
if (!tableRelations)
|
|
1306
1301
|
return;
|
|
@@ -1321,12 +1316,6 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1321
1316
|
query.q.joinedShapes = __spreadValues$2({
|
|
1322
1317
|
[pqb.getQueryAs(this)]: this.q.shape
|
|
1323
1318
|
}, this.q.joinedShapes);
|
|
1324
|
-
query.q[pqb.relationQueryKey] = {
|
|
1325
|
-
relationName,
|
|
1326
|
-
sourceQuery: this,
|
|
1327
|
-
relationQuery: toTable,
|
|
1328
|
-
joinQuery: data.joinQuery
|
|
1329
|
-
};
|
|
1330
1319
|
const setQuery = (_a = data.modifyRelatedQuery) == null ? void 0 : _a.call(data, query);
|
|
1331
1320
|
setQuery == null ? void 0 : setQuery(this);
|
|
1332
1321
|
return new Proxy(data.method, {
|