pqb 0.4.0 → 0.4.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 +61 -48
- package/dist/index.esm.js +136 -117
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +136 -117
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.test.ts +1 -1
- package/src/db.test.ts +2 -2
- package/src/errors.test.ts +4 -4
- package/src/queryMethods/aggregate.test.ts +11 -11
- package/src/queryMethods/aggregate.ts +5 -3
- package/src/queryMethods/callbacks.test.ts +6 -6
- package/src/queryMethods/callbacks.ts +8 -8
- package/src/queryMethods/columnInfo.ts +13 -19
- package/src/queryMethods/{insert.test.ts → create.test.ts} +108 -98
- package/src/queryMethods/{insert.ts → create.ts} +139 -118
- package/src/queryMethods/delete.test.ts +2 -2
- package/src/queryMethods/get.test.ts +2 -2
- package/src/queryMethods/index.ts +1 -1
- package/src/queryMethods/json.test.ts +1 -1
- package/src/queryMethods/log.test.ts +29 -8
- package/src/queryMethods/merge.test.ts +3 -3
- package/src/queryMethods/queryMethods.test.ts +4 -4
- package/src/queryMethods/queryMethods.ts +3 -3
- package/src/queryMethods/select.test.ts +7 -7
- package/src/queryMethods/then.ts +2 -2
- package/src/queryMethods/update.test.ts +8 -8
- package/src/queryMethods/update.ts +7 -7
- package/src/queryMethods/upsert.ts +3 -3
- package/src/queryMethods/window.test.ts +2 -2
- package/src/relations.ts +18 -7
- package/src/sql/insert.ts +31 -19
- package/src/sql/types.ts +3 -2
- package/src/test-utils.ts +4 -1
|
@@ -3,9 +3,9 @@ import {
|
|
|
3
3
|
Query,
|
|
4
4
|
QueryReturnsAll,
|
|
5
5
|
QueryReturnType,
|
|
6
|
+
queryTypeWithLimitOne,
|
|
6
7
|
SetQueryReturnsAll,
|
|
7
8
|
SetQueryReturnsOne,
|
|
8
|
-
SetQueryReturnsRowCount,
|
|
9
9
|
} from '../query';
|
|
10
10
|
import { pushQueryArray } from '../queryDataUtils';
|
|
11
11
|
import { RawExpression } from '../common';
|
|
@@ -26,13 +26,13 @@ import { InsertQueryData, OnConflictItem, OnConflictMergeUpdate } from '../sql';
|
|
|
26
26
|
import { WhereArg } from './where';
|
|
27
27
|
import { parseResult, queryMethodByReturnType } from './then';
|
|
28
28
|
|
|
29
|
-
export type
|
|
29
|
+
export type CreateData<
|
|
30
30
|
T extends Query,
|
|
31
31
|
DefaultKeys extends PropertyKey = keyof T[defaultsKey],
|
|
32
32
|
Data = SetOptional<T['inputType'], DefaultKeys>,
|
|
33
33
|
> = [keyof T['relations']] extends [never]
|
|
34
34
|
? Data
|
|
35
|
-
: OmitBelongsToForeignKeys<T['relations'], Data> &
|
|
35
|
+
: OmitBelongsToForeignKeys<T['relations'], Data> & CreateRelationData<T>;
|
|
36
36
|
|
|
37
37
|
type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<
|
|
38
38
|
Data,
|
|
@@ -43,17 +43,17 @@ type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<
|
|
|
43
43
|
}[keyof R]
|
|
44
44
|
>;
|
|
45
45
|
|
|
46
|
-
type
|
|
46
|
+
type CreateRelationData<T extends Query> = {
|
|
47
47
|
[K in keyof T['relations']]: T['relations'][K] extends BelongsToRelation
|
|
48
|
-
?
|
|
48
|
+
? CreateBelongsToData<T, K, T['relations'][K]>
|
|
49
49
|
: T['relations'][K] extends HasOneRelation
|
|
50
|
-
?
|
|
50
|
+
? CreateHasOneData<T, K, T['relations'][K]>
|
|
51
51
|
: T['relations'][K] extends HasManyRelation | HasAndBelongsToManyRelation
|
|
52
|
-
?
|
|
52
|
+
? CreateHasManyData<T, K, T['relations'][K]>
|
|
53
53
|
: EmptyObject;
|
|
54
54
|
}[keyof T['relations']];
|
|
55
55
|
|
|
56
|
-
type
|
|
56
|
+
type CreateBelongsToData<
|
|
57
57
|
T extends Query,
|
|
58
58
|
Key extends keyof T['relations'],
|
|
59
59
|
Rel extends BelongsToRelation,
|
|
@@ -69,7 +69,7 @@ type InsertBelongsToData<
|
|
|
69
69
|
| {
|
|
70
70
|
[K in Key]:
|
|
71
71
|
| {
|
|
72
|
-
create:
|
|
72
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
73
73
|
connect?: never;
|
|
74
74
|
connectOrCreate?: never;
|
|
75
75
|
}
|
|
@@ -83,12 +83,12 @@ type InsertBelongsToData<
|
|
|
83
83
|
connect?: never;
|
|
84
84
|
connectOrCreate: {
|
|
85
85
|
where: WhereArg<Rel['model']>;
|
|
86
|
-
create:
|
|
86
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
87
87
|
};
|
|
88
88
|
};
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
-
type
|
|
91
|
+
type CreateHasOneData<
|
|
92
92
|
T extends Query,
|
|
93
93
|
Key extends keyof T['relations'],
|
|
94
94
|
Rel extends HasOneRelation,
|
|
@@ -98,7 +98,7 @@ type InsertHasOneData<
|
|
|
98
98
|
: {
|
|
99
99
|
[K in Key]?:
|
|
100
100
|
| {
|
|
101
|
-
create:
|
|
101
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
102
102
|
connect?: never;
|
|
103
103
|
connectOrCreate?: never;
|
|
104
104
|
}
|
|
@@ -112,12 +112,12 @@ type InsertHasOneData<
|
|
|
112
112
|
connect?: never;
|
|
113
113
|
connectOrCreate: {
|
|
114
114
|
where?: WhereArg<Rel['model']>;
|
|
115
|
-
create?:
|
|
115
|
+
create?: CreateData<Rel['nestedCreateQuery']>;
|
|
116
116
|
};
|
|
117
117
|
};
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
-
type
|
|
120
|
+
type CreateHasManyData<
|
|
121
121
|
T extends Query,
|
|
122
122
|
Key extends keyof T['relations'],
|
|
123
123
|
Rel extends HasManyRelation | HasAndBelongsToManyRelation,
|
|
@@ -126,30 +126,28 @@ type InsertHasManyData<
|
|
|
126
126
|
{}
|
|
127
127
|
: {
|
|
128
128
|
[K in Key]?: {
|
|
129
|
-
create?:
|
|
129
|
+
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
130
130
|
connect?: WhereArg<Rel['model']>[];
|
|
131
131
|
connectOrCreate?: {
|
|
132
132
|
where: WhereArg<Rel['model']>;
|
|
133
|
-
create:
|
|
133
|
+
create: CreateData<Rel['nestedCreateQuery']>;
|
|
134
134
|
}[];
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
137
|
|
|
138
|
-
type
|
|
138
|
+
type CreateResult<T extends Query> = T extends { isCount: true }
|
|
139
|
+
? T
|
|
140
|
+
: QueryReturnsAll<T['returnType']> extends true
|
|
141
|
+
? SetQueryReturnsOne<T>
|
|
142
|
+
: T;
|
|
139
143
|
|
|
140
|
-
type
|
|
141
|
-
?
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
: T
|
|
146
|
-
: SetQueryReturnsRowCount<T>;
|
|
144
|
+
type CreateManyResult<T extends Query> = T extends { isCount: true }
|
|
145
|
+
? T
|
|
146
|
+
: T['returnType'] extends 'one' | 'oneOrThrow'
|
|
147
|
+
? SetQueryReturnsAll<T>
|
|
148
|
+
: T;
|
|
147
149
|
|
|
148
|
-
type
|
|
149
|
-
? T['returnType'] extends 'one' | 'oneOrThrow'
|
|
150
|
-
? SetQueryReturnsAll<T>
|
|
151
|
-
: T
|
|
152
|
-
: SetQueryReturnsRowCount<T>;
|
|
150
|
+
type CreateRawData = { columns: string[]; values: RawExpression };
|
|
153
151
|
|
|
154
152
|
type OnConflictArg<T extends Query> =
|
|
155
153
|
| keyof T['shape']
|
|
@@ -166,17 +164,31 @@ type AppendRelations = Record<
|
|
|
166
164
|
[rowIndex: number, data: NestedInsertItem][]
|
|
167
165
|
>;
|
|
168
166
|
|
|
169
|
-
type
|
|
167
|
+
type CreateCtx = {
|
|
170
168
|
prependRelations: PrependRelations;
|
|
171
169
|
appendRelations: AppendRelations;
|
|
172
170
|
requiredReturning: Record<string, boolean>;
|
|
173
171
|
relations: Record<string, Relation>;
|
|
174
172
|
};
|
|
175
173
|
|
|
176
|
-
const
|
|
174
|
+
const handleSelect = (q: Query) => {
|
|
175
|
+
const select = q.query.select?.[0];
|
|
176
|
+
const isCount =
|
|
177
|
+
typeof select === 'object' &&
|
|
178
|
+
'function' in select &&
|
|
179
|
+
select.function === 'count';
|
|
180
|
+
|
|
181
|
+
if (isCount) {
|
|
182
|
+
q.query.select = undefined;
|
|
183
|
+
} else if (isCount || !q.query.select) {
|
|
184
|
+
q.query.select = ['*'];
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const processCreateItem = (
|
|
177
189
|
item: Record<string, unknown>,
|
|
178
190
|
rowIndex: number,
|
|
179
|
-
ctx:
|
|
191
|
+
ctx: CreateCtx,
|
|
180
192
|
columns: string[],
|
|
181
193
|
columnsMap: Record<string, number>,
|
|
182
194
|
) => {
|
|
@@ -216,14 +228,14 @@ const processInsertItem = (
|
|
|
216
228
|
});
|
|
217
229
|
};
|
|
218
230
|
|
|
219
|
-
const
|
|
231
|
+
const createCtx = (q: Query): CreateCtx => ({
|
|
220
232
|
prependRelations: {},
|
|
221
233
|
appendRelations: {},
|
|
222
234
|
requiredReturning: {},
|
|
223
235
|
relations: (q as unknown as Query).relations,
|
|
224
236
|
});
|
|
225
237
|
|
|
226
|
-
const
|
|
238
|
+
const getSingleReturnType = (q: Query) => {
|
|
227
239
|
const { select, returnType = 'all' } = q.query;
|
|
228
240
|
if (select) {
|
|
229
241
|
return returnType === 'all' ? 'one' : returnType;
|
|
@@ -232,7 +244,7 @@ const getInsertSingleReturnType = (q: Query) => {
|
|
|
232
244
|
}
|
|
233
245
|
};
|
|
234
246
|
|
|
235
|
-
const
|
|
247
|
+
const getManyReturnType = (q: Query) => {
|
|
236
248
|
const { select, returnType } = q.query;
|
|
237
249
|
if (select) {
|
|
238
250
|
return returnType === 'one' || returnType === 'oneOrThrow'
|
|
@@ -243,11 +255,7 @@ const getInsertManyReturnType = (q: Query) => {
|
|
|
243
255
|
}
|
|
244
256
|
};
|
|
245
257
|
|
|
246
|
-
const
|
|
247
|
-
q: Query,
|
|
248
|
-
data: InsertData<Query>,
|
|
249
|
-
ctx: InsertCtx,
|
|
250
|
-
) => {
|
|
258
|
+
const handleOneData = (q: Query, data: CreateData<Query>, ctx: CreateCtx) => {
|
|
251
259
|
const columns: string[] = [];
|
|
252
260
|
const columnsMap: Record<string, number> = {};
|
|
253
261
|
const defaults = q.query.defaults;
|
|
@@ -256,17 +264,17 @@ const handleInsertOneData = (
|
|
|
256
264
|
data = { ...defaults, ...data };
|
|
257
265
|
}
|
|
258
266
|
|
|
259
|
-
|
|
267
|
+
processCreateItem(data, 0, ctx, columns, columnsMap);
|
|
260
268
|
|
|
261
269
|
const values = [columns.map((key) => (data as Record<string, unknown>)[key])];
|
|
262
270
|
|
|
263
271
|
return { columns, values };
|
|
264
272
|
};
|
|
265
273
|
|
|
266
|
-
const
|
|
274
|
+
const handleManyData = (
|
|
267
275
|
q: Query,
|
|
268
|
-
data:
|
|
269
|
-
ctx:
|
|
276
|
+
data: CreateData<Query>[],
|
|
277
|
+
ctx: CreateCtx,
|
|
270
278
|
) => {
|
|
271
279
|
const columns: string[] = [];
|
|
272
280
|
const columnsMap: Record<string, number> = {};
|
|
@@ -277,7 +285,7 @@ const handleInsertManyData = (
|
|
|
277
285
|
}
|
|
278
286
|
|
|
279
287
|
data.forEach((item, i) => {
|
|
280
|
-
|
|
288
|
+
processCreateItem(item, i, ctx, columns, columnsMap);
|
|
281
289
|
});
|
|
282
290
|
|
|
283
291
|
const values = Array(data.length);
|
|
@@ -299,7 +307,7 @@ const insert = (
|
|
|
299
307
|
values: unknown[][] | RawExpression;
|
|
300
308
|
},
|
|
301
309
|
returnType: QueryReturnType,
|
|
302
|
-
ctx?:
|
|
310
|
+
ctx?: CreateCtx,
|
|
303
311
|
) => {
|
|
304
312
|
const q = self as Query & { query: InsertQueryData };
|
|
305
313
|
const returning = q.query.select;
|
|
@@ -407,102 +415,115 @@ const insert = (
|
|
|
407
415
|
return q;
|
|
408
416
|
};
|
|
409
417
|
|
|
410
|
-
export
|
|
411
|
-
|
|
412
|
-
|
|
418
|
+
export type CreateMethodsNames =
|
|
419
|
+
| 'create'
|
|
420
|
+
| '_create'
|
|
421
|
+
| 'createMany'
|
|
422
|
+
| '_createMany'
|
|
423
|
+
| 'createRaw'
|
|
424
|
+
| '_createRaw'
|
|
425
|
+
| 'createFrom'
|
|
426
|
+
| '_createFrom';
|
|
427
|
+
|
|
428
|
+
export class Create {
|
|
429
|
+
create<T extends Query>(this: T, data: CreateData<T>): CreateResult<T> {
|
|
430
|
+
return this.clone()._create(data);
|
|
413
431
|
}
|
|
414
|
-
|
|
415
|
-
|
|
432
|
+
_create<T extends Query>(this: T, data: CreateData<T>): CreateResult<T> {
|
|
433
|
+
handleSelect(this);
|
|
434
|
+
|
|
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
|
+
|
|
416
462
|
return insert(
|
|
417
463
|
this,
|
|
418
|
-
|
|
419
|
-
|
|
464
|
+
{ columns, values: obj.values },
|
|
465
|
+
getSingleReturnType(this),
|
|
420
466
|
ctx,
|
|
421
|
-
) as
|
|
467
|
+
) as CreateResult<T>;
|
|
422
468
|
}
|
|
423
469
|
|
|
424
|
-
|
|
470
|
+
createMany<T extends Query>(
|
|
425
471
|
this: T,
|
|
426
|
-
data:
|
|
427
|
-
):
|
|
428
|
-
return this.clone().
|
|
472
|
+
data: CreateData<T>[],
|
|
473
|
+
): CreateManyResult<T> {
|
|
474
|
+
return this.clone()._createMany(data);
|
|
429
475
|
}
|
|
430
|
-
|
|
476
|
+
_createMany<T extends Query>(
|
|
431
477
|
this: T,
|
|
432
|
-
data:
|
|
433
|
-
):
|
|
434
|
-
|
|
478
|
+
data: CreateData<T>[],
|
|
479
|
+
): CreateManyResult<T> {
|
|
480
|
+
handleSelect(this);
|
|
481
|
+
const ctx = createCtx(this);
|
|
435
482
|
return insert(
|
|
436
483
|
this,
|
|
437
|
-
|
|
438
|
-
|
|
484
|
+
handleManyData(this, data, ctx),
|
|
485
|
+
getManyReturnType(this),
|
|
439
486
|
ctx,
|
|
440
|
-
) as
|
|
487
|
+
) as CreateManyResult<T>;
|
|
441
488
|
}
|
|
442
489
|
|
|
443
|
-
|
|
490
|
+
createRaw<T extends Query>(
|
|
444
491
|
this: T,
|
|
445
|
-
data:
|
|
446
|
-
):
|
|
447
|
-
return this.clone().
|
|
492
|
+
data: CreateRawData,
|
|
493
|
+
): CreateManyResult<T> {
|
|
494
|
+
return this.clone()._createRaw(data);
|
|
448
495
|
}
|
|
449
|
-
|
|
496
|
+
_createRaw<T extends Query>(
|
|
450
497
|
this: T,
|
|
451
|
-
data:
|
|
452
|
-
):
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
data,
|
|
456
|
-
getInsertManyReturnType(this),
|
|
457
|
-
) as InsertManyResult<T>;
|
|
498
|
+
data: CreateRawData,
|
|
499
|
+
): CreateManyResult<T> {
|
|
500
|
+
handleSelect(this);
|
|
501
|
+
return insert(this, data, getManyReturnType(this)) as CreateManyResult<T>;
|
|
458
502
|
}
|
|
459
503
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
504
|
+
createFrom<
|
|
505
|
+
T extends Query,
|
|
506
|
+
Q extends Query & { returnType: 'one' | 'oneOrThrow' },
|
|
507
|
+
>(
|
|
464
508
|
this: T,
|
|
465
|
-
|
|
509
|
+
query: Q,
|
|
510
|
+
data: Omit<CreateData<T>, keyof Q['result']>,
|
|
466
511
|
): SetQueryReturnsOne<T> {
|
|
467
|
-
|
|
468
|
-
this.query.select = ['*'];
|
|
469
|
-
}
|
|
470
|
-
return this.clone()._insert(data) as SetQueryReturnsOne<T>;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
createMany<T extends Query>(
|
|
474
|
-
this: T,
|
|
475
|
-
data: InsertData<T>[],
|
|
476
|
-
): SetQueryReturnsAll<T> {
|
|
477
|
-
return this.clone()._createMany(data);
|
|
478
|
-
}
|
|
479
|
-
_createMany<T extends Query>(
|
|
480
|
-
this: T,
|
|
481
|
-
data: InsertData<T>[],
|
|
482
|
-
): SetQueryReturnsAll<T> {
|
|
483
|
-
if (!this.query.select) {
|
|
484
|
-
this.query.select = ['*'];
|
|
485
|
-
}
|
|
486
|
-
return this.clone()._insertMany(data) as SetQueryReturnsAll<T>;
|
|
512
|
+
return this.clone()._createFrom(query, data);
|
|
487
513
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
): SetQueryReturnsAll<T> {
|
|
493
|
-
return this.clone()._createRaw(data);
|
|
494
|
-
}
|
|
495
|
-
_createRaw<T extends Query>(
|
|
514
|
+
_createFrom<
|
|
515
|
+
T extends Query,
|
|
516
|
+
Q extends Query & { returnType: 'one' | 'oneOrThrow' },
|
|
517
|
+
>(
|
|
496
518
|
this: T,
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
return this.clone()._insertRaw(data) as SetQueryReturnsAll<T>;
|
|
519
|
+
query: Q,
|
|
520
|
+
data: Omit<CreateData<T>, keyof Q['result']>,
|
|
521
|
+
): CreateResult<T> {
|
|
522
|
+
(this.query as InsertQueryData).fromQuery = query;
|
|
523
|
+
return this._create(data as CreateData<T>);
|
|
503
524
|
}
|
|
504
525
|
|
|
505
|
-
defaults<T extends Query, Data extends Partial<
|
|
526
|
+
defaults<T extends Query, Data extends Partial<CreateData<T>>>(
|
|
506
527
|
this: T,
|
|
507
528
|
data: Data,
|
|
508
529
|
): T & {
|
|
@@ -510,7 +531,7 @@ export class Insert {
|
|
|
510
531
|
} {
|
|
511
532
|
return (this.clone() as T)._defaults(data);
|
|
512
533
|
}
|
|
513
|
-
_defaults<T extends Query, Data extends Partial<
|
|
534
|
+
_defaults<T extends Query, Data extends Partial<CreateData<T>>>(
|
|
514
535
|
this: T,
|
|
515
536
|
data: Data,
|
|
516
537
|
): T & { [defaultsKey]: Record<keyof Data, true> } {
|
|
@@ -29,7 +29,7 @@ describe('delete', () => {
|
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
it('should delete records, returning value', async () => {
|
|
32
|
-
const id = await User.get('id').
|
|
32
|
+
const id = await User.get('id').create(userData);
|
|
33
33
|
const q = User.all();
|
|
34
34
|
|
|
35
35
|
const query = q.find(id).get('id').delete();
|
|
@@ -54,7 +54,7 @@ describe('delete', () => {
|
|
|
54
54
|
const rowsCount = 3;
|
|
55
55
|
|
|
56
56
|
for (let i = 0; i < rowsCount; i++) {
|
|
57
|
-
await User.
|
|
57
|
+
await User.create(userData);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
const q = User.all();
|
|
@@ -8,7 +8,7 @@ describe('get', () => {
|
|
|
8
8
|
|
|
9
9
|
describe('get', () => {
|
|
10
10
|
it('should select column and return a single value', async () => {
|
|
11
|
-
const { id } = await User.select('id').
|
|
11
|
+
const { id } = await User.select('id').create(userData);
|
|
12
12
|
|
|
13
13
|
const received = await User.get('id');
|
|
14
14
|
|
|
@@ -32,7 +32,7 @@ describe('get', () => {
|
|
|
32
32
|
|
|
33
33
|
describe('getOptional', () => {
|
|
34
34
|
it('should select column and return a single value when exists', async () => {
|
|
35
|
-
const { id } = await User.select('id').
|
|
35
|
+
const { id } = await User.select('id').create(userData);
|
|
36
36
|
|
|
37
37
|
const received = await User.getOptional('id');
|
|
38
38
|
|
|
@@ -2,6 +2,7 @@ import { createDb } from '../db';
|
|
|
2
2
|
import { adapter, dbOptions, userData, useTestDatabase } from '../test-utils';
|
|
3
3
|
import { logColors } from './log';
|
|
4
4
|
import { noop } from '../utils';
|
|
5
|
+
import { columnTypes } from '../columnSchema';
|
|
5
6
|
|
|
6
7
|
describe('query log', () => {
|
|
7
8
|
useTestDatabase();
|
|
@@ -45,7 +46,7 @@ describe('query log', () => {
|
|
|
45
46
|
error: jest.fn(),
|
|
46
47
|
};
|
|
47
48
|
|
|
48
|
-
const db = createDb({ adapter, log: true, logger });
|
|
49
|
+
const db = createDb({ adapter, columnTypes, log: true, logger });
|
|
49
50
|
|
|
50
51
|
await db('user').where({ name: 'name' });
|
|
51
52
|
|
|
@@ -68,7 +69,12 @@ describe('query log', () => {
|
|
|
68
69
|
error: jest.fn(),
|
|
69
70
|
};
|
|
70
71
|
|
|
71
|
-
const db = createDb({
|
|
72
|
+
const db = createDb({
|
|
73
|
+
adapter,
|
|
74
|
+
columnTypes,
|
|
75
|
+
log: { colors: false },
|
|
76
|
+
logger,
|
|
77
|
+
});
|
|
72
78
|
|
|
73
79
|
await db('user').where({ name: 'name' });
|
|
74
80
|
|
|
@@ -87,7 +93,7 @@ describe('query log', () => {
|
|
|
87
93
|
error: jest.fn(),
|
|
88
94
|
};
|
|
89
95
|
|
|
90
|
-
const db = createDb({ adapter, log: true, logger });
|
|
96
|
+
const db = createDb({ adapter, columnTypes, log: true, logger });
|
|
91
97
|
|
|
92
98
|
await db('user').where({ wrongColumn: 'value' }).then(noop, noop);
|
|
93
99
|
|
|
@@ -112,7 +118,12 @@ describe('query log', () => {
|
|
|
112
118
|
error: jest.fn(),
|
|
113
119
|
};
|
|
114
120
|
|
|
115
|
-
const db = createDb({
|
|
121
|
+
const db = createDb({
|
|
122
|
+
adapter,
|
|
123
|
+
columnTypes,
|
|
124
|
+
log: { colors: false },
|
|
125
|
+
logger,
|
|
126
|
+
});
|
|
116
127
|
|
|
117
128
|
await db('user').where({ wrongColumn: 'value' }).then(noop, noop);
|
|
118
129
|
|
|
@@ -133,10 +144,15 @@ describe('query log', () => {
|
|
|
133
144
|
error: jest.fn(),
|
|
134
145
|
};
|
|
135
146
|
|
|
136
|
-
const db = createDb({
|
|
147
|
+
const db = createDb({
|
|
148
|
+
adapter,
|
|
149
|
+
columnTypes,
|
|
150
|
+
log: { colors: false },
|
|
151
|
+
logger,
|
|
152
|
+
});
|
|
137
153
|
|
|
138
154
|
await db.transaction(async (q) => {
|
|
139
|
-
await db('user').transacting(q).
|
|
155
|
+
await db('user').transacting(q).create(userData);
|
|
140
156
|
});
|
|
141
157
|
|
|
142
158
|
expect(logger.log.mock.calls).toEqual([
|
|
@@ -156,11 +172,16 @@ describe('query log', () => {
|
|
|
156
172
|
error: jest.fn(),
|
|
157
173
|
};
|
|
158
174
|
|
|
159
|
-
const db = createDb({
|
|
175
|
+
const db = createDb({
|
|
176
|
+
adapter,
|
|
177
|
+
columnTypes,
|
|
178
|
+
log: { colors: false },
|
|
179
|
+
logger,
|
|
180
|
+
});
|
|
160
181
|
|
|
161
182
|
await expect(
|
|
162
183
|
db.transaction(async (q) => {
|
|
163
|
-
await db('user').transacting(q).
|
|
184
|
+
await db('user').transacting(q).create({ name: 'name' });
|
|
164
185
|
}),
|
|
165
186
|
).rejects.toThrow();
|
|
166
187
|
|
|
@@ -373,8 +373,8 @@ describe('merge queries', () => {
|
|
|
373
373
|
i2.join = [{ type: 'b', args: ['b'] }];
|
|
374
374
|
i1.onConflict = { type: 'ignore' };
|
|
375
375
|
i2.onConflict = { type: 'merge' };
|
|
376
|
-
i1.
|
|
377
|
-
i2.
|
|
376
|
+
i1.beforeCreate = [() => {}];
|
|
377
|
+
i2.beforeCreate = [() => {}];
|
|
378
378
|
|
|
379
379
|
const u1 = q1.query as unknown as UpdateQueryData;
|
|
380
380
|
const u2 = q2.query as unknown as UpdateQueryData;
|
|
@@ -451,7 +451,7 @@ describe('merge queries', () => {
|
|
|
451
451
|
expect(i.using).toEqual([...i1.using, ...i2.using]);
|
|
452
452
|
expect(i.join).toEqual([...i1.join, ...i2.join]);
|
|
453
453
|
expect(i.onConflict).toEqual(i2.onConflict);
|
|
454
|
-
expect(i.
|
|
454
|
+
expect(i.beforeCreate).toEqual([...i1.beforeCreate, ...i2.beforeCreate]);
|
|
455
455
|
|
|
456
456
|
const u = q as UpdateQueryData;
|
|
457
457
|
expect(u.updateData).toEqual([...u1.updateData, ...u2.updateData]);
|
|
@@ -46,7 +46,7 @@ describe('queryMethods', () => {
|
|
|
46
46
|
|
|
47
47
|
describe('take', () => {
|
|
48
48
|
it('limits to one and returns only one', async () => {
|
|
49
|
-
await User.
|
|
49
|
+
await User.create(userData);
|
|
50
50
|
|
|
51
51
|
const q = User.all();
|
|
52
52
|
expectSql(q.take().toSql(), `SELECT * FROM "user" LIMIT $1`, [1]);
|
|
@@ -73,7 +73,7 @@ describe('queryMethods', () => {
|
|
|
73
73
|
|
|
74
74
|
describe('takeOptional', () => {
|
|
75
75
|
it('limits to one and returns only one', async () => {
|
|
76
|
-
await User.
|
|
76
|
+
await User.create(userData);
|
|
77
77
|
|
|
78
78
|
const q = User.all();
|
|
79
79
|
expectSql(q.takeOptional().toSql(), `SELECT * FROM "user" LIMIT $1`, [1]);
|
|
@@ -114,7 +114,7 @@ describe('queryMethods', () => {
|
|
|
114
114
|
describe('pluck', () => {
|
|
115
115
|
beforeEach(async () => {
|
|
116
116
|
for (let i = 0; i < 3; i++) {
|
|
117
|
-
await User.
|
|
117
|
+
await User.create({ ...userData, createdAt: now });
|
|
118
118
|
}
|
|
119
119
|
});
|
|
120
120
|
|
|
@@ -573,7 +573,7 @@ describe('queryMethods', () => {
|
|
|
573
573
|
|
|
574
574
|
expect(await query).toBe(false);
|
|
575
575
|
|
|
576
|
-
await User.
|
|
576
|
+
await User.create(userData);
|
|
577
577
|
|
|
578
578
|
expect(await query).toBe(true);
|
|
579
579
|
|
|
@@ -33,7 +33,7 @@ import { Join } from './join';
|
|
|
33
33
|
import { With } from './with';
|
|
34
34
|
import { Union } from './union';
|
|
35
35
|
import { Json } from './json';
|
|
36
|
-
import {
|
|
36
|
+
import { Create } from './create';
|
|
37
37
|
import { Update } from './update';
|
|
38
38
|
import { Delete } from './delete';
|
|
39
39
|
import { Transaction } from './transaction';
|
|
@@ -81,7 +81,7 @@ export interface QueryMethods
|
|
|
81
81
|
With,
|
|
82
82
|
Union,
|
|
83
83
|
Json,
|
|
84
|
-
|
|
84
|
+
Create,
|
|
85
85
|
Update,
|
|
86
86
|
Delete,
|
|
87
87
|
Transaction,
|
|
@@ -381,7 +381,7 @@ applyMixins(QueryMethods, [
|
|
|
381
381
|
With,
|
|
382
382
|
Union,
|
|
383
383
|
Json,
|
|
384
|
-
|
|
384
|
+
Create,
|
|
385
385
|
Update,
|
|
386
386
|
Delete,
|
|
387
387
|
Transaction,
|