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
|
@@ -1,68 +1,74 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assertType,
|
|
3
|
+
Chat,
|
|
3
4
|
expectQueryNotMutated,
|
|
4
5
|
expectSql,
|
|
6
|
+
Message,
|
|
7
|
+
MessageRecord,
|
|
5
8
|
User,
|
|
6
9
|
userData,
|
|
10
|
+
UserRecord,
|
|
7
11
|
useTestDatabase,
|
|
8
12
|
} from '../test-utils';
|
|
9
13
|
import { raw } from '../common';
|
|
10
|
-
import { OnConflictQueryBuilder } from './
|
|
14
|
+
import { OnConflictQueryBuilder } from './create';
|
|
11
15
|
|
|
12
|
-
describe('
|
|
16
|
+
describe('create functions', () => {
|
|
13
17
|
useTestDatabase();
|
|
14
18
|
|
|
15
|
-
describe('
|
|
16
|
-
it('should
|
|
19
|
+
describe('createRaw', () => {
|
|
20
|
+
it('should create with raw sql and list of columns', () => {
|
|
17
21
|
const q = User.all();
|
|
18
22
|
|
|
19
|
-
const query = q.
|
|
23
|
+
const query = q.createRaw({
|
|
20
24
|
columns: ['name', 'password'],
|
|
21
25
|
values: raw('raw sql'),
|
|
22
26
|
});
|
|
23
27
|
expectSql(
|
|
24
28
|
query.toSql(),
|
|
25
29
|
`
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
INSERT INTO "user"("name", "password")
|
|
31
|
+
VALUES raw sql
|
|
32
|
+
RETURNING *
|
|
33
|
+
`,
|
|
29
34
|
);
|
|
30
35
|
|
|
31
|
-
assertType<Awaited<typeof query>,
|
|
36
|
+
assertType<Awaited<typeof query>, UserRecord[]>();
|
|
32
37
|
|
|
33
38
|
expectQueryNotMutated(q);
|
|
34
39
|
});
|
|
35
40
|
});
|
|
36
41
|
|
|
37
|
-
describe('
|
|
38
|
-
it('should
|
|
42
|
+
describe('create', () => {
|
|
43
|
+
it('should create one record, returning record', async () => {
|
|
39
44
|
const q = User.all();
|
|
40
45
|
|
|
41
|
-
const query = q.
|
|
46
|
+
const query = q.create(userData);
|
|
42
47
|
expectSql(
|
|
43
48
|
query.toSql(),
|
|
44
49
|
`
|
|
45
50
|
INSERT INTO "user"("name", "password")
|
|
46
51
|
VALUES ($1, $2)
|
|
52
|
+
RETURNING *
|
|
47
53
|
`,
|
|
48
54
|
['name', 'password'],
|
|
49
55
|
);
|
|
50
56
|
|
|
51
57
|
const result = await query;
|
|
52
|
-
expect(result).
|
|
58
|
+
expect(result).toMatchObject(userData);
|
|
53
59
|
|
|
54
|
-
assertType<typeof result,
|
|
60
|
+
assertType<typeof result, UserRecord>();
|
|
55
61
|
|
|
56
|
-
const
|
|
57
|
-
expect(
|
|
62
|
+
const created = await User.take();
|
|
63
|
+
expect(created).toMatchObject(userData);
|
|
58
64
|
|
|
59
65
|
expectQueryNotMutated(q);
|
|
60
66
|
});
|
|
61
67
|
|
|
62
|
-
it('should
|
|
68
|
+
it('should create one record, returning value', async () => {
|
|
63
69
|
const q = User.all();
|
|
64
70
|
|
|
65
|
-
const query = q.get('id').
|
|
71
|
+
const query = q.get('id').create(userData);
|
|
66
72
|
expectSql(
|
|
67
73
|
query.toSql(),
|
|
68
74
|
`
|
|
@@ -81,10 +87,10 @@ describe('insert functions', () => {
|
|
|
81
87
|
expectQueryNotMutated(q);
|
|
82
88
|
});
|
|
83
89
|
|
|
84
|
-
it('should
|
|
90
|
+
it('should create one record, returning columns', async () => {
|
|
85
91
|
const q = User.all();
|
|
86
92
|
|
|
87
|
-
const query = q.select('id', 'name').
|
|
93
|
+
const query = q.select('id', 'name').create(userData);
|
|
88
94
|
expectSql(
|
|
89
95
|
query.toSql(),
|
|
90
96
|
`
|
|
@@ -105,51 +111,49 @@ describe('insert functions', () => {
|
|
|
105
111
|
expectQueryNotMutated(q);
|
|
106
112
|
});
|
|
107
113
|
|
|
108
|
-
it('should
|
|
114
|
+
it('should create one record, returning created count', async () => {
|
|
109
115
|
const q = User.all();
|
|
110
116
|
|
|
111
|
-
const query = q.
|
|
117
|
+
const query = q.count().create(userData);
|
|
112
118
|
expectSql(
|
|
113
119
|
query.toSql(),
|
|
114
120
|
`
|
|
115
121
|
INSERT INTO "user"("name", "password")
|
|
116
122
|
VALUES ($1, $2)
|
|
117
|
-
RETURNING *
|
|
118
123
|
`,
|
|
119
124
|
['name', 'password'],
|
|
120
125
|
);
|
|
121
126
|
|
|
122
127
|
const result = await query;
|
|
123
|
-
assertType<typeof result,
|
|
128
|
+
assertType<typeof result, number>();
|
|
124
129
|
|
|
125
|
-
|
|
126
|
-
const { password, ...other } = userData;
|
|
127
|
-
expect(result).toMatchObject(other);
|
|
130
|
+
expect(result).toBe(1);
|
|
128
131
|
|
|
129
132
|
expectQueryNotMutated(q);
|
|
130
133
|
});
|
|
131
134
|
|
|
132
|
-
it('should
|
|
135
|
+
it('should create record with provided defaults', () => {
|
|
133
136
|
const query = User.defaults({
|
|
134
137
|
name: 'name',
|
|
135
138
|
password: 'password',
|
|
136
|
-
}).
|
|
139
|
+
}).create({
|
|
137
140
|
password: 'override',
|
|
138
141
|
});
|
|
139
142
|
|
|
140
143
|
expectSql(
|
|
141
144
|
query.toSql(),
|
|
142
145
|
`
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
INSERT INTO "user"("name", "password")
|
|
147
|
+
VALUES ($1, $2)
|
|
148
|
+
RETURNING *
|
|
149
|
+
`,
|
|
146
150
|
['name', 'override'],
|
|
147
151
|
);
|
|
148
152
|
});
|
|
149
153
|
});
|
|
150
154
|
|
|
151
|
-
describe('
|
|
152
|
-
it('should
|
|
155
|
+
describe('createMany', () => {
|
|
156
|
+
it('should create many records, returning inserted count', async () => {
|
|
153
157
|
const q = User.all();
|
|
154
158
|
|
|
155
159
|
const arr = [
|
|
@@ -160,7 +164,7 @@ describe('insert functions', () => {
|
|
|
160
164
|
userData,
|
|
161
165
|
];
|
|
162
166
|
|
|
163
|
-
const query = q.
|
|
167
|
+
const query = q.count().createMany(arr);
|
|
164
168
|
|
|
165
169
|
expectSql(
|
|
166
170
|
query.toSql(),
|
|
@@ -186,7 +190,7 @@ describe('insert functions', () => {
|
|
|
186
190
|
expectQueryNotMutated(q);
|
|
187
191
|
});
|
|
188
192
|
|
|
189
|
-
it('should
|
|
193
|
+
it('should create many records, returning columns', async () => {
|
|
190
194
|
const q = User.all();
|
|
191
195
|
|
|
192
196
|
const arr = [
|
|
@@ -197,7 +201,7 @@ describe('insert functions', () => {
|
|
|
197
201
|
userData,
|
|
198
202
|
];
|
|
199
203
|
|
|
200
|
-
const query = q.select('id', 'name').
|
|
204
|
+
const query = q.select('id', 'name').createMany(arr);
|
|
201
205
|
|
|
202
206
|
expectSql(
|
|
203
207
|
query.toSql(),
|
|
@@ -222,7 +226,7 @@ describe('insert functions', () => {
|
|
|
222
226
|
expectQueryNotMutated(q);
|
|
223
227
|
});
|
|
224
228
|
|
|
225
|
-
it('should
|
|
229
|
+
it('should create many records, returning all columns', async () => {
|
|
226
230
|
const q = User.all();
|
|
227
231
|
|
|
228
232
|
const arr = [
|
|
@@ -233,7 +237,7 @@ describe('insert functions', () => {
|
|
|
233
237
|
userData,
|
|
234
238
|
];
|
|
235
239
|
|
|
236
|
-
const query = q.
|
|
240
|
+
const query = q.createMany(arr);
|
|
237
241
|
|
|
238
242
|
expectSql(
|
|
239
243
|
query.toSql(),
|
|
@@ -263,11 +267,50 @@ describe('insert functions', () => {
|
|
|
263
267
|
});
|
|
264
268
|
});
|
|
265
269
|
|
|
270
|
+
describe('createFrom', () => {
|
|
271
|
+
it('should create record from select', () => {
|
|
272
|
+
const query = Message.createFrom(Chat.find(1).select({ chatId: 'id' }), {
|
|
273
|
+
authorId: 1,
|
|
274
|
+
text: 'text',
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
assertType<Awaited<typeof query>, MessageRecord>();
|
|
278
|
+
|
|
279
|
+
expectSql(
|
|
280
|
+
query.toSql(),
|
|
281
|
+
`
|
|
282
|
+
INSERT INTO "message"("chatId", "authorId", "text")
|
|
283
|
+
SELECT "chat"."id" AS "chatId", $1, $2
|
|
284
|
+
FROM "chat"
|
|
285
|
+
WHERE "chat"."id" = $3
|
|
286
|
+
LIMIT $4
|
|
287
|
+
RETURNING *
|
|
288
|
+
`,
|
|
289
|
+
[1, 'text', 1, 1],
|
|
290
|
+
);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('should not allow to create from query which returns multiple records', () => {
|
|
294
|
+
expect(() =>
|
|
295
|
+
Message.createFrom(
|
|
296
|
+
// @ts-expect-error creating from multiple records is not allowed
|
|
297
|
+
Chat.where({ id: { in: [1, 2] } }).select({ chatId: 'id' }),
|
|
298
|
+
{
|
|
299
|
+
authorId: 1,
|
|
300
|
+
text: 'text',
|
|
301
|
+
},
|
|
302
|
+
),
|
|
303
|
+
).toThrow(
|
|
304
|
+
'Cannot create based on a query which returns multiple records',
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
266
309
|
describe('onConflict', () => {
|
|
267
310
|
it('should return special query builder and return previous after ignore or merge', () => {
|
|
268
311
|
const q = User.all();
|
|
269
312
|
|
|
270
|
-
const originalQuery = q.
|
|
313
|
+
const originalQuery = q.count().create(userData);
|
|
271
314
|
const onConflictQuery = q.onConflict();
|
|
272
315
|
expect(originalQuery instanceof OnConflictQueryBuilder).not.toBe(true);
|
|
273
316
|
expect(onConflictQuery instanceof OnConflictQueryBuilder).toBe(true);
|
|
@@ -287,7 +330,7 @@ describe('insert functions', () => {
|
|
|
287
330
|
|
|
288
331
|
const query = q
|
|
289
332
|
.select('id')
|
|
290
|
-
.
|
|
333
|
+
.create(userData)
|
|
291
334
|
.onConflict('name')
|
|
292
335
|
.ignore()
|
|
293
336
|
.where({ name: 'where name' });
|
|
@@ -312,7 +355,7 @@ describe('insert functions', () => {
|
|
|
312
355
|
it('should set `ON CONFLICT` to all columns if no arguments provided', () => {
|
|
313
356
|
const q = User.all();
|
|
314
357
|
|
|
315
|
-
const query = q.
|
|
358
|
+
const query = q.count().create(userData).onConflict().ignore();
|
|
316
359
|
expectSql(
|
|
317
360
|
query.toSql(),
|
|
318
361
|
`
|
|
@@ -330,7 +373,7 @@ describe('insert functions', () => {
|
|
|
330
373
|
it('should accept single column', () => {
|
|
331
374
|
const q = User.all();
|
|
332
375
|
|
|
333
|
-
const query = q.
|
|
376
|
+
const query = q.count().create(userData).onConflict('id').ignore();
|
|
334
377
|
expectSql(
|
|
335
378
|
query.toSql(),
|
|
336
379
|
`
|
|
@@ -347,7 +390,11 @@ describe('insert functions', () => {
|
|
|
347
390
|
it('should accept multiple columns', () => {
|
|
348
391
|
const q = User.all();
|
|
349
392
|
|
|
350
|
-
const query = q
|
|
393
|
+
const query = q
|
|
394
|
+
.count()
|
|
395
|
+
.create(userData)
|
|
396
|
+
.onConflict(['id', 'name'])
|
|
397
|
+
.ignore();
|
|
351
398
|
expectSql(
|
|
352
399
|
query.toSql(),
|
|
353
400
|
`
|
|
@@ -364,7 +411,11 @@ describe('insert functions', () => {
|
|
|
364
411
|
it('can accept raw query', () => {
|
|
365
412
|
const q = User.all();
|
|
366
413
|
|
|
367
|
-
const query = q
|
|
414
|
+
const query = q
|
|
415
|
+
.count()
|
|
416
|
+
.create(userData)
|
|
417
|
+
.onConflict(raw('raw query'))
|
|
418
|
+
.ignore();
|
|
368
419
|
expectSql(
|
|
369
420
|
query.toSql(),
|
|
370
421
|
`
|
|
@@ -383,7 +434,7 @@ describe('insert functions', () => {
|
|
|
383
434
|
it('should update all columns when calling without arguments', () => {
|
|
384
435
|
const q = User.all();
|
|
385
436
|
|
|
386
|
-
const query = q.
|
|
437
|
+
const query = q.count().create(userData).onConflict().merge();
|
|
387
438
|
expectSql(
|
|
388
439
|
query.toSql(),
|
|
389
440
|
`
|
|
@@ -403,7 +454,11 @@ describe('insert functions', () => {
|
|
|
403
454
|
it('should accept single column', () => {
|
|
404
455
|
const q = User.all();
|
|
405
456
|
|
|
406
|
-
const query = q
|
|
457
|
+
const query = q
|
|
458
|
+
.count()
|
|
459
|
+
.create(userData)
|
|
460
|
+
.onConflict('name')
|
|
461
|
+
.merge('name');
|
|
407
462
|
expectSql(
|
|
408
463
|
query.toSql(),
|
|
409
464
|
`
|
|
@@ -422,7 +477,8 @@ describe('insert functions', () => {
|
|
|
422
477
|
const q = User.all();
|
|
423
478
|
|
|
424
479
|
const query = q
|
|
425
|
-
.
|
|
480
|
+
.count()
|
|
481
|
+
.create(userData)
|
|
426
482
|
.onConflict(['name', 'password'])
|
|
427
483
|
.merge(['name', 'password']);
|
|
428
484
|
|
|
@@ -446,7 +502,8 @@ describe('insert functions', () => {
|
|
|
446
502
|
const q = User.all();
|
|
447
503
|
|
|
448
504
|
const query = q
|
|
449
|
-
.
|
|
505
|
+
.count()
|
|
506
|
+
.create(userData)
|
|
450
507
|
.onConflict('name')
|
|
451
508
|
.merge({ name: 'new name' });
|
|
452
509
|
|
|
@@ -468,7 +525,8 @@ describe('insert functions', () => {
|
|
|
468
525
|
const q = User.all();
|
|
469
526
|
|
|
470
527
|
const query = q
|
|
471
|
-
.
|
|
528
|
+
.count()
|
|
529
|
+
.create(userData)
|
|
472
530
|
.onConflict(raw('on conflict raw'))
|
|
473
531
|
.merge(raw('merge raw'));
|
|
474
532
|
|
|
@@ -487,52 +545,4 @@ describe('insert functions', () => {
|
|
|
487
545
|
});
|
|
488
546
|
});
|
|
489
547
|
});
|
|
490
|
-
|
|
491
|
-
describe('create functions', () => {
|
|
492
|
-
describe('create', () => {
|
|
493
|
-
it('should return full record', async () => {
|
|
494
|
-
const result = await User.create(userData);
|
|
495
|
-
expect(result).toMatchObject(userData);
|
|
496
|
-
|
|
497
|
-
assertType<typeof result, typeof User.type>();
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
it('should return columns from select', async () => {
|
|
501
|
-
const result = await User.select('id', 'name').create(userData);
|
|
502
|
-
expect(result).toEqual({
|
|
503
|
-
id: result.id,
|
|
504
|
-
name: userData.name,
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
assertType<typeof result, { id: number; name: string }>();
|
|
508
|
-
});
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
describe('createMany', () => {
|
|
512
|
-
it('should return full records', async () => {
|
|
513
|
-
const result = await User.createMany([userData, userData]);
|
|
514
|
-
expect(result[0]).toMatchObject(userData);
|
|
515
|
-
expect(result[1]).toMatchObject(userData);
|
|
516
|
-
|
|
517
|
-
assertType<typeof result, typeof User.type[]>();
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
it('should return columns from select', async () => {
|
|
521
|
-
const result = await User.select('id', 'name').createMany([
|
|
522
|
-
userData,
|
|
523
|
-
userData,
|
|
524
|
-
]);
|
|
525
|
-
expect(result[0]).toEqual({
|
|
526
|
-
id: result[0].id,
|
|
527
|
-
name: userData.name,
|
|
528
|
-
});
|
|
529
|
-
expect(result[1]).toEqual({
|
|
530
|
-
id: result[1].id,
|
|
531
|
-
name: userData.name,
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
assertType<typeof result, { id: number; name: string }[]>();
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
});
|
|
538
548
|
});
|