pqb 0.2.2 → 0.2.3
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 +58 -47
- package/dist/index.esm.js +197 -150
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +197 -150
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnsSchema.ts +15 -4
- package/src/columnSchema/number.ts +3 -0
- package/src/queryMethods/aggregate.test.ts +2 -2
- package/src/queryMethods/insert.test.ts +217 -205
- package/src/queryMethods/insert.ts +354 -274
- package/src/queryMethods/update.test.ts +3 -3
- package/src/queryMethods/upsert.test.ts +1 -1
- package/src/queryMethods/window.test.ts +2 -2
- package/src/utils.test.ts +37 -0
- package/src/utils.ts +9 -0
|
@@ -1,98 +1,111 @@
|
|
|
1
1
|
import {
|
|
2
2
|
defaultsKey,
|
|
3
3
|
Query,
|
|
4
|
+
QueryReturnType,
|
|
4
5
|
SetQueryReturnsAll,
|
|
5
6
|
SetQueryReturnsOne,
|
|
6
7
|
SetQueryReturnsRowCount,
|
|
7
8
|
} from '../query';
|
|
8
9
|
import { pushQueryArray } from '../queryDataUtils';
|
|
9
|
-
import {
|
|
10
|
+
import { RawExpression } from '../common';
|
|
10
11
|
import {
|
|
11
12
|
BelongsToNestedInsert,
|
|
12
13
|
BelongsToRelation,
|
|
14
|
+
HasAndBelongsToManyRelation,
|
|
15
|
+
HasManyRelation,
|
|
13
16
|
HasOneNestedInsert,
|
|
14
17
|
HasOneRelation,
|
|
15
18
|
NestedInsertItem,
|
|
16
19
|
NestedInsertOneItem,
|
|
17
20
|
Relation,
|
|
21
|
+
RelationsBase,
|
|
18
22
|
} from '../relations';
|
|
19
23
|
import { SetOptional } from '../utils';
|
|
20
24
|
import { InsertQueryData, OnConflictItem, OnConflictMergeUpdate } from '../sql';
|
|
21
25
|
import { WhereArg } from './where';
|
|
22
26
|
import { parseResult, queryMethodByReturnType } from './then';
|
|
23
27
|
|
|
24
|
-
export type OptionalKeys<T extends Query> = {
|
|
25
|
-
[K in keyof T['shape']]: T['shape'][K]['isPrimaryKey'] extends true
|
|
26
|
-
? K
|
|
27
|
-
: T['shape'][K]['isNullable'] extends true
|
|
28
|
-
? K
|
|
29
|
-
: never;
|
|
30
|
-
}[keyof T['shape']];
|
|
31
|
-
|
|
32
28
|
export type InsertData<
|
|
33
29
|
T extends Query,
|
|
34
30
|
DefaultKeys extends PropertyKey = keyof T[defaultsKey],
|
|
35
|
-
Data = SetOptional<
|
|
31
|
+
Data = SetOptional<T['inputType'], DefaultKeys>,
|
|
36
32
|
> = [keyof T['relations']] extends [never]
|
|
37
33
|
? Data
|
|
38
|
-
:
|
|
39
|
-
|
|
34
|
+
: OmitBelongsToForeignKeys<T['relations'], Data> & InsertRelationData<T>;
|
|
35
|
+
|
|
36
|
+
type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<
|
|
37
|
+
Data,
|
|
38
|
+
{
|
|
39
|
+
[K in keyof R]: R[K] extends BelongsToRelation
|
|
40
|
+
? R[K]['options']['foreignKey']
|
|
41
|
+
: never;
|
|
42
|
+
}[keyof R]
|
|
43
|
+
>;
|
|
44
|
+
|
|
45
|
+
type InsertRelationData<T extends Query> = {
|
|
46
|
+
[Key in keyof T['relations']]: T['relations'][Key] extends BelongsToRelation
|
|
47
|
+
? InsertBelongsToData<T, Key, T['relations'][Key]>
|
|
48
|
+
: T['relations'][Key] extends HasOneRelation
|
|
49
|
+
? InsertHasOneData<T, Key, T['relations'][Key]>
|
|
50
|
+
: T['relations'][Key] extends HasManyRelation | HasAndBelongsToManyRelation
|
|
51
|
+
? InsertHasManyData<T, Key, T['relations'][Key]>
|
|
52
|
+
: // eslint-disable-next-line @typescript-eslint/ban-types
|
|
53
|
+
{};
|
|
54
|
+
}[keyof T['relations']];
|
|
55
|
+
|
|
56
|
+
type InsertBelongsToData<
|
|
57
|
+
T extends Query,
|
|
58
|
+
Key extends keyof T['relations'],
|
|
59
|
+
Rel extends BelongsToRelation,
|
|
60
|
+
> =
|
|
61
|
+
| SetOptional<
|
|
40
62
|
{
|
|
41
|
-
[K in
|
|
42
|
-
? T['
|
|
63
|
+
[K in Rel['options']['foreignKey']]: Rel['options']['foreignKey'] extends keyof T['inputType']
|
|
64
|
+
? T['inputType'][Rel['options']['foreignKey']]
|
|
43
65
|
: never;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
create: InsertData<
|
|
88
|
-
T['relations'][Key]['nestedCreateQuery']
|
|
89
|
-
>;
|
|
90
|
-
}[];
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
: // eslint-disable-next-line @typescript-eslint/ban-types
|
|
94
|
-
{};
|
|
95
|
-
}[keyof T['relations']];
|
|
66
|
+
},
|
|
67
|
+
keyof T[defaultsKey]
|
|
68
|
+
>
|
|
69
|
+
| {
|
|
70
|
+
[K in Key]: {
|
|
71
|
+
create?: InsertData<Rel['nestedCreateQuery']>;
|
|
72
|
+
connect?: WhereArg<Rel['model']>;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
type InsertHasOneData<
|
|
77
|
+
T extends Query,
|
|
78
|
+
Key extends keyof T['relations'],
|
|
79
|
+
Rel extends HasOneRelation,
|
|
80
|
+
> = 'through' extends Rel['options']
|
|
81
|
+
? // eslint-disable-next-line @typescript-eslint/ban-types
|
|
82
|
+
{}
|
|
83
|
+
: {
|
|
84
|
+
[K in Key]?: {
|
|
85
|
+
create?: InsertData<Rel['nestedCreateQuery']>;
|
|
86
|
+
connect?: WhereArg<Rel['model']>;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
type InsertHasManyData<
|
|
91
|
+
T extends Query,
|
|
92
|
+
Key extends keyof T['relations'],
|
|
93
|
+
Rel extends HasManyRelation | HasAndBelongsToManyRelation,
|
|
94
|
+
> = 'through' extends Rel['options']
|
|
95
|
+
? // eslint-disable-next-line @typescript-eslint/ban-types
|
|
96
|
+
{}
|
|
97
|
+
: {
|
|
98
|
+
[K in Key]?: {
|
|
99
|
+
create?: InsertData<Rel['nestedCreateQuery']>[];
|
|
100
|
+
connect?: WhereArg<Rel['model']>[];
|
|
101
|
+
connectOrCreate?: {
|
|
102
|
+
where: WhereArg<Rel['model']>;
|
|
103
|
+
create: InsertData<Rel['nestedCreateQuery']>;
|
|
104
|
+
}[];
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
type InsertRawData = { columns: string[]; values: RawExpression };
|
|
96
109
|
|
|
97
110
|
type InsertOneResult<T extends Query> = T['hasSelect'] extends false
|
|
98
111
|
? SetQueryReturnsRowCount<T>
|
|
@@ -123,20 +136,24 @@ type AppendRelations = Record<
|
|
|
123
136
|
[rowIndex: number, data: NestedInsertItem][]
|
|
124
137
|
>;
|
|
125
138
|
|
|
139
|
+
type InsertCtx = {
|
|
140
|
+
prependRelations: PrependRelations;
|
|
141
|
+
appendRelations: AppendRelations;
|
|
142
|
+
requiredReturning: Record<string, boolean>;
|
|
143
|
+
relations: Record<string, Relation>;
|
|
144
|
+
};
|
|
145
|
+
|
|
126
146
|
const processInsertItem = (
|
|
127
147
|
item: Record<string, unknown>,
|
|
128
148
|
rowIndex: number,
|
|
129
|
-
|
|
130
|
-
prependRelations: PrependRelations,
|
|
131
|
-
appendRelations: AppendRelations,
|
|
132
|
-
requiredReturning: Record<string, boolean>,
|
|
149
|
+
ctx: InsertCtx,
|
|
133
150
|
columns: string[],
|
|
134
151
|
columnsMap: Record<string, number>,
|
|
135
152
|
) => {
|
|
136
153
|
Object.keys(item).forEach((key) => {
|
|
137
|
-
if (relations[key]) {
|
|
138
|
-
if (relations[key].type === 'belongsTo') {
|
|
139
|
-
const foreignKey = (relations[key] as BelongsToRelation).options
|
|
154
|
+
if (ctx.relations[key]) {
|
|
155
|
+
if (ctx.relations[key].type === 'belongsTo') {
|
|
156
|
+
const foreignKey = (ctx.relations[key] as BelongsToRelation).options
|
|
140
157
|
.foreignKey;
|
|
141
158
|
|
|
142
159
|
let columnIndex = columnsMap[foreignKey];
|
|
@@ -145,19 +162,22 @@ const processInsertItem = (
|
|
|
145
162
|
columns.push(foreignKey);
|
|
146
163
|
}
|
|
147
164
|
|
|
148
|
-
if (!prependRelations[key]) prependRelations[key] = [];
|
|
165
|
+
if (!ctx.prependRelations[key]) ctx.prependRelations[key] = [];
|
|
149
166
|
|
|
150
|
-
prependRelations[key].push([
|
|
167
|
+
ctx.prependRelations[key].push([
|
|
151
168
|
rowIndex,
|
|
152
169
|
columnIndex,
|
|
153
170
|
item[key] as Record<string, unknown>,
|
|
154
171
|
]);
|
|
155
172
|
} else {
|
|
156
|
-
requiredReturning[relations[key].primaryKey] = true;
|
|
173
|
+
ctx.requiredReturning[ctx.relations[key].primaryKey] = true;
|
|
157
174
|
|
|
158
|
-
if (!appendRelations[key]) appendRelations[key] = [];
|
|
175
|
+
if (!ctx.appendRelations[key]) ctx.appendRelations[key] = [];
|
|
159
176
|
|
|
160
|
-
appendRelations[key].push([
|
|
177
|
+
ctx.appendRelations[key].push([
|
|
178
|
+
rowIndex,
|
|
179
|
+
item[key] as NestedInsertItem,
|
|
180
|
+
]);
|
|
161
181
|
}
|
|
162
182
|
} else if (columnsMap[key] === undefined) {
|
|
163
183
|
columnsMap[key] = columns.length;
|
|
@@ -166,230 +186,290 @@ const processInsertItem = (
|
|
|
166
186
|
});
|
|
167
187
|
};
|
|
168
188
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
189
|
+
const createInsertCtx = (q: Query): InsertCtx => ({
|
|
190
|
+
prependRelations: {},
|
|
191
|
+
appendRelations: {},
|
|
192
|
+
requiredReturning: {},
|
|
193
|
+
relations: (q as unknown as Query).relations,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const getInsertSingleReturnType = (q: Query) => {
|
|
197
|
+
const { select, returnType } = q.query;
|
|
198
|
+
if (select) {
|
|
199
|
+
return returnType === 'all' ? 'one' : returnType;
|
|
200
|
+
} else {
|
|
201
|
+
return 'rowCount';
|
|
178
202
|
}
|
|
203
|
+
};
|
|
179
204
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
) {
|
|
191
|
-
const q = this as unknown as Query & { query: InsertQueryData };
|
|
192
|
-
const returning = q.query.select;
|
|
193
|
-
|
|
194
|
-
delete q.query.and;
|
|
195
|
-
delete q.query.or;
|
|
196
|
-
|
|
197
|
-
let columns: string[];
|
|
198
|
-
const prependRelations: PrependRelations = {};
|
|
199
|
-
const appendRelations: AppendRelations = {};
|
|
200
|
-
const requiredReturning: Record<string, boolean> = {};
|
|
201
|
-
const relations = (this as unknown as Query).relations as unknown as Record<
|
|
202
|
-
string,
|
|
203
|
-
Relation
|
|
204
|
-
>;
|
|
205
|
-
let values: unknown[][] | RawExpression;
|
|
206
|
-
|
|
207
|
-
let returnType = q.query.returnType;
|
|
208
|
-
if (returning) {
|
|
209
|
-
if (Array.isArray(data)) {
|
|
210
|
-
if (returnType === 'one' || returnType === 'oneOrThrow') {
|
|
211
|
-
returnType = 'all';
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
if (returnType === 'all') {
|
|
215
|
-
returnType = 'one';
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} else {
|
|
219
|
-
returnType = 'rowCount';
|
|
220
|
-
}
|
|
205
|
+
const getInsertManyReturnType = (q: Query) => {
|
|
206
|
+
const { select, returnType } = q.query;
|
|
207
|
+
if (select) {
|
|
208
|
+
return returnType === 'one' || returnType === 'oneOrThrow'
|
|
209
|
+
? 'all'
|
|
210
|
+
: returnType;
|
|
211
|
+
} else {
|
|
212
|
+
return 'rowCount';
|
|
213
|
+
}
|
|
214
|
+
};
|
|
221
215
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
216
|
+
const handleInsertOneData = (
|
|
217
|
+
q: Query,
|
|
218
|
+
data: InsertData<Query>,
|
|
219
|
+
ctx: InsertCtx,
|
|
220
|
+
) => {
|
|
221
|
+
const columns: string[] = [];
|
|
222
|
+
const columnsMap: Record<string, number> = {};
|
|
223
|
+
const defaults = q.query.defaults;
|
|
224
|
+
|
|
225
|
+
if (defaults) {
|
|
226
|
+
data = { ...defaults, ...data };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
processInsertItem(data, 0, ctx, columns, columnsMap);
|
|
230
|
+
|
|
231
|
+
const values = [columns.map((key) => (data as Record<string, unknown>)[key])];
|
|
232
|
+
|
|
233
|
+
return { columns, values };
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const handleInsertManyData = (
|
|
237
|
+
q: Query,
|
|
238
|
+
data: InsertData<Query>[],
|
|
239
|
+
ctx: InsertCtx,
|
|
240
|
+
) => {
|
|
241
|
+
const columns: string[] = [];
|
|
242
|
+
const columnsMap: Record<string, number> = {};
|
|
243
|
+
const defaults = q.query.defaults;
|
|
244
|
+
|
|
245
|
+
if (defaults) {
|
|
246
|
+
data = data.map((item) => ({ ...defaults, ...item }));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
data.forEach((item, i) => {
|
|
250
|
+
processInsertItem(item, i, ctx, columns, columnsMap);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const values = Array(data.length);
|
|
254
|
+
|
|
255
|
+
data.forEach((item, i) => {
|
|
256
|
+
(values as unknown[][])[i] = columns.map((key) => item[key]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
return { columns, values };
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const insert = (
|
|
263
|
+
self: Query,
|
|
264
|
+
{
|
|
265
|
+
columns,
|
|
266
|
+
values,
|
|
267
|
+
}: {
|
|
268
|
+
columns: string[];
|
|
269
|
+
values: unknown[][] | RawExpression;
|
|
270
|
+
},
|
|
271
|
+
returnType: QueryReturnType,
|
|
272
|
+
ctx?: InsertCtx,
|
|
273
|
+
) => {
|
|
274
|
+
const q = self as Query & { query: InsertQueryData };
|
|
275
|
+
const returning = q.query.select;
|
|
239
276
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
277
|
+
delete q.query.and;
|
|
278
|
+
delete q.query.or;
|
|
279
|
+
|
|
280
|
+
q.query.type = 'insert';
|
|
281
|
+
q.query.columns = columns;
|
|
282
|
+
q.query.values = values;
|
|
283
|
+
|
|
284
|
+
if (!ctx) {
|
|
285
|
+
q.query.returnType = returnType;
|
|
286
|
+
return q;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const prependRelationsKeys = Object.keys(ctx.prependRelations);
|
|
290
|
+
if (prependRelationsKeys.length) {
|
|
291
|
+
pushQueryArray(
|
|
292
|
+
q,
|
|
293
|
+
'beforeQuery',
|
|
294
|
+
prependRelationsKeys.map((relationName) => {
|
|
295
|
+
return async (q: Query) => {
|
|
296
|
+
const relationData = ctx.prependRelations[relationName];
|
|
297
|
+
const relation = ctx.relations[relationName];
|
|
298
|
+
|
|
299
|
+
const inserted = await (
|
|
300
|
+
relation.nestedInsert as BelongsToNestedInsert
|
|
301
|
+
)(
|
|
302
|
+
q,
|
|
303
|
+
relationData.map(([, , data]) => data as NestedInsertOneItem),
|
|
250
304
|
);
|
|
251
|
-
});
|
|
252
305
|
|
|
253
|
-
|
|
306
|
+
const primaryKey = (relation as BelongsToRelation).options.primaryKey;
|
|
307
|
+
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
308
|
+
(values as unknown[][])[rowIndex][columnIndex] =
|
|
309
|
+
inserted[index][primaryKey];
|
|
310
|
+
});
|
|
311
|
+
};
|
|
312
|
+
}),
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const appendRelationsKeys = Object.keys(ctx.appendRelations);
|
|
317
|
+
if (appendRelationsKeys.length) {
|
|
318
|
+
if (!returning?.includes('*')) {
|
|
319
|
+
const requiredColumns = Object.keys(ctx.requiredReturning);
|
|
254
320
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
});
|
|
321
|
+
if (!returning) {
|
|
322
|
+
q.query.select = requiredColumns;
|
|
258
323
|
} else {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
processInsertItem(
|
|
264
|
-
data,
|
|
265
|
-
0,
|
|
266
|
-
relations,
|
|
267
|
-
prependRelations,
|
|
268
|
-
appendRelations,
|
|
269
|
-
requiredReturning,
|
|
270
|
-
columns,
|
|
271
|
-
columnsMap,
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
values = [columns.map((key) => (data as Record<string, unknown>)[key])];
|
|
324
|
+
q.query.select = [
|
|
325
|
+
...new Set([...(returning as string[]), ...requiredColumns]),
|
|
326
|
+
];
|
|
275
327
|
}
|
|
276
328
|
}
|
|
277
329
|
|
|
278
|
-
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
const primaryKey = (relation as BelongsToRelation).options
|
|
296
|
-
.primaryKey;
|
|
297
|
-
relationData.forEach(([rowIndex, columnIndex], index) => {
|
|
298
|
-
(values as unknown[][])[rowIndex][columnIndex] =
|
|
299
|
-
inserted[index][primaryKey];
|
|
300
|
-
});
|
|
301
|
-
};
|
|
302
|
-
}),
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const appendRelationsKeys = Object.keys(appendRelations);
|
|
307
|
-
if (appendRelationsKeys.length) {
|
|
308
|
-
if (!returning?.includes('*')) {
|
|
309
|
-
const requiredColumns = Object.keys(requiredReturning);
|
|
310
|
-
|
|
311
|
-
if (!returning) {
|
|
312
|
-
q.query.select = requiredColumns;
|
|
313
|
-
} else {
|
|
314
|
-
q.query.select = [
|
|
315
|
-
...new Set([...(returning as string[]), ...requiredColumns]),
|
|
316
|
-
];
|
|
330
|
+
let resultOfTypeAll: Record<string, unknown>[] | undefined;
|
|
331
|
+
if (returnType !== 'all') {
|
|
332
|
+
const { handleResult } = q.query;
|
|
333
|
+
q.query.handleResult = async (q, queryResult) => {
|
|
334
|
+
resultOfTypeAll = (await handleResult(q, queryResult)) as Record<
|
|
335
|
+
string,
|
|
336
|
+
unknown
|
|
337
|
+
>[];
|
|
338
|
+
|
|
339
|
+
if (queryMethodByReturnType[returnType] === 'arrays') {
|
|
340
|
+
queryResult.rows.forEach(
|
|
341
|
+
(row, i) =>
|
|
342
|
+
((queryResult.rows as unknown as unknown[][])[i] =
|
|
343
|
+
Object.values(row)),
|
|
344
|
+
);
|
|
317
345
|
}
|
|
318
|
-
}
|
|
319
346
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
)
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
347
|
+
return parseResult(q, returnType, queryResult);
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
pushQueryArray(
|
|
352
|
+
q,
|
|
353
|
+
'afterQuery',
|
|
354
|
+
appendRelationsKeys.map((relationName) => {
|
|
355
|
+
return (q: Query, result: Record<string, unknown>[]) => {
|
|
356
|
+
const all = resultOfTypeAll || result;
|
|
357
|
+
return (
|
|
358
|
+
ctx.relations[relationName].nestedInsert as HasOneNestedInsert
|
|
359
|
+
)?.(
|
|
360
|
+
q,
|
|
361
|
+
ctx.appendRelations[relationName].map(([rowIndex, data]) => [
|
|
362
|
+
all[rowIndex],
|
|
363
|
+
data as NestedInsertOneItem,
|
|
364
|
+
]),
|
|
365
|
+
);
|
|
338
366
|
};
|
|
339
|
-
}
|
|
367
|
+
}),
|
|
368
|
+
);
|
|
369
|
+
}
|
|
340
370
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
appendRelationsKeys.map((relationName) => {
|
|
345
|
-
return (q: Query, result: Record<string, unknown>[]) => {
|
|
346
|
-
const all = resultOfTypeAll || result;
|
|
347
|
-
return (
|
|
348
|
-
relations[relationName].nestedInsert as HasOneNestedInsert
|
|
349
|
-
)?.(
|
|
350
|
-
q,
|
|
351
|
-
appendRelations[relationName].map(([rowIndex, data]) => [
|
|
352
|
-
all[rowIndex],
|
|
353
|
-
data as NestedInsertOneItem,
|
|
354
|
-
]),
|
|
355
|
-
);
|
|
356
|
-
};
|
|
357
|
-
}),
|
|
358
|
-
);
|
|
359
|
-
}
|
|
371
|
+
if (prependRelationsKeys.length || appendRelationsKeys.length) {
|
|
372
|
+
q.query.wrapInTransaction = true;
|
|
373
|
+
}
|
|
360
374
|
|
|
361
|
-
|
|
362
|
-
q.query.columns = columns;
|
|
363
|
-
q.query.values = values;
|
|
364
|
-
if (prependRelationsKeys.length || appendRelationsKeys.length) {
|
|
365
|
-
q.query.wrapInTransaction = true;
|
|
366
|
-
}
|
|
375
|
+
q.query.returnType = appendRelationsKeys.length ? 'all' : returnType;
|
|
367
376
|
|
|
368
|
-
|
|
377
|
+
return q;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
export class Insert {
|
|
381
|
+
insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T> {
|
|
382
|
+
return this.clone()._insert(data);
|
|
383
|
+
}
|
|
384
|
+
_insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T> {
|
|
385
|
+
const ctx = createInsertCtx(this);
|
|
386
|
+
return insert(
|
|
387
|
+
this,
|
|
388
|
+
handleInsertOneData(this, data, ctx),
|
|
389
|
+
getInsertSingleReturnType(this),
|
|
390
|
+
ctx,
|
|
391
|
+
) as InsertOneResult<T>;
|
|
392
|
+
}
|
|
369
393
|
|
|
370
|
-
|
|
394
|
+
insertMany<T extends Query>(
|
|
395
|
+
this: T,
|
|
396
|
+
data: InsertData<T>[],
|
|
397
|
+
): InsertManyResult<T> {
|
|
398
|
+
return this.clone()._insertMany(data);
|
|
399
|
+
}
|
|
400
|
+
_insertMany<T extends Query>(
|
|
401
|
+
this: T,
|
|
402
|
+
data: InsertData<T>[],
|
|
403
|
+
): InsertManyResult<T> {
|
|
404
|
+
const ctx = createInsertCtx(this);
|
|
405
|
+
return insert(
|
|
406
|
+
this,
|
|
407
|
+
handleInsertManyData(this, data, ctx),
|
|
408
|
+
getInsertManyReturnType(this),
|
|
409
|
+
ctx,
|
|
410
|
+
) as InsertManyResult<T>;
|
|
371
411
|
}
|
|
372
412
|
|
|
373
|
-
|
|
374
|
-
|
|
413
|
+
insertRaw<T extends Query>(
|
|
414
|
+
this: T,
|
|
415
|
+
data: InsertRawData,
|
|
416
|
+
): InsertManyResult<T> {
|
|
417
|
+
return this.clone()._insertRaw(data);
|
|
418
|
+
}
|
|
419
|
+
_insertRaw<T extends Query>(
|
|
375
420
|
this: T,
|
|
376
|
-
data:
|
|
377
|
-
):
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
421
|
+
data: InsertRawData,
|
|
422
|
+
): InsertManyResult<T> {
|
|
423
|
+
return insert(
|
|
424
|
+
this,
|
|
425
|
+
data,
|
|
426
|
+
getInsertManyReturnType(this),
|
|
427
|
+
) as InsertManyResult<T>;
|
|
381
428
|
}
|
|
382
429
|
|
|
383
|
-
|
|
430
|
+
create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T> {
|
|
431
|
+
return this.clone()._create(data);
|
|
432
|
+
}
|
|
384
433
|
_create<T extends Query>(
|
|
385
434
|
this: T,
|
|
386
|
-
data: InsertData<T
|
|
387
|
-
):
|
|
388
|
-
|
|
435
|
+
data: InsertData<T>,
|
|
436
|
+
): SetQueryReturnsOne<T> {
|
|
437
|
+
if (!this.query.select) {
|
|
438
|
+
this.query.select = ['*'];
|
|
439
|
+
}
|
|
440
|
+
return this.clone()._insert(data) as SetQueryReturnsOne<T>;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
createMany<T extends Query>(
|
|
444
|
+
this: T,
|
|
445
|
+
data: InsertData<T>[],
|
|
446
|
+
): SetQueryReturnsAll<T> {
|
|
447
|
+
return this.clone()._createMany(data);
|
|
448
|
+
}
|
|
449
|
+
_createMany<T extends Query>(
|
|
450
|
+
this: T,
|
|
451
|
+
data: InsertData<T>[],
|
|
452
|
+
): SetQueryReturnsAll<T> {
|
|
453
|
+
if (!this.query.select) {
|
|
454
|
+
this.query.select = ['*'];
|
|
455
|
+
}
|
|
456
|
+
return this.clone()._insertMany(data) as SetQueryReturnsAll<T>;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
createRaw<T extends Query>(
|
|
460
|
+
this: T,
|
|
461
|
+
data: InsertRawData,
|
|
462
|
+
): SetQueryReturnsAll<T> {
|
|
463
|
+
return this.clone()._createRaw(data);
|
|
464
|
+
}
|
|
465
|
+
_createRaw<T extends Query>(
|
|
466
|
+
this: T,
|
|
467
|
+
data: InsertRawData,
|
|
468
|
+
): SetQueryReturnsAll<T> {
|
|
389
469
|
if (!this.query.select) {
|
|
390
470
|
this.query.select = ['*'];
|
|
391
471
|
}
|
|
392
|
-
return this.
|
|
472
|
+
return this.clone()._insertRaw(data) as SetQueryReturnsAll<T>;
|
|
393
473
|
}
|
|
394
474
|
|
|
395
475
|
defaults<T extends Query, Data extends Partial<InsertData<T>>>(
|