pqb 0.4.6 → 0.4.8
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 -11
- package/dist/index.esm.js +78 -40
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +79 -39
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.test.ts +220 -0
- package/src/columnSchema/columnType.ts +7 -0
- package/src/columnSchema/columnTypes.test.ts +55 -53
- package/src/columnSchema/dateTime.ts +11 -0
- package/src/columnSchema/timestamps.test.ts +3 -4
- package/src/columnSchema/timestamps.ts +1 -1
- package/src/columnsOperators.test.ts +18 -19
- package/src/columnsOperators.ts +1 -1
- package/src/common.ts +17 -30
- package/src/db.ts +23 -9
- package/src/errors.test.ts +2 -4
- package/src/index.ts +1 -1
- package/src/query.ts +7 -1
- package/src/queryMethods/aggregate.test.ts +9 -7
- package/src/queryMethods/create.test.ts +48 -5
- package/src/queryMethods/create.ts +28 -6
- package/src/queryMethods/for.test.ts +1 -2
- package/src/queryMethods/for.ts +1 -1
- package/src/queryMethods/from.test.ts +2 -3
- package/src/queryMethods/get.test.ts +5 -5
- package/src/queryMethods/having.test.ts +6 -5
- package/src/queryMethods/index.ts +1 -0
- package/src/queryMethods/json.ts +2 -2
- package/src/queryMethods/merge.test.ts +10 -4
- package/src/queryMethods/queryMethods.test.ts +10 -12
- package/src/queryMethods/queryMethods.ts +7 -4
- package/src/queryMethods/raw.test.ts +19 -0
- package/src/queryMethods/raw.ts +27 -0
- package/src/queryMethods/select.test.ts +4 -4
- package/src/queryMethods/then.test.ts +2 -4
- package/src/queryMethods/union.test.ts +11 -6
- package/src/queryMethods/update.test.ts +21 -3
- package/src/queryMethods/update.ts +11 -1
- package/src/queryMethods/where.test.ts +65 -56
- package/src/queryMethods/where.ts +1 -1
- package/src/queryMethods/with.test.ts +5 -6
- package/src/sql/common.ts +0 -1
- package/src/sql/fromAndAs.ts +1 -1
- package/src/sql/insert.ts +1 -1
- package/src/sql/join.ts +1 -1
- package/src/sql/orderBy.ts +1 -1
- package/src/sql/select.ts +1 -2
- package/src/sql/toSql.ts +1 -1
- package/src/sql/update.ts +1 -1
- package/src/sql/where.ts +1 -1
- package/src/sql/window.ts +3 -4
- package/src/sql/with.ts +1 -1
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { db, expectSql, now, useTestDatabase } from '../test-utils';
|
|
2
|
-
import { raw } from '../common';
|
|
3
2
|
|
|
4
3
|
describe('timestamps', () => {
|
|
5
4
|
useTestDatabase();
|
|
@@ -37,7 +36,7 @@ describe('timestamps', () => {
|
|
|
37
36
|
});
|
|
38
37
|
|
|
39
38
|
it('should update updatedAt when updating with raw sql', async () => {
|
|
40
|
-
const query = model.updateRaw(raw('name = $1', 'name'), true);
|
|
39
|
+
const query = model.updateRaw(db.raw('name = $1', 'name'), true);
|
|
41
40
|
await query;
|
|
42
41
|
|
|
43
42
|
expectSql(
|
|
@@ -51,7 +50,7 @@ describe('timestamps', () => {
|
|
|
51
50
|
});
|
|
52
51
|
|
|
53
52
|
it('should update updatedAt when updating with raw sql which has updatedAt somewhere but not in set', async () => {
|
|
54
|
-
const query = model.updateRaw(raw('"createdAt" = "updatedAt"'), true);
|
|
53
|
+
const query = model.updateRaw(db.raw('"createdAt" = "updatedAt"'), true);
|
|
55
54
|
await query;
|
|
56
55
|
|
|
57
56
|
expectSql(
|
|
@@ -64,7 +63,7 @@ describe('timestamps', () => {
|
|
|
64
63
|
});
|
|
65
64
|
|
|
66
65
|
it('should not update updatedAt column when updating with raw sql which contains `updatedAt = `', async () => {
|
|
67
|
-
const query = model.updateRaw(raw('"updatedAt" = $1', now), true);
|
|
66
|
+
const query = model.updateRaw(db.raw('"updatedAt" = $1', now), true);
|
|
68
67
|
await query;
|
|
69
68
|
|
|
70
69
|
expectSql(
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ColumnType } from './columnType';
|
|
2
|
-
import { getRawSql, isRaw, raw } from '../common';
|
|
3
2
|
import { Query } from '../query';
|
|
4
3
|
import { makeRegexToFindInSql, pushOrNewArrayToObject } from '../utils';
|
|
5
4
|
import {
|
|
@@ -7,6 +6,7 @@ import {
|
|
|
7
6
|
UpdateQueryData,
|
|
8
7
|
UpdateQueryDataItem,
|
|
9
8
|
} from '../sql';
|
|
9
|
+
import { getRawSql, isRaw, raw } from '../common';
|
|
10
10
|
|
|
11
11
|
export function timestamps<T extends ColumnType>(this: {
|
|
12
12
|
timestamp(): T;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { expectSql, User } from './test-utils';
|
|
2
|
-
import { raw } from './common';
|
|
1
|
+
import { db, expectSql, User } from './test-utils';
|
|
3
2
|
|
|
4
3
|
describe('operators', () => {
|
|
5
4
|
describe('equals', () => {
|
|
@@ -27,7 +26,7 @@ describe('operators', () => {
|
|
|
27
26
|
|
|
28
27
|
it('should handle raw query', () => {
|
|
29
28
|
expectSql(
|
|
30
|
-
User.where({ name: { equals: raw("'name'") } }).toSql(),
|
|
29
|
+
User.where({ name: { equals: db.raw("'name'") } }).toSql(),
|
|
31
30
|
`
|
|
32
31
|
SELECT * FROM "user"
|
|
33
32
|
WHERE "user"."name" = 'name'
|
|
@@ -61,7 +60,7 @@ describe('operators', () => {
|
|
|
61
60
|
|
|
62
61
|
it('should handle raw query', () => {
|
|
63
62
|
expectSql(
|
|
64
|
-
User.where({ name: { not: raw("'name'") } }).toSql(),
|
|
63
|
+
User.where({ name: { not: db.raw("'name'") } }).toSql(),
|
|
65
64
|
`
|
|
66
65
|
SELECT * FROM "user"
|
|
67
66
|
WHERE "user"."name" <> 'name'
|
|
@@ -94,7 +93,7 @@ describe('operators', () => {
|
|
|
94
93
|
|
|
95
94
|
it('should handle raw query', () => {
|
|
96
95
|
expectSql(
|
|
97
|
-
User.where({ name: { in: raw("('a', 'b')") } }).toSql(),
|
|
96
|
+
User.where({ name: { in: db.raw("('a', 'b')") } }).toSql(),
|
|
98
97
|
`
|
|
99
98
|
SELECT * FROM "user"
|
|
100
99
|
WHERE "user"."name" IN ('a', 'b')
|
|
@@ -127,7 +126,7 @@ describe('operators', () => {
|
|
|
127
126
|
|
|
128
127
|
it('should handle raw query', () => {
|
|
129
128
|
expectSql(
|
|
130
|
-
User.where({ name: { notIn: raw("('a', 'b')") } }).toSql(),
|
|
129
|
+
User.where({ name: { notIn: db.raw("('a', 'b')") } }).toSql(),
|
|
131
130
|
`
|
|
132
131
|
SELECT * FROM "user"
|
|
133
132
|
WHERE NOT "user"."name" IN ('a', 'b')
|
|
@@ -161,7 +160,7 @@ describe('operators', () => {
|
|
|
161
160
|
|
|
162
161
|
it('should handle raw query', () => {
|
|
163
162
|
expectSql(
|
|
164
|
-
User.where({ id: { lt: raw('5') } }).toSql(),
|
|
163
|
+
User.where({ id: { lt: db.raw('5') } }).toSql(),
|
|
165
164
|
`
|
|
166
165
|
SELECT * FROM "user"
|
|
167
166
|
WHERE "user"."id" < 5
|
|
@@ -195,7 +194,7 @@ describe('operators', () => {
|
|
|
195
194
|
|
|
196
195
|
it('should handle raw query', () => {
|
|
197
196
|
expectSql(
|
|
198
|
-
User.where({ id: { lte: raw('5') } }).toSql(),
|
|
197
|
+
User.where({ id: { lte: db.raw('5') } }).toSql(),
|
|
199
198
|
`
|
|
200
199
|
SELECT * FROM "user"
|
|
201
200
|
WHERE "user"."id" <= 5
|
|
@@ -229,7 +228,7 @@ describe('operators', () => {
|
|
|
229
228
|
|
|
230
229
|
it('should handle raw query', () => {
|
|
231
230
|
expectSql(
|
|
232
|
-
User.where({ id: { gt: raw('5') } }).toSql(),
|
|
231
|
+
User.where({ id: { gt: db.raw('5') } }).toSql(),
|
|
233
232
|
`
|
|
234
233
|
SELECT * FROM "user"
|
|
235
234
|
WHERE "user"."id" > 5
|
|
@@ -263,7 +262,7 @@ describe('operators', () => {
|
|
|
263
262
|
|
|
264
263
|
it('should handle raw query', () => {
|
|
265
264
|
expectSql(
|
|
266
|
-
User.where({ id: { gte: raw('5') } }).toSql(),
|
|
265
|
+
User.where({ id: { gte: db.raw('5') } }).toSql(),
|
|
267
266
|
`
|
|
268
267
|
SELECT * FROM "user"
|
|
269
268
|
WHERE "user"."id" >= 5
|
|
@@ -297,7 +296,7 @@ describe('operators', () => {
|
|
|
297
296
|
|
|
298
297
|
it('should handle raw query', () => {
|
|
299
298
|
expectSql(
|
|
300
|
-
User.where({ name: { contains: raw("'ko'") } }).toSql(),
|
|
299
|
+
User.where({ name: { contains: db.raw("'ko'") } }).toSql(),
|
|
301
300
|
`
|
|
302
301
|
SELECT * FROM "user"
|
|
303
302
|
WHERE "user"."name" LIKE '%' || 'ko' || '%'
|
|
@@ -333,7 +332,7 @@ describe('operators', () => {
|
|
|
333
332
|
|
|
334
333
|
it('should handle raw query', () => {
|
|
335
334
|
expectSql(
|
|
336
|
-
User.where({ name: { containsInsensitive: raw("'ko'") } }).toSql(),
|
|
335
|
+
User.where({ name: { containsInsensitive: db.raw("'ko'") } }).toSql(),
|
|
337
336
|
`
|
|
338
337
|
SELECT * FROM "user"
|
|
339
338
|
WHERE "user"."name" ILIKE '%' || 'ko' || '%'
|
|
@@ -369,7 +368,7 @@ describe('operators', () => {
|
|
|
369
368
|
|
|
370
369
|
it('should handle raw query', () => {
|
|
371
370
|
expectSql(
|
|
372
|
-
User.where({ name: { startsWith: raw("'ko'") } }).toSql(),
|
|
371
|
+
User.where({ name: { startsWith: db.raw("'ko'") } }).toSql(),
|
|
373
372
|
`
|
|
374
373
|
SELECT * FROM "user"
|
|
375
374
|
WHERE "user"."name" LIKE 'ko' || '%'
|
|
@@ -405,7 +404,7 @@ describe('operators', () => {
|
|
|
405
404
|
|
|
406
405
|
it('should handle raw query', () => {
|
|
407
406
|
expectSql(
|
|
408
|
-
User.where({ name: { startsWithInsensitive: raw("'ko'") } }).toSql(),
|
|
407
|
+
User.where({ name: { startsWithInsensitive: db.raw("'ko'") } }).toSql(),
|
|
409
408
|
`
|
|
410
409
|
SELECT * FROM "user"
|
|
411
410
|
WHERE "user"."name" ILIKE 'ko' || '%'
|
|
@@ -441,7 +440,7 @@ describe('operators', () => {
|
|
|
441
440
|
|
|
442
441
|
it('should handle raw query', () => {
|
|
443
442
|
expectSql(
|
|
444
|
-
User.where({ name: { endsWith: raw("'ko'") } }).toSql(),
|
|
443
|
+
User.where({ name: { endsWith: db.raw("'ko'") } }).toSql(),
|
|
445
444
|
`
|
|
446
445
|
SELECT * FROM "user"
|
|
447
446
|
WHERE "user"."name" LIKE '%' || 'ko'
|
|
@@ -477,7 +476,7 @@ describe('operators', () => {
|
|
|
477
476
|
|
|
478
477
|
it('should handle raw query', () => {
|
|
479
478
|
expectSql(
|
|
480
|
-
User.where({ name: { endsWithInsensitive: raw("'ko'") } }).toSql(),
|
|
479
|
+
User.where({ name: { endsWithInsensitive: db.raw("'ko'") } }).toSql(),
|
|
481
480
|
`
|
|
482
481
|
SELECT * FROM "user"
|
|
483
482
|
WHERE "user"."name" ILIKE '%' || 'ko'
|
|
@@ -515,7 +514,7 @@ describe('operators', () => {
|
|
|
515
514
|
|
|
516
515
|
it('should handle raw query', () => {
|
|
517
516
|
expectSql(
|
|
518
|
-
User.where({ id: { between: [raw('1'), raw('10')] } }).toSql(),
|
|
517
|
+
User.where({ id: { between: [db.raw('1'), db.raw('10')] } }).toSql(),
|
|
519
518
|
`
|
|
520
519
|
SELECT * FROM "user"
|
|
521
520
|
WHERE "user"."id" BETWEEN 1 AND 10
|
|
@@ -554,7 +553,7 @@ describe('operators', () => {
|
|
|
554
553
|
it('should handle raw query', () => {
|
|
555
554
|
expectSql(
|
|
556
555
|
User.where({
|
|
557
|
-
data: { jsonPath: ['$.name', '=', raw("'name'")] },
|
|
556
|
+
data: { jsonPath: ['$.name', '=', db.raw("'name'")] },
|
|
558
557
|
}).toSql(),
|
|
559
558
|
`
|
|
560
559
|
SELECT * FROM "user"
|
|
@@ -596,7 +595,7 @@ describe('operators', () => {
|
|
|
596
595
|
it('should handle raw query', () => {
|
|
597
596
|
expectSql(
|
|
598
597
|
User.where({
|
|
599
|
-
data: { [method]: raw(`'{"a":"b"}'`) },
|
|
598
|
+
data: { [method]: db.raw(`'{"a":"b"}'`) },
|
|
600
599
|
}).toSql(),
|
|
601
600
|
`
|
|
602
601
|
SELECT * FROM "user"
|
package/src/columnsOperators.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Query } from './query';
|
|
2
|
-
import { getRaw, isRaw, RawExpression } from './common';
|
|
3
2
|
import { quote } from './quote';
|
|
4
3
|
import { addValue } from './sql/common';
|
|
4
|
+
import { getRaw, isRaw, RawExpression } from './common';
|
|
5
5
|
|
|
6
6
|
type Fn<T> = (key: string, value: T, values: unknown[]) => string;
|
|
7
7
|
|
package/src/common.ts
CHANGED
|
@@ -16,6 +16,23 @@ export type RawExpression<C extends ColumnType = ColumnType> = {
|
|
|
16
16
|
__column: C;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
export const raw = (sql: string, ...values: unknown[]): RawExpression =>
|
|
20
|
+
({
|
|
21
|
+
__raw: sql,
|
|
22
|
+
__values: values,
|
|
23
|
+
} as RawExpression);
|
|
24
|
+
|
|
25
|
+
export const isRaw = (obj: object): obj is RawExpression => '__raw' in obj;
|
|
26
|
+
|
|
27
|
+
export const getRaw = (raw: RawExpression, values: unknown[]) => {
|
|
28
|
+
values.push(...raw.__values);
|
|
29
|
+
return raw.__raw;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const getRawSql = (raw: RawExpression) => {
|
|
33
|
+
return raw.__raw;
|
|
34
|
+
};
|
|
35
|
+
|
|
19
36
|
export type Expression<
|
|
20
37
|
T extends Query = Query,
|
|
21
38
|
C extends ColumnType = ColumnType,
|
|
@@ -55,36 +72,6 @@ export type ExpressionOutput<
|
|
|
55
72
|
? ColumnType
|
|
56
73
|
: never;
|
|
57
74
|
|
|
58
|
-
export const raw = <C extends ColumnType>(
|
|
59
|
-
...args:
|
|
60
|
-
| [column: C, sql: string, ...values: unknown[]]
|
|
61
|
-
| [sql: string, ...values: unknown[]]
|
|
62
|
-
) => {
|
|
63
|
-
if (typeof args[0] === 'string') {
|
|
64
|
-
return {
|
|
65
|
-
__raw: args[0],
|
|
66
|
-
__values: args.slice(1),
|
|
67
|
-
} as RawExpression<C>;
|
|
68
|
-
} else {
|
|
69
|
-
return {
|
|
70
|
-
__column: args[0],
|
|
71
|
-
__raw: args[1],
|
|
72
|
-
__values: args.slice(2),
|
|
73
|
-
} as RawExpression<C>;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const isRaw = (obj: object): obj is RawExpression => '__raw' in obj;
|
|
78
|
-
|
|
79
|
-
export const getRaw = (raw: RawExpression, values: unknown[]) => {
|
|
80
|
-
values.push(...raw.__values);
|
|
81
|
-
return raw.__raw;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export const getRawSql = (raw: RawExpression) => {
|
|
85
|
-
return raw.__raw;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
75
|
export const EMPTY_OBJECT = {};
|
|
89
76
|
|
|
90
77
|
export const getQueryParsers = (q: Query) => {
|
package/src/db.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
ColumnTypesBase,
|
|
23
23
|
getColumnTypes,
|
|
24
24
|
SinglePrimaryKey,
|
|
25
|
+
ColumnType,
|
|
25
26
|
} from './columnSchema';
|
|
26
27
|
import { applyMixins, pushOrNewArray } from './utils';
|
|
27
28
|
import { StringKey } from './common';
|
|
@@ -35,10 +36,11 @@ export interface Db<
|
|
|
35
36
|
Table extends string | undefined = undefined,
|
|
36
37
|
Shape extends ColumnsShape = Record<string, never>,
|
|
37
38
|
Relations extends Query['relations'] = Query['relations'],
|
|
39
|
+
CT extends ColumnTypesBase = ColumnTypesBase,
|
|
38
40
|
> extends QueryMethods {
|
|
39
41
|
new (
|
|
40
42
|
adapter: Adapter,
|
|
41
|
-
queryBuilder: Db,
|
|
43
|
+
queryBuilder: Db<Table, Shape, Relations, CT>,
|
|
42
44
|
table?: Table,
|
|
43
45
|
shape?: Shape,
|
|
44
46
|
options?: DbTableOptions,
|
|
@@ -48,6 +50,7 @@ export interface Db<
|
|
|
48
50
|
columns: (keyof ColumnShapeOutput<Shape>)[];
|
|
49
51
|
|
|
50
52
|
queryBuilder: Db;
|
|
53
|
+
columnTypes: CT;
|
|
51
54
|
whereQueryBuilder: Query['whereQueryBuilder'];
|
|
52
55
|
onQueryBuilder: Query['onQueryBuilder'];
|
|
53
56
|
table: Table;
|
|
@@ -91,10 +94,13 @@ export interface Db<
|
|
|
91
94
|
>;
|
|
92
95
|
}
|
|
93
96
|
|
|
97
|
+
export const anyShape = {} as Record<string, ColumnType>;
|
|
98
|
+
|
|
94
99
|
export class Db<
|
|
95
100
|
Table extends string | undefined = undefined,
|
|
96
101
|
Shape extends ColumnsShape = Record<string, never>,
|
|
97
102
|
Relations extends Query['relations'] = Query['relations'],
|
|
103
|
+
CT extends ColumnTypesBase = ColumnTypesBase,
|
|
98
104
|
> implements Query
|
|
99
105
|
{
|
|
100
106
|
whereQueryBuilder = WhereQueryBuilder;
|
|
@@ -104,10 +110,11 @@ export class Db<
|
|
|
104
110
|
public adapter: Adapter,
|
|
105
111
|
public queryBuilder: Db,
|
|
106
112
|
public table: Table = undefined as Table,
|
|
107
|
-
public shape: Shape =
|
|
113
|
+
public shape: Shape = anyShape as Shape,
|
|
114
|
+
public columnTypes: CT,
|
|
108
115
|
options: DbTableOptions,
|
|
109
116
|
) {
|
|
110
|
-
this.__model = this;
|
|
117
|
+
this.__model = this as Query;
|
|
111
118
|
|
|
112
119
|
const logger = options.logger || console;
|
|
113
120
|
this.query = {
|
|
@@ -181,7 +188,12 @@ export class Db<
|
|
|
181
188
|
applyMixins(Db, [QueryMethods]);
|
|
182
189
|
Db.prototype.constructor = Db;
|
|
183
190
|
|
|
184
|
-
type DbResult<CT extends ColumnTypesBase> = Db
|
|
191
|
+
type DbResult<CT extends ColumnTypesBase> = Db<
|
|
192
|
+
string,
|
|
193
|
+
Record<string, never>,
|
|
194
|
+
Query['relations'],
|
|
195
|
+
CT
|
|
196
|
+
> & {
|
|
185
197
|
<Table extends string, Shape extends ColumnsShape = ColumnsShape>(
|
|
186
198
|
table: Table,
|
|
187
199
|
shape?: ((t: CT) => Shape) | Shape,
|
|
@@ -214,21 +226,23 @@ export const createDb = <CT extends ColumnTypesBase>({
|
|
|
214
226
|
undefined as unknown as Db,
|
|
215
227
|
undefined,
|
|
216
228
|
{},
|
|
229
|
+
ct,
|
|
217
230
|
commonOptions,
|
|
218
231
|
);
|
|
219
|
-
qb.queryBuilder = qb;
|
|
232
|
+
qb.queryBuilder = qb as unknown as Db;
|
|
220
233
|
|
|
221
234
|
const db = Object.assign(
|
|
222
235
|
<Table extends string, Shape extends ColumnsShape = ColumnsShape>(
|
|
223
236
|
table: Table,
|
|
224
237
|
shape?: ((t: CT) => Shape) | Shape,
|
|
225
238
|
options?: DbTableOptions,
|
|
226
|
-
): Db<Table, Shape> => {
|
|
227
|
-
return new Db<Table, Shape>(
|
|
239
|
+
): Db<Table, Shape, Query['relations'], CT> => {
|
|
240
|
+
return new Db<Table, Shape, Query['relations'], CT>(
|
|
228
241
|
adapter,
|
|
229
|
-
qb,
|
|
242
|
+
qb as unknown as Db,
|
|
230
243
|
table as Table,
|
|
231
244
|
typeof shape === 'function' ? getColumnTypes(ct, shape) : shape,
|
|
245
|
+
ct,
|
|
232
246
|
{ ...commonOptions, ...options },
|
|
233
247
|
);
|
|
234
248
|
},
|
|
@@ -242,5 +256,5 @@ export const createDb = <CT extends ColumnTypesBase>({
|
|
|
242
256
|
Db.prototype[name as keyof typeof Db.prototype];
|
|
243
257
|
});
|
|
244
258
|
|
|
245
|
-
return db as DbResult<CT>;
|
|
259
|
+
return db as unknown as DbResult<CT>;
|
|
246
260
|
};
|
package/src/errors.test.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { UniqueTable, User, useTestDatabase } from './test-utils';
|
|
2
|
-
import { raw } from './common';
|
|
3
|
-
import { columnTypes } from './columnSchema';
|
|
1
|
+
import { db, UniqueTable, User, useTestDatabase } from './test-utils';
|
|
4
2
|
import { QueryError } from './errors';
|
|
5
3
|
|
|
6
4
|
describe('errors', () => {
|
|
@@ -10,7 +8,7 @@ describe('errors', () => {
|
|
|
10
8
|
let err: Error | undefined;
|
|
11
9
|
|
|
12
10
|
try {
|
|
13
|
-
await User.select({ column: raw(
|
|
11
|
+
await User.select({ column: db.raw((t) => t.boolean(), 'koko') });
|
|
14
12
|
} catch (error) {
|
|
15
13
|
err = error as Error;
|
|
16
14
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from './sql';
|
|
|
3
3
|
export * from './adapter';
|
|
4
4
|
export * from './common';
|
|
5
5
|
export * from './db';
|
|
6
|
-
export * from './columnsOperators';
|
|
7
6
|
export * from './query';
|
|
8
7
|
export * from './queryMethods';
|
|
9
8
|
export * from './quote';
|
|
@@ -11,3 +10,4 @@ export * from './utils';
|
|
|
11
10
|
export * from './queryDataUtils';
|
|
12
11
|
export * from './errors';
|
|
13
12
|
export * from './relations';
|
|
13
|
+
export * from './columnsOperators';
|
package/src/query.ts
CHANGED
|
@@ -8,7 +8,12 @@ import {
|
|
|
8
8
|
getValueKey,
|
|
9
9
|
} from './queryMethods';
|
|
10
10
|
import { QueryData } from './sql';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
ColumnShapeOutput,
|
|
13
|
+
ColumnsShape,
|
|
14
|
+
ColumnType,
|
|
15
|
+
ColumnTypesBase,
|
|
16
|
+
} from './columnSchema';
|
|
12
17
|
import { EmptyObject, Spread } from './utils';
|
|
13
18
|
import { AliasOrTable, RawExpression, StringKey } from './common';
|
|
14
19
|
import { Db } from './db';
|
|
@@ -43,6 +48,7 @@ export const defaultsKey: unique symbol = Symbol('defaults');
|
|
|
43
48
|
|
|
44
49
|
export type Query = QueryMethods & {
|
|
45
50
|
queryBuilder: Db;
|
|
51
|
+
columnTypes: ColumnTypesBase;
|
|
46
52
|
whereQueryBuilder: typeof WhereQueryBuilder;
|
|
47
53
|
onQueryBuilder: typeof OnQueryBuilder;
|
|
48
54
|
table?: string;
|
|
@@ -5,8 +5,8 @@ import {
|
|
|
5
5
|
useTestDatabase,
|
|
6
6
|
userData,
|
|
7
7
|
assertType,
|
|
8
|
+
db,
|
|
8
9
|
} from '../test-utils';
|
|
9
|
-
import { raw } from '../common';
|
|
10
10
|
|
|
11
11
|
describe('aggregate', () => {
|
|
12
12
|
useTestDatabase();
|
|
@@ -340,7 +340,7 @@ describe('aggregate', () => {
|
|
|
340
340
|
|
|
341
341
|
it('should support raw sql parameter', () => {
|
|
342
342
|
const q = User.all();
|
|
343
|
-
expectSql(q[method as 'count'](raw('name')).toSql(), getSql('name'));
|
|
343
|
+
expectSql(q[method as 'count'](db.raw('name')).toSql(), getSql('name'));
|
|
344
344
|
expectQueryNotMutated(q);
|
|
345
345
|
});
|
|
346
346
|
|
|
@@ -359,7 +359,9 @@ describe('aggregate', () => {
|
|
|
359
359
|
const q = User.all();
|
|
360
360
|
const expectedSql = getSql('name', 'name');
|
|
361
361
|
expectSql(
|
|
362
|
-
q[selectMethod as 'selectCount'](raw('name'), {
|
|
362
|
+
q[selectMethod as 'selectCount'](db.raw('name'), {
|
|
363
|
+
as: 'name',
|
|
364
|
+
}).toSql(),
|
|
363
365
|
expectedSql,
|
|
364
366
|
);
|
|
365
367
|
expectQueryNotMutated(q);
|
|
@@ -436,7 +438,7 @@ describe('aggregate', () => {
|
|
|
436
438
|
const q = User.clone();
|
|
437
439
|
expectSql(
|
|
438
440
|
q[method as 'jsonObjectAgg']({
|
|
439
|
-
alias: raw('name'),
|
|
441
|
+
alias: db.raw('name'),
|
|
440
442
|
}).toSql(),
|
|
441
443
|
`SELECT ${functionName}($1::text, name) FROM "user"`,
|
|
442
444
|
['alias'],
|
|
@@ -463,7 +465,7 @@ describe('aggregate', () => {
|
|
|
463
465
|
const expectedSql = `SELECT ${functionName}($1::text, name) AS "name" FROM "user"`;
|
|
464
466
|
expectSql(
|
|
465
467
|
q[selectMethod as 'jsonObjectAgg'](
|
|
466
|
-
{ alias: raw('name') },
|
|
468
|
+
{ alias: db.raw('name') },
|
|
467
469
|
{ as: 'name' },
|
|
468
470
|
).toSql(),
|
|
469
471
|
expectedSql,
|
|
@@ -525,7 +527,7 @@ describe('aggregate', () => {
|
|
|
525
527
|
it('should support raw sql parameter', async () => {
|
|
526
528
|
const q = User.all();
|
|
527
529
|
expectSql(
|
|
528
|
-
q.stringAgg(raw('name'), ' & ').toSql(),
|
|
530
|
+
q.stringAgg(db.raw('name'), ' & ').toSql(),
|
|
529
531
|
`SELECT string_agg(name, $1) FROM "user"`,
|
|
530
532
|
[' & '],
|
|
531
533
|
);
|
|
@@ -547,7 +549,7 @@ describe('aggregate', () => {
|
|
|
547
549
|
const q = User.all();
|
|
548
550
|
const expectedSql = `SELECT string_agg(name, $1) AS "name" FROM "user"`;
|
|
549
551
|
expectSql(
|
|
550
|
-
q.stringAgg(raw('name'), ' & ', { as: 'name' }).toSql(),
|
|
552
|
+
q.stringAgg(db.raw('name'), ' & ', { as: 'name' }).toSql(),
|
|
551
553
|
expectedSql,
|
|
552
554
|
[' & '],
|
|
553
555
|
);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
assertType,
|
|
3
3
|
Chat,
|
|
4
|
+
db,
|
|
4
5
|
expectQueryNotMutated,
|
|
5
6
|
expectSql,
|
|
6
7
|
Message,
|
|
@@ -10,7 +11,6 @@ import {
|
|
|
10
11
|
UserRecord,
|
|
11
12
|
useTestDatabase,
|
|
12
13
|
} from '../test-utils';
|
|
13
|
-
import { raw } from '../common';
|
|
14
14
|
import { OnConflictQueryBuilder } from './create';
|
|
15
15
|
|
|
16
16
|
describe('create functions', () => {
|
|
@@ -22,7 +22,7 @@ describe('create functions', () => {
|
|
|
22
22
|
|
|
23
23
|
const query = q.createRaw({
|
|
24
24
|
columns: ['name', 'password'],
|
|
25
|
-
values: raw('raw sql'),
|
|
25
|
+
values: db.raw('raw sql'),
|
|
26
26
|
});
|
|
27
27
|
expectSql(
|
|
28
28
|
query.toSql(),
|
|
@@ -150,6 +150,24 @@ describe('create functions', () => {
|
|
|
150
150
|
['name', 'override'],
|
|
151
151
|
);
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
it('should strip unknown keys', () => {
|
|
155
|
+
const query = User.create({
|
|
156
|
+
name: 'name',
|
|
157
|
+
password: 'password',
|
|
158
|
+
unknown: 'should be stripped',
|
|
159
|
+
} as unknown as UserRecord);
|
|
160
|
+
|
|
161
|
+
expectSql(
|
|
162
|
+
query.toSql(),
|
|
163
|
+
`
|
|
164
|
+
INSERT INTO "user"("name", "password")
|
|
165
|
+
VALUES ($1, $2)
|
|
166
|
+
RETURNING *
|
|
167
|
+
`,
|
|
168
|
+
['name', 'password'],
|
|
169
|
+
);
|
|
170
|
+
});
|
|
153
171
|
});
|
|
154
172
|
|
|
155
173
|
describe('createMany', () => {
|
|
@@ -265,6 +283,31 @@ describe('create functions', () => {
|
|
|
265
283
|
|
|
266
284
|
expectQueryNotMutated(q);
|
|
267
285
|
});
|
|
286
|
+
|
|
287
|
+
it('should strip unknown keys', () => {
|
|
288
|
+
const query = User.createMany([
|
|
289
|
+
{
|
|
290
|
+
name: 'name',
|
|
291
|
+
password: 'password',
|
|
292
|
+
unknown: 'should be stripped',
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
name: 'name',
|
|
296
|
+
password: 'password',
|
|
297
|
+
unknown: 'should be stripped',
|
|
298
|
+
},
|
|
299
|
+
] as unknown as UserRecord[]);
|
|
300
|
+
|
|
301
|
+
expectSql(
|
|
302
|
+
query.toSql(),
|
|
303
|
+
`
|
|
304
|
+
INSERT INTO "user"("name", "password")
|
|
305
|
+
VALUES ($1, $2), ($3, $4)
|
|
306
|
+
RETURNING *
|
|
307
|
+
`,
|
|
308
|
+
['name', 'password', 'name', 'password'],
|
|
309
|
+
);
|
|
310
|
+
});
|
|
268
311
|
});
|
|
269
312
|
|
|
270
313
|
describe('createFrom', () => {
|
|
@@ -414,7 +457,7 @@ describe('create functions', () => {
|
|
|
414
457
|
const query = q
|
|
415
458
|
.count()
|
|
416
459
|
.create(userData)
|
|
417
|
-
.onConflict(raw('raw query'))
|
|
460
|
+
.onConflict(db.raw('raw query'))
|
|
418
461
|
.ignore();
|
|
419
462
|
expectSql(
|
|
420
463
|
query.toSql(),
|
|
@@ -527,8 +570,8 @@ describe('create functions', () => {
|
|
|
527
570
|
const query = q
|
|
528
571
|
.count()
|
|
529
572
|
.create(userData)
|
|
530
|
-
.onConflict(raw('on conflict raw'))
|
|
531
|
-
.merge(raw('merge raw'));
|
|
573
|
+
.onConflict(db.raw('on conflict raw'))
|
|
574
|
+
.merge(db.raw('merge raw'));
|
|
532
575
|
|
|
533
576
|
expectSql(
|
|
534
577
|
query.toSql(),
|