pqb 0.4.1 → 0.4.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 +9 -5
- package/dist/index.esm.js +26 -31
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +26 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/queryMethods/create.test.ts +3 -1
- package/src/queryMethods/create.ts +41 -36
- package/src/relations.ts +20 -8
package/package.json
CHANGED
|
@@ -308,7 +308,6 @@ const insert = (
|
|
|
308
308
|
},
|
|
309
309
|
returnType: QueryReturnType,
|
|
310
310
|
ctx?: CreateCtx,
|
|
311
|
-
fromQuery?: Query,
|
|
312
311
|
) => {
|
|
313
312
|
const q = self as Query & { query: InsertQueryData };
|
|
314
313
|
const returning = q.query.select;
|
|
@@ -319,7 +318,6 @@ const insert = (
|
|
|
319
318
|
q.query.type = 'insert';
|
|
320
319
|
q.query.columns = columns;
|
|
321
320
|
q.query.values = values;
|
|
322
|
-
q.query.fromQuery = fromQuery;
|
|
323
321
|
|
|
324
322
|
if (!ctx) {
|
|
325
323
|
q.query.returnType = returnType;
|
|
@@ -417,16 +415,53 @@ const insert = (
|
|
|
417
415
|
return q;
|
|
418
416
|
};
|
|
419
417
|
|
|
418
|
+
export type CreateMethodsNames =
|
|
419
|
+
| 'create'
|
|
420
|
+
| '_create'
|
|
421
|
+
| 'createMany'
|
|
422
|
+
| '_createMany'
|
|
423
|
+
| 'createRaw'
|
|
424
|
+
| '_createRaw'
|
|
425
|
+
| 'createFrom'
|
|
426
|
+
| '_createFrom';
|
|
427
|
+
|
|
420
428
|
export class Create {
|
|
421
429
|
create<T extends Query>(this: T, data: CreateData<T>): CreateResult<T> {
|
|
422
430
|
return this.clone()._create(data);
|
|
423
431
|
}
|
|
424
432
|
_create<T extends Query>(this: T, data: CreateData<T>): CreateResult<T> {
|
|
425
433
|
handleSelect(this);
|
|
434
|
+
|
|
426
435
|
const ctx = createCtx(this);
|
|
436
|
+
|
|
437
|
+
const obj = handleOneData(this, data, ctx);
|
|
438
|
+
let { columns } = obj;
|
|
439
|
+
|
|
440
|
+
const { fromQuery } = this.query as InsertQueryData;
|
|
441
|
+
if (fromQuery) {
|
|
442
|
+
if (!queryTypeWithLimitOne[fromQuery.query.returnType]) {
|
|
443
|
+
throw new Error(
|
|
444
|
+
'Cannot create based on a query which returns multiple records',
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const queryColumns: string[] = [];
|
|
449
|
+
fromQuery.query.select?.forEach((item) => {
|
|
450
|
+
if (typeof item === 'string') {
|
|
451
|
+
const index = item.indexOf('.');
|
|
452
|
+
queryColumns.push(index === -1 ? item : item.slice(index + 1));
|
|
453
|
+
} else if ('selectAs' in item) {
|
|
454
|
+
queryColumns.push(...Object.keys(item.selectAs));
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
queryColumns.push(...columns);
|
|
459
|
+
columns = queryColumns;
|
|
460
|
+
}
|
|
461
|
+
|
|
427
462
|
return insert(
|
|
428
463
|
this,
|
|
429
|
-
|
|
464
|
+
{ columns, values: obj.values },
|
|
430
465
|
getSingleReturnType(this),
|
|
431
466
|
ctx,
|
|
432
467
|
) as CreateResult<T>;
|
|
@@ -483,39 +518,9 @@ export class Create {
|
|
|
483
518
|
this: T,
|
|
484
519
|
query: Q,
|
|
485
520
|
data: Omit<CreateData<T>, keyof Q['result']>,
|
|
486
|
-
):
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
'createFrom accepts only a query which returns one record',
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
if (!this.query.select) {
|
|
494
|
-
this.query.select = ['*'];
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const ctx = createCtx(this);
|
|
498
|
-
|
|
499
|
-
const queryColumns: string[] = [];
|
|
500
|
-
query.query.select?.forEach((item) => {
|
|
501
|
-
if (typeof item === 'string') {
|
|
502
|
-
const index = item.indexOf('.');
|
|
503
|
-
queryColumns.push(index === -1 ? item : item.slice(index + 1));
|
|
504
|
-
} else if ('selectAs' in item) {
|
|
505
|
-
queryColumns.push(...Object.keys(item.selectAs));
|
|
506
|
-
}
|
|
507
|
-
});
|
|
508
|
-
|
|
509
|
-
const { columns, values } = handleOneData(this, data, ctx);
|
|
510
|
-
queryColumns.push(...columns);
|
|
511
|
-
|
|
512
|
-
return insert(
|
|
513
|
-
this,
|
|
514
|
-
{ columns: queryColumns, values },
|
|
515
|
-
'one',
|
|
516
|
-
ctx,
|
|
517
|
-
query,
|
|
518
|
-
) as SetQueryReturnsOne<T>;
|
|
521
|
+
): CreateResult<T> {
|
|
522
|
+
(this.query as InsertQueryData).fromQuery = query;
|
|
523
|
+
return this._create(data as CreateData<T>);
|
|
519
524
|
}
|
|
520
525
|
|
|
521
526
|
defaults<T extends Query, Data extends Partial<CreateData<T>>>(
|
package/src/relations.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultsKey, Query, QueryBase, QueryWithTable } from './query';
|
|
2
|
-
import { WhereArg, UpdateData } from './queryMethods';
|
|
2
|
+
import { WhereArg, UpdateData, CreateMethodsNames } from './queryMethods';
|
|
3
3
|
import { MaybeArray } from './utils';
|
|
4
4
|
|
|
5
5
|
export type NestedInsertOneItem = {
|
|
@@ -184,15 +184,27 @@ export type RelationQueryBase = Query & {
|
|
|
184
184
|
[isRequiredRelationKey]: boolean;
|
|
185
185
|
};
|
|
186
186
|
|
|
187
|
+
type PrepareRelationQuery<
|
|
188
|
+
T extends Query,
|
|
189
|
+
RelationName extends PropertyKey,
|
|
190
|
+
Required extends boolean,
|
|
191
|
+
Populate extends string,
|
|
192
|
+
> = Omit<T, 'tableAlias'> & {
|
|
193
|
+
tableAlias: RelationName extends string ? RelationName : never;
|
|
194
|
+
[isRequiredRelationKey]: Required;
|
|
195
|
+
[relationQueryKey]: string;
|
|
196
|
+
} & { [defaultsKey]: Record<Populate, true> };
|
|
197
|
+
|
|
187
198
|
export type RelationQuery<
|
|
188
|
-
|
|
199
|
+
Name extends PropertyKey = string,
|
|
189
200
|
Params extends Record<string, unknown> = never,
|
|
190
201
|
Populate extends string = never,
|
|
191
202
|
T extends Query = Query,
|
|
192
203
|
Required extends boolean = boolean,
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
204
|
+
ChainedCreate extends boolean = false,
|
|
205
|
+
Q extends RelationQueryBase = ChainedCreate extends true
|
|
206
|
+
? PrepareRelationQuery<T, Name, Required, Populate>
|
|
207
|
+
: PrepareRelationQuery<T, Name, Required, Populate> & {
|
|
208
|
+
[K in CreateMethodsNames]: never;
|
|
209
|
+
},
|
|
210
|
+
> = ((params: Params) => Q) & Q;
|