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.
Files changed (91) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.d.ts +618 -563
  3. package/dist/index.esm.js +1011 -402
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +1014 -401
  6. package/dist/index.js.map +1 -1
  7. package/package.json +1 -1
  8. package/src/columnSchema/array.test.ts +67 -0
  9. package/src/columnSchema/array.ts +39 -13
  10. package/src/columnSchema/boolean.test.ts +17 -0
  11. package/src/columnSchema/boolean.ts +5 -1
  12. package/src/columnSchema/columnType.test.ts +230 -107
  13. package/src/columnSchema/columnType.ts +198 -28
  14. package/src/columnSchema/columnTypes.ts +28 -15
  15. package/src/columnSchema/columnsSchema.ts +6 -4
  16. package/src/columnSchema/commonMethods.ts +11 -4
  17. package/src/columnSchema/dateTime.test.ts +298 -0
  18. package/src/columnSchema/dateTime.ts +59 -2
  19. package/src/columnSchema/enum.test.ts +33 -0
  20. package/src/columnSchema/enum.ts +11 -1
  21. package/src/columnSchema/json/array.test.ts +21 -0
  22. package/src/columnSchema/json/array.ts +27 -13
  23. package/src/columnSchema/json/discriminatedUnion.test.ts +32 -0
  24. package/src/columnSchema/json/discriminatedUnion.ts +17 -2
  25. package/src/columnSchema/json/enum.test.ts +9 -0
  26. package/src/columnSchema/json/enum.ts +9 -1
  27. package/src/columnSchema/json/index.ts +19 -19
  28. package/src/columnSchema/json/instanceOf.test.ts +8 -0
  29. package/src/columnSchema/json/instanceOf.ts +4 -1
  30. package/src/columnSchema/json/intersection.test.ts +19 -0
  31. package/src/columnSchema/json/intersection.ts +9 -1
  32. package/src/columnSchema/json/lazy.test.ts +22 -0
  33. package/src/columnSchema/json/lazy.ts +22 -1
  34. package/src/columnSchema/json/literal.test.ts +7 -0
  35. package/src/columnSchema/json/literal.ts +12 -1
  36. package/src/columnSchema/json/map.test.ts +10 -0
  37. package/src/columnSchema/json/map.ts +21 -1
  38. package/src/columnSchema/json/nativeEnum.test.ts +10 -0
  39. package/src/columnSchema/json/nativeEnum.ts +4 -1
  40. package/src/columnSchema/json/nullable.test.ts +18 -0
  41. package/src/columnSchema/json/nullish.test.ts +18 -0
  42. package/src/columnSchema/json/object.test.ts +77 -0
  43. package/src/columnSchema/json/object.ts +31 -3
  44. package/src/columnSchema/json/optional.test.ts +18 -0
  45. package/src/columnSchema/json/record.test.ts +14 -0
  46. package/src/columnSchema/json/record.ts +12 -1
  47. package/src/columnSchema/json/scalarTypes.test.ts +133 -0
  48. package/src/columnSchema/json/scalarTypes.ts +90 -1
  49. package/src/columnSchema/json/set.test.ts +29 -0
  50. package/src/columnSchema/json/set.ts +26 -7
  51. package/src/columnSchema/json/tuple.test.ts +17 -0
  52. package/src/columnSchema/json/tuple.ts +16 -1
  53. package/src/columnSchema/json/typeBase.test.ts +123 -0
  54. package/src/columnSchema/json/typeBase.ts +52 -13
  55. package/src/columnSchema/json/union.test.ts +10 -0
  56. package/src/columnSchema/json/union.ts +18 -1
  57. package/src/columnSchema/json.test.ts +17 -0
  58. package/src/columnSchema/json.ts +10 -2
  59. package/src/columnSchema/number.test.ts +176 -0
  60. package/src/columnSchema/number.ts +48 -1
  61. package/src/columnSchema/string.test.ts +412 -0
  62. package/src/columnSchema/string.ts +126 -15
  63. package/src/columnSchema/timestamps.test.ts +6 -6
  64. package/src/columnSchema/virtual.ts +4 -0
  65. package/src/db.ts +1 -1
  66. package/src/query.ts +1 -1
  67. package/src/queryMethods/create.ts +6 -6
  68. package/src/queryMethods/for.ts +3 -3
  69. package/src/queryMethods/having.ts +1 -1
  70. package/src/queryMethods/join.ts +4 -4
  71. package/src/queryMethods/json.ts +1 -1
  72. package/src/queryMethods/queryMethods.ts +2 -2
  73. package/src/queryMethods/select.ts +3 -3
  74. package/src/queryMethods/update.ts +17 -17
  75. package/src/queryMethods/where.test.ts +1 -1
  76. package/src/queryMethods/where.ts +4 -4
  77. package/src/relations.ts +1 -1
  78. package/src/sql/aggregate.ts +2 -2
  79. package/src/sql/copy.ts +3 -3
  80. package/src/sql/delete.ts +5 -5
  81. package/src/sql/fromAndAs.ts +4 -4
  82. package/src/sql/having.ts +7 -7
  83. package/src/sql/insert.ts +5 -5
  84. package/src/sql/join.ts +16 -16
  85. package/src/sql/select.ts +6 -6
  86. package/src/sql/toSql.ts +24 -24
  87. package/src/sql/update.ts +4 -4
  88. package/src/sql/where.ts +18 -18
  89. package/src/utils.test.ts +9 -0
  90. package/src/utils.ts +3 -0
  91. 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 & { max: Limit };
71
+ data: TextColumnData & { arg: Limit };
47
72
 
48
73
  constructor(limit?: Limit) {
49
74
  super();
50
75
 
51
- this.data = { max: limit } as TextColumnData & { max: Limit };
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.max !== undefined && `(${this.data.max})`,
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 = 'point' as const;
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 = 'point' as const;
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 = 'point' as const;
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 = 'point' as const;
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 = 'point' as const;
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 = 'point' as const;
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
- parseFn = (input: unknown) => {
135
- return parseFloat((input as string).replace(/,/g, '').replace(/\$/g, ''));
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: 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 model = db('user', (t) => ({
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 = model.where().update({});
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 = model.where().update({ updatedAt: now });
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 = model
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 = model.where().updateRaw(db.raw('"createdAt" = "updatedAt"'));
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 = model
69
+ const query = table
70
70
  .where()
71
71
  .updateRaw(db.raw('"updatedAt" = $time', { time: now }));
72
72
 
@@ -10,6 +10,10 @@ export abstract class VirtualColumn extends ColumnType<
10
10
  dataType = '';
11
11
  operators = Operators.any;
12
12
 
13
+ toCode(): never {
14
+ throw new Error(`toCode is not implemented for virtual column`);
15
+ }
16
+
13
17
  create?(
14
18
  q: Query,
15
19
  ctx: CreateCtx,
package/src/db.ts CHANGED
@@ -132,7 +132,7 @@ export class Db<
132
132
  public columnTypes: CT,
133
133
  options: DbTableOptions,
134
134
  ) {
135
- this.__model = this as Query;
135
+ this.__table = this as Query;
136
136
 
137
137
  const logger = options.logger || console;
138
138
  this.query = {
package/src/query.ts CHANGED
@@ -38,7 +38,7 @@ export type QueryBase = {
38
38
  clone(): QueryBase;
39
39
  selectable: SelectableBase;
40
40
  shape: ColumnsShape;
41
- __model: Query;
41
+ __table: Query;
42
42
  relations: RelationsBase;
43
43
  withData: WithDataBase;
44
44
  };
@@ -72,14 +72,14 @@ type CreateBelongsToData<
72
72
  }
73
73
  | {
74
74
  create?: never;
75
- connect: WhereArg<Rel['model']>;
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['model']>;
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['model']>;
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['model']>;
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['model']>[];
127
+ connect?: WhereArg<Rel['table']>[];
128
128
  connectOrCreate?: {
129
- where: WhereArg<Rel['model']>;
129
+ where: WhereArg<Rel['table']>;
130
130
  create: CreateData<Rel['nestedCreateQuery']>;
131
131
  }[];
132
132
  };
@@ -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.__model = Object.create(q.__model);
19
- q.__model.__model = q.__model;
18
+ q.__table = Object.create(q.__table);
19
+ q.__table.__table = q.__table;
20
20
 
21
- Object.assign(q.__model, {
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 ('__model' in arg || isRaw(arg)) {
32
+ if ('__table' in arg || isRaw(arg)) {
33
33
  return arg;
34
34
  } else {
35
35
  const processed = { ...arg } as Record<
@@ -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]['model']
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
- __model: Query;
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]['model']
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]['model']
143
+ ? T['relations'][Arg]['table']
144
144
  : Arg extends keyof T['withData']
145
145
  ? T['withData'][Arg] extends WithDataItem
146
146
  ? {
@@ -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.__model.clone()) as T;
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
- __model!: Query;
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.__model);
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]['model']['result']>
53
+ ? ArrayOfColumnsObjects<T['relations'][Arg]['table']['result']>
54
54
  : T['relations'][Arg]['options']['required'] extends true
55
- ? ColumnsObject<T['relations'][Arg]['model']['result']>
55
+ ? ColumnsObject<T['relations'][Arg]['table']['result']>
56
56
  : NullableColumn<
57
- ColumnsObject<T['relations'][Arg]['model']['result']>
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['model']> }
39
+ | { set: WhereArg<Rel['table']> }
40
40
  | { delete: boolean }
41
- | { update: UpdateData<Rel['model']> }
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['model']>;
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['model']> }
57
+ | { update: UpdateData<Rel['table']> }
58
58
  | (QueryReturnsAll<T['returnType']> extends true
59
59
  ? never
60
60
  :
61
- | { set: WhereArg<Rel['model']> }
61
+ | { set: WhereArg<Rel['table']> }
62
62
  | {
63
63
  upsert: {
64
- update: UpdateData<Rel['model']>;
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['model']>>;
74
- delete?: MaybeArray<WhereArg<Rel['model']>>;
73
+ disconnect?: MaybeArray<WhereArg<Rel['table']>>;
74
+ delete?: MaybeArray<WhereArg<Rel['table']>>;
75
75
  update?: {
76
- where: MaybeArray<WhereArg<Rel['model']>>;
77
- data: UpdateData<Rel['model']>;
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['model']>>;
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['model']>>;
88
- set?: MaybeArray<WhereArg<Rel['model']>>;
89
- delete?: MaybeArray<WhereArg<Rel['model']>>;
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['model']>>;
92
- data: UpdateData<Rel['model']>;
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.__model.clone().transacting(q);
216
+ const t = this.__table.clone().transacting(q);
217
217
  const keys = this.primaryKeys;
218
218
  (
219
219
  t._whereIn as unknown as (
@@ -1563,7 +1563,7 @@ export const testJoin = (
1563
1563
  };
1564
1564
  };
1565
1565
 
1566
- Object.assign(withRelation.__model, {
1566
+ Object.assign(withRelation.__table, {
1567
1567
  relations: {
1568
1568
  message: {
1569
1569
  key: 'message',
@@ -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 __model: Query;
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
- __model: Query;
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.__model = this as unknown as Query;
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.__model);
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
@@ -5,7 +5,7 @@ import { EmptyObject } from './utils';
5
5
  export type BaseRelation = {
6
6
  type: string;
7
7
  key: string;
8
- model: QueryWithTable;
8
+ table: QueryWithTable;
9
9
  query: QueryWithTable;
10
10
  joinQuery(fromQuery: QueryBase, toQuery: Query): Query;
11
11
  nestedCreateQuery: Query;