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.
Files changed (55) hide show
  1. package/README.md +5 -0
  2. package/package.json +5 -2
  3. package/rollup.config.js +2 -34
  4. package/src/adapter.ts +11 -9
  5. package/src/columnSchema/array.ts +115 -3
  6. package/src/columnSchema/boolean.ts +4 -1
  7. package/src/columnSchema/columnType.test.ts +1 -1
  8. package/src/columnSchema/columnType.ts +227 -5
  9. package/src/columnSchema/columnTypes.test.ts +568 -0
  10. package/src/columnSchema/columnTypes.ts +136 -24
  11. package/src/columnSchema/columnsSchema.ts +1 -1
  12. package/src/columnSchema/commonMethods.ts +162 -80
  13. package/src/columnSchema/dateTime.ts +41 -10
  14. package/src/columnSchema/enum.ts +11 -6
  15. package/src/columnSchema/json/discriminatedUnion.ts +51 -42
  16. package/src/columnSchema/json/enum.ts +9 -9
  17. package/src/columnSchema/json/lazy.ts +5 -3
  18. package/src/columnSchema/json/nativeEnum.ts +1 -1
  19. package/src/columnSchema/json/nullish.ts +1 -1
  20. package/src/columnSchema/json/record.ts +14 -11
  21. package/src/columnSchema/json/scalarTypes.ts +17 -8
  22. package/src/columnSchema/json/set.ts +4 -4
  23. package/src/columnSchema/json/tuple.ts +17 -2
  24. package/src/columnSchema/json/typeBase.ts +11 -16
  25. package/src/columnSchema/json/union.ts +1 -1
  26. package/src/columnSchema/json.ts +4 -4
  27. package/src/columnSchema/number.ts +41 -30
  28. package/src/columnSchema/string.ts +28 -19
  29. package/src/columnSchema/utils.ts +0 -2
  30. package/src/{operators.test.ts → columnsOperators.test.ts} +0 -0
  31. package/src/{operators.ts → columnsOperators.ts} +0 -0
  32. package/src/common.ts +18 -16
  33. package/src/db.ts +10 -8
  34. package/src/index.ts +2 -7
  35. package/src/query.ts +2 -2
  36. package/src/queryMethods/aggregate.ts +6 -3
  37. package/src/queryMethods/get.ts +6 -3
  38. package/src/queryMethods/index.ts +22 -0
  39. package/src/queryMethods/join.ts +1 -1
  40. package/src/queryMethods/log.ts +5 -5
  41. package/src/queryMethods/select.test.ts +2 -2
  42. package/src/queryMethods/select.ts +10 -7
  43. package/src/queryMethods/then.ts +8 -19
  44. package/src/queryMethods/transaction.test.ts +2 -2
  45. package/src/queryMethods/transaction.ts +5 -5
  46. package/src/queryMethods/update.ts +11 -14
  47. package/src/sql/having.ts +1 -1
  48. package/src/test-utils.ts +3 -3
  49. package/src/utils.ts +3 -0
  50. package/dist/index.d.ts +0 -3630
  51. package/dist/index.esm.js +0 -4587
  52. package/dist/index.esm.js.map +0 -1
  53. package/dist/index.js +0 -4691
  54. package/dist/index.js.map +0 -1
  55. package/tsconfig.build.json +0 -6
@@ -1,83 +1,92 @@
1
1
  import { constructType, JSONType, Primitive } from './typeBase';
2
- import { JSONObject } from './object';
2
+ import { JSONObject, JSONObjectShape } from './object';
3
3
  import { JSONLiteral } from './literal';
4
4
 
5
5
  export interface JSONDiscriminatedUnion<
6
6
  Discriminator extends string,
7
- Options extends DiscriminatedOptions<Discriminator>,
8
- > extends JSONType<Options[number]['type'], 'discriminatedUnion'> {
7
+ DiscriminatorValue extends Primitive,
8
+ Option extends JSONDiscriminatedObject<Discriminator, DiscriminatorValue>,
9
+ > extends JSONType<Option['type'], 'discriminatedUnion'> {
9
10
  discriminator: Discriminator;
10
- options: OptionsMap<Discriminator, Options>;
11
+ discriminatorValue: DiscriminatorValue;
12
+ options: Map<DiscriminatorValue, Option>;
13
+ _option: Option;
11
14
  // TODO: gave up on deepPartial type
12
15
  // deepPartial(): JSONDiscriminatedUnion<
13
16
  // Discriminator,
14
17
  // {
15
- // [Index in keyof Options]: {
16
- // [K in keyof Options[Index]['shape']]: K extends Discriminator
17
- // ? Options[Index]['shape'][K]
18
- // : JSONOptional<Options[Index]['shape'][K]>;
18
+ // [Index in keyof Types]: {
19
+ // [K in keyof Types[Index]['shape']]: K extends Discriminator
20
+ // ? Types[Index]['shape'][K]
21
+ // : JSONOptional<Types[Index]['shape'][K]>;
19
22
  // } extends JSONObject<Record<Discriminator, JSONLiteral<Primitive>>>
20
23
  // ? JSONObject<
21
24
  // {
22
- // [K in keyof Options[Index]['shape']]: K extends Discriminator
23
- // ? Options[Index]['shape'][K]
24
- // : JSONOptional<Options[Index]['shape'][K]>;
25
+ // [K in keyof Types[Index]['shape']]: K extends Discriminator
26
+ // ? Types[Index]['shape'][K]
27
+ // : JSONOptional<Types[Index]['shape'][K]>;
25
28
  // },
26
- // Options[Index]['unknownKeys'],
27
- // Options[Index]['catchAllType']
29
+ // Types[Index]['unknownKeys'],
30
+ // Types[Index]['catchAllType']
28
31
  // >
29
- // : Options[Index];
32
+ // : Types[Index];
30
33
  // } & {
31
- // length: Options['length'];
34
+ // length: Types['length'];
32
35
  // }
33
36
  // >;
34
37
  }
35
38
 
36
- type JSONDiscriminatedObject<Discriminator extends string> = JSONObject<
37
- Record<Discriminator, JSONLiteral<Primitive>>
38
- >;
39
-
40
- type DiscriminatedOptions<Discriminator extends string> = readonly [
41
- JSONDiscriminatedObject<Discriminator>,
42
- JSONDiscriminatedObject<Discriminator>,
43
- ...JSONDiscriminatedObject<Discriminator>[],
44
- ];
45
-
46
- type OptionsMap<
39
+ export type JSONDiscriminatedObject<
47
40
  Discriminator extends string,
48
- Options extends DiscriminatedOptions<Discriminator>,
49
- > = Map<Options[number]['shape'][Discriminator]['value'], Options[number]>;
41
+ DiscriminatorValue extends Primitive,
42
+ > = JSONObject<
43
+ { [K in Discriminator]: JSONLiteral<DiscriminatorValue> } & JSONObjectShape,
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ any
46
+ >;
50
47
 
51
48
  export const discriminatedUnion = <
52
49
  Discriminator extends string,
53
- Options extends DiscriminatedOptions<Discriminator>,
50
+ DiscriminatorValue extends Primitive,
51
+ Types extends [
52
+ JSONDiscriminatedObject<Discriminator, DiscriminatorValue>,
53
+ JSONDiscriminatedObject<Discriminator, DiscriminatorValue>,
54
+ ...JSONDiscriminatedObject<Discriminator, DiscriminatorValue>[],
55
+ ],
54
56
  >(
55
57
  discriminator: Discriminator,
56
- options: Options,
57
- ) => {
58
- const optionsMap: OptionsMap<
59
- Discriminator,
60
- DiscriminatedOptions<Discriminator>
61
- > = new Map();
58
+ options: Types,
59
+ ): JSONDiscriminatedUnion<Discriminator, DiscriminatorValue, Types[number]> => {
60
+ const optionsMap: Map<DiscriminatorValue, Types[number]> = new Map();
62
61
 
63
62
  options.forEach((option) => {
64
63
  const discriminatorValue = option.shape[discriminator].value;
65
- optionsMap.set(discriminatorValue, option);
64
+ optionsMap.set(discriminatorValue as DiscriminatorValue, option);
66
65
  });
67
66
 
68
- return constructType<JSONDiscriminatedUnion<Discriminator, Options>>({
67
+ return constructType<
68
+ JSONDiscriminatedUnion<Discriminator, DiscriminatorValue, Types[number]>
69
+ >({
69
70
  dataType: 'discriminatedUnion',
70
71
  discriminator,
72
+ discriminatorValue: undefined as unknown as DiscriminatorValue,
71
73
  options: optionsMap,
72
- deepPartial(this: JSONDiscriminatedUnion<Discriminator, Options>) {
73
- const newOptionsMap: OptionsMap<
74
+ _option: undefined as unknown as Types[number],
75
+ deepPartial(
76
+ this: JSONDiscriminatedUnion<
74
77
  Discriminator,
75
- DiscriminatedOptions<Discriminator>
76
- > = new Map();
78
+ DiscriminatorValue,
79
+ Types[number]
80
+ >,
81
+ ) {
82
+ const newOptionsMap: Map<DiscriminatorValue, Types[number]> = new Map();
77
83
 
78
84
  optionsMap.forEach((option, key) => {
79
85
  const partial =
80
- option.deepPartial() as unknown as JSONDiscriminatedObject<Discriminator>;
86
+ option.deepPartial() as unknown as JSONDiscriminatedObject<
87
+ Discriminator,
88
+ DiscriminatorValue
89
+ >;
81
90
  partial.shape[discriminator] = option.shape[discriminator];
82
91
  newOptionsMap.set(key, partial);
83
92
  });
@@ -1,27 +1,27 @@
1
1
  import { constructType, JSONType } from './typeBase';
2
2
 
3
- export interface JSONEnum<T extends EnumValues>
4
- extends JSONType<T[number], 'enum'> {
3
+ export interface JSONEnum<
4
+ U extends string = string,
5
+ T extends [U, ...U[]] = [U],
6
+ > extends JSONType<T[number], 'enum'> {
5
7
  enum: { [k in T[number]]: k };
6
8
  options: T;
7
9
  }
8
10
 
9
- type EnumValues = readonly [string, ...string[]];
10
-
11
- export const arrayToEnum = <T extends string, U extends readonly [T, ...T[]]>(
12
- items: U,
11
+ export const arrayToEnum = <U extends string, T extends [U, ...U[]]>(
12
+ items: T,
13
13
  ) => {
14
- const obj = {} as { [k in U[number]]: k };
14
+ const obj = {} as { [k in T[number]]: k };
15
15
  for (const item of items) {
16
16
  obj[item] = item;
17
17
  }
18
18
  return obj;
19
19
  };
20
20
 
21
- export const enumType = <T extends readonly [string, ...string[]]>(
21
+ export const enumType = <U extends string, T extends [U, ...U[]]>(
22
22
  options: T,
23
23
  ) => {
24
- return constructType<JSONEnum<T>>({
24
+ return constructType<JSONEnum<U, T>>({
25
25
  dataType: 'enum',
26
26
  enum: arrayToEnum(options),
27
27
  options,
@@ -7,10 +7,12 @@ export interface JSONLazy<T extends JSONTypeAny>
7
7
  deepPartial(): JSONLazy<ReturnType<T['deepPartial']>>;
8
8
  }
9
9
 
10
- export const lazy = <T extends JSONTypeAny>(fn: () => T) => {
11
- constructType<JSONLazy<T>>({
10
+ export const lazy = <T extends JSONTypeAny>(fn: () => T): JSONLazy<T> => {
11
+ return constructType<JSONLazy<T>>({
12
12
  dataType: 'lazy',
13
- getter: fn,
13
+ getter() {
14
+ return this.typeCache || (this.typeCache = fn());
15
+ },
14
16
  deepPartial(this: JSONLazy<T>) {
15
17
  return {
16
18
  ...this,
@@ -7,7 +7,7 @@ export interface JSONNativeEnum<T extends EnumLike>
7
7
  options: (number | string)[];
8
8
  }
9
9
 
10
- type EnumLike = { [k: string]: string | number; [nu: number]: string };
10
+ export type EnumLike = { [k: string]: string | number; [nu: number]: string };
11
11
 
12
12
  export const getValidEnumValues = (obj: EnumLike) => {
13
13
  const values: (number | string)[] = [];
@@ -2,7 +2,7 @@ import { JSONTypeAny } from './typeBase';
2
2
 
3
3
  export type JSONNullish<T extends JSONTypeAny> = Omit<T, 'type' | 'data'> & {
4
4
  type: T['type'] | undefined | null;
5
- data: T['data'] & { nullable: true };
5
+ data: T['data'] & { nullable: true; optional: true };
6
6
  };
7
7
 
8
8
  export const nullish = <T extends JSONTypeAny>(type: T): JSONNullish<T> => {
@@ -1,36 +1,39 @@
1
1
  import { constructType, JSONType, JSONTypeAny } from './typeBase';
2
2
  import { JSONNumber, JSONString, scalarTypes } from './scalarTypes';
3
3
 
4
- export interface JSONRecord<Key extends KeyType, Value extends JSONTypeAny>
5
- extends JSONType<Record<Key['type'], Value['type']>, 'record'> {
4
+ export interface JSONRecord<
5
+ Key extends JSONRecordKeyType,
6
+ Value extends JSONTypeAny,
7
+ > extends JSONType<Record<Key['type'], Value['type']>, 'record'> {
6
8
  keyType: Key;
7
9
  valueType: Value;
8
10
  deepPartial(): JSONRecord<Key, ReturnType<Value['deepPartial']>>;
9
11
  }
10
12
 
11
- type KeyType = JSONType<string | number, 'string' | 'number'>;
12
- type Args<Key extends KeyType, Value extends JSONTypeAny> =
13
+ export type JSONRecordKeyType = JSONType<string | number, 'string' | 'number'>;
14
+ type Args<Key extends JSONRecordKeyType, Value extends JSONTypeAny> =
13
15
  | Args2<Key, Value>
14
16
  | Args1<Key>;
15
17
 
16
- type Args2<Key extends KeyType, Value extends JSONTypeAny> = [
18
+ type Args2<Key extends JSONRecordKeyType, Value extends JSONTypeAny> = [
17
19
  key: Key,
18
20
  value: Value,
19
21
  ];
20
22
  type Args1<Value extends JSONTypeAny> = [value: Value];
21
23
 
22
- export function record(
23
- ...args: Args<JSONString | JSONNumber, JSONTypeAny>
24
- ): JSONRecord<JSONString | JSONNumber, JSONTypeAny> {
24
+ export function record<
25
+ KeyType extends JSONString | JSONNumber,
26
+ ValueType extends JSONTypeAny,
27
+ >(...args: Args<KeyType, ValueType>): JSONRecord<KeyType, ValueType> {
25
28
  const [keyType, valueType] = (
26
29
  args.length === 1 ? [scalarTypes.string(), args[0]] : args
27
- ) as Args2<JSONString | JSONNumber, JSONTypeAny>;
30
+ ) as Args2<KeyType, ValueType>;
28
31
 
29
- return constructType<JSONRecord<JSONString | JSONNumber, JSONTypeAny>>({
32
+ return constructType<JSONRecord<KeyType, ValueType>>({
30
33
  dataType: 'record',
31
34
  keyType,
32
35
  valueType,
33
- deepPartial(this: JSONRecord<JSONString, JSONTypeAny>) {
36
+ deepPartial(this: JSONRecord<KeyType, ValueType>) {
34
37
  return {
35
38
  ...this,
36
39
  valueType: this.valueType.deepPartial(),
@@ -1,7 +1,12 @@
1
1
  import { constructType, JSONType, JSONTypeAny } from './typeBase';
2
2
  import { BaseNumberData } from '../number';
3
3
  import { BaseStringData } from '../string';
4
- import { numberTypeMethods, stringTypeMethods } from '../commonMethods';
4
+ import {
5
+ dateTypeMethods,
6
+ numberTypeMethods,
7
+ stringTypeMethods,
8
+ } from '../commonMethods';
9
+ import { DateColumnData } from '../dateTime';
5
10
 
6
11
  export type JSONAny = JSONTypeAny & {
7
12
  dataType: 'any';
@@ -17,7 +22,7 @@ export type JSONBigInt = JSONType<bigint, 'bigint'> & {
17
22
  } & typeof bigIntMethods;
18
23
  const bigIntMethods = {
19
24
  dataType: 'bigint' as const,
20
- ...numberTypeMethods<JSONType<bigint, 'bigint'> & { data: BaseNumberData }>(),
25
+ ...numberTypeMethods,
21
26
  };
22
27
  const bigint = () => {
23
28
  return constructType<JSONBigInt>(bigIntMethods);
@@ -55,25 +60,29 @@ export type JSONNumber = JSONType<number, 'number'> & {
55
60
  data: BaseNumberData;
56
61
  } & typeof numberMethods;
57
62
  const numberMethods = {
58
- ...numberTypeMethods<JSONType<number, 'number'> & { data: BaseNumberData }>(),
63
+ ...numberTypeMethods,
59
64
  dataType: 'number' as const,
60
65
  };
61
66
  const number = () => {
62
67
  return constructType<JSONNumber>(numberMethods);
63
68
  };
64
69
 
65
- export type JSONDate = JSONType<Date, 'date'>;
70
+ export type JSONDate = JSONType<Date, 'date'> & {
71
+ data: DateColumnData;
72
+ } & typeof dateTypeMethods;
73
+ const dateMethods = {
74
+ ...dateTypeMethods,
75
+ dataType: 'date' as const,
76
+ };
66
77
  const date = () => {
67
- return constructType<JSONDate>({
68
- dataType: 'date',
69
- });
78
+ return constructType<JSONDate>(dateMethods);
70
79
  };
71
80
 
72
81
  export type JSONString = JSONType<string, 'string'> & {
73
82
  data: BaseStringData;
74
83
  } & typeof stringMethods;
75
84
  const stringMethods = {
76
- ...stringTypeMethods<JSONType<number, 'string'> & { data: BaseStringData }>(),
85
+ ...stringTypeMethods(),
77
86
  dataType: 'string' as const,
78
87
  };
79
88
  const string = () => {
@@ -1,13 +1,13 @@
1
1
  import { constructType, JSONType, JSONTypeAny, JSONTypeData } from './typeBase';
2
- import { ArrayMethods, arrayMethods } from '../commonMethods';
2
+ import { SetMethods, setMethods } from '../commonMethods';
3
3
 
4
4
  export interface JSONSet<Value extends JSONTypeAny>
5
5
  extends JSONType<Set<Value['type']>, 'set'>,
6
- ArrayMethods {
6
+ SetMethods {
7
7
  data: JSONTypeData & {
8
8
  min?: number;
9
9
  max?: number;
10
- length?: number;
10
+ size?: number;
11
11
  };
12
12
  valueType: Value;
13
13
  deepPartial(): JSONSet<ReturnType<Value['deepPartial']>>;
@@ -29,6 +29,6 @@ export const set = <Value extends JSONTypeAny>(valueType: Value) => {
29
29
  data: { min: 1 };
30
30
  };
31
31
  },
32
- ...arrayMethods,
32
+ ...setMethods,
33
33
  });
34
34
  };
@@ -5,6 +5,8 @@ export interface JSONTuple<
5
5
  Rest extends JSONTypeAny | null = null,
6
6
  > extends JSONType<OutputTypeOfTupleWithRest<T, Rest>, 'tuple'> {
7
7
  items: T;
8
+ restType: Rest;
9
+ rest<Rest extends JSONTypeAny | null>(rest: Rest): JSONTuple<T, Rest>;
8
10
  deepPartial(): {
9
11
  [k in keyof T]: T[k] extends JSONTypeAny ? DeepPartial<T[k]> : never;
10
12
  } extends infer PI
@@ -26,10 +28,23 @@ export type OutputTypeOfTupleWithRest<
26
28
  ? [...OutputTypeOfTuple<T>, ...Rest['type'][]]
27
29
  : OutputTypeOfTuple<T>;
28
30
 
29
- export const tuple = <T extends JSONTupleItems | []>(items: T) => {
30
- return constructType<JSONTuple<T>>({
31
+ export const tuple = <
32
+ T extends JSONTupleItems | [],
33
+ Rest extends JSONTypeAny | null = null,
34
+ >(
35
+ items: T,
36
+ rest: Rest = null as Rest,
37
+ ) => {
38
+ return constructType<JSONTuple<T, Rest>>({
31
39
  dataType: 'tuple',
32
40
  items,
41
+ restType: rest,
42
+ rest<Rest extends JSONTypeAny | null>(rest: Rest): JSONTuple<T, Rest> {
43
+ return {
44
+ ...this,
45
+ restType: rest,
46
+ } as unknown as JSONTuple<T, Rest>;
47
+ },
33
48
  deepPartial(this: JSONTuple<T>) {
34
49
  return {
35
50
  ...this,
@@ -9,6 +9,7 @@ import { JSONNotNullish, JSONNullish, notNullish, nullish } from './nullish';
9
9
  import { intersection, JSONIntersection } from './intersection';
10
10
  import { array, JSONArray } from './array';
11
11
  import { union } from './union';
12
+ import { ColumnData, ValidationContext } from '../columnType';
12
13
 
13
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
15
  export type JSONTypeAny = JSONType<any, string>;
@@ -19,22 +20,22 @@ export type DeepPartial<T extends JSONTypeAny> = ReturnType<
19
20
  ? T
20
21
  : ReturnType<T['deepPartial']>;
21
22
 
22
- export type JSONTypeData = {
23
+ export type JSONTypeData = ColumnData & {
23
24
  optional?: true;
24
25
  nullable?: true;
25
26
  };
26
27
 
27
28
  export type Primitive = string | number | bigint | boolean | null | undefined;
28
29
 
29
- export type JSONType<Type, DataType> = {
30
+ export type JSONType<Type, DataType extends string = string> = {
30
31
  type: Type;
31
32
  data: JSONTypeData;
32
33
  dataType: DataType;
33
34
  chain: (
34
- | ['transform', (input: unknown) => unknown]
35
+ | ['transform', (input: unknown, ctx: ValidationContext) => unknown]
35
36
  | ['to', (input: unknown) => JSONTypeAny | undefined, JSONTypeAny]
36
37
  | ['refine', (input: unknown) => unknown]
37
- | ['superRefine', (input: unknown) => unknown]
38
+ | ['superRefine', (input: unknown, ctx: ValidationContext) => unknown]
38
39
  )[];
39
40
 
40
41
  optional<T extends JSONTypeAny>(this: T): JSONOptional<T>;
@@ -47,7 +48,7 @@ export type JSONType<Type, DataType> = {
47
48
 
48
49
  transform<T extends JSONTypeAny, Transformed>(
49
50
  this: T,
50
- fn: (input: T['type']) => Transformed,
51
+ fn: (input: T['type'], ctx: ValidationContext) => Transformed,
51
52
  ): JSONType<
52
53
  Transformed extends PromiseLike<unknown>
53
54
  ? Awaited<Transformed>
@@ -68,7 +69,7 @@ export type JSONType<Type, DataType> = {
68
69
 
69
70
  superRefine<T extends JSONTypeAny, RefinedOutput extends T['type']>(
70
71
  this: T,
71
- check: (arg: T['type']) => unknown,
72
+ check: (arg: T['type'], ctx: ValidationContext) => unknown,
72
73
  ): T & { type: RefinedOutput };
73
74
 
74
75
  and<A extends JSONTypeAny, B extends JSONTypeAny>(
@@ -126,7 +127,7 @@ const baseTypeMethods: JSONTypeAny = {
126
127
 
127
128
  transform<T extends JSONTypeAny, Transformed>(
128
129
  this: T,
129
- fn: (input: unknown) => Transformed,
130
+ fn: (input: unknown, ctx: ValidationContext) => Transformed,
130
131
  ) {
131
132
  return {
132
133
  ...this,
@@ -165,15 +166,9 @@ const baseTypeMethods: JSONTypeAny = {
165
166
  },
166
167
 
167
168
  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
- });
169
+ const cloned = Object.create(this);
170
+ cloned.data = { ...cloned.data, default: value };
171
+ return cloned;
177
172
  },
178
173
 
179
174
  array() {
@@ -8,7 +8,7 @@ export interface JSONUnion<
8
8
 
9
9
  export const union = <T extends [JSONTypeAny, JSONTypeAny, ...JSONTypeAny[]]>(
10
10
  types: T,
11
- ): T[number] => {
11
+ ): JSONUnion<T> => {
12
12
  return constructType<JSONUnion<T>>({
13
13
  dataType: 'union',
14
14
  types,
@@ -1,5 +1,5 @@
1
- import { ColumnType } from './columnType';
2
- import { Operators } from '../operators';
1
+ import { ColumnData, ColumnType } from './columnType';
2
+ import { Operators } from '../columnsOperators';
3
3
  import { scalarTypes } from './json/scalarTypes';
4
4
  import { array } from './json/array';
5
5
  import { discriminatedUnion } from './json/discriminatedUnion';
@@ -21,7 +21,7 @@ import { union } from './json/union';
21
21
  import { JSONTypeAny } from './json/typeBase';
22
22
 
23
23
  export type JSONTypes = typeof jsonTypes;
24
- const jsonTypes = {
24
+ export const jsonTypes = {
25
25
  array,
26
26
  discriminatedUnion,
27
27
  enum: enumType,
@@ -47,7 +47,7 @@ export class JSONColumn<
47
47
  > extends ColumnType<Type['type'], typeof Operators.json> {
48
48
  dataType = 'jsonb' as const;
49
49
  operators = Operators.json;
50
- data: { schema: Type };
50
+ data: ColumnData & { schema: Type };
51
51
 
52
52
  constructor(schemaOrFn: Type | ((j: JSONTypes) => Type)) {
53
53
  super();
@@ -1,49 +1,61 @@
1
- import { Operators } from '../operators';
2
- import { ColumnType } from './columnType';
1
+ import { Operators } from '../columnsOperators';
2
+ import { ColumnData, ColumnType } from './columnType';
3
3
  import { joinTruthy } from '../utils';
4
4
  import { assignMethodsToClass } from './utils';
5
5
  import { numberTypeMethods } from './commonMethods';
6
6
 
7
- export interface BaseNumberData {
7
+ export type BaseNumberData = ColumnData & {
8
8
  lt?: number;
9
9
  lte?: number;
10
10
  gt?: number;
11
11
  gte?: number;
12
12
  multipleOf?: number;
13
- }
13
+ int?: boolean;
14
+ };
14
15
 
15
16
  export type NumberColumn = ColumnType<number>;
16
17
 
17
18
  export type NumberColumnData = BaseNumberData;
18
19
 
19
- type NumberMethods = typeof numberMethods;
20
- const numberMethods = numberTypeMethods<ColumnType>();
20
+ type NumberMethods = typeof numberTypeMethods;
21
21
 
22
- export interface NumberBaseColumn<Type>
23
- extends ColumnType<Type, typeof Operators.number>,
22
+ export interface NumberBaseColumn
23
+ extends ColumnType<number, typeof Operators.number>,
24
24
  NumberMethods {}
25
25
 
26
- export abstract class NumberBaseColumn<Type> extends ColumnType<
27
- Type,
26
+ export abstract class NumberBaseColumn extends ColumnType<
27
+ number,
28
28
  typeof Operators.number
29
29
  > {
30
30
  data = {} as NumberColumnData;
31
31
  operators = Operators.number;
32
32
  }
33
33
 
34
- assignMethodsToClass(NumberBaseColumn, numberMethods);
34
+ assignMethodsToClass(NumberBaseColumn, numberTypeMethods);
35
+
36
+ export abstract class IntegerBaseColumn extends NumberBaseColumn {
37
+ data = { int: true } as NumberColumnData;
38
+ }
35
39
 
36
- export interface DecimalColumnData extends NumberColumnData {
40
+ export abstract class NumberAsStringBaseColumn extends ColumnType<
41
+ string,
42
+ typeof Operators.number
43
+ > {
44
+ data = {};
45
+ operators = Operators.number;
46
+ }
47
+
48
+ export type DecimalColumnData = ColumnData & {
37
49
  precision?: number;
38
50
  scale?: number;
39
- }
51
+ };
40
52
 
41
53
  export class DecimalBaseColumn<
42
- Type extends number | bigint,
43
54
  Precision extends number | undefined = undefined,
44
55
  Scale extends number | undefined = undefined,
45
- > extends NumberBaseColumn<Type> {
56
+ > extends ColumnType<string, typeof Operators.number> {
46
57
  data: DecimalColumnData & { precision: Precision; scale: Scale };
58
+ operators = Operators.number;
47
59
  dataType = 'decimal' as const;
48
60
 
49
61
  constructor(precision?: Precision, scale?: Scale) {
@@ -70,17 +82,19 @@ export class DecimalBaseColumn<
70
82
  }
71
83
 
72
84
  // signed two-byte integer
73
- export class SmallIntColumn extends NumberBaseColumn<number> {
85
+ export class SmallIntColumn extends IntegerBaseColumn {
74
86
  dataType = 'smallint' as const;
87
+ parseItem = parseInt;
75
88
  }
76
89
 
77
90
  // signed four-byte integer
78
- export class IntegerColumn extends NumberBaseColumn<number> {
91
+ export class IntegerColumn extends IntegerBaseColumn {
79
92
  dataType = 'integer' as const;
93
+ parseItem = parseInt;
80
94
  }
81
95
 
82
96
  // signed eight-byte integer
83
- export class BigIntColumn extends NumberBaseColumn<bigint> {
97
+ export class BigIntColumn extends NumberAsStringBaseColumn {
84
98
  dataType = 'bigint' as const;
85
99
  }
86
100
 
@@ -88,35 +102,32 @@ export class BigIntColumn extends NumberBaseColumn<bigint> {
88
102
  export class DecimalColumn<
89
103
  Precision extends number | undefined = undefined,
90
104
  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> {}
105
+ > extends DecimalBaseColumn<Precision, Scale> {}
98
106
 
99
107
  // single precision floating-point number (4 bytes)
100
- export class RealColumn extends NumberBaseColumn<number> {
108
+ export class RealColumn extends NumberBaseColumn {
101
109
  dataType = 'real' as const;
110
+ parseItem = parseFloat;
102
111
  }
103
112
 
104
113
  // double precision floating-point number (8 bytes)
105
- export class DoublePrecisionColumn extends NumberBaseColumn<bigint> {
114
+ export class DoublePrecisionColumn extends NumberAsStringBaseColumn {
106
115
  dataType = 'double precision' as const;
107
116
  }
108
117
 
109
118
  // autoincrementing two-byte integer
110
- export class SmallSerialColumn extends NumberBaseColumn<number> {
119
+ export class SmallSerialColumn extends IntegerBaseColumn {
111
120
  dataType = 'smallserial' as const;
121
+ parseItem = parseInt;
112
122
  }
113
123
 
114
124
  // autoincrementing four-byte integer
115
- export class SerialColumn extends NumberBaseColumn<number> {
125
+ export class SerialColumn extends IntegerBaseColumn {
116
126
  dataType = 'serial' as const;
127
+ parseItem = parseInt;
117
128
  }
118
129
 
119
130
  // autoincrementing eight-byte integer
120
- export class BigSerialColumn extends NumberBaseColumn<bigint> {
131
+ export class BigSerialColumn extends NumberAsStringBaseColumn {
121
132
  dataType = 'bigserial' as const;
122
133
  }