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,8 +1,8 @@
|
|
|
1
1
|
import { Operator, Operators } from '../columnsOperators';
|
|
2
|
-
import { JSONTypeAny } from './json
|
|
2
|
+
import { JSONTypeAny } from './json';
|
|
3
3
|
import { ColumnsShape } from './columnsSchema';
|
|
4
4
|
import { RawExpression, StringKey } from '../common';
|
|
5
|
-
import { MaybeArray } from '../utils';
|
|
5
|
+
import { MaybeArray, singleQuote, toArray } from '../utils';
|
|
6
6
|
import { Query } from '../query';
|
|
7
7
|
|
|
8
8
|
export type ColumnOutput<T extends ColumnType> = T['type'];
|
|
@@ -100,17 +100,17 @@ export type IndexOptions = {
|
|
|
100
100
|
|
|
101
101
|
export type SingleColumnIndexOptions = IndexColumnOptions & IndexOptions;
|
|
102
102
|
|
|
103
|
-
export type
|
|
103
|
+
export type ForeignKeyTable = new () => {
|
|
104
104
|
table: string;
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
-
export type
|
|
107
|
+
export type ForeignKeyTableWithColumns = new () => {
|
|
108
108
|
table: string;
|
|
109
109
|
columns: { shape: ColumnsShape };
|
|
110
110
|
};
|
|
111
111
|
|
|
112
|
-
export type
|
|
113
|
-
StringKey<keyof InstanceType<
|
|
112
|
+
export type ColumnNameOfTable<Table extends ForeignKeyTableWithColumns> =
|
|
113
|
+
StringKey<keyof InstanceType<Table>['columns']['shape']>;
|
|
114
114
|
|
|
115
115
|
const addColumnData = <T extends ColumnType, K extends keyof ColumnData>(
|
|
116
116
|
q: T,
|
|
@@ -122,6 +122,177 @@ const addColumnData = <T extends ColumnType, K extends keyof ColumnData>(
|
|
|
122
122
|
return cloned;
|
|
123
123
|
};
|
|
124
124
|
|
|
125
|
+
export type Code = string | Code[];
|
|
126
|
+
|
|
127
|
+
export const columnChainToCode = (
|
|
128
|
+
chain: ColumnChain,
|
|
129
|
+
t: string,
|
|
130
|
+
code: Code,
|
|
131
|
+
append: Code,
|
|
132
|
+
): Code => {
|
|
133
|
+
const result = toArray(code) as Code[];
|
|
134
|
+
if (typeof append === 'string') {
|
|
135
|
+
if (append) {
|
|
136
|
+
if (result.length === 1 && typeof result[0] === 'string') {
|
|
137
|
+
result[0] += append;
|
|
138
|
+
} else {
|
|
139
|
+
result.push(append);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
if (append.length) result.push(...append);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (const item of chain) {
|
|
147
|
+
if (item[0] === 'transform') {
|
|
148
|
+
result.push(`.transform(${item[1].toString()})`);
|
|
149
|
+
} else if (item[0] === 'to') {
|
|
150
|
+
const type = toArray(item[2].toCode(t));
|
|
151
|
+
result.push(`.to(${item[1].toString()}, `, ...type, ')');
|
|
152
|
+
} else if (item[0] === 'refine') {
|
|
153
|
+
result.push(`.refine(${item[1].toString()})`);
|
|
154
|
+
} else if (item[0] === 'superRefine') {
|
|
155
|
+
result.push(`.superRefine(${item[1].toString()})`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return result.length === 1 && typeof result[0] === 'string'
|
|
160
|
+
? result[0]
|
|
161
|
+
: result;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export const columnCode = (type: ColumnType, t: string, code: Code): Code => {
|
|
165
|
+
const append: Code[] = [];
|
|
166
|
+
|
|
167
|
+
const { foreignKey, index, validationDefault } = type.data;
|
|
168
|
+
|
|
169
|
+
if (type.isPrimaryKey) append.push('.primaryKey()');
|
|
170
|
+
|
|
171
|
+
if (foreignKey) {
|
|
172
|
+
append.push(`.foreignKey(`);
|
|
173
|
+
if ('fn' in foreignKey) {
|
|
174
|
+
append.push(foreignKey.fn.toString());
|
|
175
|
+
} else {
|
|
176
|
+
append.push(singleQuote(foreignKey.table));
|
|
177
|
+
}
|
|
178
|
+
append.push(`, ${singleQuote(foreignKey.columns[0])}`);
|
|
179
|
+
|
|
180
|
+
const hasOptions =
|
|
181
|
+
foreignKey.name ||
|
|
182
|
+
foreignKey.match ||
|
|
183
|
+
foreignKey.onUpdate ||
|
|
184
|
+
foreignKey.onDelete;
|
|
185
|
+
|
|
186
|
+
if (hasOptions) {
|
|
187
|
+
const arr: string[] = [];
|
|
188
|
+
|
|
189
|
+
if (foreignKey.name) arr.push(`name: ${singleQuote(foreignKey.name)},`);
|
|
190
|
+
if (foreignKey.match)
|
|
191
|
+
arr.push(`match: ${singleQuote(foreignKey.match)},`);
|
|
192
|
+
if (foreignKey.onUpdate)
|
|
193
|
+
arr.push(`onUpdate: ${singleQuote(foreignKey.onUpdate)},`);
|
|
194
|
+
if (foreignKey.onDelete)
|
|
195
|
+
arr.push(`onDelete: ${singleQuote(foreignKey.onDelete)},`);
|
|
196
|
+
|
|
197
|
+
append.push(', {', arr, '}');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
append.push(')');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (type.isHidden) append.push('.hidden()');
|
|
204
|
+
|
|
205
|
+
if (type.isNullable) append.push('.nullable()');
|
|
206
|
+
|
|
207
|
+
if ('isNonEmpty' in type.data) append.push('.nonEmpty()');
|
|
208
|
+
|
|
209
|
+
if (type.encodeFn) append.push(`.encode(${type.encodeFn.toString()})`);
|
|
210
|
+
|
|
211
|
+
if (type.parseFn && !('hideFromCode' in type.parseFn))
|
|
212
|
+
append.push(`.parse(${type.parseFn.toString()})`);
|
|
213
|
+
|
|
214
|
+
if (type.data.as) append.push(`.as(${type.data.as.toCode(t)})`);
|
|
215
|
+
|
|
216
|
+
if (type.data.default)
|
|
217
|
+
append.push(`.default(${JSON.stringify(type.data.default)})`);
|
|
218
|
+
|
|
219
|
+
if (index) {
|
|
220
|
+
append.push(`.${index.unique ? 'unique' : 'index'}(`);
|
|
221
|
+
|
|
222
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
223
|
+
for (const key in index) {
|
|
224
|
+
if (key === 'unique') continue;
|
|
225
|
+
|
|
226
|
+
const arr: string[] = [];
|
|
227
|
+
|
|
228
|
+
if (index.expression)
|
|
229
|
+
arr.push(
|
|
230
|
+
`expression: ${
|
|
231
|
+
typeof index.expression === 'string'
|
|
232
|
+
? singleQuote(index.expression)
|
|
233
|
+
: index.expression
|
|
234
|
+
},`,
|
|
235
|
+
);
|
|
236
|
+
if (index.collate) arr.push(`collate: ${singleQuote(index.collate)},`);
|
|
237
|
+
if (index.operator) arr.push(`operator: ${singleQuote(index.operator)},`);
|
|
238
|
+
if (index.order) arr.push(`order: ${singleQuote(index.order)},`);
|
|
239
|
+
if (index.name) arr.push(`name: ${singleQuote(index.name)},`);
|
|
240
|
+
if (index.unique) arr.push(`unique: true,`);
|
|
241
|
+
if (index.using) arr.push(`using: ${singleQuote(index.using)},`);
|
|
242
|
+
if (index.include)
|
|
243
|
+
arr.push(
|
|
244
|
+
`include: ${
|
|
245
|
+
typeof index.include === 'string'
|
|
246
|
+
? singleQuote(index.include)
|
|
247
|
+
: `[${index.include.map(singleQuote).join(', ')}]`
|
|
248
|
+
},`,
|
|
249
|
+
);
|
|
250
|
+
if (index.with) arr.push(`with: ${singleQuote(index.with)},`);
|
|
251
|
+
if (index.tablespace)
|
|
252
|
+
arr.push(`tablespace: ${singleQuote(index.tablespace)},`);
|
|
253
|
+
if (index.where) arr.push(`where: ${singleQuote(index.where)},`);
|
|
254
|
+
|
|
255
|
+
append.push('{', arr, '}');
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
append.push(')');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (type.data.comment)
|
|
263
|
+
append.push(`.comment(${singleQuote(type.data.comment)})`);
|
|
264
|
+
|
|
265
|
+
if (validationDefault) {
|
|
266
|
+
append.push(
|
|
267
|
+
`.validationDefault(${
|
|
268
|
+
typeof validationDefault === 'function'
|
|
269
|
+
? validationDefault.toString()
|
|
270
|
+
: typeof validationDefault === 'string'
|
|
271
|
+
? singleQuote(validationDefault)
|
|
272
|
+
: JSON.stringify(validationDefault)
|
|
273
|
+
})`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (type.data.compression)
|
|
278
|
+
append.push(`.compression(${singleQuote(type.data.compression)})`);
|
|
279
|
+
|
|
280
|
+
if (type.data.collate)
|
|
281
|
+
append.push(`.collate(${singleQuote(type.data.collate)})`);
|
|
282
|
+
|
|
283
|
+
if (type.data.modifyQuery)
|
|
284
|
+
append.push(`.modifyQuery(${type.data.modifyQuery.toString()})`);
|
|
285
|
+
|
|
286
|
+
return columnChainToCode(type.chain, t, code, append);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export type ColumnChain = (
|
|
290
|
+
| ['transform', (input: unknown, ctx: ValidationContext) => unknown]
|
|
291
|
+
| ['to', (input: unknown) => JSONTypeAny | undefined, JSONTypeAny]
|
|
292
|
+
| ['refine', (input: unknown) => unknown]
|
|
293
|
+
| ['superRefine', (input: unknown, ctx: ValidationContext) => unknown]
|
|
294
|
+
)[];
|
|
295
|
+
|
|
125
296
|
export abstract class ColumnType<
|
|
126
297
|
Type = unknown,
|
|
127
298
|
Ops extends Operators = Operators,
|
|
@@ -129,6 +300,7 @@ export abstract class ColumnType<
|
|
|
129
300
|
> {
|
|
130
301
|
abstract dataType: string;
|
|
131
302
|
abstract operators: Ops;
|
|
303
|
+
abstract toCode(t: string): Code;
|
|
132
304
|
|
|
133
305
|
type!: Type;
|
|
134
306
|
inputType!: InputType;
|
|
@@ -144,28 +316,24 @@ export abstract class ColumnType<
|
|
|
144
316
|
// parse item in array:
|
|
145
317
|
parseItem?: (input: string) => unknown;
|
|
146
318
|
|
|
147
|
-
chain = [] as
|
|
148
|
-
| ['transform', (input: unknown, ctx: ValidationContext) => unknown]
|
|
149
|
-
| ['to', (input: unknown) => JSONTypeAny | undefined, JSONTypeAny]
|
|
150
|
-
| ['refine', (input: unknown) => unknown]
|
|
151
|
-
| ['superRefine', (input: unknown, ctx: ValidationContext) => unknown]
|
|
152
|
-
)[];
|
|
319
|
+
chain = [] as ColumnChain;
|
|
153
320
|
|
|
154
321
|
primaryKey<T extends ColumnType>(this: T): T & { isPrimaryKey: true } {
|
|
155
|
-
|
|
322
|
+
const cloned = Object.create(this);
|
|
323
|
+
return Object.assign(cloned, { isPrimaryKey: true as const });
|
|
156
324
|
}
|
|
157
325
|
|
|
158
326
|
foreignKey<
|
|
159
327
|
T extends ColumnType,
|
|
160
|
-
|
|
161
|
-
Column extends
|
|
328
|
+
Table extends ForeignKeyTableWithColumns,
|
|
329
|
+
Column extends ColumnNameOfTable<Table>,
|
|
162
330
|
>(
|
|
163
331
|
this: T,
|
|
164
|
-
fn: () =>
|
|
332
|
+
fn: () => Table,
|
|
165
333
|
column: Column,
|
|
166
334
|
options?: ForeignKeyOptions,
|
|
167
335
|
): Omit<T, 'foreignKeyData'> & {
|
|
168
|
-
foreignKeyData: ForeignKey<InstanceType<
|
|
336
|
+
foreignKeyData: ForeignKey<InstanceType<Table>['table'], [Column]>;
|
|
169
337
|
};
|
|
170
338
|
foreignKey<T extends ColumnType, Table extends string, Column extends string>(
|
|
171
339
|
this: T,
|
|
@@ -176,7 +344,7 @@ export abstract class ColumnType<
|
|
|
176
344
|
foreignKeyData: ForeignKey<Table, [Column]>;
|
|
177
345
|
};
|
|
178
346
|
foreignKey(
|
|
179
|
-
fnOrTable: (() =>
|
|
347
|
+
fnOrTable: (() => ForeignKeyTable) | string,
|
|
180
348
|
column: string,
|
|
181
349
|
options: ForeignKeyOptions = {},
|
|
182
350
|
) {
|
|
@@ -196,30 +364,32 @@ export abstract class ColumnType<
|
|
|
196
364
|
}
|
|
197
365
|
|
|
198
366
|
hidden<T extends ColumnType>(this: T): T & { isHidden: true } {
|
|
199
|
-
return Object.assign(this, { isHidden: true as const });
|
|
367
|
+
return Object.assign(Object.create(this), { isHidden: true as const });
|
|
200
368
|
}
|
|
201
369
|
|
|
202
370
|
nullable<T extends ColumnType>(this: T): NullableColumn<T> {
|
|
203
|
-
this
|
|
204
|
-
|
|
371
|
+
return Object.assign(Object.create(this), {
|
|
372
|
+
isNullable: true,
|
|
373
|
+
}) as unknown as NullableColumn<T>;
|
|
205
374
|
}
|
|
206
375
|
|
|
207
376
|
encode<T extends ColumnType, Input>(
|
|
208
377
|
this: T,
|
|
209
378
|
fn: (input: Input) => unknown,
|
|
210
379
|
): Omit<T, 'inputType'> & { inputType: Input } {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
380
|
+
return Object.assign(Object.create(this), {
|
|
381
|
+
encodeFn: fn,
|
|
382
|
+
}) as unknown as Omit<T, 'inputType'> & { inputType: Input };
|
|
214
383
|
}
|
|
215
384
|
|
|
216
385
|
parse<T extends ColumnType, Output>(
|
|
217
386
|
this: T,
|
|
218
387
|
fn: (input: T['type']) => Output,
|
|
219
388
|
): Omit<T, 'type'> & { type: Output } {
|
|
220
|
-
this
|
|
221
|
-
|
|
222
|
-
|
|
389
|
+
return Object.assign(Object.create(this), {
|
|
390
|
+
parseFn: fn,
|
|
391
|
+
parseItem: fn,
|
|
392
|
+
}) as unknown as Omit<T, 'type'> & { type: Output };
|
|
223
393
|
}
|
|
224
394
|
|
|
225
395
|
as<
|
|
@@ -290,7 +460,7 @@ export abstract class ColumnType<
|
|
|
290
460
|
fn: (input: T['type']) => ToType['type'] | undefined,
|
|
291
461
|
type: ToType,
|
|
292
462
|
): ToType {
|
|
293
|
-
const cloned = Object.create(
|
|
463
|
+
const cloned = Object.create(this);
|
|
294
464
|
cloned.chain = [...this.chain, ['to', fn, type], ...cloned.chain];
|
|
295
465
|
return cloned as ToType;
|
|
296
466
|
}
|
|
@@ -43,18 +43,17 @@ import {
|
|
|
43
43
|
} from './dateTime';
|
|
44
44
|
import { BooleanColumn } from './boolean';
|
|
45
45
|
import { EnumColumn } from './enum';
|
|
46
|
-
import { JSONColumn, JSONTextColumn, JSONTypes } from './json';
|
|
47
|
-
import { JSONTypeAny } from './json/typeBase';
|
|
46
|
+
import { JSONColumn, JSONTextColumn, JSONTypes, JSONTypeAny } from './json';
|
|
48
47
|
import { ArrayColumn } from './array';
|
|
49
48
|
import {
|
|
50
|
-
|
|
49
|
+
ColumnNameOfTable,
|
|
51
50
|
ColumnType,
|
|
52
51
|
ColumnTypesBase,
|
|
53
|
-
|
|
52
|
+
ForeignKeyTable,
|
|
54
53
|
IndexColumnOptions,
|
|
55
54
|
IndexOptions,
|
|
56
55
|
ForeignKeyOptions,
|
|
57
|
-
|
|
56
|
+
ForeignKeyTableWithColumns,
|
|
58
57
|
} from './columnType';
|
|
59
58
|
import { emptyObject, EmptyObject, MaybeArray, toArray } from '../utils';
|
|
60
59
|
import { ColumnsShape } from './columnsSchema';
|
|
@@ -63,15 +62,29 @@ import { timestamps } from './timestamps';
|
|
|
63
62
|
export type ColumnTypes = typeof columnTypes;
|
|
64
63
|
|
|
65
64
|
export type TableData = {
|
|
66
|
-
primaryKey?:
|
|
67
|
-
indexes:
|
|
68
|
-
foreignKeys:
|
|
65
|
+
primaryKey?: TableData.PrimaryKey;
|
|
66
|
+
indexes: TableData.Index[];
|
|
67
|
+
foreignKeys: TableData.ForeignKey[];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export namespace TableData {
|
|
71
|
+
export type PrimaryKey = {
|
|
72
|
+
columns: string[];
|
|
73
|
+
options?: { name?: string };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type Index = {
|
|
77
|
+
columns: IndexColumnOptions[];
|
|
78
|
+
options: IndexOptions;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export type ForeignKey = {
|
|
69
82
|
columns: string[];
|
|
70
|
-
fnOrTable: (() =>
|
|
83
|
+
fnOrTable: (() => ForeignKeyTable) | string;
|
|
71
84
|
foreignColumns: string[];
|
|
72
85
|
options: ForeignKeyOptions;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
75
88
|
|
|
76
89
|
export const newTableData = (): TableData => ({
|
|
77
90
|
indexes: [],
|
|
@@ -221,11 +234,11 @@ export const columnTypes = {
|
|
|
221
234
|
};
|
|
222
235
|
|
|
223
236
|
function foreignKey<
|
|
224
|
-
|
|
225
|
-
Columns extends [
|
|
237
|
+
Table extends ForeignKeyTableWithColumns,
|
|
238
|
+
Columns extends [ColumnNameOfTable<Table>, ...ColumnNameOfTable<Table>[]],
|
|
226
239
|
>(
|
|
227
240
|
columns: string[],
|
|
228
|
-
fn: () =>
|
|
241
|
+
fn: () => Table,
|
|
229
242
|
foreignColumns: Columns,
|
|
230
243
|
options?: ForeignKeyOptions,
|
|
231
244
|
): EmptyObject;
|
|
@@ -240,7 +253,7 @@ function foreignKey<
|
|
|
240
253
|
): EmptyObject;
|
|
241
254
|
function foreignKey(
|
|
242
255
|
columns: string[],
|
|
243
|
-
fnOrTable: (() =>
|
|
256
|
+
fnOrTable: (() => ForeignKeyTable) | string,
|
|
244
257
|
foreignColumns: string[],
|
|
245
258
|
options: ForeignKeyOptions = {},
|
|
246
259
|
) {
|
|
@@ -24,11 +24,13 @@ export type ColumnShapeInput<Shape extends ColumnsShape> = SetOptional<
|
|
|
24
24
|
OptionalColumnsForInput<Shape>
|
|
25
25
|
>;
|
|
26
26
|
|
|
27
|
-
export class ColumnsObject<
|
|
27
|
+
export abstract class ColumnsObject<
|
|
28
|
+
Shape extends ColumnsShape,
|
|
29
|
+
> extends ColumnType<
|
|
28
30
|
{ [K in keyof Shape]: Shape[K]['type'] },
|
|
29
31
|
typeof Operators.any
|
|
30
32
|
> {
|
|
31
|
-
dataType = 'object';
|
|
33
|
+
dataType = 'object' as const;
|
|
32
34
|
operators = Operators.any;
|
|
33
35
|
|
|
34
36
|
constructor(public shape: Shape) {
|
|
@@ -36,13 +38,13 @@ export class ColumnsObject<Shape extends ColumnsShape> extends ColumnType<
|
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
export class ArrayOfColumnsObjects<
|
|
41
|
+
export abstract class ArrayOfColumnsObjects<
|
|
40
42
|
Shape extends ColumnsShape,
|
|
41
43
|
> extends ColumnType<
|
|
42
44
|
{ [K in keyof Shape]: Shape[K]['type'] }[],
|
|
43
45
|
typeof Operators.any
|
|
44
46
|
> {
|
|
45
|
-
dataType = 'array';
|
|
47
|
+
dataType = 'array' as const;
|
|
46
48
|
operators = Operators.any;
|
|
47
49
|
|
|
48
50
|
constructor(public shape: Shape) {
|
|
@@ -42,8 +42,15 @@ function size<T extends { data: { size?: number } }, Value extends number>(
|
|
|
42
42
|
return setDataValue(this, 'size', value);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
export type NonEmptyBase = { data: { min?: number; isNonEmpty?: true } };
|
|
46
|
+
export type NonEmptyResult<T extends NonEmptyBase> = Omit<T, 'data'> & {
|
|
47
|
+
data: Omit<T['data'], 'min'> & { min: 1; isNonEmpty: true };
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function nonEmpty<T extends NonEmptyBase>(this: T): NonEmptyResult<T> {
|
|
51
|
+
const cloned = setDataValue(this, 'min', 1);
|
|
52
|
+
cloned.data.isNonEmpty = true;
|
|
53
|
+
return cloned as NonEmptyResult<T>;
|
|
47
54
|
}
|
|
48
55
|
|
|
49
56
|
export type ArrayMethods = typeof arrayMethods;
|
|
@@ -52,7 +59,7 @@ export const arrayMethods = {
|
|
|
52
59
|
min,
|
|
53
60
|
max,
|
|
54
61
|
length,
|
|
55
|
-
|
|
62
|
+
nonEmpty,
|
|
56
63
|
};
|
|
57
64
|
|
|
58
65
|
export type SetMethods = typeof setMethods;
|
|
@@ -61,7 +68,7 @@ export const setMethods = {
|
|
|
61
68
|
min,
|
|
62
69
|
max,
|
|
63
70
|
size,
|
|
64
|
-
|
|
71
|
+
nonEmpty,
|
|
65
72
|
};
|
|
66
73
|
|
|
67
74
|
export const stringTypeMethods = () => ({
|