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.
@@ -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 { isRaw, RawExpression } from '../common';
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<SetOptional<T['inputType'], OptionalKeys<T>>, DefaultKeys>,
31
+ Data = SetOptional<T['inputType'], DefaultKeys>,
36
32
  > = [keyof T['relations']] extends [never]
37
33
  ? Data
38
- : Omit<
39
- Data,
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 keyof T['relations']]: T['relations'][K] extends BelongsToRelation
42
- ? T['relations'][K]['options']['foreignKey']
63
+ [K in Rel['options']['foreignKey']]: Rel['options']['foreignKey'] extends keyof T['inputType']
64
+ ? T['inputType'][Rel['options']['foreignKey']]
43
65
  : never;
44
- }[keyof T['relations']]
45
- > &
46
- {
47
- [Key in keyof T['relations']]: T['relations'][Key] extends BelongsToRelation
48
- ?
49
- | SetOptional<
50
- {
51
- [K in T['relations'][Key]['options']['foreignKey']]: T['relations'][Key]['options']['foreignKey'] extends keyof T['inputType']
52
- ? T['inputType'][T['relations'][Key]['options']['foreignKey']]
53
- : never;
54
- },
55
- DefaultKeys
56
- >
57
- | {
58
- [K in Key]: {
59
- create?: InsertData<
60
- T['relations'][Key]['nestedCreateQuery']
61
- >;
62
- connect?: WhereArg<T['relations'][Key]['model']>;
63
- };
64
- }
65
- : T['relations'][Key] extends HasOneRelation
66
- ? 'through' extends T['relations'][Key]['options']
67
- ? // eslint-disable-next-line @typescript-eslint/ban-types
68
- {}
69
- : {
70
- [K in Key]?: {
71
- create?: InsertData<T['relations'][Key]['nestedCreateQuery']>;
72
- connect?: WhereArg<T['relations'][Key]['model']>;
73
- };
74
- }
75
- : T['relations'][Key] extends Relation
76
- ? 'through' extends T['relations'][Key]['options']
77
- ? // eslint-disable-next-line @typescript-eslint/ban-types
78
- {}
79
- : {
80
- [K in Key]?: {
81
- create?: InsertData<
82
- T['relations'][Key]['nestedCreateQuery']
83
- >[];
84
- connect?: WhereArg<T['relations'][Key]['model']>[];
85
- connectOrCreate?: {
86
- where: WhereArg<T['relations'][Key]['model']>;
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
- relations: Record<string, Relation>,
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([rowIndex, item[key] as NestedInsertItem]);
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
- export class Insert {
170
- insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
171
- insert<T extends Query>(
172
- this: T,
173
- data: InsertData<T>[] | { columns: string[]; values: RawExpression },
174
- ): InsertManyResult<T>;
175
- insert(this: Query, data: InsertData<Query> & InsertData<Query>[]) {
176
- return this.clone()._insert(data) as unknown as InsertOneResult<Query> &
177
- InsertManyResult<Query>;
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
- _insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
181
- _insert<T extends Query>(
182
- this: T,
183
- data: InsertData<T>[] | { columns: string[]; values: RawExpression },
184
- ): InsertManyResult<T>;
185
- _insert(
186
- data:
187
- | Record<string, unknown>
188
- | Record<string, unknown>[]
189
- | { columns: string[]; values: RawExpression },
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
- if (
223
- 'values' in data &&
224
- typeof data.values === 'object' &&
225
- data.values &&
226
- isRaw(data.values)
227
- ) {
228
- columns = (data as { columns: string[] }).columns;
229
- values = data.values;
230
- } else {
231
- columns = [];
232
- const columnsMap: Record<string, number> = {};
233
- const defaults = q.query.defaults;
234
-
235
- if (Array.isArray(data)) {
236
- if (defaults) {
237
- data = data.map((item) => ({ ...defaults, ...item }));
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
- data.forEach((item, i) => {
241
- processInsertItem(
242
- item,
243
- i,
244
- relations,
245
- prependRelations,
246
- appendRelations,
247
- requiredReturning,
248
- columns,
249
- columnsMap,
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
- values = Array(data.length);
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
- data.forEach((item, i) => {
256
- (values as unknown[][])[i] = columns.map((key) => item[key]);
257
- });
321
+ if (!returning) {
322
+ q.query.select = requiredColumns;
258
323
  } else {
259
- if (defaults) {
260
- data = { ...defaults, ...data };
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
- const prependRelationsKeys = Object.keys(prependRelations);
279
- if (prependRelationsKeys.length) {
280
- pushQueryArray(
281
- q,
282
- 'beforeQuery',
283
- prependRelationsKeys.map((relationName) => {
284
- return async (q: Query) => {
285
- const relationData = prependRelations[relationName];
286
- const relation = relations[relationName];
287
-
288
- const inserted = await (
289
- relation.nestedInsert as BelongsToNestedInsert
290
- )(
291
- q,
292
- relationData.map(([, , data]) => data as NestedInsertOneItem),
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
- let resultOfTypeAll: Record<string, unknown>[] | undefined;
321
- if (returnType !== 'all') {
322
- const { handleResult } = q.query;
323
- q.query.handleResult = async (q, queryResult) => {
324
- resultOfTypeAll = (await handleResult(q, queryResult)) as Record<
325
- string,
326
- unknown
327
- >[];
328
-
329
- if (queryMethodByReturnType[returnType] === 'arrays') {
330
- queryResult.rows.forEach(
331
- (row, i) =>
332
- ((queryResult.rows as unknown as unknown[][])[i] =
333
- Object.values(row)),
334
- );
335
- }
336
-
337
- return parseResult(q, returnType, queryResult);
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
- pushQueryArray(
342
- q,
343
- 'afterQuery',
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
- q.query.type = 'insert';
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
- q.query.returnType = appendRelationsKeys.length ? 'all' : returnType;
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
- return q as unknown as InsertOneResult<Query> & InsertManyResult<Query>;
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
- create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
374
- create<T extends Query>(
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: InsertData<T>[] | { columns: string[]; values: RawExpression },
377
- ): SetQueryReturnsAll<T>;
378
- create(this: Query, data: InsertData<Query> & InsertData<Query>[]) {
379
- return this.clone()._create(data) as unknown as SetQueryReturnsOne<Query> &
380
- SetQueryReturnsAll<Query>;
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
- _create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
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>[] | { columns: string[]; values: RawExpression },
387
- ): SetQueryReturnsAll<T>;
388
- _create(this: Query, data: InsertData<Query> & InsertData<Query>[]) {
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.insert(data) as unknown as never;
472
+ return this.clone()._insertRaw(data) as SetQueryReturnsAll<T>;
393
473
  }
394
474
 
395
475
  defaults<T extends Query, Data extends Partial<InsertData<T>>>(