pqb 0.2.0 → 0.2.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 +25 -12
- package/dist/index.esm.js +1 -0
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.ts +6 -2
- package/src/columnSchema/columnTypes.ts +4 -1
- package/src/db.ts +6 -1
- package/src/query.ts +2 -1
- package/src/queryMethods/insert.test.ts +78 -92
- package/src/queryMethods/insert.ts +6 -6
- package/src/queryMethods/queryMethods.test.ts +1 -1
- package/src/queryMethods/select.test.ts +2 -0
- package/src/queryMethods/update.test.ts +8 -9
- package/src/queryMethods/upsert.ts +1 -1
- package/src/queryMethods/where.test.ts +45 -45
- package/src/relations.ts +1 -1
- package/src/test-utils.ts +4 -31
package/package.json
CHANGED
|
@@ -123,6 +123,7 @@ export abstract class ColumnType<
|
|
|
123
123
|
isPrimaryKey = false;
|
|
124
124
|
isHidden = false;
|
|
125
125
|
isNullable = false;
|
|
126
|
+
hasDefault = false;
|
|
126
127
|
|
|
127
128
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
129
|
encodeFn?: (input: any) => unknown;
|
|
@@ -212,7 +213,10 @@ export abstract class ColumnType<
|
|
|
212
213
|
return this.dataType;
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
default<T extends ColumnType>(
|
|
216
|
+
default<T extends ColumnType>(
|
|
217
|
+
this: T,
|
|
218
|
+
value: T['type'] | RawExpression,
|
|
219
|
+
): T & { hasDefault: true } {
|
|
216
220
|
const cloned = Object.create(this);
|
|
217
221
|
cloned.data = { ...cloned.data, default: value };
|
|
218
222
|
return cloned;
|
|
@@ -221,7 +225,7 @@ export abstract class ColumnType<
|
|
|
221
225
|
index<T extends ColumnType>(
|
|
222
226
|
this: T,
|
|
223
227
|
options: Omit<SingleColumnIndexOptions, 'column'> = {},
|
|
224
|
-
) {
|
|
228
|
+
): T {
|
|
225
229
|
const cloned = Object.create(this);
|
|
226
230
|
cloned.data = { ...cloned.data, index: options };
|
|
227
231
|
return cloned;
|
|
@@ -179,7 +179,10 @@ export const columnTypes = {
|
|
|
179
179
|
|
|
180
180
|
timestamps<T extends ColumnType>(this: {
|
|
181
181
|
timestamp(): T;
|
|
182
|
-
}): {
|
|
182
|
+
}): {
|
|
183
|
+
createdAt: T & { hasDefault: true };
|
|
184
|
+
updatedAt: T & { hasDefault: true };
|
|
185
|
+
} {
|
|
183
186
|
return {
|
|
184
187
|
createdAt: this.timestamp().default(raw('now()')),
|
|
185
188
|
updatedAt: this.timestamp().default(raw('now()')),
|
package/src/db.ts
CHANGED
|
@@ -71,7 +71,12 @@ export interface Db<
|
|
|
71
71
|
withData: Query['withData'];
|
|
72
72
|
joinedTables: Query['joinedTables'];
|
|
73
73
|
relations: Relations;
|
|
74
|
-
[defaultsKey]:
|
|
74
|
+
[defaultsKey]: Record<
|
|
75
|
+
{
|
|
76
|
+
[K in keyof Shape]: Shape[K]['hasDefault'] extends true ? K : never;
|
|
77
|
+
}[keyof Shape],
|
|
78
|
+
true
|
|
79
|
+
>;
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
export class Db<
|
package/src/query.ts
CHANGED
|
@@ -70,7 +70,8 @@ export type Query = QueryMethods & {
|
|
|
70
70
|
columnsParsers?: ColumnsParsers;
|
|
71
71
|
relations: RelationsBase;
|
|
72
72
|
withData: WithDataBase;
|
|
73
|
-
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
74
|
+
[defaultsKey]: {};
|
|
74
75
|
};
|
|
75
76
|
|
|
76
77
|
export type Selectable<T extends QueryBase> = StringKey<keyof T['selectable']>;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AssertEqual,
|
|
3
|
-
expectMatchObjectWithTimestamps,
|
|
4
3
|
expectQueryNotMutated,
|
|
5
4
|
expectSql,
|
|
6
|
-
now,
|
|
7
5
|
User,
|
|
8
6
|
userData,
|
|
9
7
|
useTestDatabase,
|
|
@@ -42,10 +40,10 @@ describe('insert', () => {
|
|
|
42
40
|
expectSql(
|
|
43
41
|
query.toSql(),
|
|
44
42
|
`
|
|
45
|
-
INSERT INTO "user"("name", "password"
|
|
46
|
-
VALUES ($1, $2
|
|
43
|
+
INSERT INTO "user"("name", "password")
|
|
44
|
+
VALUES ($1, $2)
|
|
47
45
|
`,
|
|
48
|
-
['name', 'password'
|
|
46
|
+
['name', 'password'],
|
|
49
47
|
);
|
|
50
48
|
|
|
51
49
|
const result = await query;
|
|
@@ -55,7 +53,7 @@ describe('insert', () => {
|
|
|
55
53
|
expect(eq).toBe(true);
|
|
56
54
|
|
|
57
55
|
const inserted = await User.take();
|
|
58
|
-
|
|
56
|
+
expect(inserted).toMatchObject(userData);
|
|
59
57
|
|
|
60
58
|
expectQueryNotMutated(q);
|
|
61
59
|
});
|
|
@@ -67,11 +65,11 @@ describe('insert', () => {
|
|
|
67
65
|
expectSql(
|
|
68
66
|
query.toSql(),
|
|
69
67
|
`
|
|
70
|
-
INSERT INTO "user"("name", "password"
|
|
71
|
-
VALUES ($1, $2
|
|
68
|
+
INSERT INTO "user"("name", "password")
|
|
69
|
+
VALUES ($1, $2)
|
|
72
70
|
RETURNING "user"."id"
|
|
73
71
|
`,
|
|
74
|
-
['name', 'password'
|
|
72
|
+
['name', 'password'],
|
|
75
73
|
);
|
|
76
74
|
|
|
77
75
|
const result = await query;
|
|
@@ -86,29 +84,24 @@ describe('insert', () => {
|
|
|
86
84
|
it('should insert one record, returning columns', async () => {
|
|
87
85
|
const q = User.all();
|
|
88
86
|
|
|
89
|
-
const query = q
|
|
90
|
-
.select('id', 'name', 'createdAt', 'updatedAt')
|
|
91
|
-
.insert(userData);
|
|
87
|
+
const query = q.select('id', 'name').insert(userData);
|
|
92
88
|
expectSql(
|
|
93
89
|
query.toSql(),
|
|
94
90
|
`
|
|
95
|
-
INSERT INTO "user"("name", "password"
|
|
96
|
-
VALUES ($1, $2
|
|
97
|
-
RETURNING "user"."id", "user"."name"
|
|
91
|
+
INSERT INTO "user"("name", "password")
|
|
92
|
+
VALUES ($1, $2)
|
|
93
|
+
RETURNING "user"."id", "user"."name"
|
|
98
94
|
`,
|
|
99
|
-
['name', 'password'
|
|
95
|
+
['name', 'password'],
|
|
100
96
|
);
|
|
101
97
|
|
|
102
98
|
const result = await query;
|
|
103
|
-
const eq: AssertEqual<
|
|
104
|
-
typeof result,
|
|
105
|
-
{ id: number; name: string; createdAt: Date; updatedAt: Date }
|
|
106
|
-
> = true;
|
|
99
|
+
const eq: AssertEqual<typeof result, { id: number; name: string }> = true;
|
|
107
100
|
expect(eq).toBe(true);
|
|
108
101
|
|
|
109
102
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
110
103
|
const { password, ...other } = userData;
|
|
111
|
-
|
|
104
|
+
expect(result).toMatchObject(other);
|
|
112
105
|
|
|
113
106
|
expectQueryNotMutated(q);
|
|
114
107
|
});
|
|
@@ -120,11 +113,11 @@ describe('insert', () => {
|
|
|
120
113
|
expectSql(
|
|
121
114
|
query.toSql(),
|
|
122
115
|
`
|
|
123
|
-
INSERT INTO "user"("name", "password"
|
|
124
|
-
VALUES ($1, $2
|
|
116
|
+
INSERT INTO "user"("name", "password")
|
|
117
|
+
VALUES ($1, $2)
|
|
125
118
|
RETURNING *
|
|
126
119
|
`,
|
|
127
|
-
['name', 'password'
|
|
120
|
+
['name', 'password'],
|
|
128
121
|
);
|
|
129
122
|
|
|
130
123
|
const result = await query;
|
|
@@ -133,7 +126,7 @@ describe('insert', () => {
|
|
|
133
126
|
|
|
134
127
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
135
128
|
const { password, ...other } = userData;
|
|
136
|
-
|
|
129
|
+
expect(result).toMatchObject(other);
|
|
137
130
|
|
|
138
131
|
expectQueryNotMutated(q);
|
|
139
132
|
});
|
|
@@ -154,12 +147,12 @@ describe('insert', () => {
|
|
|
154
147
|
expectSql(
|
|
155
148
|
query.toSql(),
|
|
156
149
|
`
|
|
157
|
-
INSERT INTO "user"("name", "password", "
|
|
150
|
+
INSERT INTO "user"("name", "password", "picture")
|
|
158
151
|
VALUES
|
|
159
|
-
($1, $2, $3
|
|
160
|
-
($
|
|
152
|
+
($1, $2, $3),
|
|
153
|
+
($4, $5, DEFAULT)
|
|
161
154
|
`,
|
|
162
|
-
['name', 'password',
|
|
155
|
+
['name', 'password', null, 'name', 'password'],
|
|
163
156
|
);
|
|
164
157
|
|
|
165
158
|
const result = await query;
|
|
@@ -170,7 +163,7 @@ describe('insert', () => {
|
|
|
170
163
|
|
|
171
164
|
const inserted = await User.all();
|
|
172
165
|
inserted.forEach((item, i) => {
|
|
173
|
-
|
|
166
|
+
expect(item).toMatchObject(arr[i]);
|
|
174
167
|
});
|
|
175
168
|
|
|
176
169
|
expectQueryNotMutated(q);
|
|
@@ -187,30 +180,27 @@ describe('insert', () => {
|
|
|
187
180
|
userData,
|
|
188
181
|
];
|
|
189
182
|
|
|
190
|
-
const query = q.select('id', 'name'
|
|
183
|
+
const query = q.select('id', 'name').insert(arr);
|
|
191
184
|
|
|
192
185
|
expectSql(
|
|
193
186
|
query.toSql(),
|
|
194
187
|
`
|
|
195
|
-
INSERT INTO "user"("name", "password", "
|
|
188
|
+
INSERT INTO "user"("name", "password", "picture")
|
|
196
189
|
VALUES
|
|
197
|
-
($1, $2, $3
|
|
198
|
-
($
|
|
199
|
-
RETURNING "user"."id", "user"."name"
|
|
190
|
+
($1, $2, $3),
|
|
191
|
+
($4, $5, DEFAULT)
|
|
192
|
+
RETURNING "user"."id", "user"."name"
|
|
200
193
|
`,
|
|
201
|
-
['name', 'password',
|
|
194
|
+
['name', 'password', null, 'name', 'password'],
|
|
202
195
|
);
|
|
203
196
|
|
|
204
197
|
const result = await query;
|
|
205
|
-
const eq: AssertEqual<
|
|
206
|
-
typeof result,
|
|
207
|
-
{ id: number; name: string; createdAt: Date; updatedAt: Date }[]
|
|
208
|
-
> = true;
|
|
198
|
+
const eq: AssertEqual<typeof result, { id: number; name: string }[]> = true;
|
|
209
199
|
expect(eq).toBe(true);
|
|
210
200
|
|
|
211
201
|
const inserted = await User.all();
|
|
212
202
|
inserted.forEach((item, i) => {
|
|
213
|
-
|
|
203
|
+
expect(item).toMatchObject(arr[i]);
|
|
214
204
|
});
|
|
215
205
|
|
|
216
206
|
expectQueryNotMutated(q);
|
|
@@ -232,18 +222,18 @@ describe('insert', () => {
|
|
|
232
222
|
expectSql(
|
|
233
223
|
query.toSql(),
|
|
234
224
|
`
|
|
235
|
-
INSERT INTO "user"("name", "password", "
|
|
225
|
+
INSERT INTO "user"("name", "password", "picture")
|
|
236
226
|
VALUES
|
|
237
|
-
($1, $2, $3
|
|
238
|
-
($
|
|
227
|
+
($1, $2, $3),
|
|
228
|
+
($4, $5, DEFAULT)
|
|
239
229
|
RETURNING *
|
|
240
230
|
`,
|
|
241
|
-
['name', 'password',
|
|
231
|
+
['name', 'password', null, 'name', 'password'],
|
|
242
232
|
);
|
|
243
233
|
|
|
244
234
|
const result = await query;
|
|
245
235
|
result.forEach((item, i) => {
|
|
246
|
-
|
|
236
|
+
expect(item).toMatchObject(arr[i]);
|
|
247
237
|
});
|
|
248
238
|
|
|
249
239
|
const eq: AssertEqual<typeof result, typeof User['type'][]> = true;
|
|
@@ -251,7 +241,7 @@ describe('insert', () => {
|
|
|
251
241
|
|
|
252
242
|
const inserted = await User.all();
|
|
253
243
|
inserted.forEach((item, i) => {
|
|
254
|
-
|
|
244
|
+
expect(item).toMatchObject(arr[i]);
|
|
255
245
|
});
|
|
256
246
|
|
|
257
247
|
expectQueryNotMutated(q);
|
|
@@ -263,17 +253,15 @@ describe('insert', () => {
|
|
|
263
253
|
password: 'password',
|
|
264
254
|
}).insert({
|
|
265
255
|
password: 'override',
|
|
266
|
-
updatedAt: now,
|
|
267
|
-
createdAt: now,
|
|
268
256
|
});
|
|
269
257
|
|
|
270
258
|
expectSql(
|
|
271
259
|
query.toSql(),
|
|
272
260
|
`
|
|
273
|
-
INSERT INTO "user"("name", "password"
|
|
274
|
-
VALUES ($1, $2
|
|
261
|
+
INSERT INTO "user"("name", "password")
|
|
262
|
+
VALUES ($1, $2)
|
|
275
263
|
`,
|
|
276
|
-
['name', 'override'
|
|
264
|
+
['name', 'override'],
|
|
277
265
|
);
|
|
278
266
|
});
|
|
279
267
|
|
|
@@ -309,14 +297,14 @@ describe('insert', () => {
|
|
|
309
297
|
expectSql(
|
|
310
298
|
query.toSql(),
|
|
311
299
|
`
|
|
312
|
-
INSERT INTO "user"("name", "password"
|
|
313
|
-
VALUES ($1, $2
|
|
300
|
+
INSERT INTO "user"("name", "password")
|
|
301
|
+
VALUES ($1, $2)
|
|
314
302
|
ON CONFLICT ("name")
|
|
315
303
|
DO NOTHING
|
|
316
|
-
WHERE "user"."name" = $
|
|
304
|
+
WHERE "user"."name" = $3
|
|
317
305
|
RETURNING "user"."id"
|
|
318
306
|
`,
|
|
319
|
-
['name', 'password',
|
|
307
|
+
['name', 'password', 'where name'],
|
|
320
308
|
);
|
|
321
309
|
|
|
322
310
|
expectQueryNotMutated(q);
|
|
@@ -330,12 +318,12 @@ describe('insert', () => {
|
|
|
330
318
|
expectSql(
|
|
331
319
|
query.toSql(),
|
|
332
320
|
`
|
|
333
|
-
INSERT INTO "user"("name", "password"
|
|
334
|
-
VALUES ($1, $2
|
|
335
|
-
ON CONFLICT ("name", "password"
|
|
321
|
+
INSERT INTO "user"("name", "password")
|
|
322
|
+
VALUES ($1, $2)
|
|
323
|
+
ON CONFLICT ("name", "password")
|
|
336
324
|
DO NOTHING
|
|
337
325
|
`,
|
|
338
|
-
['name', 'password'
|
|
326
|
+
['name', 'password'],
|
|
339
327
|
);
|
|
340
328
|
|
|
341
329
|
expectQueryNotMutated(q);
|
|
@@ -348,11 +336,11 @@ describe('insert', () => {
|
|
|
348
336
|
expectSql(
|
|
349
337
|
query.toSql(),
|
|
350
338
|
`
|
|
351
|
-
INSERT INTO "user"("name", "password"
|
|
352
|
-
VALUES ($1, $2
|
|
339
|
+
INSERT INTO "user"("name", "password")
|
|
340
|
+
VALUES ($1, $2)
|
|
353
341
|
ON CONFLICT ("id") DO NOTHING
|
|
354
342
|
`,
|
|
355
|
-
['name', 'password'
|
|
343
|
+
['name', 'password'],
|
|
356
344
|
);
|
|
357
345
|
|
|
358
346
|
expectQueryNotMutated(q);
|
|
@@ -365,11 +353,11 @@ describe('insert', () => {
|
|
|
365
353
|
expectSql(
|
|
366
354
|
query.toSql(),
|
|
367
355
|
`
|
|
368
|
-
INSERT INTO "user"("name", "password"
|
|
369
|
-
VALUES ($1, $2
|
|
356
|
+
INSERT INTO "user"("name", "password")
|
|
357
|
+
VALUES ($1, $2)
|
|
370
358
|
ON CONFLICT ("id", "name") DO NOTHING
|
|
371
359
|
`,
|
|
372
|
-
['name', 'password'
|
|
360
|
+
['name', 'password'],
|
|
373
361
|
);
|
|
374
362
|
|
|
375
363
|
expectQueryNotMutated(q);
|
|
@@ -382,11 +370,11 @@ describe('insert', () => {
|
|
|
382
370
|
expectSql(
|
|
383
371
|
query.toSql(),
|
|
384
372
|
`
|
|
385
|
-
INSERT INTO "user"("name", "password"
|
|
386
|
-
VALUES ($1, $2
|
|
373
|
+
INSERT INTO "user"("name", "password")
|
|
374
|
+
VALUES ($1, $2)
|
|
387
375
|
ON CONFLICT raw query DO NOTHING
|
|
388
376
|
`,
|
|
389
|
-
['name', 'password'
|
|
377
|
+
['name', 'password'],
|
|
390
378
|
);
|
|
391
379
|
|
|
392
380
|
expectQueryNotMutated(q);
|
|
@@ -401,16 +389,14 @@ describe('insert', () => {
|
|
|
401
389
|
expectSql(
|
|
402
390
|
query.toSql(),
|
|
403
391
|
`
|
|
404
|
-
INSERT INTO "user"("name", "password"
|
|
405
|
-
VALUES ($1, $2
|
|
406
|
-
ON CONFLICT ("name", "password"
|
|
392
|
+
INSERT INTO "user"("name", "password")
|
|
393
|
+
VALUES ($1, $2)
|
|
394
|
+
ON CONFLICT ("name", "password")
|
|
407
395
|
DO UPDATE SET
|
|
408
396
|
"name" = excluded."name",
|
|
409
|
-
"password" = excluded."password"
|
|
410
|
-
"createdAt" = excluded."createdAt",
|
|
411
|
-
"updatedAt" = excluded."updatedAt"
|
|
397
|
+
"password" = excluded."password"
|
|
412
398
|
`,
|
|
413
|
-
['name', 'password'
|
|
399
|
+
['name', 'password'],
|
|
414
400
|
);
|
|
415
401
|
|
|
416
402
|
expectQueryNotMutated(q);
|
|
@@ -423,12 +409,12 @@ describe('insert', () => {
|
|
|
423
409
|
expectSql(
|
|
424
410
|
query.toSql(),
|
|
425
411
|
`
|
|
426
|
-
INSERT INTO "user"("name", "password"
|
|
427
|
-
VALUES ($1, $2
|
|
412
|
+
INSERT INTO "user"("name", "password")
|
|
413
|
+
VALUES ($1, $2)
|
|
428
414
|
ON CONFLICT ("name")
|
|
429
415
|
DO UPDATE SET "name" = excluded."name"
|
|
430
416
|
`,
|
|
431
|
-
['name', 'password'
|
|
417
|
+
['name', 'password'],
|
|
432
418
|
);
|
|
433
419
|
|
|
434
420
|
expectQueryNotMutated(q);
|
|
@@ -445,14 +431,14 @@ describe('insert', () => {
|
|
|
445
431
|
expectSql(
|
|
446
432
|
query.toSql(),
|
|
447
433
|
`
|
|
448
|
-
INSERT INTO "user"("name", "password"
|
|
449
|
-
VALUES ($1, $2
|
|
434
|
+
INSERT INTO "user"("name", "password")
|
|
435
|
+
VALUES ($1, $2)
|
|
450
436
|
ON CONFLICT ("name", "password")
|
|
451
437
|
DO UPDATE SET
|
|
452
438
|
"name" = excluded."name",
|
|
453
439
|
"password" = excluded."password"
|
|
454
440
|
`,
|
|
455
|
-
['name', 'password'
|
|
441
|
+
['name', 'password'],
|
|
456
442
|
);
|
|
457
443
|
|
|
458
444
|
expectQueryNotMutated(q);
|
|
@@ -469,12 +455,12 @@ describe('insert', () => {
|
|
|
469
455
|
expectSql(
|
|
470
456
|
query.toSql(),
|
|
471
457
|
`
|
|
472
|
-
INSERT INTO "user"("name", "password"
|
|
473
|
-
VALUES ($1, $2
|
|
458
|
+
INSERT INTO "user"("name", "password")
|
|
459
|
+
VALUES ($1, $2)
|
|
474
460
|
ON CONFLICT ("name")
|
|
475
|
-
DO UPDATE SET "name" = $
|
|
461
|
+
DO UPDATE SET "name" = $3
|
|
476
462
|
`,
|
|
477
|
-
['name', 'password',
|
|
463
|
+
['name', 'password', 'new name'],
|
|
478
464
|
);
|
|
479
465
|
|
|
480
466
|
expectQueryNotMutated(q);
|
|
@@ -491,12 +477,12 @@ describe('insert', () => {
|
|
|
491
477
|
expectSql(
|
|
492
478
|
query.toSql(),
|
|
493
479
|
`
|
|
494
|
-
INSERT INTO "user"("name", "password"
|
|
495
|
-
VALUES ($1, $2
|
|
480
|
+
INSERT INTO "user"("name", "password")
|
|
481
|
+
VALUES ($1, $2)
|
|
496
482
|
ON CONFLICT on conflict raw
|
|
497
483
|
DO UPDATE SET merge raw
|
|
498
484
|
`,
|
|
499
|
-
['name', 'password'
|
|
485
|
+
['name', 'password'],
|
|
500
486
|
);
|
|
501
487
|
|
|
502
488
|
expectQueryNotMutated(q);
|
|
@@ -507,7 +493,7 @@ describe('insert', () => {
|
|
|
507
493
|
describe('create', () => {
|
|
508
494
|
it('should return full record', async () => {
|
|
509
495
|
const result = await User.create(userData);
|
|
510
|
-
|
|
496
|
+
expect(result).toMatchObject(userData);
|
|
511
497
|
|
|
512
498
|
const eq: AssertEqual<typeof result, typeof User.type> = true;
|
|
513
499
|
expect(eq).toBe(true);
|
|
@@ -526,8 +512,8 @@ describe('insert', () => {
|
|
|
526
512
|
|
|
527
513
|
it('should return full records when creating many', async () => {
|
|
528
514
|
const result = await User.create([userData, userData]);
|
|
529
|
-
|
|
530
|
-
|
|
515
|
+
expect(result[0]).toMatchObject(userData);
|
|
516
|
+
expect(result[1]).toMatchObject(userData);
|
|
531
517
|
|
|
532
518
|
const eq: AssertEqual<typeof result, typeof User.type[]> = true;
|
|
533
519
|
expect(eq).toBe(true);
|
|
@@ -31,9 +31,7 @@ export type OptionalKeys<T extends Query> = {
|
|
|
31
31
|
|
|
32
32
|
export type InsertData<
|
|
33
33
|
T extends Query,
|
|
34
|
-
DefaultKeys extends
|
|
35
|
-
? T[defaultsKey]
|
|
36
|
-
: never,
|
|
34
|
+
DefaultKeys extends PropertyKey = keyof T[defaultsKey],
|
|
37
35
|
Data = SetOptional<SetOptional<T['inputType'], OptionalKeys<T>>, DefaultKeys>,
|
|
38
36
|
> = [keyof T['relations']] extends [never]
|
|
39
37
|
? Data
|
|
@@ -397,15 +395,17 @@ export class Insert {
|
|
|
397
395
|
defaults<T extends Query, Data extends Partial<InsertData<T>>>(
|
|
398
396
|
this: T,
|
|
399
397
|
data: Data,
|
|
400
|
-
): T & {
|
|
398
|
+
): T & {
|
|
399
|
+
[defaultsKey]: Record<keyof Data, true>;
|
|
400
|
+
} {
|
|
401
401
|
return (this.clone() as T)._defaults(data);
|
|
402
402
|
}
|
|
403
403
|
_defaults<T extends Query, Data extends Partial<InsertData<T>>>(
|
|
404
404
|
this: T,
|
|
405
405
|
data: Data,
|
|
406
|
-
): T & { [defaultsKey]: keyof Data } {
|
|
406
|
+
): T & { [defaultsKey]: Record<keyof Data, true> } {
|
|
407
407
|
this.query.defaults = data;
|
|
408
|
-
return this as T & { [defaultsKey]: keyof Data };
|
|
408
|
+
return this as T & { [defaultsKey]: Record<keyof Data, true> };
|
|
409
409
|
}
|
|
410
410
|
|
|
411
411
|
onConflict<T extends Query, Arg extends OnConflictArg<T>>(
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AssertEqual,
|
|
3
|
-
expectMatchObjectWithTimestamps,
|
|
4
3
|
expectQueryNotMutated,
|
|
5
4
|
expectSql,
|
|
6
5
|
User,
|
|
@@ -83,7 +82,7 @@ describe('update', () => {
|
|
|
83
82
|
expect(result).toBe(1);
|
|
84
83
|
|
|
85
84
|
const updated = await User.take();
|
|
86
|
-
|
|
85
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
87
86
|
});
|
|
88
87
|
|
|
89
88
|
it('should update record, returning value', async () => {
|
|
@@ -114,7 +113,7 @@ describe('update', () => {
|
|
|
114
113
|
expect(typeof result).toBe('number');
|
|
115
114
|
|
|
116
115
|
const updated = await User.take();
|
|
117
|
-
|
|
116
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
118
117
|
});
|
|
119
118
|
|
|
120
119
|
it('should update one record, return selected columns', async () => {
|
|
@@ -139,7 +138,7 @@ describe('update', () => {
|
|
|
139
138
|
expect(eq).toBe(true);
|
|
140
139
|
|
|
141
140
|
const updated = await User.take();
|
|
142
|
-
|
|
141
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
143
142
|
});
|
|
144
143
|
|
|
145
144
|
it('should update one record, return all columns', async () => {
|
|
@@ -164,7 +163,7 @@ describe('update', () => {
|
|
|
164
163
|
expect(eq).toBe(true);
|
|
165
164
|
|
|
166
165
|
const updated = await User.take();
|
|
167
|
-
|
|
166
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
168
167
|
});
|
|
169
168
|
|
|
170
169
|
it('should update multiple records, returning selected columns', async () => {
|
|
@@ -196,7 +195,7 @@ describe('update', () => {
|
|
|
196
195
|
expect(eq).toBe(true);
|
|
197
196
|
|
|
198
197
|
const updated = await User.take();
|
|
199
|
-
|
|
198
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
200
199
|
});
|
|
201
200
|
|
|
202
201
|
it('should update multiple records, returning all columns', async () => {
|
|
@@ -224,13 +223,13 @@ describe('update', () => {
|
|
|
224
223
|
);
|
|
225
224
|
|
|
226
225
|
const result = await query;
|
|
227
|
-
|
|
226
|
+
expect(result[0]).toMatchObject({ ...userData, ...update });
|
|
228
227
|
|
|
229
228
|
const eq: AssertEqual<typeof result, typeof User['type'][]> = true;
|
|
230
229
|
expect(eq).toBe(true);
|
|
231
230
|
|
|
232
231
|
const updated = await User.take();
|
|
233
|
-
|
|
232
|
+
expect(updated).toMatchObject({ ...userData, ...update });
|
|
234
233
|
});
|
|
235
234
|
|
|
236
235
|
it('should ignore undefined values, and should not ignore null', () => {
|
|
@@ -297,7 +296,7 @@ describe('update', () => {
|
|
|
297
296
|
const eq: AssertEqual<typeof result, typeof User.type> = true;
|
|
298
297
|
expect(eq).toBe(true);
|
|
299
298
|
|
|
300
|
-
|
|
299
|
+
expect(result).toMatchObject({ ...userData, ...update });
|
|
301
300
|
});
|
|
302
301
|
|
|
303
302
|
it('should throw when searching for one to update and it is not found', async () => {
|
|
@@ -29,7 +29,7 @@ export class QueryUpsert {
|
|
|
29
29
|
const { handleResult } = this.query;
|
|
30
30
|
this.query.handleResult = async (q, queryResult) => {
|
|
31
31
|
if (queryResult.rowCount === 0) {
|
|
32
|
-
return q.insert(data.create);
|
|
32
|
+
return (q as Query).insert(data.create as InsertData<Query>);
|
|
33
33
|
} else if (queryResult.rowCount > 1) {
|
|
34
34
|
throw new MoreThanOneRowError(
|
|
35
35
|
`Only one row was expected to find for upsert, found ${queryResult.rowCount} rows.`,
|