pqb 0.4.0 → 0.4.1
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 +56 -47
- package/dist/index.esm.js +139 -115
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +139 -115
- 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} +106 -98
- package/src/queryMethods/{insert.ts → create.ts} +129 -113
- 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/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,48 @@ 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('accepts only a query which returns one record');
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
266
307
|
describe('onConflict', () => {
|
|
267
308
|
it('should return special query builder and return previous after ignore or merge', () => {
|
|
268
309
|
const q = User.all();
|
|
269
310
|
|
|
270
|
-
const originalQuery = q.
|
|
311
|
+
const originalQuery = q.count().create(userData);
|
|
271
312
|
const onConflictQuery = q.onConflict();
|
|
272
313
|
expect(originalQuery instanceof OnConflictQueryBuilder).not.toBe(true);
|
|
273
314
|
expect(onConflictQuery instanceof OnConflictQueryBuilder).toBe(true);
|
|
@@ -287,7 +328,7 @@ describe('insert functions', () => {
|
|
|
287
328
|
|
|
288
329
|
const query = q
|
|
289
330
|
.select('id')
|
|
290
|
-
.
|
|
331
|
+
.create(userData)
|
|
291
332
|
.onConflict('name')
|
|
292
333
|
.ignore()
|
|
293
334
|
.where({ name: 'where name' });
|
|
@@ -312,7 +353,7 @@ describe('insert functions', () => {
|
|
|
312
353
|
it('should set `ON CONFLICT` to all columns if no arguments provided', () => {
|
|
313
354
|
const q = User.all();
|
|
314
355
|
|
|
315
|
-
const query = q.
|
|
356
|
+
const query = q.count().create(userData).onConflict().ignore();
|
|
316
357
|
expectSql(
|
|
317
358
|
query.toSql(),
|
|
318
359
|
`
|
|
@@ -330,7 +371,7 @@ describe('insert functions', () => {
|
|
|
330
371
|
it('should accept single column', () => {
|
|
331
372
|
const q = User.all();
|
|
332
373
|
|
|
333
|
-
const query = q.
|
|
374
|
+
const query = q.count().create(userData).onConflict('id').ignore();
|
|
334
375
|
expectSql(
|
|
335
376
|
query.toSql(),
|
|
336
377
|
`
|
|
@@ -347,7 +388,11 @@ describe('insert functions', () => {
|
|
|
347
388
|
it('should accept multiple columns', () => {
|
|
348
389
|
const q = User.all();
|
|
349
390
|
|
|
350
|
-
const query = q
|
|
391
|
+
const query = q
|
|
392
|
+
.count()
|
|
393
|
+
.create(userData)
|
|
394
|
+
.onConflict(['id', 'name'])
|
|
395
|
+
.ignore();
|
|
351
396
|
expectSql(
|
|
352
397
|
query.toSql(),
|
|
353
398
|
`
|
|
@@ -364,7 +409,11 @@ describe('insert functions', () => {
|
|
|
364
409
|
it('can accept raw query', () => {
|
|
365
410
|
const q = User.all();
|
|
366
411
|
|
|
367
|
-
const query = q
|
|
412
|
+
const query = q
|
|
413
|
+
.count()
|
|
414
|
+
.create(userData)
|
|
415
|
+
.onConflict(raw('raw query'))
|
|
416
|
+
.ignore();
|
|
368
417
|
expectSql(
|
|
369
418
|
query.toSql(),
|
|
370
419
|
`
|
|
@@ -383,7 +432,7 @@ describe('insert functions', () => {
|
|
|
383
432
|
it('should update all columns when calling without arguments', () => {
|
|
384
433
|
const q = User.all();
|
|
385
434
|
|
|
386
|
-
const query = q.
|
|
435
|
+
const query = q.count().create(userData).onConflict().merge();
|
|
387
436
|
expectSql(
|
|
388
437
|
query.toSql(),
|
|
389
438
|
`
|
|
@@ -403,7 +452,11 @@ describe('insert functions', () => {
|
|
|
403
452
|
it('should accept single column', () => {
|
|
404
453
|
const q = User.all();
|
|
405
454
|
|
|
406
|
-
const query = q
|
|
455
|
+
const query = q
|
|
456
|
+
.count()
|
|
457
|
+
.create(userData)
|
|
458
|
+
.onConflict('name')
|
|
459
|
+
.merge('name');
|
|
407
460
|
expectSql(
|
|
408
461
|
query.toSql(),
|
|
409
462
|
`
|
|
@@ -422,7 +475,8 @@ describe('insert functions', () => {
|
|
|
422
475
|
const q = User.all();
|
|
423
476
|
|
|
424
477
|
const query = q
|
|
425
|
-
.
|
|
478
|
+
.count()
|
|
479
|
+
.create(userData)
|
|
426
480
|
.onConflict(['name', 'password'])
|
|
427
481
|
.merge(['name', 'password']);
|
|
428
482
|
|
|
@@ -446,7 +500,8 @@ describe('insert functions', () => {
|
|
|
446
500
|
const q = User.all();
|
|
447
501
|
|
|
448
502
|
const query = q
|
|
449
|
-
.
|
|
503
|
+
.count()
|
|
504
|
+
.create(userData)
|
|
450
505
|
.onConflict('name')
|
|
451
506
|
.merge({ name: 'new name' });
|
|
452
507
|
|
|
@@ -468,7 +523,8 @@ describe('insert functions', () => {
|
|
|
468
523
|
const q = User.all();
|
|
469
524
|
|
|
470
525
|
const query = q
|
|
471
|
-
.
|
|
526
|
+
.count()
|
|
527
|
+
.create(userData)
|
|
472
528
|
.onConflict(raw('on conflict raw'))
|
|
473
529
|
.merge(raw('merge raw'));
|
|
474
530
|
|
|
@@ -487,52 +543,4 @@ describe('insert functions', () => {
|
|
|
487
543
|
});
|
|
488
544
|
});
|
|
489
545
|
});
|
|
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
546
|
});
|