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
package/package.json
CHANGED
|
@@ -102,7 +102,7 @@ describe('column base', () => {
|
|
|
102
102
|
|
|
103
103
|
describe('parsing columns', () => {
|
|
104
104
|
beforeEach(async () => {
|
|
105
|
-
await User.
|
|
105
|
+
await User.create(userData);
|
|
106
106
|
});
|
|
107
107
|
|
|
108
108
|
it('should return column data as returned from db if not set', async () => {
|
package/src/db.test.ts
CHANGED
|
@@ -28,7 +28,7 @@ describe('db', () => {
|
|
|
28
28
|
|
|
29
29
|
describe('overriding column types', () => {
|
|
30
30
|
it('should return date as string by default', async () => {
|
|
31
|
-
await User.
|
|
31
|
+
await User.create(userData);
|
|
32
32
|
|
|
33
33
|
const db = createDb({ adapter, columnTypes });
|
|
34
34
|
const table = db('user', (t) => ({
|
|
@@ -42,7 +42,7 @@ describe('db', () => {
|
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
it('should return date as Date when overridden', async () => {
|
|
45
|
-
await User.
|
|
45
|
+
await User.create(userData);
|
|
46
46
|
|
|
47
47
|
const db = createDb({
|
|
48
48
|
adapter,
|
package/src/errors.test.ts
CHANGED
|
@@ -19,7 +19,7 @@ describe('errors', () => {
|
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it('should have isUnique and column names map when violating unique error over single column', async () => {
|
|
22
|
-
await UniqueTable.
|
|
22
|
+
await UniqueTable.create({
|
|
23
23
|
one: 'one',
|
|
24
24
|
two: 1,
|
|
25
25
|
thirdColumn: 'three',
|
|
@@ -29,7 +29,7 @@ describe('errors', () => {
|
|
|
29
29
|
let err: InstanceType<typeof UniqueTable.error> | undefined;
|
|
30
30
|
|
|
31
31
|
try {
|
|
32
|
-
await UniqueTable.
|
|
32
|
+
await UniqueTable.create({
|
|
33
33
|
one: 'one',
|
|
34
34
|
two: 2,
|
|
35
35
|
thirdColumn: 'three',
|
|
@@ -48,7 +48,7 @@ describe('errors', () => {
|
|
|
48
48
|
});
|
|
49
49
|
|
|
50
50
|
it('should have isUnique and column names map when violating unique error over multiple columns', async () => {
|
|
51
|
-
await UniqueTable.
|
|
51
|
+
await UniqueTable.create({
|
|
52
52
|
one: 'one',
|
|
53
53
|
two: 1,
|
|
54
54
|
thirdColumn: 'three',
|
|
@@ -58,7 +58,7 @@ describe('errors', () => {
|
|
|
58
58
|
let err: QueryError | undefined;
|
|
59
59
|
|
|
60
60
|
try {
|
|
61
|
-
await UniqueTable.
|
|
61
|
+
await UniqueTable.create({
|
|
62
62
|
one: 'two',
|
|
63
63
|
two: 2,
|
|
64
64
|
thirdColumn: 'three',
|
|
@@ -133,7 +133,7 @@ describe('aggregate', () => {
|
|
|
133
133
|
|
|
134
134
|
describe('selectCount', () => {
|
|
135
135
|
it('should select number', async () => {
|
|
136
|
-
await User.
|
|
136
|
+
await User.create(userData);
|
|
137
137
|
|
|
138
138
|
const user = await User.selectCount().take();
|
|
139
139
|
expect(user.count).toBe(1);
|
|
@@ -161,7 +161,7 @@ describe('aggregate', () => {
|
|
|
161
161
|
});
|
|
162
162
|
|
|
163
163
|
it('should return number when have records', async () => {
|
|
164
|
-
await User.
|
|
164
|
+
await User.create(userData);
|
|
165
165
|
|
|
166
166
|
const value = await User[method as 'avg']('id');
|
|
167
167
|
|
|
@@ -183,7 +183,7 @@ describe('aggregate', () => {
|
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
it('should return number when have records', async () => {
|
|
186
|
-
const id = await User.get('id').
|
|
186
|
+
const id = await User.get('id').create(userData);
|
|
187
187
|
|
|
188
188
|
const value = await User[selectMethod]('id').take();
|
|
189
189
|
|
|
@@ -209,7 +209,7 @@ describe('aggregate', () => {
|
|
|
209
209
|
});
|
|
210
210
|
|
|
211
211
|
it('should return boolean when have records', async () => {
|
|
212
|
-
await User.
|
|
212
|
+
await User.create({ ...userData, active: true });
|
|
213
213
|
|
|
214
214
|
const value = await User[method as 'boolAnd']('active');
|
|
215
215
|
|
|
@@ -231,7 +231,7 @@ describe('aggregate', () => {
|
|
|
231
231
|
});
|
|
232
232
|
|
|
233
233
|
it('should return boolean when have records', async () => {
|
|
234
|
-
await User.
|
|
234
|
+
await User.create({ ...userData, active: true });
|
|
235
235
|
|
|
236
236
|
const value = await User[selectMethod]('active').take();
|
|
237
237
|
|
|
@@ -261,7 +261,7 @@ describe('aggregate', () => {
|
|
|
261
261
|
});
|
|
262
262
|
|
|
263
263
|
it('should return json array when have records', async () => {
|
|
264
|
-
await User.
|
|
264
|
+
await User.create({ ...userData, data });
|
|
265
265
|
|
|
266
266
|
const value = await User[method as 'jsonAgg']('data');
|
|
267
267
|
|
|
@@ -289,7 +289,7 @@ describe('aggregate', () => {
|
|
|
289
289
|
});
|
|
290
290
|
|
|
291
291
|
it('should return json array when have records', async () => {
|
|
292
|
-
await User.
|
|
292
|
+
await User.create({ ...userData, data });
|
|
293
293
|
|
|
294
294
|
const value = await User[selectMethod]('data').take();
|
|
295
295
|
|
|
@@ -380,7 +380,7 @@ describe('aggregate', () => {
|
|
|
380
380
|
});
|
|
381
381
|
|
|
382
382
|
it('should return json object when have records', async () => {
|
|
383
|
-
await User.
|
|
383
|
+
await User.create(userData);
|
|
384
384
|
|
|
385
385
|
const value = await User[method as 'jsonObjectAgg']({ alias: 'name' });
|
|
386
386
|
|
|
@@ -405,7 +405,7 @@ describe('aggregate', () => {
|
|
|
405
405
|
});
|
|
406
406
|
|
|
407
407
|
it('should return json object when have records', async () => {
|
|
408
|
-
await User.
|
|
408
|
+
await User.create(userData);
|
|
409
409
|
|
|
410
410
|
const value = await User[selectMethod]({ alias: 'name' }).take();
|
|
411
411
|
|
|
@@ -483,7 +483,7 @@ describe('aggregate', () => {
|
|
|
483
483
|
});
|
|
484
484
|
|
|
485
485
|
it('should return json object when have records', async () => {
|
|
486
|
-
await User.
|
|
486
|
+
await User.createMany([userData, userData]);
|
|
487
487
|
|
|
488
488
|
const value = await User.stringAgg('name', ', ');
|
|
489
489
|
|
|
@@ -502,7 +502,7 @@ describe('aggregate', () => {
|
|
|
502
502
|
});
|
|
503
503
|
|
|
504
504
|
it('should return json object when have records', async () => {
|
|
505
|
-
await User.
|
|
505
|
+
await User.createMany([userData, userData]);
|
|
506
506
|
|
|
507
507
|
const value = await User.selectStringAgg('name', ', ').take();
|
|
508
508
|
|
|
@@ -195,7 +195,7 @@ export class Aggregate {
|
|
|
195
195
|
this: T,
|
|
196
196
|
arg?: AT1<T>['count'] | '*',
|
|
197
197
|
options?: AggregateOptions<T>,
|
|
198
|
-
): SetQueryReturnsValue<T, NumberColumn> {
|
|
198
|
+
): SetQueryReturnsValue<T, NumberColumn> & { isCount: true } {
|
|
199
199
|
return this.clone()._count(arg, options);
|
|
200
200
|
}
|
|
201
201
|
|
|
@@ -203,8 +203,10 @@ export class Aggregate {
|
|
|
203
203
|
this: T,
|
|
204
204
|
arg: AT1<T>['count'] | '*' = '*',
|
|
205
205
|
options?: AggregateOptions<T>,
|
|
206
|
-
): SetQueryReturnsValue<T, NumberColumn> {
|
|
207
|
-
return get<T, NumberColumn>(
|
|
206
|
+
): SetQueryReturnsValue<T, NumberColumn> & { isCount: true } {
|
|
207
|
+
return get<T, NumberColumn>(
|
|
208
|
+
this._selectCount(arg, options),
|
|
209
|
+
) as unknown as SetQueryReturnsValue<T, NumberColumn> & { isCount: true };
|
|
208
210
|
}
|
|
209
211
|
|
|
210
212
|
selectCount<T extends Query, As extends string | undefined = undefined>(
|
|
@@ -23,20 +23,20 @@ describe('callbacks', () => {
|
|
|
23
23
|
});
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
describe('
|
|
27
|
-
it('should run callback before
|
|
26
|
+
describe('beforeCreate', () => {
|
|
27
|
+
it('should run callback before create', async () => {
|
|
28
28
|
const fn = jest.fn();
|
|
29
|
-
const query = User.
|
|
29
|
+
const query = User.beforeCreate(fn).create(userData);
|
|
30
30
|
await query;
|
|
31
31
|
|
|
32
32
|
expect(fn.mock.calls[0]).toEqual([query]);
|
|
33
33
|
});
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
describe('
|
|
37
|
-
it('should run callback after
|
|
36
|
+
describe('afterCreate', () => {
|
|
37
|
+
it('should run callback after create', async () => {
|
|
38
38
|
const fn = jest.fn();
|
|
39
|
-
const query = User.
|
|
39
|
+
const query = User.afterCreate(fn).select('id').create(userData);
|
|
40
40
|
const result = await query;
|
|
41
41
|
|
|
42
42
|
expect(fn.mock.calls[0]).toEqual([query, result]);
|
|
@@ -25,18 +25,18 @@ export class QueryCallbacks {
|
|
|
25
25
|
return pushQueryValue(this, 'afterQuery', cb);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
return this.clone().
|
|
28
|
+
beforeCreate<T extends Query>(this: T, cb: BeforeCallback<T>): T {
|
|
29
|
+
return this.clone()._beforeCreate(cb);
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
return pushQueryValue(this, '
|
|
31
|
+
_beforeCreate<T extends Query>(this: T, cb: BeforeCallback<T>): T {
|
|
32
|
+
return pushQueryValue(this, 'beforeCreate', cb);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
return this.clone().
|
|
35
|
+
afterCreate<T extends Query>(this: T, cb: AfterCallback<T>): T {
|
|
36
|
+
return this.clone()._afterCreate(cb);
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
return pushQueryValue(this, '
|
|
38
|
+
_afterCreate<T extends Query>(this: T, cb: AfterCallback<T>): T {
|
|
39
|
+
return pushQueryValue(this, 'afterCreate', cb);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
beforeUpdate<T extends Query>(this: T, cb: BeforeCallback<T>): T {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Query, SetQueryReturnsColumnInfo } from '../query';
|
|
2
|
-
import { ThenResult, Then } from './then';
|
|
3
2
|
import { ColumnInfoQueryData } from '../sql';
|
|
4
3
|
|
|
5
4
|
export type ColumnInfo = {
|
|
@@ -44,24 +43,19 @@ export class ColumnInfoMethods {
|
|
|
44
43
|
(this.query as ColumnInfoQueryData).column = column as string;
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
this.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
reject,
|
|
63
|
-
);
|
|
64
|
-
} as ThenResult<unknown>;
|
|
46
|
+
this.query.handleResult = async (_, result) => {
|
|
47
|
+
if (column) {
|
|
48
|
+
return rowToColumnInfo(result.rows[0]);
|
|
49
|
+
} else {
|
|
50
|
+
const info: Record<string, ColumnInfo> = {};
|
|
51
|
+
result.rows.forEach((row) => {
|
|
52
|
+
info[(row as { column_name: string }).column_name] =
|
|
53
|
+
rowToColumnInfo(row);
|
|
54
|
+
});
|
|
55
|
+
return info;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
65
59
|
return this as unknown as SetQueryReturnsColumnInfo<T, Column>;
|
|
66
60
|
}
|
|
67
61
|
}
|