pqb 0.7.13 → 0.8.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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +618 -563
- package/dist/index.esm.js +1011 -402
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1014 -401
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/array.test.ts +67 -0
- package/src/columnSchema/array.ts +39 -13
- package/src/columnSchema/boolean.test.ts +17 -0
- package/src/columnSchema/boolean.ts +5 -1
- package/src/columnSchema/columnType.test.ts +230 -107
- package/src/columnSchema/columnType.ts +198 -28
- package/src/columnSchema/columnTypes.ts +28 -15
- package/src/columnSchema/columnsSchema.ts +6 -4
- package/src/columnSchema/commonMethods.ts +11 -4
- package/src/columnSchema/dateTime.test.ts +298 -0
- package/src/columnSchema/dateTime.ts +59 -2
- package/src/columnSchema/enum.test.ts +33 -0
- package/src/columnSchema/enum.ts +11 -1
- package/src/columnSchema/json/array.test.ts +21 -0
- package/src/columnSchema/json/array.ts +27 -13
- package/src/columnSchema/json/discriminatedUnion.test.ts +32 -0
- package/src/columnSchema/json/discriminatedUnion.ts +17 -2
- package/src/columnSchema/json/enum.test.ts +9 -0
- package/src/columnSchema/json/enum.ts +9 -1
- package/src/columnSchema/json/index.ts +19 -19
- package/src/columnSchema/json/instanceOf.test.ts +8 -0
- package/src/columnSchema/json/instanceOf.ts +4 -1
- package/src/columnSchema/json/intersection.test.ts +19 -0
- package/src/columnSchema/json/intersection.ts +9 -1
- package/src/columnSchema/json/lazy.test.ts +22 -0
- package/src/columnSchema/json/lazy.ts +22 -1
- package/src/columnSchema/json/literal.test.ts +7 -0
- package/src/columnSchema/json/literal.ts +12 -1
- package/src/columnSchema/json/map.test.ts +10 -0
- package/src/columnSchema/json/map.ts +21 -1
- package/src/columnSchema/json/nativeEnum.test.ts +10 -0
- package/src/columnSchema/json/nativeEnum.ts +4 -1
- package/src/columnSchema/json/nullable.test.ts +18 -0
- package/src/columnSchema/json/nullish.test.ts +18 -0
- package/src/columnSchema/json/object.test.ts +77 -0
- package/src/columnSchema/json/object.ts +31 -3
- package/src/columnSchema/json/optional.test.ts +18 -0
- package/src/columnSchema/json/record.test.ts +14 -0
- package/src/columnSchema/json/record.ts +12 -1
- package/src/columnSchema/json/scalarTypes.test.ts +133 -0
- package/src/columnSchema/json/scalarTypes.ts +90 -1
- package/src/columnSchema/json/set.test.ts +29 -0
- package/src/columnSchema/json/set.ts +26 -7
- package/src/columnSchema/json/tuple.test.ts +17 -0
- package/src/columnSchema/json/tuple.ts +16 -1
- package/src/columnSchema/json/typeBase.test.ts +123 -0
- package/src/columnSchema/json/typeBase.ts +52 -13
- package/src/columnSchema/json/union.test.ts +10 -0
- package/src/columnSchema/json/union.ts +18 -1
- package/src/columnSchema/json.test.ts +17 -0
- package/src/columnSchema/json.ts +10 -2
- package/src/columnSchema/number.test.ts +176 -0
- package/src/columnSchema/number.ts +48 -1
- package/src/columnSchema/string.test.ts +412 -0
- package/src/columnSchema/string.ts +126 -15
- package/src/columnSchema/timestamps.test.ts +6 -6
- package/src/columnSchema/virtual.ts +4 -0
- package/src/db.ts +1 -1
- package/src/query.ts +1 -1
- package/src/queryMethods/create.ts +6 -6
- package/src/queryMethods/for.ts +3 -3
- package/src/queryMethods/having.ts +1 -1
- package/src/queryMethods/join.ts +4 -4
- package/src/queryMethods/json.ts +1 -1
- package/src/queryMethods/queryMethods.ts +2 -2
- package/src/queryMethods/select.ts +3 -3
- package/src/queryMethods/update.ts +17 -17
- package/src/queryMethods/where.test.ts +1 -1
- package/src/queryMethods/where.ts +4 -4
- package/src/relations.ts +1 -1
- package/src/sql/aggregate.ts +2 -2
- package/src/sql/copy.ts +3 -3
- package/src/sql/delete.ts +5 -5
- package/src/sql/fromAndAs.ts +4 -4
- package/src/sql/having.ts +7 -7
- package/src/sql/insert.ts +5 -5
- package/src/sql/join.ts +16 -16
- package/src/sql/select.ts +6 -6
- package/src/sql/toSql.ts +24 -24
- package/src/sql/update.ts +4 -4
- package/src/sql/where.ts +18 -18
- package/src/utils.test.ts +9 -0
- package/src/utils.ts +3 -0
- package/src/columnSchema/columnTypes.test.ts +0 -527
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ColumnData, ColumnType } from './columnType';
|
|
1
|
+
import { Code, columnCode, ColumnData, ColumnType } from './columnType';
|
|
2
2
|
import { Operators } from '../columnsOperators';
|
|
3
|
-
import { joinTruthy } from '../utils';
|
|
3
|
+
import { joinTruthy, singleQuote } from '../utils';
|
|
4
4
|
import { NumberBaseColumn } from './number';
|
|
5
5
|
import { assignMethodsToClass } from './utils';
|
|
6
6
|
import { stringTypeMethods } from './commonMethods';
|
|
@@ -17,6 +17,31 @@ export type BaseStringData = ColumnData & {
|
|
|
17
17
|
startsWith?: string;
|
|
18
18
|
endsWith?: string;
|
|
19
19
|
trim?: boolean;
|
|
20
|
+
isNonEmpty?: true;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const stringDataToCode = (data: BaseStringData) => {
|
|
24
|
+
let code = '';
|
|
25
|
+
|
|
26
|
+
const { min, isNonEmpty } = data;
|
|
27
|
+
|
|
28
|
+
if (min !== undefined && (!isNonEmpty || (isNonEmpty && min !== 1)))
|
|
29
|
+
code += `.min(${min})`;
|
|
30
|
+
|
|
31
|
+
if (data.max !== undefined) code += `.max(${data.max})`;
|
|
32
|
+
if (data.length !== undefined) code += `.length(${data.length})`;
|
|
33
|
+
if (data.email !== undefined) code += `.email()`;
|
|
34
|
+
if (data.url !== undefined) code += `.url()`;
|
|
35
|
+
if (data.uuid !== undefined) code += `.uuid()`;
|
|
36
|
+
if (data.cuid !== undefined) code += `.cuid()`;
|
|
37
|
+
if (data.regex) code += `.regex(${data.regex.toString()})`;
|
|
38
|
+
if (data.startsWith !== undefined)
|
|
39
|
+
code += `.startsWith(${singleQuote(data.startsWith)})`;
|
|
40
|
+
if (data.endsWith !== undefined)
|
|
41
|
+
code += `.endsWith(${singleQuote(data.endsWith)})`;
|
|
42
|
+
if (data.cuid !== undefined) code += `.trim()`;
|
|
43
|
+
|
|
44
|
+
return code;
|
|
20
45
|
};
|
|
21
46
|
|
|
22
47
|
export type StringColumn = ColumnType<string>;
|
|
@@ -43,18 +68,18 @@ assignMethodsToClass(TextBaseColumn, textMethods);
|
|
|
43
68
|
export abstract class LimitedTextBaseColumn<
|
|
44
69
|
Limit extends number | undefined = undefined,
|
|
45
70
|
> extends TextBaseColumn {
|
|
46
|
-
data: TextColumnData & {
|
|
71
|
+
data: TextColumnData & { arg: Limit };
|
|
47
72
|
|
|
48
73
|
constructor(limit?: Limit) {
|
|
49
74
|
super();
|
|
50
75
|
|
|
51
|
-
this.data = {
|
|
76
|
+
this.data = { arg: limit } as TextColumnData & { arg: Limit };
|
|
52
77
|
}
|
|
53
78
|
|
|
54
79
|
toSQL() {
|
|
55
80
|
return joinTruthy(
|
|
56
81
|
this.dataType,
|
|
57
|
-
this.data.
|
|
82
|
+
this.data.arg !== undefined && `(${this.data.arg})`,
|
|
58
83
|
);
|
|
59
84
|
}
|
|
60
85
|
}
|
|
@@ -64,6 +89,14 @@ export class VarCharColumn<
|
|
|
64
89
|
Limit extends number | undefined = undefined,
|
|
65
90
|
> extends LimitedTextBaseColumn<Limit> {
|
|
66
91
|
dataType = 'varchar' as const;
|
|
92
|
+
toCode(t: string): Code {
|
|
93
|
+
const { arg } = this.data;
|
|
94
|
+
return columnCode(
|
|
95
|
+
this,
|
|
96
|
+
t,
|
|
97
|
+
`${t}.varchar(${arg ?? ''})${stringDataToCode(this.data)}`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
67
100
|
}
|
|
68
101
|
|
|
69
102
|
// character(n), char(n) fixed-length, blank padded
|
|
@@ -71,93 +104,149 @@ export class CharColumn<
|
|
|
71
104
|
Limit extends number | undefined = undefined,
|
|
72
105
|
> extends LimitedTextBaseColumn<Limit> {
|
|
73
106
|
dataType = 'char' as const;
|
|
107
|
+
toCode(t: string): Code {
|
|
108
|
+
const { arg } = this.data;
|
|
109
|
+
return columnCode(
|
|
110
|
+
this,
|
|
111
|
+
t,
|
|
112
|
+
`${t}.char(${arg ?? ''})${stringDataToCode(this.data)}`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
74
115
|
}
|
|
75
116
|
|
|
76
117
|
// text variable unlimited length
|
|
77
118
|
export class TextColumn extends TextBaseColumn {
|
|
78
119
|
dataType = 'text' as const;
|
|
79
120
|
operators = Operators.text;
|
|
121
|
+
toCode(t: string): Code {
|
|
122
|
+
return columnCode(this, t, `${t}.text()${stringDataToCode(this.data)}`);
|
|
123
|
+
}
|
|
80
124
|
}
|
|
81
125
|
|
|
82
126
|
// To store binary strings
|
|
83
127
|
export class ByteaColumn extends ColumnType<Buffer, typeof Operators.text> {
|
|
84
128
|
dataType = 'bytea' as const;
|
|
85
129
|
operators = Operators.text;
|
|
130
|
+
toCode(t: string): Code {
|
|
131
|
+
return columnCode(this, t, `${t}.bytea()`);
|
|
132
|
+
}
|
|
86
133
|
}
|
|
87
134
|
|
|
88
135
|
// point 16 bytes Point on a plane (x,y)
|
|
89
136
|
export class PointColumn extends ColumnType<string, typeof Operators.text> {
|
|
90
137
|
dataType = 'point' as const;
|
|
91
138
|
operators = Operators.text;
|
|
139
|
+
toCode(t: string): Code {
|
|
140
|
+
return columnCode(this, t, `${t}.point()`);
|
|
141
|
+
}
|
|
92
142
|
}
|
|
93
143
|
|
|
94
144
|
// line 32 bytes Infinite line {A,B,C}
|
|
95
145
|
export class LineColumn extends ColumnType<string, typeof Operators.text> {
|
|
96
|
-
dataType = '
|
|
146
|
+
dataType = 'line' as const;
|
|
97
147
|
operators = Operators.text;
|
|
148
|
+
toCode(t: string): Code {
|
|
149
|
+
return columnCode(this, t, `${t}.line()`);
|
|
150
|
+
}
|
|
98
151
|
}
|
|
99
152
|
|
|
100
153
|
// lseg 32 bytes Finite line segment ((x1,y1),(x2,y2))
|
|
101
154
|
export class LsegColumn extends ColumnType<string, typeof Operators.text> {
|
|
102
|
-
dataType = '
|
|
155
|
+
dataType = 'lseg' as const;
|
|
103
156
|
operators = Operators.text;
|
|
157
|
+
toCode(t: string): Code {
|
|
158
|
+
return columnCode(this, t, `${t}.lseg()`);
|
|
159
|
+
}
|
|
104
160
|
}
|
|
105
161
|
|
|
106
162
|
// box 32 bytes Rectangular box ((x1,y1),(x2,y2))
|
|
107
163
|
export class BoxColumn extends ColumnType<string, typeof Operators.text> {
|
|
108
|
-
dataType = '
|
|
164
|
+
dataType = 'box' as const;
|
|
109
165
|
operators = Operators.text;
|
|
166
|
+
toCode(t: string): Code {
|
|
167
|
+
return columnCode(this, t, `${t}.box()`);
|
|
168
|
+
}
|
|
110
169
|
}
|
|
111
170
|
|
|
112
171
|
// path 16+16n bytes Closed path (similar to polygon) ((x1,y1),...)
|
|
113
172
|
// path 16+16n bytes Open path [(x1,y1),...]
|
|
114
173
|
export class PathColumn extends ColumnType<string, typeof Operators.text> {
|
|
115
|
-
dataType = '
|
|
174
|
+
dataType = 'path' as const;
|
|
116
175
|
operators = Operators.text;
|
|
176
|
+
toCode(t: string): Code {
|
|
177
|
+
return columnCode(this, t, `${t}.path()`);
|
|
178
|
+
}
|
|
117
179
|
}
|
|
118
180
|
|
|
119
181
|
// polygon 40+16n bytes Polygon (similar to closed path) ((x1,y1),...)
|
|
120
182
|
export class PolygonColumn extends ColumnType<string, typeof Operators.text> {
|
|
121
|
-
dataType = '
|
|
183
|
+
dataType = 'polygon' as const;
|
|
122
184
|
operators = Operators.text;
|
|
185
|
+
toCode(t: string): Code {
|
|
186
|
+
return columnCode(this, t, `${t}.polygon()`);
|
|
187
|
+
}
|
|
123
188
|
}
|
|
124
189
|
|
|
125
190
|
// circle 24 bytes Circle <(x,y),r> (center point and radius)
|
|
126
191
|
export class CircleColumn extends ColumnType<string, typeof Operators.text> {
|
|
127
|
-
dataType = '
|
|
192
|
+
dataType = 'circle' as const;
|
|
128
193
|
operators = Operators.text;
|
|
194
|
+
toCode(t: string): Code {
|
|
195
|
+
return columnCode(this, t, `${t}.circle()`);
|
|
196
|
+
}
|
|
129
197
|
}
|
|
130
198
|
|
|
131
199
|
export class MoneyColumn extends NumberBaseColumn {
|
|
132
200
|
dataType = 'money' as const;
|
|
133
201
|
|
|
134
|
-
|
|
135
|
-
return
|
|
136
|
-
}
|
|
202
|
+
toCode(t: string): Code {
|
|
203
|
+
return columnCode(this, t, `${t}.money()`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
parseFn = Object.assign(
|
|
207
|
+
function (input: unknown) {
|
|
208
|
+
return parseFloat((input as string).replace(/,/g, '').replace(/\$/g, ''));
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
hideFromCode: true,
|
|
212
|
+
},
|
|
213
|
+
);
|
|
137
214
|
}
|
|
138
215
|
|
|
139
216
|
// cidr 7 or 19 bytes IPv4 and IPv6 networks
|
|
140
217
|
export class CidrColumn extends ColumnType<string, typeof Operators.text> {
|
|
141
218
|
dataType = 'cidr' as const;
|
|
142
219
|
operators = Operators.text;
|
|
220
|
+
toCode(t: string): Code {
|
|
221
|
+
return columnCode(this, t, `${t}.cidr()`);
|
|
222
|
+
}
|
|
143
223
|
}
|
|
144
224
|
|
|
145
225
|
// inet 7 or 19 bytes IPv4 and IPv6 hosts and networks
|
|
146
226
|
export class InetColumn extends ColumnType<string, typeof Operators.text> {
|
|
147
227
|
dataType = 'inet' as const;
|
|
148
228
|
operators = Operators.text;
|
|
229
|
+
toCode(t: string): Code {
|
|
230
|
+
return columnCode(this, t, `${t}.inet()`);
|
|
231
|
+
}
|
|
149
232
|
}
|
|
150
233
|
|
|
151
234
|
// macaddr 6 bytes MAC addresses
|
|
152
235
|
export class MacAddrColumn extends ColumnType<string, typeof Operators.text> {
|
|
153
236
|
dataType = 'macaddr' as const;
|
|
154
237
|
operators = Operators.text;
|
|
238
|
+
toCode(t: string): Code {
|
|
239
|
+
return columnCode(this, t, `${t}.macaddr()`);
|
|
240
|
+
}
|
|
155
241
|
}
|
|
156
242
|
|
|
157
243
|
// macaddr8 8 bytes MAC addresses (EUI-64 format)
|
|
158
244
|
export class MacAddr8Column extends ColumnType<string, typeof Operators.text> {
|
|
159
245
|
dataType = 'macaddr8' as const;
|
|
160
246
|
operators = Operators.text;
|
|
247
|
+
toCode(t: string): Code {
|
|
248
|
+
return columnCode(this, t, `${t}.macaddr8()`);
|
|
249
|
+
}
|
|
161
250
|
}
|
|
162
251
|
|
|
163
252
|
// Bit strings are strings of 1's and 0's.
|
|
@@ -177,6 +266,11 @@ export class BitColumn<Length extends number> extends ColumnType<
|
|
|
177
266
|
this.data = { length } as { length: Length };
|
|
178
267
|
}
|
|
179
268
|
|
|
269
|
+
toCode(t: string): Code {
|
|
270
|
+
const { length } = this.data;
|
|
271
|
+
return columnCode(this, t, `${t}.bit(${length})`);
|
|
272
|
+
}
|
|
273
|
+
|
|
180
274
|
toSQL() {
|
|
181
275
|
return joinTruthy(
|
|
182
276
|
this.dataType,
|
|
@@ -192,12 +286,17 @@ export class BitVaryingColumn<
|
|
|
192
286
|
operators = Operators.text;
|
|
193
287
|
data: ColumnData & { length: Length };
|
|
194
288
|
|
|
195
|
-
constructor(length
|
|
289
|
+
constructor(length?: Length) {
|
|
196
290
|
super();
|
|
197
291
|
|
|
198
292
|
this.data = { length } as { length: Length };
|
|
199
293
|
}
|
|
200
294
|
|
|
295
|
+
toCode(t: string): Code {
|
|
296
|
+
const { length } = this.data;
|
|
297
|
+
return columnCode(this, t, `${t}.bitVarying(${length ?? ''})`);
|
|
298
|
+
}
|
|
299
|
+
|
|
201
300
|
toSQL() {
|
|
202
301
|
return joinTruthy(
|
|
203
302
|
this.dataType,
|
|
@@ -210,22 +309,34 @@ export class BitVaryingColumn<
|
|
|
210
309
|
export class TsVectorColumn extends ColumnType<string, typeof Operators.text> {
|
|
211
310
|
dataType = 'tsvector' as const;
|
|
212
311
|
operators = Operators.text;
|
|
312
|
+
toCode(t: string): Code {
|
|
313
|
+
return columnCode(this, t, `${t}.tsvector()`);
|
|
314
|
+
}
|
|
213
315
|
}
|
|
214
316
|
|
|
215
317
|
// A tsquery value stores lexemes that are to be searched for
|
|
216
318
|
export class TsQueryColumn extends ColumnType<string, typeof Operators.text> {
|
|
217
319
|
dataType = 'tsquery' as const;
|
|
218
320
|
operators = Operators.text;
|
|
321
|
+
toCode(t: string): Code {
|
|
322
|
+
return columnCode(this, t, `${t}.tsquery()`);
|
|
323
|
+
}
|
|
219
324
|
}
|
|
220
325
|
|
|
221
326
|
// uuid stores Universally Unique Identifiers (UUID)
|
|
222
327
|
export class UUIDColumn extends ColumnType<string, typeof Operators.text> {
|
|
223
328
|
dataType = 'uuid' as const;
|
|
224
329
|
operators = Operators.text;
|
|
330
|
+
toCode(t: string): Code {
|
|
331
|
+
return columnCode(this, t, `${t}.uuid()`);
|
|
332
|
+
}
|
|
225
333
|
}
|
|
226
334
|
|
|
227
335
|
// xml data type can be used to store XML data
|
|
228
336
|
export class XMLColumn extends ColumnType<string, typeof Operators.text> {
|
|
229
337
|
dataType = 'xml' as const;
|
|
230
338
|
operators = Operators.text;
|
|
339
|
+
toCode(t: string): Code {
|
|
340
|
+
return columnCode(this, t, `${t}.xml()`);
|
|
341
|
+
}
|
|
231
342
|
}
|
|
@@ -3,13 +3,13 @@ import { db, expectSql, now, useTestDatabase } from '../test-utils/test-utils';
|
|
|
3
3
|
describe('timestamps', () => {
|
|
4
4
|
useTestDatabase();
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const table = db('user', (t) => ({
|
|
7
7
|
name: t.text().primaryKey(),
|
|
8
8
|
...t.timestamps(),
|
|
9
9
|
}));
|
|
10
10
|
|
|
11
11
|
it('should update updatedAt column when updating', async () => {
|
|
12
|
-
const query =
|
|
12
|
+
const query = table.where().update({});
|
|
13
13
|
await query;
|
|
14
14
|
|
|
15
15
|
expectSql(
|
|
@@ -22,7 +22,7 @@ describe('timestamps', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('should not update updatedAt column when updating it via object', async () => {
|
|
25
|
-
const query =
|
|
25
|
+
const query = table.where().update({ updatedAt: now });
|
|
26
26
|
await query;
|
|
27
27
|
|
|
28
28
|
expectSql(
|
|
@@ -36,7 +36,7 @@ describe('timestamps', () => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it('should update updatedAt when updating with raw sql', async () => {
|
|
39
|
-
const query =
|
|
39
|
+
const query = table
|
|
40
40
|
.where()
|
|
41
41
|
.updateRaw(db.raw('name = $name', { name: 'name' }));
|
|
42
42
|
|
|
@@ -53,7 +53,7 @@ describe('timestamps', () => {
|
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
it('should update updatedAt when updating with raw sql which has updatedAt somewhere but not in set', async () => {
|
|
56
|
-
const query =
|
|
56
|
+
const query = table.where().updateRaw(db.raw('"createdAt" = "updatedAt"'));
|
|
57
57
|
await query;
|
|
58
58
|
|
|
59
59
|
expectSql(
|
|
@@ -66,7 +66,7 @@ describe('timestamps', () => {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
it('should not update updatedAt column when updating with raw sql which contains `updatedAt = `', async () => {
|
|
69
|
-
const query =
|
|
69
|
+
const query = table
|
|
70
70
|
.where()
|
|
71
71
|
.updateRaw(db.raw('"updatedAt" = $time', { time: now }));
|
|
72
72
|
|
package/src/db.ts
CHANGED
package/src/query.ts
CHANGED
|
@@ -72,14 +72,14 @@ type CreateBelongsToData<
|
|
|
72
72
|
}
|
|
73
73
|
| {
|
|
74
74
|
create?: never;
|
|
75
|
-
connect: WhereArg<Rel['
|
|
75
|
+
connect: WhereArg<Rel['table']>;
|
|
76
76
|
connectOrCreate?: never;
|
|
77
77
|
}
|
|
78
78
|
| {
|
|
79
79
|
create?: never;
|
|
80
80
|
connect?: never;
|
|
81
81
|
connectOrCreate: {
|
|
82
|
-
where: WhereArg<Rel['
|
|
82
|
+
where: WhereArg<Rel['table']>;
|
|
83
83
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
84
84
|
};
|
|
85
85
|
};
|
|
@@ -101,14 +101,14 @@ type CreateHasOneData<
|
|
|
101
101
|
}
|
|
102
102
|
| {
|
|
103
103
|
create?: never;
|
|
104
|
-
connect: WhereArg<Rel['
|
|
104
|
+
connect: WhereArg<Rel['table']>;
|
|
105
105
|
connectOrCreate?: never;
|
|
106
106
|
}
|
|
107
107
|
| {
|
|
108
108
|
create?: never;
|
|
109
109
|
connect?: never;
|
|
110
110
|
connectOrCreate: {
|
|
111
|
-
where?: WhereArg<Rel['
|
|
111
|
+
where?: WhereArg<Rel['table']>;
|
|
112
112
|
create?: CreateData<Rel['nestedCreateQuery']>;
|
|
113
113
|
};
|
|
114
114
|
};
|
|
@@ -124,9 +124,9 @@ type CreateHasManyData<
|
|
|
124
124
|
: {
|
|
125
125
|
[K in Key]?: {
|
|
126
126
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
127
|
-
connect?: WhereArg<Rel['
|
|
127
|
+
connect?: WhereArg<Rel['table']>[];
|
|
128
128
|
connectOrCreate?: {
|
|
129
|
-
where: WhereArg<Rel['
|
|
129
|
+
where: WhereArg<Rel['table']>;
|
|
130
130
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
131
131
|
}[];
|
|
132
132
|
};
|
package/src/queryMethods/for.ts
CHANGED
|
@@ -15,10 +15,10 @@ const forQueryBuilder = <T extends Query>(
|
|
|
15
15
|
tableNames?: string[] | RawExpression,
|
|
16
16
|
) => {
|
|
17
17
|
(q.query as SelectQueryData).for = { type, tableNames };
|
|
18
|
-
q.
|
|
19
|
-
q.
|
|
18
|
+
q.__table = Object.create(q.__table);
|
|
19
|
+
q.__table.__table = q.__table;
|
|
20
20
|
|
|
21
|
-
Object.assign(q.
|
|
21
|
+
Object.assign(q.__table, {
|
|
22
22
|
noWait<T extends ForQueryBuilder<Query>>(this: T): T {
|
|
23
23
|
return this.clone()._noWait();
|
|
24
24
|
},
|
|
@@ -29,7 +29,7 @@ export type HavingArg<T extends Query = Query> =
|
|
|
29
29
|
| RawExpression;
|
|
30
30
|
|
|
31
31
|
const processHavingArg = <T extends Query>(arg: HavingArg<T>): HavingItem => {
|
|
32
|
-
if ('
|
|
32
|
+
if ('__table' in arg || isRaw(arg)) {
|
|
33
33
|
return arg;
|
|
34
34
|
} else {
|
|
35
35
|
const processed = { ...arg } as Record<
|
package/src/queryMethods/join.ts
CHANGED
|
@@ -78,7 +78,7 @@ type JoinResult<
|
|
|
78
78
|
? A
|
|
79
79
|
: T['relations'] extends Record<string, Relation>
|
|
80
80
|
? A extends keyof T['relations']
|
|
81
|
-
? T['relations'][A]['
|
|
81
|
+
? T['relations'][A]['table']
|
|
82
82
|
: A extends keyof T['withData']
|
|
83
83
|
? T['withData'][A] extends WithDataItem
|
|
84
84
|
? {
|
|
@@ -116,7 +116,7 @@ export type JoinCallback<
|
|
|
116
116
|
};
|
|
117
117
|
};
|
|
118
118
|
shape: T['withData'][Arg]['shape'];
|
|
119
|
-
|
|
119
|
+
__table: Query;
|
|
120
120
|
relations: RelationsBase;
|
|
121
121
|
withData: WithDataBase;
|
|
122
122
|
}
|
|
@@ -125,7 +125,7 @@ export type JoinCallback<
|
|
|
125
125
|
? Arg
|
|
126
126
|
: T['relations'] extends Record<string, Relation>
|
|
127
127
|
? Arg extends keyof T['relations']
|
|
128
|
-
? T['relations'][Arg]['
|
|
128
|
+
? T['relations'][Arg]['table']
|
|
129
129
|
: never
|
|
130
130
|
: never
|
|
131
131
|
>,
|
|
@@ -140,7 +140,7 @@ type JoinCallbackResult<
|
|
|
140
140
|
? Arg
|
|
141
141
|
: T['relations'] extends Record<string, Relation>
|
|
142
142
|
? Arg extends keyof T['relations']
|
|
143
|
-
? T['relations'][Arg]['
|
|
143
|
+
? T['relations'][Arg]['table']
|
|
144
144
|
: Arg extends keyof T['withData']
|
|
145
145
|
? T['withData'][Arg] extends WithDataItem
|
|
146
146
|
? {
|
package/src/queryMethods/json.ts
CHANGED
|
@@ -53,7 +53,7 @@ export class Json {
|
|
|
53
53
|
_json<T extends Query>(
|
|
54
54
|
this: T,
|
|
55
55
|
): SetQueryReturnsValueOptional<T, StringColumn> {
|
|
56
|
-
const q = this._wrap(this.
|
|
56
|
+
const q = this._wrap(this.__table.clone()) as T;
|
|
57
57
|
q._getOptional(
|
|
58
58
|
raw(
|
|
59
59
|
queryTypeWithLimitOne[this.query.returnType]
|
|
@@ -104,7 +104,7 @@ export interface QueryMethods
|
|
|
104
104
|
|
|
105
105
|
export class QueryMethods {
|
|
106
106
|
windows!: EmptyObject;
|
|
107
|
-
|
|
107
|
+
__table!: Query;
|
|
108
108
|
|
|
109
109
|
all<T extends Query>(this: T): SetQueryReturnsAll<T> {
|
|
110
110
|
return this.clone()._all();
|
|
@@ -169,7 +169,7 @@ export class QueryMethods {
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
clone<T extends QueryBase>(this: T): T {
|
|
172
|
-
const cloned = Object.create(this.
|
|
172
|
+
const cloned = Object.create(this.__table);
|
|
173
173
|
cloned.query = getClonedQueryData(this.query);
|
|
174
174
|
return cloned;
|
|
175
175
|
}
|
|
@@ -50,11 +50,11 @@ type SelectResult<
|
|
|
50
50
|
: T['relations'] extends Record<string, Relation>
|
|
51
51
|
? Arg extends keyof T['relations']
|
|
52
52
|
? T['relations'][Arg]['returns'] extends 'many'
|
|
53
|
-
? ArrayOfColumnsObjects<T['relations'][Arg]['
|
|
53
|
+
? ArrayOfColumnsObjects<T['relations'][Arg]['table']['result']>
|
|
54
54
|
: T['relations'][Arg]['options']['required'] extends true
|
|
55
|
-
? ColumnsObject<T['relations'][Arg]['
|
|
55
|
+
? ColumnsObject<T['relations'][Arg]['table']['result']>
|
|
56
56
|
: NullableColumn<
|
|
57
|
-
ColumnsObject<T['relations'][Arg]['
|
|
57
|
+
ColumnsObject<T['relations'][Arg]['table']['result']>
|
|
58
58
|
>
|
|
59
59
|
: never
|
|
60
60
|
: never;
|
|
@@ -36,9 +36,9 @@ export type UpdateData<T extends Query> = {
|
|
|
36
36
|
|
|
37
37
|
type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
38
38
|
| { disconnect: boolean }
|
|
39
|
-
| { set: WhereArg<Rel['
|
|
39
|
+
| { set: WhereArg<Rel['table']> }
|
|
40
40
|
| { delete: boolean }
|
|
41
|
-
| { update: UpdateData<Rel['
|
|
41
|
+
| { update: UpdateData<Rel['table']> }
|
|
42
42
|
| {
|
|
43
43
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
44
44
|
}
|
|
@@ -46,7 +46,7 @@ type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
|
46
46
|
? never
|
|
47
47
|
: {
|
|
48
48
|
upsert: {
|
|
49
|
-
update: UpdateData<Rel['
|
|
49
|
+
update: UpdateData<Rel['table']>;
|
|
50
50
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
51
51
|
};
|
|
52
52
|
});
|
|
@@ -54,14 +54,14 @@ type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> =
|
|
|
54
54
|
type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
55
55
|
| { disconnect: boolean }
|
|
56
56
|
| { delete: boolean }
|
|
57
|
-
| { update: UpdateData<Rel['
|
|
57
|
+
| { update: UpdateData<Rel['table']> }
|
|
58
58
|
| (QueryReturnsAll<T['returnType']> extends true
|
|
59
59
|
? never
|
|
60
60
|
:
|
|
61
|
-
| { set: WhereArg<Rel['
|
|
61
|
+
| { set: WhereArg<Rel['table']> }
|
|
62
62
|
| {
|
|
63
63
|
upsert: {
|
|
64
|
-
update: UpdateData<Rel['
|
|
64
|
+
update: UpdateData<Rel['table']>;
|
|
65
65
|
create: CreateData<Rel['nestedCreateQuery']>;
|
|
66
66
|
};
|
|
67
67
|
}
|
|
@@ -70,26 +70,26 @@ type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> =
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
|
|
73
|
-
disconnect?: MaybeArray<WhereArg<Rel['
|
|
74
|
-
delete?: MaybeArray<WhereArg<Rel['
|
|
73
|
+
disconnect?: MaybeArray<WhereArg<Rel['table']>>;
|
|
74
|
+
delete?: MaybeArray<WhereArg<Rel['table']>>;
|
|
75
75
|
update?: {
|
|
76
|
-
where: MaybeArray<WhereArg<Rel['
|
|
77
|
-
data: UpdateData<Rel['
|
|
76
|
+
where: MaybeArray<WhereArg<Rel['table']>>;
|
|
77
|
+
data: UpdateData<Rel['table']>;
|
|
78
78
|
};
|
|
79
79
|
} & (QueryReturnsAll<T['returnType']> extends true
|
|
80
80
|
? EmptyObject
|
|
81
81
|
: {
|
|
82
|
-
set?: MaybeArray<WhereArg<Rel['
|
|
82
|
+
set?: MaybeArray<WhereArg<Rel['table']>>;
|
|
83
83
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
type UpdateHasAndBelongsToManyData<Rel extends HasAndBelongsToManyRelation> = {
|
|
87
|
-
disconnect?: MaybeArray<WhereArg<Rel['
|
|
88
|
-
set?: MaybeArray<WhereArg<Rel['
|
|
89
|
-
delete?: MaybeArray<WhereArg<Rel['
|
|
87
|
+
disconnect?: MaybeArray<WhereArg<Rel['table']>>;
|
|
88
|
+
set?: MaybeArray<WhereArg<Rel['table']>>;
|
|
89
|
+
delete?: MaybeArray<WhereArg<Rel['table']>>;
|
|
90
90
|
update?: {
|
|
91
|
-
where: MaybeArray<WhereArg<Rel['
|
|
92
|
-
data: UpdateData<Rel['
|
|
91
|
+
where: MaybeArray<WhereArg<Rel['table']>>;
|
|
92
|
+
data: UpdateData<Rel['table']>;
|
|
93
93
|
};
|
|
94
94
|
create?: CreateData<Rel['nestedCreateQuery']>[];
|
|
95
95
|
};
|
|
@@ -213,7 +213,7 @@ export class Update {
|
|
|
213
213
|
if (ctx.updateLater) {
|
|
214
214
|
await Promise.all(ctx.updateLaterPromises as Promise<void>[]);
|
|
215
215
|
|
|
216
|
-
const t = this.
|
|
216
|
+
const t = this.__table.clone().transacting(q);
|
|
217
217
|
const keys = this.primaryKeys;
|
|
218
218
|
(
|
|
219
219
|
t._whereIn as unknown as (
|
|
@@ -143,7 +143,7 @@ export abstract class Where implements QueryBase {
|
|
|
143
143
|
abstract shape: ColumnsShape;
|
|
144
144
|
abstract relations: RelationsBase;
|
|
145
145
|
abstract withData: WithDataBase;
|
|
146
|
-
abstract
|
|
146
|
+
abstract __table: Query;
|
|
147
147
|
|
|
148
148
|
query = {} as QueryData;
|
|
149
149
|
table?: string;
|
|
@@ -437,21 +437,21 @@ export class WhereQueryBuilder<Q extends QueryBase = QueryBase>
|
|
|
437
437
|
selectable!: Q['selectable'];
|
|
438
438
|
shape: Q['shape'];
|
|
439
439
|
relations!: Q['relations'];
|
|
440
|
-
|
|
440
|
+
__table: Query;
|
|
441
441
|
withData = {};
|
|
442
442
|
|
|
443
443
|
constructor(q: QueryBase | string, shape: ColumnsShape) {
|
|
444
444
|
super();
|
|
445
445
|
this.table = typeof q === 'object' ? q.table : q;
|
|
446
446
|
this.shape = shape;
|
|
447
|
-
this.
|
|
447
|
+
this.__table = this as unknown as Query;
|
|
448
448
|
if (typeof q === 'object' && q.query.as) {
|
|
449
449
|
this.query.as = q.query.as;
|
|
450
450
|
}
|
|
451
451
|
}
|
|
452
452
|
|
|
453
453
|
clone<T extends this>(this: T): T {
|
|
454
|
-
const cloned = Object.create(this.
|
|
454
|
+
const cloned = Object.create(this.__table);
|
|
455
455
|
cloned.query = getClonedQueryData(this.query);
|
|
456
456
|
return cloned as unknown as T;
|
|
457
457
|
}
|
package/src/relations.ts
CHANGED