pqb 0.0.1 → 0.0.3
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/README.md +5 -0
- package/package.json +5 -2
- package/rollup.config.js +2 -34
- package/src/adapter.ts +11 -9
- package/src/columnSchema/array.ts +115 -3
- package/src/columnSchema/boolean.ts +4 -1
- package/src/columnSchema/columnType.test.ts +1 -1
- package/src/columnSchema/columnType.ts +227 -5
- package/src/columnSchema/columnTypes.test.ts +568 -0
- package/src/columnSchema/columnTypes.ts +136 -24
- package/src/columnSchema/columnsSchema.ts +1 -1
- package/src/columnSchema/commonMethods.ts +162 -80
- package/src/columnSchema/dateTime.ts +41 -10
- package/src/columnSchema/enum.ts +11 -6
- package/src/columnSchema/json/discriminatedUnion.ts +51 -42
- package/src/columnSchema/json/enum.ts +9 -9
- package/src/columnSchema/json/lazy.ts +5 -3
- package/src/columnSchema/json/nativeEnum.ts +1 -1
- package/src/columnSchema/json/nullish.ts +1 -1
- package/src/columnSchema/json/record.ts +14 -11
- package/src/columnSchema/json/scalarTypes.ts +17 -8
- package/src/columnSchema/json/set.ts +4 -4
- package/src/columnSchema/json/tuple.ts +17 -2
- package/src/columnSchema/json/typeBase.ts +11 -16
- package/src/columnSchema/json/union.ts +1 -1
- package/src/columnSchema/json.ts +4 -4
- package/src/columnSchema/number.ts +41 -30
- package/src/columnSchema/string.ts +28 -19
- package/src/columnSchema/utils.ts +0 -2
- package/src/{operators.test.ts → columnsOperators.test.ts} +0 -0
- package/src/{operators.ts → columnsOperators.ts} +0 -0
- package/src/common.ts +18 -16
- package/src/db.ts +10 -8
- package/src/index.ts +2 -7
- package/src/query.ts +2 -2
- package/src/queryMethods/aggregate.ts +6 -3
- package/src/queryMethods/get.ts +6 -3
- package/src/queryMethods/index.ts +22 -0
- package/src/queryMethods/join.ts +1 -1
- package/src/queryMethods/log.ts +5 -5
- package/src/queryMethods/select.test.ts +2 -2
- package/src/queryMethods/select.ts +10 -7
- package/src/queryMethods/then.ts +8 -19
- package/src/queryMethods/transaction.test.ts +2 -2
- package/src/queryMethods/transaction.ts +5 -5
- package/src/queryMethods/update.ts +11 -14
- package/src/sql/having.ts +1 -1
- package/src/test-utils.ts +3 -3
- package/src/utils.ts +3 -0
- package/dist/index.d.ts +0 -3630
- package/dist/index.esm.js +0 -4587
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -4691
- package/dist/index.js.map +0 -1
- package/tsconfig.build.json +0 -6
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BigIntColumn,
|
|
3
3
|
BigSerialColumn,
|
|
4
|
-
DecimalBigIntColumn,
|
|
5
4
|
DecimalColumn,
|
|
6
5
|
DoublePrecisionColumn,
|
|
7
6
|
IntegerColumn,
|
|
@@ -47,10 +46,58 @@ import { EnumColumn } from './enum';
|
|
|
47
46
|
import { JSONColumn, JSONTextColumn, JSONTypes } from './json';
|
|
48
47
|
import { JSONTypeAny } from './json/typeBase';
|
|
49
48
|
import { ArrayColumn } from './array';
|
|
50
|
-
import {
|
|
49
|
+
import {
|
|
50
|
+
ColumnNameOfModel,
|
|
51
|
+
ColumnType,
|
|
52
|
+
ColumnTypesBase,
|
|
53
|
+
ForeignKeyModel,
|
|
54
|
+
IndexColumnOptions,
|
|
55
|
+
IndexOptions,
|
|
56
|
+
ForeignKeyOptions,
|
|
57
|
+
ForeignKeyModelWithColumns,
|
|
58
|
+
} from './columnType';
|
|
59
|
+
import { emptyObject, EmptyObject, MaybeArray, toArray } from '../utils';
|
|
60
|
+
import { ColumnsShape } from './columnsSchema';
|
|
61
|
+
import { raw } from '../common';
|
|
51
62
|
|
|
52
63
|
export type ColumnTypes = typeof columnTypes;
|
|
53
64
|
|
|
65
|
+
export type TableData = {
|
|
66
|
+
primaryKey?: { columns: string[]; options?: { name?: string } };
|
|
67
|
+
indexes: { columns: IndexColumnOptions[]; options: IndexOptions }[];
|
|
68
|
+
foreignKeys: {
|
|
69
|
+
columns: string[];
|
|
70
|
+
fnOrTable: (() => ForeignKeyModel) | string;
|
|
71
|
+
foreignColumns: string[];
|
|
72
|
+
options: ForeignKeyOptions;
|
|
73
|
+
}[];
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const newTableData = (): TableData => ({
|
|
77
|
+
indexes: [],
|
|
78
|
+
foreignKeys: [],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
let tableData: TableData = newTableData();
|
|
82
|
+
|
|
83
|
+
export const getTableData = () => tableData;
|
|
84
|
+
|
|
85
|
+
export const resetTableData = (data: TableData = newTableData()) => {
|
|
86
|
+
tableData = data;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const getColumnTypes = <
|
|
90
|
+
CT extends ColumnTypesBase,
|
|
91
|
+
Shape extends ColumnsShape,
|
|
92
|
+
>(
|
|
93
|
+
types: CT,
|
|
94
|
+
fn: (t: CT) => Shape,
|
|
95
|
+
data: TableData = newTableData(),
|
|
96
|
+
) => {
|
|
97
|
+
resetTableData(data);
|
|
98
|
+
return fn(types);
|
|
99
|
+
};
|
|
100
|
+
|
|
54
101
|
export const columnTypes = {
|
|
55
102
|
smallint: () => new SmallIntColumn(),
|
|
56
103
|
integer: () => new IntegerColumn(),
|
|
@@ -62,13 +109,6 @@ export const columnTypes = {
|
|
|
62
109
|
precision?: Precision,
|
|
63
110
|
scale?: Scale,
|
|
64
111
|
) => new DecimalColumn(precision, scale),
|
|
65
|
-
numericBigInt: <
|
|
66
|
-
Precision extends number | undefined = undefined,
|
|
67
|
-
Scale extends number | undefined = undefined,
|
|
68
|
-
>(
|
|
69
|
-
precision?: Precision,
|
|
70
|
-
scale?: Scale,
|
|
71
|
-
) => new DecimalBigIntColumn(precision, scale),
|
|
72
112
|
decimal: <
|
|
73
113
|
Precision extends number | undefined = undefined,
|
|
74
114
|
Scale extends number | undefined = undefined,
|
|
@@ -76,24 +116,18 @@ export const columnTypes = {
|
|
|
76
116
|
precision?: Precision,
|
|
77
117
|
scale?: Scale,
|
|
78
118
|
) => new DecimalColumn(precision, scale),
|
|
79
|
-
decimalBigInt: <
|
|
80
|
-
Precision extends number | undefined = undefined,
|
|
81
|
-
Scale extends number | undefined = undefined,
|
|
82
|
-
>(
|
|
83
|
-
precision?: Precision,
|
|
84
|
-
scale?: Scale,
|
|
85
|
-
) => new DecimalBigIntColumn(precision, scale),
|
|
86
119
|
real: () => new RealColumn(),
|
|
87
120
|
doublePrecision: () => new DoublePrecisionColumn(),
|
|
88
121
|
smallSerial: () => new SmallSerialColumn(),
|
|
89
122
|
serial: () => new SerialColumn(),
|
|
90
|
-
|
|
123
|
+
bigSerial: () => new BigSerialColumn(),
|
|
91
124
|
money: () => new MoneyColumn(),
|
|
92
125
|
varchar: <Limit extends number | undefined = undefined>(limit?: Limit) =>
|
|
93
126
|
new VarCharColumn(limit),
|
|
94
127
|
char: <Limit extends number | undefined = undefined>(limit?: Limit) =>
|
|
95
128
|
new CharColumn(limit),
|
|
96
129
|
text: () => new TextColumn(),
|
|
130
|
+
string: () => new TextColumn(),
|
|
97
131
|
bytea: () => new ByteaColumn(),
|
|
98
132
|
date: () => new DateColumn(),
|
|
99
133
|
timestamp: <Precision extends number | undefined = undefined>(
|
|
@@ -116,8 +150,8 @@ export const columnTypes = {
|
|
|
116
150
|
precision?: Precision,
|
|
117
151
|
) => new IntervalColumn(fields, precision),
|
|
118
152
|
boolean: () => new BooleanColumn(),
|
|
119
|
-
enum: <
|
|
120
|
-
new EnumColumn<
|
|
153
|
+
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) =>
|
|
154
|
+
new EnumColumn<U, T>(dataType, type),
|
|
121
155
|
point: () => new PointColumn(),
|
|
122
156
|
line: () => new LineColumn(),
|
|
123
157
|
lseg: () => new LsegColumn(),
|
|
@@ -125,14 +159,14 @@ export const columnTypes = {
|
|
|
125
159
|
path: () => new PathColumn(),
|
|
126
160
|
polygon: () => new PolygonColumn(),
|
|
127
161
|
circle: () => new CircleColumn(),
|
|
128
|
-
cidr()
|
|
129
|
-
return new CidrColumn();
|
|
130
|
-
},
|
|
162
|
+
cidr: () => new CidrColumn(),
|
|
131
163
|
inet: () => new InetColumn(),
|
|
132
164
|
macaddr: () => new MacAddrColumn(),
|
|
133
165
|
macaddr8: () => new MacAddr8Column(),
|
|
134
|
-
bit: () => new BitColumn(),
|
|
135
|
-
bitVarying:
|
|
166
|
+
bit: <Length extends number>(length: Length) => new BitColumn(length),
|
|
167
|
+
bitVarying: <Length extends number | undefined = undefined>(
|
|
168
|
+
length?: Length,
|
|
169
|
+
) => new BitVaryingColumn(length),
|
|
136
170
|
tsvector: () => new TsVectorColumn(),
|
|
137
171
|
tsquery: () => new TsQueryColumn(),
|
|
138
172
|
uuid: () => new UUIDColumn(),
|
|
@@ -142,4 +176,82 @@ export const columnTypes = {
|
|
|
142
176
|
) => new JSONColumn(schemaOrFn),
|
|
143
177
|
jsonText: () => new JSONTextColumn(),
|
|
144
178
|
array: <Item extends ColumnType>(item: Item) => new ArrayColumn(item),
|
|
179
|
+
|
|
180
|
+
timestamps: () => ({
|
|
181
|
+
createdAt: new TimestampColumn().default(raw('now()')),
|
|
182
|
+
updatedAt: new TimestampColumn().default(raw('now()')),
|
|
183
|
+
}),
|
|
184
|
+
|
|
185
|
+
primaryKey(columns: string[], options?: { name?: string }) {
|
|
186
|
+
tableData.primaryKey = { columns, options };
|
|
187
|
+
return emptyObject;
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
index(
|
|
191
|
+
columns: MaybeArray<string | IndexColumnOptions>,
|
|
192
|
+
options: IndexOptions = {},
|
|
193
|
+
) {
|
|
194
|
+
const index = {
|
|
195
|
+
columns: toArray(columns).map((column) =>
|
|
196
|
+
typeof column === 'string' ? { column } : column,
|
|
197
|
+
),
|
|
198
|
+
options,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
tableData.indexes.push(index);
|
|
202
|
+
return emptyObject;
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
unique(
|
|
206
|
+
columns: MaybeArray<string | IndexColumnOptions>,
|
|
207
|
+
options: IndexOptions = {},
|
|
208
|
+
) {
|
|
209
|
+
const index = {
|
|
210
|
+
columns: toArray(columns).map((column) =>
|
|
211
|
+
typeof column === 'string' ? { column } : column,
|
|
212
|
+
),
|
|
213
|
+
options: { ...options, unique: true },
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
tableData.indexes.push(index);
|
|
217
|
+
|
|
218
|
+
return emptyObject;
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
foreignKey,
|
|
145
222
|
};
|
|
223
|
+
|
|
224
|
+
function foreignKey<
|
|
225
|
+
Model extends ForeignKeyModelWithColumns,
|
|
226
|
+
Columns extends [ColumnNameOfModel<Model>, ...ColumnNameOfModel<Model>[]],
|
|
227
|
+
>(
|
|
228
|
+
columns: string[],
|
|
229
|
+
fn: () => Model,
|
|
230
|
+
foreignColumns: Columns,
|
|
231
|
+
options?: ForeignKeyOptions,
|
|
232
|
+
): EmptyObject;
|
|
233
|
+
function foreignKey<
|
|
234
|
+
Table extends string,
|
|
235
|
+
Columns extends [string, ...string[]],
|
|
236
|
+
>(
|
|
237
|
+
columns: string[],
|
|
238
|
+
table: Table,
|
|
239
|
+
foreignColumns: Columns,
|
|
240
|
+
options?: ForeignKeyOptions,
|
|
241
|
+
): EmptyObject;
|
|
242
|
+
function foreignKey(
|
|
243
|
+
columns: string[],
|
|
244
|
+
fnOrTable: (() => ForeignKeyModel) | string,
|
|
245
|
+
foreignColumns: string[],
|
|
246
|
+
options: ForeignKeyOptions = {},
|
|
247
|
+
) {
|
|
248
|
+
const foreignKey = {
|
|
249
|
+
columns,
|
|
250
|
+
fnOrTable,
|
|
251
|
+
foreignColumns,
|
|
252
|
+
options,
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
tableData.foreignKeys.push(foreignKey);
|
|
256
|
+
return emptyObject;
|
|
257
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ColumnInput, ColumnOutput, ColumnType } from './columnType';
|
|
2
|
-
import { Operators } from '../
|
|
2
|
+
import { Operators } from '../columnsOperators';
|
|
3
3
|
import { UnionToIntersection } from '../utils';
|
|
4
4
|
|
|
5
5
|
export type ColumnsShape = Record<string, ColumnType>;
|
|
@@ -1,130 +1,212 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export const setDataValue = <
|
|
2
|
+
T extends { data: object },
|
|
3
|
+
Key extends string,
|
|
4
|
+
Value,
|
|
5
|
+
>(
|
|
6
|
+
item: T,
|
|
7
|
+
key: Key,
|
|
8
|
+
value: Value,
|
|
9
|
+
) => {
|
|
10
|
+
const cloned = Object.create(item);
|
|
11
|
+
cloned.data = { ...item.data, [key]: value };
|
|
12
|
+
return cloned as Omit<T, 'data'> & {
|
|
13
|
+
data: Omit<T['data'], Key> & { [K in Key]: Value };
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function min<T extends { data: { min?: number } }, Value extends number>(
|
|
18
|
+
this: T,
|
|
19
|
+
value: Value,
|
|
20
|
+
) {
|
|
21
|
+
return setDataValue(this, 'min', value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function max<T extends { data: { max?: number } }, Value extends number>(
|
|
25
|
+
this: T,
|
|
26
|
+
value: Value,
|
|
27
|
+
) {
|
|
28
|
+
return setDataValue(this, 'max', value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function length<T extends { data: { length?: number } }, Value extends number>(
|
|
32
|
+
this: T,
|
|
33
|
+
value: Value,
|
|
34
|
+
) {
|
|
35
|
+
return setDataValue(this, 'length', value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function size<T extends { data: { size?: number } }, Value extends number>(
|
|
39
|
+
this: T,
|
|
40
|
+
value: Value,
|
|
41
|
+
) {
|
|
42
|
+
return setDataValue(this, 'size', value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function nonempty<T extends { data: { min?: number } }>(this: T) {
|
|
46
|
+
return setDataValue(this, 'min', 1);
|
|
47
|
+
}
|
|
3
48
|
|
|
4
49
|
export type ArrayMethods = typeof arrayMethods;
|
|
5
50
|
|
|
6
51
|
export const arrayMethods = {
|
|
7
|
-
min
|
|
52
|
+
min,
|
|
53
|
+
max,
|
|
54
|
+
length,
|
|
55
|
+
nonempty,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type SetMethods = typeof setMethods;
|
|
59
|
+
|
|
60
|
+
export const setMethods = {
|
|
61
|
+
min,
|
|
62
|
+
max,
|
|
63
|
+
size,
|
|
64
|
+
nonempty,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const stringTypeMethods = () => ({
|
|
68
|
+
...arrayMethods,
|
|
69
|
+
|
|
70
|
+
email<T extends { data: { email?: boolean } }>(this: T) {
|
|
71
|
+
return setDataValue(this, 'email', true);
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
url<T extends { data: { url?: boolean } }>(this: T) {
|
|
75
|
+
return setDataValue(this, 'url', true);
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
uuid<T extends { data: { uuid?: boolean } }>(this: T) {
|
|
79
|
+
return setDataValue(this, 'uuid', true);
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
cuid<T extends { data: { cuid?: boolean } }>(this: T) {
|
|
83
|
+
return setDataValue(this, 'cuid', true);
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
regex<T extends { data: { regex?: RegExp } }, Value extends RegExp>(
|
|
8
87
|
this: T,
|
|
9
88
|
value: Value,
|
|
10
89
|
) {
|
|
11
|
-
this
|
|
12
|
-
return this as T & { data: Omit<T['data'], 'min'> & { min: Value } };
|
|
90
|
+
return setDataValue(this, 'regex', value);
|
|
13
91
|
},
|
|
14
92
|
|
|
15
|
-
|
|
93
|
+
startsWith<T extends { data: { startsWith?: string } }, Value extends string>(
|
|
16
94
|
this: T,
|
|
17
95
|
value: Value,
|
|
18
96
|
) {
|
|
19
|
-
this
|
|
20
|
-
return this as T & { data: Omit<T['data'], 'max'> & { max: Value } };
|
|
97
|
+
return setDataValue(this, 'startsWith', value);
|
|
21
98
|
},
|
|
22
99
|
|
|
23
|
-
|
|
100
|
+
endsWith<T extends { data: { endsWith?: string } }, Value extends string>(
|
|
24
101
|
this: T,
|
|
25
102
|
value: Value,
|
|
26
103
|
) {
|
|
27
|
-
this
|
|
28
|
-
return this as T & { data: Omit<T['data'], 'length'> & { length: Value } };
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const stringTypeMethods = <Base extends { data: BaseStringData }>() => ({
|
|
33
|
-
...arrayMethods,
|
|
34
|
-
|
|
35
|
-
email<T extends Base>(this: T) {
|
|
36
|
-
this.data.email = true;
|
|
37
|
-
return this as T & { data: Omit<T['data'], 'email'> & { email: true } };
|
|
104
|
+
return setDataValue(this, 'endsWith', value);
|
|
38
105
|
},
|
|
39
106
|
|
|
40
|
-
|
|
41
|
-
this
|
|
42
|
-
return this as T & { data: Omit<T['data'], 'url'> & { url: true } };
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
uuid<T extends Base>(this: T) {
|
|
46
|
-
this.data.uuid = true;
|
|
47
|
-
return this as T & { data: Omit<T['data'], 'uuid'> & { uuid: true } };
|
|
107
|
+
trim<T extends { data: { trim?: boolean } }>(this: T) {
|
|
108
|
+
return setDataValue(this, 'trim', true);
|
|
48
109
|
},
|
|
110
|
+
});
|
|
49
111
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
112
|
+
export const numberTypeMethods = {
|
|
113
|
+
lt<T extends { data: { lt?: number } }, Value extends number>(
|
|
114
|
+
this: T,
|
|
115
|
+
value: Value,
|
|
116
|
+
) {
|
|
117
|
+
return setDataValue(this, 'lt', value);
|
|
53
118
|
},
|
|
54
119
|
|
|
55
|
-
|
|
56
|
-
this
|
|
57
|
-
|
|
120
|
+
lte<T extends { data: { lte?: number } }, Value extends number>(
|
|
121
|
+
this: T,
|
|
122
|
+
value: Value,
|
|
123
|
+
) {
|
|
124
|
+
return setDataValue(this, 'lte', value);
|
|
58
125
|
},
|
|
59
126
|
|
|
60
|
-
|
|
61
|
-
this
|
|
62
|
-
|
|
127
|
+
max<T extends { data: { lte?: number } }, Value extends number>(
|
|
128
|
+
this: T,
|
|
129
|
+
value: Value,
|
|
130
|
+
) {
|
|
131
|
+
return setDataValue(this, 'lte', value);
|
|
63
132
|
},
|
|
64
|
-
});
|
|
65
133
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
134
|
+
gt<T extends { data: { gt?: number } }, Value extends number>(
|
|
135
|
+
this: T,
|
|
136
|
+
value: Value,
|
|
137
|
+
) {
|
|
138
|
+
return setDataValue(this, 'gt', value);
|
|
70
139
|
},
|
|
71
140
|
|
|
72
|
-
|
|
73
|
-
this
|
|
74
|
-
|
|
141
|
+
gte<T extends { data: { gte?: number } }, Value extends number>(
|
|
142
|
+
this: T,
|
|
143
|
+
value: Value,
|
|
144
|
+
) {
|
|
145
|
+
return setDataValue(this, 'gte', value);
|
|
75
146
|
},
|
|
76
147
|
|
|
77
|
-
|
|
78
|
-
this
|
|
79
|
-
|
|
148
|
+
min<T extends { data: { gte?: number } }, Value extends number>(
|
|
149
|
+
this: T,
|
|
150
|
+
value: Value,
|
|
151
|
+
) {
|
|
152
|
+
return setDataValue(this, 'gte', value);
|
|
80
153
|
},
|
|
81
154
|
|
|
82
|
-
|
|
83
|
-
this
|
|
84
|
-
return this as T & { data: Omit<T['data'], 'gt'> & { gt: Value } };
|
|
155
|
+
positive<T extends { data: { gt?: number } }>(this: T) {
|
|
156
|
+
return setDataValue(this, 'gt', 0);
|
|
85
157
|
},
|
|
86
158
|
|
|
87
|
-
|
|
88
|
-
this
|
|
89
|
-
return this as T & { data: Omit<T['data'], 'gte'> & { gte: Value } };
|
|
159
|
+
nonNegative<T extends { data: { gte?: number } }>(this: T) {
|
|
160
|
+
return setDataValue(this, 'gte', 0);
|
|
90
161
|
},
|
|
91
162
|
|
|
92
|
-
|
|
93
|
-
this
|
|
94
|
-
return this as T & { data: Omit<T['data'], 'gte'> & { gte: Value } };
|
|
163
|
+
negative<T extends { data: { lt?: number } }>(this: T) {
|
|
164
|
+
return setDataValue(this, 'lt', 0);
|
|
95
165
|
},
|
|
96
166
|
|
|
97
|
-
|
|
98
|
-
this
|
|
99
|
-
return this as T & { data: Omit<T['data'], 'gt'> & { gt: 0 } };
|
|
167
|
+
nonPositive<T extends { data: { lte?: number } }>(this: T) {
|
|
168
|
+
return setDataValue(this, 'lte', 0);
|
|
100
169
|
},
|
|
101
170
|
|
|
102
|
-
|
|
103
|
-
this
|
|
104
|
-
|
|
171
|
+
multipleOf<T extends { data: { multipleOf?: number } }, Value extends number>(
|
|
172
|
+
this: T,
|
|
173
|
+
value: Value,
|
|
174
|
+
) {
|
|
175
|
+
return setDataValue(
|
|
176
|
+
this,
|
|
177
|
+
'multipleOf',
|
|
178
|
+
Array.isArray(value) ? value[0] : value,
|
|
179
|
+
);
|
|
105
180
|
},
|
|
106
181
|
|
|
107
|
-
|
|
108
|
-
this
|
|
109
|
-
|
|
182
|
+
step<T extends { data: { multipleOf?: number } }, Value extends number>(
|
|
183
|
+
this: T,
|
|
184
|
+
value: Value,
|
|
185
|
+
) {
|
|
186
|
+
return setDataValue(
|
|
187
|
+
this,
|
|
188
|
+
'multipleOf',
|
|
189
|
+
Array.isArray(value) ? value[0] : value,
|
|
190
|
+
);
|
|
110
191
|
},
|
|
111
192
|
|
|
112
|
-
|
|
113
|
-
this
|
|
114
|
-
return this as T & { data: Omit<T['data'], 'lte'> & { lte: 0 } };
|
|
193
|
+
int<T extends { data: { int?: boolean } }>(this: T) {
|
|
194
|
+
return setDataValue(this, 'int', true);
|
|
115
195
|
},
|
|
196
|
+
};
|
|
116
197
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
198
|
+
export const dateTypeMethods = {
|
|
199
|
+
min<T extends { data: { min?: Date } }, Value extends Date>(
|
|
200
|
+
this: T,
|
|
201
|
+
value: Value,
|
|
202
|
+
) {
|
|
203
|
+
return setDataValue(this, 'min', Array.isArray(value) ? value[0] : value);
|
|
122
204
|
},
|
|
123
205
|
|
|
124
|
-
|
|
125
|
-
this
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
206
|
+
max<T extends { data: { max?: Date } }, Value extends Date>(
|
|
207
|
+
this: T,
|
|
208
|
+
value: Value,
|
|
209
|
+
) {
|
|
210
|
+
return setDataValue(this, 'max', Array.isArray(value) ? value[0] : value);
|
|
129
211
|
},
|
|
130
|
-
}
|
|
212
|
+
};
|
|
@@ -1,22 +1,44 @@
|
|
|
1
|
-
import { ColumnType } from './columnType';
|
|
2
|
-
import { Operators } from '../
|
|
1
|
+
import { ColumnData, ColumnType } from './columnType';
|
|
2
|
+
import { Operators } from '../columnsOperators';
|
|
3
3
|
import { joinTruthy } from '../utils';
|
|
4
|
+
import { dateTypeMethods } from './commonMethods';
|
|
5
|
+
import { assignMethodsToClass } from './utils';
|
|
6
|
+
|
|
7
|
+
export type DateColumnData = ColumnData & {
|
|
8
|
+
min?: Date;
|
|
9
|
+
max?: Date;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type DateMethods = typeof dateTypeMethods;
|
|
13
|
+
|
|
14
|
+
export interface DateBaseColumn
|
|
15
|
+
extends ColumnType<string, typeof Operators.date, string | Date>,
|
|
16
|
+
DateMethods {}
|
|
17
|
+
|
|
18
|
+
export abstract class DateBaseColumn extends ColumnType<
|
|
19
|
+
string,
|
|
20
|
+
typeof Operators.date,
|
|
21
|
+
string | Date
|
|
22
|
+
> {
|
|
23
|
+
data = {} as DateColumnData;
|
|
24
|
+
operators = Operators.date;
|
|
25
|
+
}
|
|
4
26
|
|
|
5
|
-
|
|
6
|
-
|
|
27
|
+
assignMethodsToClass(DateBaseColumn, dateTypeMethods);
|
|
28
|
+
|
|
29
|
+
// date 4 bytes date (no time of day) 4713 BC 5874897 AD 1 day
|
|
30
|
+
export class DateColumn extends DateBaseColumn {
|
|
7
31
|
dataType = 'date' as const;
|
|
8
|
-
operators = Operators.date;
|
|
9
32
|
}
|
|
10
33
|
|
|
11
|
-
export
|
|
34
|
+
export type DateTimeColumnData = DateColumnData & {
|
|
12
35
|
precision?: number;
|
|
13
|
-
}
|
|
36
|
+
};
|
|
14
37
|
|
|
15
38
|
export abstract class DateTimeBaseClass<
|
|
16
39
|
Precision extends number | undefined = undefined,
|
|
17
|
-
> extends
|
|
40
|
+
> extends DateBaseColumn {
|
|
18
41
|
data: DateTimeColumnData & { precision: Precision };
|
|
19
|
-
operators = Operators.date;
|
|
20
42
|
|
|
21
43
|
constructor(precision?: Precision) {
|
|
22
44
|
super();
|
|
@@ -76,11 +98,20 @@ export class TimeWithTimeZoneColumn<
|
|
|
76
98
|
baseDataType = 'time' as const;
|
|
77
99
|
}
|
|
78
100
|
|
|
101
|
+
export type TimeInterval = {
|
|
102
|
+
years?: number;
|
|
103
|
+
months?: number;
|
|
104
|
+
days?: number;
|
|
105
|
+
hours?: number;
|
|
106
|
+
minutes?: number;
|
|
107
|
+
seconds?: number;
|
|
108
|
+
};
|
|
109
|
+
|
|
79
110
|
// interval [ fields ] [ (p) ] 16 bytes time interval -178000000 years 178000000 years 1 microsecond
|
|
80
111
|
export class IntervalColumn<
|
|
81
112
|
Fields extends string | undefined = undefined,
|
|
82
113
|
Precision extends number | undefined = undefined,
|
|
83
|
-
> extends ColumnType<
|
|
114
|
+
> extends ColumnType<TimeInterval, typeof Operators.date> {
|
|
84
115
|
dataType = 'interval' as const;
|
|
85
116
|
data: DateTimeColumnData & { fields: Fields; precision: Precision };
|
|
86
117
|
operators = Operators.date;
|
package/src/columnSchema/enum.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { ColumnType } from './columnType';
|
|
2
|
-
import { Operators } from '../
|
|
2
|
+
import { Operators } from '../columnsOperators';
|
|
3
3
|
|
|
4
|
-
export class EnumColumn<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
> {
|
|
4
|
+
export class EnumColumn<
|
|
5
|
+
U extends string = string,
|
|
6
|
+
T extends [U, ...U[]] = [U],
|
|
7
|
+
> extends ColumnType<T[number], typeof Operators.any> {
|
|
8
8
|
operators = Operators.any;
|
|
9
|
+
dataType = 'enum';
|
|
9
10
|
|
|
10
|
-
constructor(public
|
|
11
|
+
constructor(public enumName: string, public options: T) {
|
|
11
12
|
super();
|
|
12
13
|
}
|
|
14
|
+
|
|
15
|
+
toSql() {
|
|
16
|
+
return this.enumName;
|
|
17
|
+
}
|
|
13
18
|
}
|