pqb 0.0.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/dist/index.d.ts +3630 -0
- package/dist/index.esm.js +4587 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +4691 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
- package/rollup.config.js +35 -0
- package/src/adapter.test.ts +10 -0
- package/src/adapter.ts +171 -0
- package/src/columnSchema/array.ts +21 -0
- package/src/columnSchema/boolean.ts +10 -0
- package/src/columnSchema/columnType.test.ts +129 -0
- package/src/columnSchema/columnType.ts +77 -0
- package/src/columnSchema/columnTypes.ts +145 -0
- package/src/columnSchema/columnsSchema.test.ts +32 -0
- package/src/columnSchema/columnsSchema.ts +100 -0
- package/src/columnSchema/commonMethods.ts +130 -0
- package/src/columnSchema/dateTime.ts +104 -0
- package/src/columnSchema/enum.ts +13 -0
- package/src/columnSchema/index.ts +11 -0
- package/src/columnSchema/json/array.ts +55 -0
- package/src/columnSchema/json/discriminatedUnion.ts +91 -0
- package/src/columnSchema/json/enum.ts +29 -0
- package/src/columnSchema/json/instanceOf.ts +16 -0
- package/src/columnSchema/json/intersection.ts +23 -0
- package/src/columnSchema/json/lazy.ts +22 -0
- package/src/columnSchema/json/literal.ts +12 -0
- package/src/columnSchema/json/map.ts +29 -0
- package/src/columnSchema/json/nativeEnum.ts +30 -0
- package/src/columnSchema/json/nullable.ts +33 -0
- package/src/columnSchema/json/nullish.ts +30 -0
- package/src/columnSchema/json/object.ts +206 -0
- package/src/columnSchema/json/optional.ts +28 -0
- package/src/columnSchema/json/record.ts +40 -0
- package/src/columnSchema/json/scalarTypes.ts +117 -0
- package/src/columnSchema/json/set.ts +34 -0
- package/src/columnSchema/json/tuple.ts +40 -0
- package/src/columnSchema/json/typeBase.ts +202 -0
- package/src/columnSchema/json/union.ts +16 -0
- package/src/columnSchema/json.ts +64 -0
- package/src/columnSchema/number.ts +122 -0
- package/src/columnSchema/string.ts +222 -0
- package/src/columnSchema/utils.ts +27 -0
- package/src/common.ts +86 -0
- package/src/db.test.ts +67 -0
- package/src/db.ts +212 -0
- package/src/errors.ts +7 -0
- package/src/index.ts +18 -0
- package/src/operators.test.ts +608 -0
- package/src/operators.ts +177 -0
- package/src/query.ts +292 -0
- package/src/queryDataUtils.ts +50 -0
- package/src/queryMethods/aggregate.test.ts +583 -0
- package/src/queryMethods/aggregate.ts +878 -0
- package/src/queryMethods/callbacks.test.ts +69 -0
- package/src/queryMethods/callbacks.ts +55 -0
- package/src/queryMethods/clear.test.ts +64 -0
- package/src/queryMethods/clear.ts +58 -0
- package/src/queryMethods/columnInfo.test.ts +45 -0
- package/src/queryMethods/columnInfo.ts +67 -0
- package/src/queryMethods/delete.test.ts +135 -0
- package/src/queryMethods/delete.ts +50 -0
- package/src/queryMethods/for.test.ts +57 -0
- package/src/queryMethods/for.ts +99 -0
- package/src/queryMethods/from.test.ts +66 -0
- package/src/queryMethods/from.ts +58 -0
- package/src/queryMethods/get.test.ts +66 -0
- package/src/queryMethods/get.ts +88 -0
- package/src/queryMethods/having.test.ts +247 -0
- package/src/queryMethods/having.ts +99 -0
- package/src/queryMethods/insert.test.ts +555 -0
- package/src/queryMethods/insert.ts +453 -0
- package/src/queryMethods/join.test.ts +150 -0
- package/src/queryMethods/join.ts +508 -0
- package/src/queryMethods/json.test.ts +398 -0
- package/src/queryMethods/json.ts +259 -0
- package/src/queryMethods/log.test.ts +172 -0
- package/src/queryMethods/log.ts +123 -0
- package/src/queryMethods/queryMethods.test.ts +629 -0
- package/src/queryMethods/queryMethods.ts +428 -0
- package/src/queryMethods/select.test.ts +479 -0
- package/src/queryMethods/select.ts +249 -0
- package/src/queryMethods/then.ts +236 -0
- package/src/queryMethods/transaction.test.ts +66 -0
- package/src/queryMethods/transaction.ts +66 -0
- package/src/queryMethods/union.test.ts +59 -0
- package/src/queryMethods/union.ts +89 -0
- package/src/queryMethods/update.test.ts +417 -0
- package/src/queryMethods/update.ts +350 -0
- package/src/queryMethods/upsert.test.ts +56 -0
- package/src/queryMethods/upsert.ts +43 -0
- package/src/queryMethods/where.test.ts +1594 -0
- package/src/queryMethods/where.ts +450 -0
- package/src/queryMethods/window.test.ts +66 -0
- package/src/queryMethods/window.ts +108 -0
- package/src/queryMethods/with.test.ts +191 -0
- package/src/queryMethods/with.ts +92 -0
- package/src/quote.ts +36 -0
- package/src/relations.ts +194 -0
- package/src/sql/aggregate.ts +80 -0
- package/src/sql/columnInfo.ts +22 -0
- package/src/sql/common.ts +42 -0
- package/src/sql/delete.ts +41 -0
- package/src/sql/distinct.ts +19 -0
- package/src/sql/fromAndAs.ts +51 -0
- package/src/sql/having.ts +140 -0
- package/src/sql/index.ts +2 -0
- package/src/sql/insert.ts +102 -0
- package/src/sql/join.ts +242 -0
- package/src/sql/orderBy.ts +41 -0
- package/src/sql/select.ts +153 -0
- package/src/sql/toSql.ts +153 -0
- package/src/sql/truncate.ts +13 -0
- package/src/sql/types.ts +355 -0
- package/src/sql/update.ts +62 -0
- package/src/sql/where.ts +314 -0
- package/src/sql/window.ts +38 -0
- package/src/sql/with.ts +32 -0
- package/src/test-utils.ts +172 -0
- package/src/utils.ts +140 -0
- package/tsconfig.build.json +6 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { JSONOptional, JSONRequired, optional, required } from './optional';
|
|
2
|
+
import {
|
|
3
|
+
JSONNotNullable,
|
|
4
|
+
JSONNullable,
|
|
5
|
+
notNullable,
|
|
6
|
+
nullable,
|
|
7
|
+
} from './nullable';
|
|
8
|
+
import { JSONNotNullish, JSONNullish, notNullish, nullish } from './nullish';
|
|
9
|
+
import { intersection, JSONIntersection } from './intersection';
|
|
10
|
+
import { array, JSONArray } from './array';
|
|
11
|
+
import { union } from './union';
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
export type JSONTypeAny = JSONType<any, string>;
|
|
15
|
+
|
|
16
|
+
export type DeepPartial<T extends JSONTypeAny> = ReturnType<
|
|
17
|
+
JSONTypeAny['deepPartial']
|
|
18
|
+
> extends ReturnType<T['deepPartial']>
|
|
19
|
+
? T
|
|
20
|
+
: ReturnType<T['deepPartial']>;
|
|
21
|
+
|
|
22
|
+
export type JSONTypeData = {
|
|
23
|
+
optional?: true;
|
|
24
|
+
nullable?: true;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type Primitive = string | number | bigint | boolean | null | undefined;
|
|
28
|
+
|
|
29
|
+
export type JSONType<Type, DataType> = {
|
|
30
|
+
type: Type;
|
|
31
|
+
data: JSONTypeData;
|
|
32
|
+
dataType: DataType;
|
|
33
|
+
chain: (
|
|
34
|
+
| ['transform', (input: unknown) => unknown]
|
|
35
|
+
| ['to', (input: unknown) => JSONTypeAny | undefined, JSONTypeAny]
|
|
36
|
+
| ['refine', (input: unknown) => unknown]
|
|
37
|
+
| ['superRefine', (input: unknown) => unknown]
|
|
38
|
+
)[];
|
|
39
|
+
|
|
40
|
+
optional<T extends JSONTypeAny>(this: T): JSONOptional<T>;
|
|
41
|
+
required<T extends JSONTypeAny>(this: T): JSONRequired<T>;
|
|
42
|
+
nullable<T extends JSONTypeAny>(this: T): JSONNullable<T>;
|
|
43
|
+
notNullable<T extends JSONTypeAny>(this: T): JSONNotNullable<T>;
|
|
44
|
+
nullish<T extends JSONTypeAny>(this: T): JSONNullish<T>;
|
|
45
|
+
notNullish<T extends JSONTypeAny>(this: T): JSONNotNullish<T>;
|
|
46
|
+
deepPartial(): JSONTypeAny;
|
|
47
|
+
|
|
48
|
+
transform<T extends JSONTypeAny, Transformed>(
|
|
49
|
+
this: T,
|
|
50
|
+
fn: (input: T['type']) => Transformed,
|
|
51
|
+
): JSONType<
|
|
52
|
+
Transformed extends PromiseLike<unknown>
|
|
53
|
+
? Awaited<Transformed>
|
|
54
|
+
: Transformed,
|
|
55
|
+
T['dataType']
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
to<T extends JSONTypeAny, ToType extends JSONTypeAny>(
|
|
59
|
+
this: T,
|
|
60
|
+
fn: (input: T['type']) => ToType['type'] | undefined,
|
|
61
|
+
type: ToType,
|
|
62
|
+
): ToType;
|
|
63
|
+
|
|
64
|
+
refine<T extends JSONTypeAny, RefinedOutput extends T['type']>(
|
|
65
|
+
this: T,
|
|
66
|
+
check: (arg: T['type']) => unknown,
|
|
67
|
+
): T & { type: RefinedOutput };
|
|
68
|
+
|
|
69
|
+
superRefine<T extends JSONTypeAny, RefinedOutput extends T['type']>(
|
|
70
|
+
this: T,
|
|
71
|
+
check: (arg: T['type']) => unknown,
|
|
72
|
+
): T & { type: RefinedOutput };
|
|
73
|
+
|
|
74
|
+
and<A extends JSONTypeAny, B extends JSONTypeAny>(
|
|
75
|
+
this: A,
|
|
76
|
+
type: B,
|
|
77
|
+
): JSONIntersection<A, B>;
|
|
78
|
+
|
|
79
|
+
or<T extends JSONTypeAny, U extends [JSONTypeAny, ...JSONTypeAny[]]>(
|
|
80
|
+
this: T,
|
|
81
|
+
...types: U
|
|
82
|
+
): T | U[number];
|
|
83
|
+
|
|
84
|
+
default<T extends JSONTypeAny>(
|
|
85
|
+
this: T,
|
|
86
|
+
value: T['type'] | (() => T['type']),
|
|
87
|
+
): JSONNotNullish<T>;
|
|
88
|
+
|
|
89
|
+
array<T extends JSONTypeAny>(this: T): JSONArray<T>;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const baseTypeMethods: JSONTypeAny = {
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
type: undefined as any,
|
|
95
|
+
data: {},
|
|
96
|
+
dataType: 'any',
|
|
97
|
+
chain: [],
|
|
98
|
+
|
|
99
|
+
optional<T extends JSONTypeAny>(this: T) {
|
|
100
|
+
return optional(this);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
required<T extends JSONTypeAny>(this: T) {
|
|
104
|
+
return required(this);
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
nullable<T extends JSONTypeAny>(this: T) {
|
|
108
|
+
return nullable(this);
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
notNullable<T extends JSONTypeAny>(this: T) {
|
|
112
|
+
return notNullable(this);
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
nullish<T extends JSONTypeAny>(this: T) {
|
|
116
|
+
return nullish(this);
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
notNullish<T extends JSONTypeAny>(this: T) {
|
|
120
|
+
return notNullish(this);
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
deepPartial() {
|
|
124
|
+
return this;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
transform<T extends JSONTypeAny, Transformed>(
|
|
128
|
+
this: T,
|
|
129
|
+
fn: (input: unknown) => Transformed,
|
|
130
|
+
) {
|
|
131
|
+
return {
|
|
132
|
+
...this,
|
|
133
|
+
chain: [...this.chain, ['transform', fn]],
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
to(fn, type) {
|
|
138
|
+
return {
|
|
139
|
+
...type,
|
|
140
|
+
chain: [...this.chain, ['to', fn, type], ...type.chain],
|
|
141
|
+
};
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
refine(check) {
|
|
145
|
+
return {
|
|
146
|
+
...this,
|
|
147
|
+
chain: [...this.chain, ['refine', check]],
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
superRefine(check) {
|
|
152
|
+
return {
|
|
153
|
+
...this,
|
|
154
|
+
chain: [...this.chain, ['superRefine', check]],
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
and(type) {
|
|
159
|
+
return intersection(this, type);
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
or(...args) {
|
|
163
|
+
const [type, ...types] = args;
|
|
164
|
+
return union([this, type, ...types]);
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
default(value) {
|
|
168
|
+
const defaultFn =
|
|
169
|
+
typeof value === 'function'
|
|
170
|
+
? (input: unknown) => input ?? (value as () => unknown)()
|
|
171
|
+
: (input: unknown) => input ?? value;
|
|
172
|
+
|
|
173
|
+
return notNullish({
|
|
174
|
+
...this,
|
|
175
|
+
chain: ['transform', defaultFn],
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
array() {
|
|
180
|
+
return array(this);
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
type BaseTypeProps<T extends JSONTypeAny> = Omit<
|
|
185
|
+
JSONType<T['type'], string>,
|
|
186
|
+
'dataType'
|
|
187
|
+
>;
|
|
188
|
+
export type OwnTypeProps<T extends JSONTypeAny> = Omit<
|
|
189
|
+
T,
|
|
190
|
+
keyof BaseTypeProps<T>
|
|
191
|
+
> & {
|
|
192
|
+
[k in keyof BaseTypeProps<T>]?: BaseTypeProps<T>[k];
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const constructType = <T extends JSONTypeAny>(
|
|
196
|
+
type: OwnTypeProps<T>,
|
|
197
|
+
): T => {
|
|
198
|
+
return {
|
|
199
|
+
...(baseTypeMethods as BaseTypeProps<T>),
|
|
200
|
+
...type,
|
|
201
|
+
} as T;
|
|
202
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { constructType, JSONType, JSONTypeAny } from './typeBase';
|
|
2
|
+
|
|
3
|
+
export interface JSONUnion<
|
|
4
|
+
T extends [JSONTypeAny, JSONTypeAny, ...JSONTypeAny[]],
|
|
5
|
+
> extends JSONType<T[number]['type'], 'union'> {
|
|
6
|
+
types: T;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const union = <T extends [JSONTypeAny, JSONTypeAny, ...JSONTypeAny[]]>(
|
|
10
|
+
types: T,
|
|
11
|
+
): T[number] => {
|
|
12
|
+
return constructType<JSONUnion<T>>({
|
|
13
|
+
dataType: 'union',
|
|
14
|
+
types,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ColumnType } from './columnType';
|
|
2
|
+
import { Operators } from '../operators';
|
|
3
|
+
import { scalarTypes } from './json/scalarTypes';
|
|
4
|
+
import { array } from './json/array';
|
|
5
|
+
import { discriminatedUnion } from './json/discriminatedUnion';
|
|
6
|
+
import { enumType } from './json/enum';
|
|
7
|
+
import { instanceOf } from './json/instanceOf';
|
|
8
|
+
import { intersection } from './json/intersection';
|
|
9
|
+
import { lazy } from './json/lazy';
|
|
10
|
+
import { literal } from './json/literal';
|
|
11
|
+
import { map } from './json/map';
|
|
12
|
+
import { nativeEnum } from './json/nativeEnum';
|
|
13
|
+
import { nullable } from './json/nullable';
|
|
14
|
+
import { nullish } from './json/nullish';
|
|
15
|
+
import { object } from './json/object';
|
|
16
|
+
import { optional } from './json/optional';
|
|
17
|
+
import { record } from './json/record';
|
|
18
|
+
import { set } from './json/set';
|
|
19
|
+
import { tuple } from './json/tuple';
|
|
20
|
+
import { union } from './json/union';
|
|
21
|
+
import { JSONTypeAny } from './json/typeBase';
|
|
22
|
+
|
|
23
|
+
export type JSONTypes = typeof jsonTypes;
|
|
24
|
+
const jsonTypes = {
|
|
25
|
+
array,
|
|
26
|
+
discriminatedUnion,
|
|
27
|
+
enum: enumType,
|
|
28
|
+
instanceOf,
|
|
29
|
+
intersection,
|
|
30
|
+
lazy,
|
|
31
|
+
literal,
|
|
32
|
+
map,
|
|
33
|
+
nativeEnum,
|
|
34
|
+
nullable,
|
|
35
|
+
nullish,
|
|
36
|
+
object,
|
|
37
|
+
optional,
|
|
38
|
+
record,
|
|
39
|
+
...scalarTypes,
|
|
40
|
+
set,
|
|
41
|
+
tuple,
|
|
42
|
+
union,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export class JSONColumn<
|
|
46
|
+
Type extends JSONTypeAny = JSONTypeAny,
|
|
47
|
+
> extends ColumnType<Type['type'], typeof Operators.json> {
|
|
48
|
+
dataType = 'jsonb' as const;
|
|
49
|
+
operators = Operators.json;
|
|
50
|
+
data: { schema: Type };
|
|
51
|
+
|
|
52
|
+
constructor(schemaOrFn: Type | ((j: JSONTypes) => Type)) {
|
|
53
|
+
super();
|
|
54
|
+
|
|
55
|
+
const schema =
|
|
56
|
+
typeof schemaOrFn === 'function' ? schemaOrFn(jsonTypes) : schemaOrFn;
|
|
57
|
+
this.data = { schema };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class JSONTextColumn extends ColumnType<string, typeof Operators.text> {
|
|
62
|
+
dataType = 'json' as const;
|
|
63
|
+
operators = Operators.text;
|
|
64
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Operators } from '../operators';
|
|
2
|
+
import { ColumnType } from './columnType';
|
|
3
|
+
import { joinTruthy } from '../utils';
|
|
4
|
+
import { assignMethodsToClass } from './utils';
|
|
5
|
+
import { numberTypeMethods } from './commonMethods';
|
|
6
|
+
|
|
7
|
+
export interface BaseNumberData {
|
|
8
|
+
lt?: number;
|
|
9
|
+
lte?: number;
|
|
10
|
+
gt?: number;
|
|
11
|
+
gte?: number;
|
|
12
|
+
multipleOf?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type NumberColumn = ColumnType<number>;
|
|
16
|
+
|
|
17
|
+
export type NumberColumnData = BaseNumberData;
|
|
18
|
+
|
|
19
|
+
type NumberMethods = typeof numberMethods;
|
|
20
|
+
const numberMethods = numberTypeMethods<ColumnType>();
|
|
21
|
+
|
|
22
|
+
export interface NumberBaseColumn<Type>
|
|
23
|
+
extends ColumnType<Type, typeof Operators.number>,
|
|
24
|
+
NumberMethods {}
|
|
25
|
+
|
|
26
|
+
export abstract class NumberBaseColumn<Type> extends ColumnType<
|
|
27
|
+
Type,
|
|
28
|
+
typeof Operators.number
|
|
29
|
+
> {
|
|
30
|
+
data = {} as NumberColumnData;
|
|
31
|
+
operators = Operators.number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
assignMethodsToClass(NumberBaseColumn, numberMethods);
|
|
35
|
+
|
|
36
|
+
export interface DecimalColumnData extends NumberColumnData {
|
|
37
|
+
precision?: number;
|
|
38
|
+
scale?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class DecimalBaseColumn<
|
|
42
|
+
Type extends number | bigint,
|
|
43
|
+
Precision extends number | undefined = undefined,
|
|
44
|
+
Scale extends number | undefined = undefined,
|
|
45
|
+
> extends NumberBaseColumn<Type> {
|
|
46
|
+
data: DecimalColumnData & { precision: Precision; scale: Scale };
|
|
47
|
+
dataType = 'decimal' as const;
|
|
48
|
+
|
|
49
|
+
constructor(precision?: Precision, scale?: Scale) {
|
|
50
|
+
super();
|
|
51
|
+
|
|
52
|
+
this.data = {
|
|
53
|
+
precision,
|
|
54
|
+
scale,
|
|
55
|
+
} as DecimalColumnData & { precision: Precision; scale: Scale };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
toSQL() {
|
|
59
|
+
const { precision, scale } = this.data;
|
|
60
|
+
|
|
61
|
+
return joinTruthy(
|
|
62
|
+
this.dataType,
|
|
63
|
+
precision
|
|
64
|
+
? scale
|
|
65
|
+
? `(${precision}, ${scale})`
|
|
66
|
+
: `(${precision})`
|
|
67
|
+
: undefined,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// signed two-byte integer
|
|
73
|
+
export class SmallIntColumn extends NumberBaseColumn<number> {
|
|
74
|
+
dataType = 'smallint' as const;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// signed four-byte integer
|
|
78
|
+
export class IntegerColumn extends NumberBaseColumn<number> {
|
|
79
|
+
dataType = 'integer' as const;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// signed eight-byte integer
|
|
83
|
+
export class BigIntColumn extends NumberBaseColumn<bigint> {
|
|
84
|
+
dataType = 'bigint' as const;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// exact numeric of selectable precision
|
|
88
|
+
export class DecimalColumn<
|
|
89
|
+
Precision extends number | undefined = undefined,
|
|
90
|
+
Scale extends number | undefined = undefined,
|
|
91
|
+
> extends DecimalBaseColumn<number, Precision, Scale> {}
|
|
92
|
+
|
|
93
|
+
// exact numeric of selectable precision, bigint JS type
|
|
94
|
+
export class DecimalBigIntColumn<
|
|
95
|
+
Precision extends number | undefined = undefined,
|
|
96
|
+
Scale extends number | undefined = undefined,
|
|
97
|
+
> extends DecimalBaseColumn<bigint, Precision, Scale> {}
|
|
98
|
+
|
|
99
|
+
// single precision floating-point number (4 bytes)
|
|
100
|
+
export class RealColumn extends NumberBaseColumn<number> {
|
|
101
|
+
dataType = 'real' as const;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// double precision floating-point number (8 bytes)
|
|
105
|
+
export class DoublePrecisionColumn extends NumberBaseColumn<bigint> {
|
|
106
|
+
dataType = 'double precision' as const;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// autoincrementing two-byte integer
|
|
110
|
+
export class SmallSerialColumn extends NumberBaseColumn<number> {
|
|
111
|
+
dataType = 'smallserial' as const;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// autoincrementing four-byte integer
|
|
115
|
+
export class SerialColumn extends NumberBaseColumn<number> {
|
|
116
|
+
dataType = 'serial' as const;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// autoincrementing eight-byte integer
|
|
120
|
+
export class BigSerialColumn extends NumberBaseColumn<bigint> {
|
|
121
|
+
dataType = 'bigserial' as const;
|
|
122
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { ColumnType } from './columnType';
|
|
2
|
+
import { Operators } from '../operators';
|
|
3
|
+
import { joinTruthy } from '../utils';
|
|
4
|
+
import { NumberBaseColumn } from './number';
|
|
5
|
+
import { assignMethodsToClass } from './utils';
|
|
6
|
+
import { stringTypeMethods } from './commonMethods';
|
|
7
|
+
|
|
8
|
+
export interface BaseStringData {
|
|
9
|
+
min?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
length?: number;
|
|
12
|
+
email?: boolean;
|
|
13
|
+
url?: boolean;
|
|
14
|
+
uuid?: boolean;
|
|
15
|
+
cuid?: boolean;
|
|
16
|
+
regex?: RegExp;
|
|
17
|
+
trim?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type StringColumn = ColumnType<string>;
|
|
21
|
+
|
|
22
|
+
export type TextColumnData = BaseStringData;
|
|
23
|
+
|
|
24
|
+
type TextMethods = typeof textMethods;
|
|
25
|
+
const textMethods = stringTypeMethods<ColumnType<string>>();
|
|
26
|
+
|
|
27
|
+
export interface TextBaseColumn
|
|
28
|
+
extends ColumnType<string, typeof Operators.text>,
|
|
29
|
+
TextMethods {}
|
|
30
|
+
|
|
31
|
+
export abstract class TextBaseColumn extends ColumnType<
|
|
32
|
+
string,
|
|
33
|
+
typeof Operators.text
|
|
34
|
+
> {
|
|
35
|
+
data = {} as TextColumnData;
|
|
36
|
+
operators = Operators.text;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
assignMethodsToClass(TextBaseColumn, textMethods);
|
|
40
|
+
|
|
41
|
+
export abstract class LimitedTextBaseColumn<
|
|
42
|
+
Limit extends number | undefined = undefined,
|
|
43
|
+
> extends TextBaseColumn {
|
|
44
|
+
data: TextColumnData & { max: Limit };
|
|
45
|
+
|
|
46
|
+
constructor(limit?: Limit) {
|
|
47
|
+
super();
|
|
48
|
+
|
|
49
|
+
this.data = { max: limit } as TextColumnData & { max: Limit };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
toSQL() {
|
|
53
|
+
return joinTruthy(
|
|
54
|
+
this.dataType,
|
|
55
|
+
this.data.max !== undefined && `(${this.data.max})`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// character varying(n), varchar(n) variable-length with limit
|
|
61
|
+
export class VarCharColumn<
|
|
62
|
+
Limit extends number | undefined = undefined,
|
|
63
|
+
> extends LimitedTextBaseColumn<Limit> {
|
|
64
|
+
dataType = 'varchar' as const;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// character(n), char(n) fixed-length, blank padded
|
|
68
|
+
export class CharColumn<
|
|
69
|
+
Limit extends number | undefined = undefined,
|
|
70
|
+
> extends LimitedTextBaseColumn<Limit> {
|
|
71
|
+
dataType = 'char' as const;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// text variable unlimited length
|
|
75
|
+
export class TextColumn extends ColumnType<string> {
|
|
76
|
+
dataType = 'text' as const;
|
|
77
|
+
operators = Operators.text;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// To store binary strings
|
|
81
|
+
export class ByteaColumn extends NumberBaseColumn<string> {
|
|
82
|
+
dataType = 'bytea' as const;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// point 16 bytes Point on a plane (x,y)
|
|
86
|
+
export class PointColumn extends ColumnType<string, typeof Operators.text> {
|
|
87
|
+
dataType = 'point' as const;
|
|
88
|
+
operators = Operators.text;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// line 32 bytes Infinite line {A,B,C}
|
|
92
|
+
export class LineColumn extends ColumnType<string, typeof Operators.text> {
|
|
93
|
+
dataType = 'point' as const;
|
|
94
|
+
operators = Operators.text;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// lseg 32 bytes Finite line segment ((x1,y1),(x2,y2))
|
|
98
|
+
export class LsegColumn extends ColumnType<string, typeof Operators.text> {
|
|
99
|
+
dataType = 'point' as const;
|
|
100
|
+
operators = Operators.text;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// box 32 bytes Rectangular box ((x1,y1),(x2,y2))
|
|
104
|
+
export class BoxColumn extends ColumnType<string, typeof Operators.text> {
|
|
105
|
+
dataType = 'point' as const;
|
|
106
|
+
operators = Operators.text;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// path 16+16n bytes Closed path (similar to polygon) ((x1,y1),...)
|
|
110
|
+
// path 16+16n bytes Open path [(x1,y1),...]
|
|
111
|
+
export class PathColumn extends ColumnType<string, typeof Operators.text> {
|
|
112
|
+
dataType = 'point' as const;
|
|
113
|
+
operators = Operators.text;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// polygon 40+16n bytes Polygon (similar to closed path) ((x1,y1),...)
|
|
117
|
+
export class PolygonColumn extends ColumnType<string, typeof Operators.text> {
|
|
118
|
+
dataType = 'point' as const;
|
|
119
|
+
operators = Operators.text;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// circle 24 bytes Circle <(x,y),r> (center point and radius)
|
|
123
|
+
export class CircleColumn extends ColumnType<string, typeof Operators.text> {
|
|
124
|
+
dataType = 'point' as const;
|
|
125
|
+
operators = Operators.text;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export class MoneyColumn extends NumberBaseColumn<string> {
|
|
129
|
+
dataType = 'money' as const;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// cidr 7 or 19 bytes IPv4 and IPv6 networks
|
|
133
|
+
export class CidrColumn extends ColumnType<string, typeof Operators.text> {
|
|
134
|
+
dataType = 'cidr' as const;
|
|
135
|
+
operators = Operators.text;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// inet 7 or 19 bytes IPv4 and IPv6 hosts and networks
|
|
139
|
+
export class InetColumn extends ColumnType<string, typeof Operators.text> {
|
|
140
|
+
dataType = 'inet' as const;
|
|
141
|
+
operators = Operators.text;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// macaddr 6 bytes MAC addresses
|
|
145
|
+
export class MacAddrColumn extends ColumnType<string, typeof Operators.text> {
|
|
146
|
+
dataType = 'macaddr' as const;
|
|
147
|
+
operators = Operators.text;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// macaddr8 8 bytes MAC addresses (EUI-64 format)
|
|
151
|
+
export class MacAddr8Column extends ColumnType<string, typeof Operators.text> {
|
|
152
|
+
dataType = 'macaddr8' as const;
|
|
153
|
+
operators = Operators.text;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Bit strings are strings of 1's and 0's.
|
|
157
|
+
// They can be used to store or visualize bit masks.
|
|
158
|
+
// There are two SQL bit types: bit(n) and bit varying(n), where n is a positive integer.
|
|
159
|
+
export class BitColumn<
|
|
160
|
+
Length extends number | undefined = undefined,
|
|
161
|
+
> extends ColumnType<string, typeof Operators.text> {
|
|
162
|
+
dataType = 'bit' as const;
|
|
163
|
+
operators = Operators.text;
|
|
164
|
+
data: { length: Length };
|
|
165
|
+
|
|
166
|
+
constructor(length?: Length) {
|
|
167
|
+
super();
|
|
168
|
+
|
|
169
|
+
this.data = { length } as { length: Length };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
toSQL() {
|
|
173
|
+
return joinTruthy(
|
|
174
|
+
this.dataType,
|
|
175
|
+
this.data.length && `(${this.data.length})`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export class BitVaryingColumn<
|
|
181
|
+
Length extends number | undefined = undefined,
|
|
182
|
+
> extends ColumnType<string, typeof Operators.text> {
|
|
183
|
+
dataType = 'bit varying' as const;
|
|
184
|
+
operators = Operators.text;
|
|
185
|
+
data: { length: Length };
|
|
186
|
+
|
|
187
|
+
constructor(length?: Length) {
|
|
188
|
+
super();
|
|
189
|
+
|
|
190
|
+
this.data = { length } as { length: Length };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
toSQL() {
|
|
194
|
+
return joinTruthy(
|
|
195
|
+
this.dataType,
|
|
196
|
+
this.data.length && `(${this.data.length})`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// A tsvector value is a sorted list of distinct lexemes
|
|
202
|
+
export class TsVectorColumn extends ColumnType<string, typeof Operators.text> {
|
|
203
|
+
dataType = 'tsvector' as const;
|
|
204
|
+
operators = Operators.text;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// A tsquery value stores lexemes that are to be searched for
|
|
208
|
+
export class TsQueryColumn extends ColumnType<string, typeof Operators.text> {
|
|
209
|
+
dataType = 'tsquery' as const;
|
|
210
|
+
operators = Operators.text;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// A tsquery value stores lexemes that are to be searched for
|
|
214
|
+
export class UUIDColumn extends ColumnType<string, typeof Operators.text> {
|
|
215
|
+
dataType = 'uuid' as const;
|
|
216
|
+
operators = Operators.text;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export class XMLColumn extends ColumnType<string, typeof Operators.text> {
|
|
220
|
+
dataType = 'uuid' as const;
|
|
221
|
+
operators = Operators.text;
|
|
222
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type EmptyObject = Record<never, never>;
|
|
2
|
+
|
|
3
|
+
export const cloneInstance = <T>(instance: T): T => {
|
|
4
|
+
return Object.assign(
|
|
5
|
+
Object.create(Object.getPrototypeOf(instance)),
|
|
6
|
+
instance,
|
|
7
|
+
);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const assignMethodsToClass = <Methods extends Record<string, unknown>>(
|
|
11
|
+
klass: { prototype: unknown },
|
|
12
|
+
methods: Methods,
|
|
13
|
+
) => {
|
|
14
|
+
for (const name in methods) {
|
|
15
|
+
Object.defineProperty(klass.prototype, name, {
|
|
16
|
+
configurable: true,
|
|
17
|
+
enumerable: false,
|
|
18
|
+
writable: true,
|
|
19
|
+
value(...args: unknown[]) {
|
|
20
|
+
const cloned = cloneInstance(this);
|
|
21
|
+
return (
|
|
22
|
+
methods as unknown as Record<string, (...args: unknown[]) => unknown>
|
|
23
|
+
)[name].call(cloned, args);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
};
|