orchid-orm 1.3.15 → 1.4.16
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/CHANGELOG.md +20 -0
- package/dist/index.d.ts +59 -54
- package/dist/index.esm.js +777 -547
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +776 -546
- package/dist/index.js.map +1 -1
- package/jest-setup.ts +4 -0
- package/package.json +8 -4
- package/src/appCodeUpdater/appCodeUpdater.ts +19 -0
- package/src/appCodeUpdater/fileChanges.ts +41 -0
- package/src/appCodeUpdater/testUtils.ts +31 -0
- package/src/appCodeUpdater/tsUtils.ts +137 -0
- package/src/appCodeUpdater/updateMainFile.test.ts +230 -0
- package/src/appCodeUpdater/updateMainFile.ts +163 -0
- package/src/appCodeUpdater/updateTableFile.ts +19 -0
- package/src/index.ts +5 -1
- package/src/orm.test.ts +13 -13
- package/src/orm.ts +21 -21
- package/src/relations/belongsTo.test.ts +1 -1
- package/src/relations/belongsTo.ts +291 -186
- package/src/relations/hasAndBelongsToMany.test.ts +1 -1
- package/src/relations/hasAndBelongsToMany.ts +292 -218
- package/src/relations/hasMany.test.ts +16 -10
- package/src/relations/hasMany.ts +243 -172
- package/src/relations/hasOne.test.ts +10 -10
- package/src/relations/hasOne.ts +211 -138
- package/src/relations/relations.ts +85 -77
- package/src/relations/utils.ts +154 -4
- package/src/repo.test.ts +29 -29
- package/src/repo.ts +6 -6
- package/src/{model.test.ts → table.test.ts} +15 -15
- package/src/{model.ts → table.ts} +17 -17
- package/src/test-utils/test-db.ts +15 -15
- package/src/test-utils/{test-models.ts → test-tables.ts} +42 -42
- package/src/transaction.test.ts +1 -1
- package/src/transaction.ts +4 -4
- package/src/utils.ts +9 -0
- package/tsconfig.json +1 -0
package/src/orm.ts
CHANGED
|
@@ -8,12 +8,12 @@ import {
|
|
|
8
8
|
anyShape,
|
|
9
9
|
DbTableOptions,
|
|
10
10
|
} from 'pqb';
|
|
11
|
-
import {
|
|
11
|
+
import { DbTable, Table, TableClasses } from './table';
|
|
12
12
|
import { applyRelations } from './relations/relations';
|
|
13
13
|
import { transaction } from './transaction';
|
|
14
14
|
|
|
15
|
-
export type OrchidORM<T extends
|
|
16
|
-
[K in keyof T]:
|
|
15
|
+
export type OrchidORM<T extends TableClasses> = {
|
|
16
|
+
[K in keyof T]: DbTable<T[K]>;
|
|
17
17
|
} & {
|
|
18
18
|
$transaction: typeof transaction;
|
|
19
19
|
$adapter: Adapter;
|
|
@@ -21,7 +21,7 @@ export type OrchidORM<T extends ModelClasses> = {
|
|
|
21
21
|
$close(): Promise<void>;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export const orchidORM = <T extends
|
|
24
|
+
export const orchidORM = <T extends TableClasses>(
|
|
25
25
|
{
|
|
26
26
|
log,
|
|
27
27
|
logger,
|
|
@@ -33,7 +33,7 @@ export const orchidORM = <T extends ModelClasses>(
|
|
|
33
33
|
autoPreparedStatements?: boolean;
|
|
34
34
|
noPrimaryKey?: NoPrimaryKeyOption;
|
|
35
35
|
},
|
|
36
|
-
|
|
36
|
+
tables: T,
|
|
37
37
|
): OrchidORM<T> => {
|
|
38
38
|
const adapter = 'adapter' in options ? options.adapter : new Adapter(options);
|
|
39
39
|
const commonOptions = {
|
|
@@ -57,42 +57,42 @@ export const orchidORM = <T extends ModelClasses>(
|
|
|
57
57
|
$adapter: adapter,
|
|
58
58
|
$queryBuilder: qb,
|
|
59
59
|
$close: () => adapter.close(),
|
|
60
|
-
} as unknown as OrchidORM<
|
|
60
|
+
} as unknown as OrchidORM<TableClasses>;
|
|
61
61
|
|
|
62
|
-
const
|
|
62
|
+
const tableInstances: Record<string, Table> = {};
|
|
63
63
|
|
|
64
|
-
for (const key in
|
|
64
|
+
for (const key in tables) {
|
|
65
65
|
if (key[0] === '$') {
|
|
66
|
-
throw new Error(`
|
|
66
|
+
throw new Error(`Table class name must not start with $`);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
const
|
|
70
|
-
|
|
69
|
+
const table = new tables[key]();
|
|
70
|
+
tableInstances[key] = table;
|
|
71
71
|
|
|
72
72
|
const options: DbTableOptions = {
|
|
73
73
|
...commonOptions,
|
|
74
|
-
schema:
|
|
74
|
+
schema: table.schema,
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
if (
|
|
77
|
+
if (table.noPrimaryKey) options.noPrimaryKey = 'ignore';
|
|
78
78
|
|
|
79
|
-
const
|
|
79
|
+
const dbTable = new Db(
|
|
80
80
|
adapter,
|
|
81
81
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
82
|
qb as any,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
table.table,
|
|
84
|
+
table.columns.shape,
|
|
85
|
+
table.columnTypes,
|
|
86
86
|
options,
|
|
87
87
|
);
|
|
88
88
|
|
|
89
|
-
(
|
|
90
|
-
(
|
|
89
|
+
(dbTable as unknown as { definedAs: string }).definedAs = key;
|
|
90
|
+
(dbTable as unknown as { db: unknown }).db = result;
|
|
91
91
|
|
|
92
|
-
(result as Record<string, unknown>)[key] =
|
|
92
|
+
(result as Record<string, unknown>)[key] = dbTable;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
applyRelations(qb,
|
|
95
|
+
applyRelations(qb, tableInstances, result);
|
|
96
96
|
|
|
97
97
|
return result as unknown as OrchidORM<T>;
|
|
98
98
|
};
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
useTestDatabase,
|
|
11
11
|
} from '../test-utils/test-utils';
|
|
12
12
|
import { RelationQuery } from 'pqb';
|
|
13
|
-
import { User } from '../test-utils/test-
|
|
13
|
+
import { User } from '../test-utils/test-tables';
|
|
14
14
|
|
|
15
15
|
describe('belongsTo', () => {
|
|
16
16
|
useTestDatabase();
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Table } from '../table';
|
|
2
2
|
import {
|
|
3
3
|
addQueryOn,
|
|
4
|
-
BelongsToNestedInsert,
|
|
5
|
-
BelongsToNestedUpdate,
|
|
6
4
|
BelongsToRelation,
|
|
5
|
+
CreateCtx,
|
|
6
|
+
InsertQueryData,
|
|
7
7
|
isQueryReturnsAll,
|
|
8
|
+
pushQueryValue,
|
|
8
9
|
Query,
|
|
9
10
|
QueryBase,
|
|
11
|
+
UpdateCtx,
|
|
12
|
+
VirtualColumn,
|
|
10
13
|
WhereArg,
|
|
11
14
|
WhereResult,
|
|
12
15
|
} from 'pqb';
|
|
13
16
|
import { RelationData, RelationThunkBase } from './relations';
|
|
17
|
+
import { NestedInsertOneItem, NestedUpdateOneItem } from './utils';
|
|
14
18
|
|
|
15
19
|
export interface BelongsTo extends RelationThunkBase {
|
|
16
20
|
type: 'belongsTo';
|
|
@@ -19,7 +23,7 @@ export interface BelongsTo extends RelationThunkBase {
|
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export type BelongsToInfo<
|
|
22
|
-
T extends
|
|
26
|
+
T extends Table,
|
|
23
27
|
Relation extends BelongsTo,
|
|
24
28
|
FK extends string = Relation['options']['foreignKey'],
|
|
25
29
|
> = {
|
|
@@ -29,219 +33,320 @@ export type BelongsToInfo<
|
|
|
29
33
|
chainedDelete: false;
|
|
30
34
|
};
|
|
31
35
|
|
|
36
|
+
type State = {
|
|
37
|
+
query: Query;
|
|
38
|
+
primaryKey: string;
|
|
39
|
+
foreignKey: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type BelongsToNestedInsert = (
|
|
43
|
+
query: Query,
|
|
44
|
+
relationData: NestedInsertOneItem[],
|
|
45
|
+
) => Promise<Record<string, unknown>[]>;
|
|
46
|
+
|
|
47
|
+
type BelongsToNestedUpdate = (
|
|
48
|
+
q: Query,
|
|
49
|
+
update: Record<string, unknown>,
|
|
50
|
+
params: NestedUpdateOneItem,
|
|
51
|
+
state: {
|
|
52
|
+
updateLater?: Record<string, unknown>;
|
|
53
|
+
updateLaterPromises?: Promise<void>[];
|
|
54
|
+
},
|
|
55
|
+
) => boolean;
|
|
56
|
+
|
|
57
|
+
class BelongsToVirtualColumn extends VirtualColumn {
|
|
58
|
+
private readonly nestedInsert: BelongsToNestedInsert;
|
|
59
|
+
private readonly nestedUpdate: BelongsToNestedUpdate;
|
|
60
|
+
|
|
61
|
+
constructor(private key: string, private state: State) {
|
|
62
|
+
super();
|
|
63
|
+
this.nestedInsert = nestedInsert(this.state);
|
|
64
|
+
this.nestedUpdate = nestedUpdate(this.state);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
create(
|
|
68
|
+
q: Query,
|
|
69
|
+
ctx: CreateCtx,
|
|
70
|
+
item: Record<string, unknown>,
|
|
71
|
+
rowIndex: number,
|
|
72
|
+
) {
|
|
73
|
+
const {
|
|
74
|
+
key,
|
|
75
|
+
state: { foreignKey, primaryKey },
|
|
76
|
+
} = this;
|
|
77
|
+
|
|
78
|
+
let columnIndex = ctx.columns.get(foreignKey);
|
|
79
|
+
if (columnIndex === undefined) {
|
|
80
|
+
ctx.columns.set(foreignKey, (columnIndex = ctx.columns.size));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const store = ctx as unknown as {
|
|
84
|
+
belongsTo?: Record<string, [number, number, unknown][]>;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if (!store.belongsTo) store.belongsTo = {};
|
|
88
|
+
|
|
89
|
+
const values = [rowIndex, columnIndex, item[key]] as [
|
|
90
|
+
number,
|
|
91
|
+
number,
|
|
92
|
+
unknown,
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
if (store.belongsTo[key]) {
|
|
96
|
+
store.belongsTo[key].push(values);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const relationData = [values];
|
|
101
|
+
store.belongsTo[key] = relationData;
|
|
102
|
+
q.query.wrapInTransaction = true;
|
|
103
|
+
|
|
104
|
+
pushQueryValue(q, 'beforeCreate', async (q: Query) => {
|
|
105
|
+
const inserted = await this.nestedInsert(
|
|
106
|
+
q,
|
|
107
|
+
relationData.map(([, , data]) => data as NestedInsertOneItem),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const { values } = q.query as InsertQueryData;
|
|
111
|
+
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
112
|
+
(values as unknown[][])[rowIndex][columnIndex] =
|
|
113
|
+
inserted[index][primaryKey];
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
update(q: Query, ctx: UpdateCtx, set: Record<string, unknown>) {
|
|
119
|
+
q.query.wrapInTransaction = true;
|
|
120
|
+
|
|
121
|
+
const data = set[this.key] as NestedUpdateOneItem;
|
|
122
|
+
if (this.nestedUpdate(q, set, data, ctx)) {
|
|
123
|
+
ctx.willSetKeys = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
32
128
|
export const makeBelongsToMethod = (
|
|
33
129
|
relation: BelongsTo,
|
|
130
|
+
relationName: string,
|
|
34
131
|
query: Query,
|
|
35
132
|
): RelationData => {
|
|
36
133
|
const { primaryKey, foreignKey } = relation.options;
|
|
134
|
+
const state: State = { query, primaryKey, foreignKey };
|
|
37
135
|
|
|
38
136
|
return {
|
|
39
137
|
returns: 'one',
|
|
40
138
|
method: (params: Record<string, unknown>) => {
|
|
41
139
|
return query.findBy({ [primaryKey]: params[foreignKey] });
|
|
42
140
|
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
);
|
|
141
|
+
virtualColumn: new BelongsToVirtualColumn(relationName, state),
|
|
142
|
+
joinQuery(fromQuery, toQuery) {
|
|
143
|
+
return addQueryOn(toQuery, fromQuery, toQuery, primaryKey, foreignKey);
|
|
144
|
+
},
|
|
145
|
+
reverseJoin(fromQuery, toQuery) {
|
|
146
|
+
return addQueryOn(fromQuery, toQuery, fromQuery, foreignKey, primaryKey);
|
|
147
|
+
},
|
|
148
|
+
primaryKey,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
54
151
|
|
|
55
|
-
|
|
152
|
+
const nestedInsert = ({ query, primaryKey }: State) => {
|
|
153
|
+
return (async (q, data) => {
|
|
154
|
+
const connectOrCreate = data.filter(
|
|
155
|
+
(
|
|
156
|
+
item,
|
|
157
|
+
): item is {
|
|
158
|
+
connectOrCreate: {
|
|
159
|
+
where: WhereArg<QueryBase>;
|
|
160
|
+
create: Record<string, unknown>;
|
|
161
|
+
};
|
|
162
|
+
} => Boolean(item.connectOrCreate),
|
|
163
|
+
);
|
|
56
164
|
|
|
57
|
-
|
|
58
|
-
if (connectOrCreate.length) {
|
|
59
|
-
connectOrCreated = await Promise.all(
|
|
60
|
-
connectOrCreate.map((item) =>
|
|
61
|
-
t.findBy(item.connectOrCreate.where)._takeOptional(),
|
|
62
|
-
),
|
|
63
|
-
);
|
|
64
|
-
} else {
|
|
65
|
-
connectOrCreated = [];
|
|
66
|
-
}
|
|
165
|
+
const t = query.transacting(q);
|
|
67
166
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
create: Record<string, unknown>;
|
|
75
|
-
}
|
|
76
|
-
| {
|
|
77
|
-
connectOrCreate: {
|
|
78
|
-
where: WhereArg<QueryBase>;
|
|
79
|
-
create: Record<string, unknown>;
|
|
80
|
-
};
|
|
81
|
-
} => {
|
|
82
|
-
if (item.connectOrCreate) {
|
|
83
|
-
return !connectOrCreated[connectOrCreatedI++];
|
|
84
|
-
} else {
|
|
85
|
-
return Boolean(item.create);
|
|
86
|
-
}
|
|
87
|
-
},
|
|
167
|
+
let connectOrCreated: unknown[];
|
|
168
|
+
if (connectOrCreate.length) {
|
|
169
|
+
connectOrCreated = await Promise.all(
|
|
170
|
+
connectOrCreate.map((item) =>
|
|
171
|
+
t.findBy(item.connectOrCreate.where)._takeOptional(),
|
|
172
|
+
),
|
|
88
173
|
);
|
|
174
|
+
} else {
|
|
175
|
+
connectOrCreated = [];
|
|
176
|
+
}
|
|
89
177
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
178
|
+
let connectOrCreatedI = 0;
|
|
179
|
+
const create = data.filter(
|
|
180
|
+
(
|
|
181
|
+
item,
|
|
182
|
+
): item is
|
|
183
|
+
| {
|
|
184
|
+
create: Record<string, unknown>;
|
|
185
|
+
}
|
|
186
|
+
| {
|
|
187
|
+
connectOrCreate: {
|
|
188
|
+
where: WhereArg<QueryBase>;
|
|
189
|
+
create: Record<string, unknown>;
|
|
190
|
+
};
|
|
191
|
+
} => {
|
|
192
|
+
if (item.connectOrCreate) {
|
|
193
|
+
return !connectOrCreated[connectOrCreatedI++];
|
|
194
|
+
} else {
|
|
195
|
+
return Boolean(item.create);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
);
|
|
102
199
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
200
|
+
let created: unknown[];
|
|
201
|
+
if (create.length) {
|
|
202
|
+
created = (await t
|
|
203
|
+
.select(primaryKey)
|
|
204
|
+
._createMany(
|
|
205
|
+
create.map((item) =>
|
|
206
|
+
'create' in item ? item.create : item.connectOrCreate.create,
|
|
207
|
+
),
|
|
208
|
+
)) as unknown[];
|
|
209
|
+
} else {
|
|
210
|
+
created = [];
|
|
211
|
+
}
|
|
110
212
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
}
|
|
213
|
+
const connect = data.filter(
|
|
214
|
+
(
|
|
215
|
+
item,
|
|
216
|
+
): item is {
|
|
217
|
+
connect: WhereArg<QueryBase>;
|
|
218
|
+
} => Boolean(item.connect),
|
|
219
|
+
);
|
|
119
220
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
item.connectOrCreate
|
|
125
|
-
? connectOrCreated[connectOrCreatedI++] || created[createdI++]
|
|
126
|
-
: item.connect
|
|
127
|
-
? connected[connectedI++]
|
|
128
|
-
: created[createdI++],
|
|
221
|
+
let connected: unknown[];
|
|
222
|
+
if (connect.length) {
|
|
223
|
+
connected = await Promise.all(
|
|
224
|
+
connect.map((item) => t.findBy(item.connect)._take()),
|
|
129
225
|
);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
throw new Error('`upsert` option is not allowed in a batch update');
|
|
134
|
-
}
|
|
226
|
+
} else {
|
|
227
|
+
connected = [];
|
|
228
|
+
}
|
|
135
229
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
230
|
+
let createdI = 0;
|
|
231
|
+
let connectedI = 0;
|
|
232
|
+
connectOrCreatedI = 0;
|
|
233
|
+
return data.map((item) =>
|
|
234
|
+
item.connectOrCreate
|
|
235
|
+
? connectOrCreated[connectOrCreatedI++] || created[createdI++]
|
|
236
|
+
: item.connect
|
|
237
|
+
? connected[connectedI++]
|
|
238
|
+
: created[createdI++],
|
|
239
|
+
) as Record<string, unknown>[];
|
|
240
|
+
}) as BelongsToNestedInsert;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const nestedUpdate = ({ query, primaryKey, foreignKey }: State) => {
|
|
244
|
+
return ((q, update, params, state) => {
|
|
245
|
+
if (params.upsert && isQueryReturnsAll(q)) {
|
|
246
|
+
throw new Error('`upsert` option is not allowed in a batch update');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
let idForDelete: unknown;
|
|
250
|
+
|
|
251
|
+
q._beforeUpdate(async (q) => {
|
|
252
|
+
if (params.disconnect) {
|
|
253
|
+
update[foreignKey] = null;
|
|
254
|
+
} else if (params.set) {
|
|
255
|
+
if (primaryKey in params.set) {
|
|
256
|
+
update[foreignKey] =
|
|
257
|
+
params.set[primaryKey as keyof typeof params.set];
|
|
258
|
+
} else {
|
|
152
259
|
update[foreignKey] = await query
|
|
153
260
|
.transacting(q)
|
|
154
|
-
.
|
|
155
|
-
.
|
|
156
|
-
} else if (params.delete) {
|
|
157
|
-
const selectQuery = q.transacting(q);
|
|
158
|
-
selectQuery.query.type = undefined;
|
|
159
|
-
idForDelete = await selectQuery._getOptional(foreignKey);
|
|
160
|
-
update[foreignKey] = null;
|
|
261
|
+
._findBy(params.set)
|
|
262
|
+
._get(primaryKey);
|
|
161
263
|
}
|
|
162
|
-
})
|
|
264
|
+
} else if (params.create) {
|
|
265
|
+
update[foreignKey] = await query
|
|
266
|
+
.transacting(q)
|
|
267
|
+
._get(primaryKey)
|
|
268
|
+
._create(params.create);
|
|
269
|
+
} else if (params.delete) {
|
|
270
|
+
const selectQuery = q.transacting(q);
|
|
271
|
+
selectQuery.query.type = undefined;
|
|
272
|
+
idForDelete = await selectQuery._getOptional(foreignKey);
|
|
273
|
+
update[foreignKey] = null;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
163
276
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
277
|
+
const { upsert } = params;
|
|
278
|
+
if (upsert || params.update || params.delete) {
|
|
279
|
+
if (
|
|
280
|
+
!q.query.select?.includes('*') &&
|
|
281
|
+
!q.query.select?.includes(foreignKey)
|
|
282
|
+
) {
|
|
283
|
+
q._select(foreignKey);
|
|
172
284
|
}
|
|
285
|
+
}
|
|
173
286
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
287
|
+
if (upsert) {
|
|
288
|
+
if (!state.updateLater) {
|
|
289
|
+
state.updateLater = {};
|
|
290
|
+
state.updateLaterPromises = [];
|
|
291
|
+
}
|
|
179
292
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
293
|
+
const { handleResult } = q.query;
|
|
294
|
+
q.query.handleResult = async (q, queryResult) => {
|
|
295
|
+
const data = (await handleResult(q, queryResult)) as Record<
|
|
296
|
+
string,
|
|
297
|
+
unknown
|
|
298
|
+
>[];
|
|
186
299
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
300
|
+
const id = data[0][foreignKey];
|
|
301
|
+
if (id !== null) {
|
|
302
|
+
await query
|
|
303
|
+
.transacting(q)
|
|
304
|
+
._findBy({ [primaryKey]: id })
|
|
305
|
+
._update<WhereResult<Query>>(upsert.update);
|
|
306
|
+
} else {
|
|
307
|
+
(state.updateLaterPromises as Promise<void>[]).push(
|
|
308
|
+
query
|
|
190
309
|
.transacting(q)
|
|
191
|
-
.
|
|
192
|
-
.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
(state.updateLater as Record<string, unknown>)[foreignKey] = (
|
|
201
|
-
result as Record<string, unknown>
|
|
202
|
-
)[primaryKey];
|
|
203
|
-
}) as unknown as Promise<void>,
|
|
204
|
-
);
|
|
205
|
-
}
|
|
310
|
+
._select(primaryKey)
|
|
311
|
+
._create(upsert.create)
|
|
312
|
+
.then((result) => {
|
|
313
|
+
(state.updateLater as Record<string, unknown>)[foreignKey] = (
|
|
314
|
+
result as Record<string, unknown>
|
|
315
|
+
)[primaryKey];
|
|
316
|
+
}) as unknown as Promise<void>,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
206
319
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
320
|
+
return data;
|
|
321
|
+
};
|
|
322
|
+
} else if (params.delete || params.update) {
|
|
323
|
+
q._afterQuery(async (q, data) => {
|
|
324
|
+
const id = params.delete
|
|
325
|
+
? idForDelete
|
|
326
|
+
: Array.isArray(data)
|
|
327
|
+
? data.length === 0
|
|
328
|
+
? null
|
|
329
|
+
: {
|
|
330
|
+
in: data
|
|
331
|
+
.map((item) => item[foreignKey])
|
|
332
|
+
.filter((id) => id !== null),
|
|
333
|
+
}
|
|
334
|
+
: (data as Record<string, unknown>)[foreignKey];
|
|
335
|
+
|
|
336
|
+
if (id !== undefined && id !== null) {
|
|
337
|
+
const t = query.transacting(q)._findBy({
|
|
338
|
+
[primaryKey]: id,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
if (params.delete) {
|
|
342
|
+
await t._delete();
|
|
343
|
+
} else if (params.update) {
|
|
344
|
+
await t._update<WhereResult<Query>>(params.update);
|
|
233
345
|
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
236
349
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
joinQuery(fromQuery, toQuery) {
|
|
240
|
-
return addQueryOn(toQuery, fromQuery, toQuery, primaryKey, foreignKey);
|
|
241
|
-
},
|
|
242
|
-
reverseJoin(fromQuery, toQuery) {
|
|
243
|
-
return addQueryOn(fromQuery, toQuery, fromQuery, foreignKey, primaryKey);
|
|
244
|
-
},
|
|
245
|
-
primaryKey,
|
|
246
|
-
};
|
|
350
|
+
return !params.update && !params.upsert;
|
|
351
|
+
}) as BelongsToNestedUpdate;
|
|
247
352
|
};
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
useRelationCallback,
|
|
10
10
|
} from '../test-utils/test-utils';
|
|
11
11
|
import { RelationQuery, Sql, TransactionAdapter } from 'pqb';
|
|
12
|
-
import { Chat, User } from '../test-utils/test-
|
|
12
|
+
import { Chat, User } from '../test-utils/test-tables';
|
|
13
13
|
|
|
14
14
|
describe('hasAndBelongsToMany', () => {
|
|
15
15
|
useTestDatabase();
|