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,11 +1,11 @@
|
|
|
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
4
|
import { NumberBaseColumn } from './number';
|
|
5
5
|
import { assignMethodsToClass } from './utils';
|
|
6
6
|
import { stringTypeMethods } from './commonMethods';
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export type BaseStringData = ColumnData & {
|
|
9
9
|
min?: number;
|
|
10
10
|
max?: number;
|
|
11
11
|
length?: number;
|
|
@@ -14,15 +14,17 @@ export interface BaseStringData {
|
|
|
14
14
|
uuid?: boolean;
|
|
15
15
|
cuid?: boolean;
|
|
16
16
|
regex?: RegExp;
|
|
17
|
+
startsWith?: string;
|
|
18
|
+
endsWith?: string;
|
|
17
19
|
trim?: boolean;
|
|
18
|
-
}
|
|
20
|
+
};
|
|
19
21
|
|
|
20
22
|
export type StringColumn = ColumnType<string>;
|
|
21
23
|
|
|
22
24
|
export type TextColumnData = BaseStringData;
|
|
23
25
|
|
|
24
26
|
type TextMethods = typeof textMethods;
|
|
25
|
-
const textMethods = stringTypeMethods
|
|
27
|
+
const textMethods = stringTypeMethods();
|
|
26
28
|
|
|
27
29
|
export interface TextBaseColumn
|
|
28
30
|
extends ColumnType<string, typeof Operators.text>,
|
|
@@ -57,14 +59,14 @@ export abstract class LimitedTextBaseColumn<
|
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
// character varying(n), varchar(n)
|
|
62
|
+
// character varying(n), varchar(n) variable-length with limit
|
|
61
63
|
export class VarCharColumn<
|
|
62
64
|
Limit extends number | undefined = undefined,
|
|
63
65
|
> extends LimitedTextBaseColumn<Limit> {
|
|
64
66
|
dataType = 'varchar' as const;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
// character(n), char(n)
|
|
69
|
+
// character(n), char(n) fixed-length, blank padded
|
|
68
70
|
export class CharColumn<
|
|
69
71
|
Limit extends number | undefined = undefined,
|
|
70
72
|
> extends LimitedTextBaseColumn<Limit> {
|
|
@@ -72,14 +74,15 @@ export class CharColumn<
|
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
// text variable unlimited length
|
|
75
|
-
export class TextColumn extends
|
|
77
|
+
export class TextColumn extends TextBaseColumn {
|
|
76
78
|
dataType = 'text' as const;
|
|
77
79
|
operators = Operators.text;
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
// To store binary strings
|
|
81
|
-
export class ByteaColumn extends
|
|
83
|
+
export class ByteaColumn extends ColumnType<Buffer, typeof Operators.text> {
|
|
82
84
|
dataType = 'bytea' as const;
|
|
85
|
+
operators = Operators.text;
|
|
83
86
|
}
|
|
84
87
|
|
|
85
88
|
// point 16 bytes Point on a plane (x,y)
|
|
@@ -125,8 +128,12 @@ export class CircleColumn extends ColumnType<string, typeof Operators.text> {
|
|
|
125
128
|
operators = Operators.text;
|
|
126
129
|
}
|
|
127
130
|
|
|
128
|
-
export class MoneyColumn extends NumberBaseColumn
|
|
131
|
+
export class MoneyColumn extends NumberBaseColumn {
|
|
129
132
|
dataType = 'money' as const;
|
|
133
|
+
|
|
134
|
+
parseFn = (input: unknown) => {
|
|
135
|
+
return parseFloat((input as string).replace(/,/g, '').replace(/\$/g, ''));
|
|
136
|
+
};
|
|
130
137
|
}
|
|
131
138
|
|
|
132
139
|
// cidr 7 or 19 bytes IPv4 and IPv6 networks
|
|
@@ -156,14 +163,15 @@ export class MacAddr8Column extends ColumnType<string, typeof Operators.text> {
|
|
|
156
163
|
// Bit strings are strings of 1's and 0's.
|
|
157
164
|
// They can be used to store or visualize bit masks.
|
|
158
165
|
// There are two SQL bit types: bit(n) and bit varying(n), where n is a positive integer.
|
|
159
|
-
export class BitColumn<
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
export class BitColumn<Length extends number> extends ColumnType<
|
|
167
|
+
string,
|
|
168
|
+
typeof Operators.text
|
|
169
|
+
> {
|
|
162
170
|
dataType = 'bit' as const;
|
|
163
171
|
operators = Operators.text;
|
|
164
|
-
data: { length: Length };
|
|
172
|
+
data: ColumnData & { length: Length };
|
|
165
173
|
|
|
166
|
-
constructor(length
|
|
174
|
+
constructor(length: Length) {
|
|
167
175
|
super();
|
|
168
176
|
|
|
169
177
|
this.data = { length } as { length: Length };
|
|
@@ -182,9 +190,9 @@ export class BitVaryingColumn<
|
|
|
182
190
|
> extends ColumnType<string, typeof Operators.text> {
|
|
183
191
|
dataType = 'bit varying' as const;
|
|
184
192
|
operators = Operators.text;
|
|
185
|
-
data: { length: Length };
|
|
193
|
+
data: ColumnData & { length: Length };
|
|
186
194
|
|
|
187
|
-
constructor(length
|
|
195
|
+
constructor(length: Length) {
|
|
188
196
|
super();
|
|
189
197
|
|
|
190
198
|
this.data = { length } as { length: Length };
|
|
@@ -210,13 +218,14 @@ export class TsQueryColumn extends ColumnType<string, typeof Operators.text> {
|
|
|
210
218
|
operators = Operators.text;
|
|
211
219
|
}
|
|
212
220
|
|
|
213
|
-
//
|
|
221
|
+
// uuid stores Universally Unique Identifiers (UUID)
|
|
214
222
|
export class UUIDColumn extends ColumnType<string, typeof Operators.text> {
|
|
215
223
|
dataType = 'uuid' as const;
|
|
216
224
|
operators = Operators.text;
|
|
217
225
|
}
|
|
218
226
|
|
|
227
|
+
// xml data type can be used to store XML data
|
|
219
228
|
export class XMLColumn extends ColumnType<string, typeof Operators.text> {
|
|
220
|
-
dataType = '
|
|
229
|
+
dataType = 'xml' as const;
|
|
221
230
|
operators = Operators.text;
|
|
222
231
|
}
|
|
File without changes
|
|
File without changes
|
package/src/common.ts
CHANGED
|
@@ -55,22 +55,24 @@ export type ExpressionOutput<
|
|
|
55
55
|
? ColumnType
|
|
56
56
|
: never;
|
|
57
57
|
|
|
58
|
-
export const raw = <C extends ColumnType>(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
58
|
+
export const raw = <C extends ColumnType>(
|
|
59
|
+
...args:
|
|
60
|
+
| [column: C, sql: string, ...values: unknown[]]
|
|
61
|
+
| [sql: string, ...values: unknown[]]
|
|
62
|
+
) => {
|
|
63
|
+
if (typeof args[0] === 'string') {
|
|
64
|
+
return {
|
|
65
|
+
__raw: args[0],
|
|
66
|
+
__values: args.slice(1),
|
|
67
|
+
} as RawExpression<C>;
|
|
68
|
+
} else {
|
|
69
|
+
return {
|
|
70
|
+
__column: args[0],
|
|
71
|
+
__raw: args[1],
|
|
72
|
+
__values: args.slice(2),
|
|
73
|
+
} as RawExpression<C>;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
74
76
|
|
|
75
77
|
export const isRaw = (obj: object): obj is RawExpression => '__raw' in obj;
|
|
76
78
|
|
package/src/db.ts
CHANGED
|
@@ -12,9 +12,10 @@ import {
|
|
|
12
12
|
columnTypes,
|
|
13
13
|
ColumnShapeOutput,
|
|
14
14
|
TableSchema,
|
|
15
|
-
AnyColumnTypeCreator,
|
|
16
15
|
ColumnShapeInput,
|
|
17
16
|
ColumnTypes,
|
|
17
|
+
ColumnTypesBase,
|
|
18
|
+
getColumnTypes,
|
|
18
19
|
} from './columnSchema';
|
|
19
20
|
import { applyMixins } from './utils';
|
|
20
21
|
import { StringKey } from './common';
|
|
@@ -120,7 +121,7 @@ export class Db<
|
|
|
120
121
|
? undefined
|
|
121
122
|
: this.defaultSelectColumns;
|
|
122
123
|
|
|
123
|
-
const columnsParsers
|
|
124
|
+
const columnsParsers = {} as ColumnsParsers;
|
|
124
125
|
let hasParsers = false;
|
|
125
126
|
for (const key in shape) {
|
|
126
127
|
const column = shape[key];
|
|
@@ -148,7 +149,7 @@ export class Db<
|
|
|
148
149
|
applyMixins(Db, [QueryMethods]);
|
|
149
150
|
Db.prototype.constructor = Db;
|
|
150
151
|
|
|
151
|
-
type DbResult<CT extends
|
|
152
|
+
type DbResult<CT extends ColumnTypesBase> = Db & {
|
|
152
153
|
<Table extends string, Shape extends ColumnsShape = ColumnsShape>(
|
|
153
154
|
table: Table,
|
|
154
155
|
shape?: ((t: CT) => Shape) | Shape,
|
|
@@ -159,14 +160,15 @@ type DbResult<CT extends Record<string, AnyColumnTypeCreator>> = Db & {
|
|
|
159
160
|
destroy: Adapter['destroy'];
|
|
160
161
|
};
|
|
161
162
|
|
|
162
|
-
export type DbOptions<
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
export type DbOptions<CT extends ColumnTypesBase = ColumnTypes> = (
|
|
164
|
+
| { adapter: Adapter }
|
|
165
|
+
| Omit<AdapterOptions, 'log'>
|
|
166
|
+
) &
|
|
165
167
|
QueryLogOptions & {
|
|
166
168
|
columnTypes?: CT;
|
|
167
169
|
};
|
|
168
170
|
|
|
169
|
-
export const createDb = <CT extends
|
|
171
|
+
export const createDb = <CT extends ColumnTypesBase>({
|
|
170
172
|
log,
|
|
171
173
|
logger,
|
|
172
174
|
columnTypes: ct = columnTypes as unknown as CT,
|
|
@@ -194,7 +196,7 @@ export const createDb = <CT extends Record<string, AnyColumnTypeCreator>>({
|
|
|
194
196
|
adapter,
|
|
195
197
|
qb,
|
|
196
198
|
table as Table,
|
|
197
|
-
typeof shape === 'function' ?
|
|
199
|
+
typeof shape === 'function' ? getColumnTypes(ct, shape) : shape,
|
|
198
200
|
{ ...commonOptions, ...options },
|
|
199
201
|
);
|
|
200
202
|
},
|
package/src/index.ts
CHANGED
|
@@ -3,16 +3,11 @@ export * from './sql';
|
|
|
3
3
|
export * from './adapter';
|
|
4
4
|
export * from './common';
|
|
5
5
|
export * from './db';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './columnsOperators';
|
|
7
7
|
export * from './query';
|
|
8
|
-
export * from './queryMethods
|
|
9
|
-
export * from './queryMethods/join';
|
|
10
|
-
export * from './queryMethods/transaction';
|
|
11
|
-
export * from './queryMethods/queryMethods';
|
|
12
|
-
export * from './queryMethods/columnInfo';
|
|
8
|
+
export * from './queryMethods';
|
|
13
9
|
export * from './quote';
|
|
14
10
|
export * from './utils';
|
|
15
11
|
export * from './queryDataUtils';
|
|
16
|
-
export * from './queryMethods/then';
|
|
17
12
|
export * from './errors';
|
|
18
13
|
export * from './relations';
|
package/src/query.ts
CHANGED
|
@@ -14,10 +14,10 @@ import { ColumnInfo } from './queryMethods/columnInfo';
|
|
|
14
14
|
import { RelationQueryBase, RelationsBase } from './relations';
|
|
15
15
|
import { WhereQueryBuilder } from './queryMethods/where';
|
|
16
16
|
import { OnQueryBuilder } from './queryMethods/join';
|
|
17
|
-
import { GetArg } from './queryMethods/get';
|
|
17
|
+
import { GetArg, getValueKey } from './queryMethods/get';
|
|
18
18
|
|
|
19
19
|
export type ColumnParser = (input: unknown) => unknown;
|
|
20
|
-
export type ColumnsParsers = Record<string, ColumnParser>;
|
|
20
|
+
export type ColumnsParsers = Record<string | getValueKey, ColumnParser>;
|
|
21
21
|
|
|
22
22
|
export type SelectableBase = Record<
|
|
23
23
|
PropertyKey,
|
|
@@ -9,19 +9,20 @@ import {
|
|
|
9
9
|
import { AddQuerySelect, Query, SetQueryReturnsValue } from '../query';
|
|
10
10
|
import { pushQueryValue, removeFromQuery } from '../queryDataUtils';
|
|
11
11
|
import {
|
|
12
|
+
ArrayColumn,
|
|
12
13
|
BooleanColumn,
|
|
13
14
|
ColumnType,
|
|
14
|
-
ArrayColumn,
|
|
15
|
-
NumberColumn,
|
|
16
|
-
StringColumn,
|
|
17
15
|
IntegerColumn,
|
|
18
16
|
NullableColumn,
|
|
17
|
+
NumberColumn,
|
|
18
|
+
StringColumn,
|
|
19
19
|
} from '../columnSchema';
|
|
20
20
|
import { CoalesceString } from '../utils';
|
|
21
21
|
import { OrderArg, WindowArgDeclaration } from './queryMethods';
|
|
22
22
|
import { WhereArg } from './where';
|
|
23
23
|
import { addParserToQuery } from './select';
|
|
24
24
|
import { SelectItem } from '../sql';
|
|
25
|
+
import { getValueKey } from './get';
|
|
25
26
|
|
|
26
27
|
const allColumns = raw('*');
|
|
27
28
|
|
|
@@ -179,6 +180,8 @@ export class Aggregate {
|
|
|
179
180
|
);
|
|
180
181
|
|
|
181
182
|
if (columnType?.parseFn) {
|
|
183
|
+
addParserToQuery(this.query, getValueKey, columnType.parseFn);
|
|
184
|
+
|
|
182
185
|
addParserToQuery(
|
|
183
186
|
this.query,
|
|
184
187
|
options?.as || functionName,
|
package/src/queryMethods/get.ts
CHANGED
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
import { RelationQueryBase } from '../relations';
|
|
8
8
|
import { isRaw, RawExpression } from '../common';
|
|
9
9
|
import { addParserForRawExpression, processSelectArg } from './select';
|
|
10
|
-
import { getQueryAs } from '../utils';
|
|
11
10
|
|
|
12
11
|
export type GetArg<T extends QueryBase> =
|
|
13
12
|
| keyof T['selectable']
|
|
@@ -29,6 +28,9 @@ type GetOptionalResult<
|
|
|
29
28
|
Arg extends GetArg<T>,
|
|
30
29
|
> = SetQueryReturnsValueOptional<T, UnwrapRaw<T, Arg>>;
|
|
31
30
|
|
|
31
|
+
export type getValueKey = typeof getValueKey;
|
|
32
|
+
export const getValueKey = Symbol('get');
|
|
33
|
+
|
|
32
34
|
const _get = <
|
|
33
35
|
T extends Query,
|
|
34
36
|
R extends 'value' | 'valueOrThrow',
|
|
@@ -42,14 +44,15 @@ const _get = <
|
|
|
42
44
|
q.query.take = true;
|
|
43
45
|
|
|
44
46
|
if (typeof arg === 'object' && isRaw(arg)) {
|
|
45
|
-
addParserForRawExpression(q,
|
|
47
|
+
addParserForRawExpression(q, getValueKey, arg);
|
|
46
48
|
q.query.select = [arg];
|
|
47
49
|
} else {
|
|
48
50
|
q.query.select = [
|
|
49
51
|
processSelectArg(
|
|
50
52
|
q,
|
|
51
|
-
|
|
53
|
+
q.query.as || q.table,
|
|
52
54
|
arg as Exclude<GetArg<T>, RawExpression>,
|
|
55
|
+
getValueKey,
|
|
53
56
|
),
|
|
54
57
|
];
|
|
55
58
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from './aggregate';
|
|
2
|
+
export * from './callbacks';
|
|
3
|
+
export * from './clear';
|
|
4
|
+
export * from './columnInfo';
|
|
5
|
+
export * from './delete';
|
|
6
|
+
export * from './for';
|
|
7
|
+
export * from './from';
|
|
8
|
+
export * from './get';
|
|
9
|
+
export * from './having';
|
|
10
|
+
export * from './insert';
|
|
11
|
+
export * from './join';
|
|
12
|
+
export * from './json';
|
|
13
|
+
export * from './log';
|
|
14
|
+
export * from './queryMethods';
|
|
15
|
+
export * from './select';
|
|
16
|
+
export * from './then';
|
|
17
|
+
export * from './transaction';
|
|
18
|
+
export * from './union';
|
|
19
|
+
export * from './update';
|
|
20
|
+
export * from './upsert';
|
|
21
|
+
export * from './where';
|
|
22
|
+
export * from './with';
|
package/src/queryMethods/join.ts
CHANGED
package/src/queryMethods/log.ts
CHANGED
|
@@ -4,9 +4,9 @@ import { quote } from '../quote';
|
|
|
4
4
|
|
|
5
5
|
export type QueryLogObject = {
|
|
6
6
|
colors: boolean;
|
|
7
|
-
beforeQuery(
|
|
8
|
-
afterQuery(
|
|
9
|
-
onError(error: Error,
|
|
7
|
+
beforeQuery(sql: Sql): unknown;
|
|
8
|
+
afterQuery(sql: Sql, logData: unknown): void;
|
|
9
|
+
onError(error: Error, sql: Sql, logData: unknown): void;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export type QueryLogger = {
|
|
@@ -74,7 +74,7 @@ export const logParamToLogObject = (
|
|
|
74
74
|
beforeQuery() {
|
|
75
75
|
return process.hrtime();
|
|
76
76
|
},
|
|
77
|
-
afterQuery(
|
|
77
|
+
afterQuery(sql, time: [number, number]) {
|
|
78
78
|
logger.log(
|
|
79
79
|
makeMessage(
|
|
80
80
|
colors,
|
|
@@ -87,7 +87,7 @@ export const logParamToLogObject = (
|
|
|
87
87
|
),
|
|
88
88
|
);
|
|
89
89
|
},
|
|
90
|
-
onError(error,
|
|
90
|
+
onError(error, sql, time: [number, number]) {
|
|
91
91
|
const message = `Error: ${error.message}`;
|
|
92
92
|
|
|
93
93
|
logger.error(
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
userData,
|
|
14
14
|
useTestDatabase,
|
|
15
15
|
} from '../test-utils';
|
|
16
|
-
import { raw
|
|
16
|
+
import { raw } from '../common';
|
|
17
17
|
import { DateColumn } from '../columnSchema';
|
|
18
18
|
import { addQueryOn } from './join';
|
|
19
19
|
import { RelationQuery, relationQueryKey } from '../relations';
|
|
@@ -465,7 +465,7 @@ describe('selectMethods', () => {
|
|
|
465
465
|
|
|
466
466
|
it('should parse raw column', async () => {
|
|
467
467
|
const q = User.select({
|
|
468
|
-
date:
|
|
468
|
+
date: raw(
|
|
469
469
|
new DateColumn().parse((input) => new Date(input)),
|
|
470
470
|
'"createdAt"',
|
|
471
471
|
),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AddQuerySelect,
|
|
3
3
|
ColumnParser,
|
|
4
|
+
ColumnsParsers,
|
|
4
5
|
Query,
|
|
5
6
|
QueryBase,
|
|
6
7
|
QuerySelectAll,
|
|
@@ -21,6 +22,7 @@ import {
|
|
|
21
22
|
RelationQueryBase,
|
|
22
23
|
relationQueryKey,
|
|
23
24
|
} from '../relations';
|
|
25
|
+
import { getValueKey } from './get';
|
|
24
26
|
|
|
25
27
|
export type SelectArg<T extends QueryBase> =
|
|
26
28
|
| keyof T['selectable']
|
|
@@ -86,7 +88,7 @@ type SelectResult<
|
|
|
86
88
|
|
|
87
89
|
export const addParserForRawExpression = (
|
|
88
90
|
q: Query,
|
|
89
|
-
key: string,
|
|
91
|
+
key: string | getValueKey,
|
|
90
92
|
raw: RawExpression,
|
|
91
93
|
) => {
|
|
92
94
|
const parser = raw.__column?.parseFn;
|
|
@@ -95,7 +97,7 @@ export const addParserForRawExpression = (
|
|
|
95
97
|
|
|
96
98
|
export const addParserForSelectItem = <T extends Query>(
|
|
97
99
|
q: T,
|
|
98
|
-
as: string | undefined,
|
|
100
|
+
as: string | getValueKey | undefined,
|
|
99
101
|
key: string,
|
|
100
102
|
item: keyof T['selectable'] | Query | RawExpression,
|
|
101
103
|
) => {
|
|
@@ -138,17 +140,18 @@ export const addParserForSelectItem = <T extends Query>(
|
|
|
138
140
|
|
|
139
141
|
export const addParserToQuery = (
|
|
140
142
|
query: QueryData,
|
|
141
|
-
key: string,
|
|
143
|
+
key: string | getValueKey,
|
|
142
144
|
parser: ColumnParser,
|
|
143
145
|
) => {
|
|
144
146
|
if (query.parsers) query.parsers[key] = parser;
|
|
145
|
-
else query.parsers = { [key]: parser };
|
|
147
|
+
else query.parsers = { [key]: parser } as ColumnsParsers;
|
|
146
148
|
};
|
|
147
149
|
|
|
148
150
|
export const processSelectArg = <T extends Query>(
|
|
149
151
|
q: T,
|
|
150
152
|
as: string | undefined,
|
|
151
153
|
item: SelectArg<T>,
|
|
154
|
+
columnAs?: string | getValueKey,
|
|
152
155
|
): SelectItem => {
|
|
153
156
|
if (typeof item === 'string') {
|
|
154
157
|
if ((q.relations as Record<string, Relation>)[item]) {
|
|
@@ -161,16 +164,16 @@ export const processSelectArg = <T extends Query>(
|
|
|
161
164
|
|
|
162
165
|
if (table === as) {
|
|
163
166
|
const parser = q.columnsParsers?.[column];
|
|
164
|
-
if (parser) addParserToQuery(q.query, column, parser);
|
|
167
|
+
if (parser) addParserToQuery(q.query, columnAs || column, parser);
|
|
165
168
|
} else {
|
|
166
169
|
const parser = (q.query as SelectQueryData).joinedParsers?.[table]?.[
|
|
167
170
|
column
|
|
168
171
|
];
|
|
169
|
-
if (parser) addParserToQuery(q.query, column, parser);
|
|
172
|
+
if (parser) addParserToQuery(q.query, columnAs || column, parser);
|
|
170
173
|
}
|
|
171
174
|
} else {
|
|
172
175
|
const parser = q.columnsParsers?.[item];
|
|
173
|
-
if (parser) addParserToQuery(q.query, item, parser);
|
|
176
|
+
if (parser) addParserToQuery(q.query, columnAs || item, parser);
|
|
174
177
|
}
|
|
175
178
|
return item;
|
|
176
179
|
}
|
package/src/queryMethods/then.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { NotFoundError } from '../errors';
|
|
|
4
4
|
import { QueryArraysResult, QueryResult } from '../adapter';
|
|
5
5
|
import { CommonQueryData, Sql } from '../sql';
|
|
6
6
|
import { AfterCallback, BeforeCallback } from './callbacks';
|
|
7
|
+
import { getValueKey } from './get';
|
|
7
8
|
|
|
8
9
|
export type ThenResult<Res> = <T extends Query>(
|
|
9
10
|
this: T,
|
|
@@ -84,7 +85,7 @@ const then = async (
|
|
|
84
85
|
sql = q.toSql();
|
|
85
86
|
|
|
86
87
|
if (q.query.log) {
|
|
87
|
-
logData = q.query.log
|
|
88
|
+
logData = q.query.log.beforeQuery(sql);
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
const queryResult = await q.query.adapter[
|
|
@@ -92,7 +93,7 @@ const then = async (
|
|
|
92
93
|
](sql);
|
|
93
94
|
|
|
94
95
|
if (q.query.log) {
|
|
95
|
-
q.query.log
|
|
96
|
+
q.query.log.afterQuery(sql, logData);
|
|
96
97
|
// set sql to be undefined to prevent logging on error in case if afterCallbacks throws
|
|
97
98
|
sql = undefined;
|
|
98
99
|
}
|
|
@@ -112,7 +113,7 @@ const then = async (
|
|
|
112
113
|
resolve?.(result);
|
|
113
114
|
} catch (error) {
|
|
114
115
|
if (q.query.log && sql && logData) {
|
|
115
|
-
q.query.log.onError(error as Error,
|
|
116
|
+
q.query.log.onError(error as Error, sql, logData);
|
|
116
117
|
}
|
|
117
118
|
reject?.(error);
|
|
118
119
|
}
|
|
@@ -166,19 +167,12 @@ export const parseResult = (
|
|
|
166
167
|
}
|
|
167
168
|
case 'value': {
|
|
168
169
|
const value = result.rows[0]?.[0];
|
|
169
|
-
return value !== undefined
|
|
170
|
-
? parseValue(value, (result as unknown as QueryArraysResult).fields, q)
|
|
171
|
-
: undefined;
|
|
170
|
+
return value !== undefined ? parseValue(value, q) : undefined;
|
|
172
171
|
}
|
|
173
172
|
case 'valueOrThrow': {
|
|
174
173
|
const value = result.rows[0]?.[0];
|
|
175
174
|
if (value === undefined) throw new NotFoundError();
|
|
176
|
-
|
|
177
|
-
return parseValue(
|
|
178
|
-
value,
|
|
179
|
-
(result as unknown as QueryArraysResult).fields,
|
|
180
|
-
q,
|
|
181
|
-
);
|
|
175
|
+
return parseValue(value, q);
|
|
182
176
|
}
|
|
183
177
|
case 'rowCount': {
|
|
184
178
|
if (q.query.throwOnNotFound && result.rowCount === 0) {
|
|
@@ -219,15 +213,10 @@ const parseRows = (
|
|
|
219
213
|
return rows;
|
|
220
214
|
};
|
|
221
215
|
|
|
222
|
-
const parseValue = (
|
|
223
|
-
value: unknown,
|
|
224
|
-
fields: { name: string }[],
|
|
225
|
-
query: Query,
|
|
226
|
-
) => {
|
|
227
|
-
const field = fields[0];
|
|
216
|
+
const parseValue = (value: unknown, query: Query) => {
|
|
228
217
|
if (value !== null) {
|
|
229
218
|
const parsers = getQueryParsers(query);
|
|
230
|
-
const parser = parsers?.[
|
|
219
|
+
const parser = parsers?.[getValueKey];
|
|
231
220
|
if (parser) {
|
|
232
221
|
return parser(value);
|
|
233
222
|
}
|
|
@@ -12,10 +12,10 @@ describe('transaction', () => {
|
|
|
12
12
|
|
|
13
13
|
const {
|
|
14
14
|
rows: [{ a }],
|
|
15
|
-
} = await db.adapter.query('SELECT 1 AS a');
|
|
15
|
+
} = await db.query.adapter.query('SELECT 1 AS a');
|
|
16
16
|
const {
|
|
17
17
|
rows: [{ b }],
|
|
18
|
-
} = await db.adapter.query('SELECT 2 AS b');
|
|
18
|
+
} = await db.query.adapter.query('SELECT 2 AS b');
|
|
19
19
|
return a + b;
|
|
20
20
|
});
|
|
21
21
|
|
|
@@ -23,11 +23,11 @@ export class Transaction {
|
|
|
23
23
|
const log = this.query.log;
|
|
24
24
|
let logData: unknown | undefined;
|
|
25
25
|
if (log) {
|
|
26
|
-
logData = log.beforeQuery(
|
|
26
|
+
logData = log.beforeQuery(beginSql);
|
|
27
27
|
}
|
|
28
28
|
const t = this.query.adapter.transaction((adapter) => {
|
|
29
29
|
if (log) {
|
|
30
|
-
log.afterQuery(
|
|
30
|
+
log.afterQuery(beginSql, logData);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
const q = this.clone();
|
|
@@ -35,7 +35,7 @@ export class Transaction {
|
|
|
35
35
|
q.query.inTransaction = true;
|
|
36
36
|
|
|
37
37
|
if (log) {
|
|
38
|
-
logData = log.beforeQuery(
|
|
38
|
+
logData = log.beforeQuery(commitSql);
|
|
39
39
|
}
|
|
40
40
|
return cb(q);
|
|
41
41
|
});
|
|
@@ -43,10 +43,10 @@ export class Transaction {
|
|
|
43
43
|
if (log) {
|
|
44
44
|
t.then(
|
|
45
45
|
() => {
|
|
46
|
-
log.afterQuery(
|
|
46
|
+
log.afterQuery(commitSql, logData);
|
|
47
47
|
},
|
|
48
48
|
() => {
|
|
49
|
-
log.afterQuery(
|
|
49
|
+
log.afterQuery(rollbackSql, logData);
|
|
50
50
|
},
|
|
51
51
|
);
|
|
52
52
|
}
|
|
@@ -22,21 +22,18 @@ export type UpdateData<T extends Query> = {
|
|
|
22
22
|
| { set: WhereArg<T['relations'][K]['model']> }
|
|
23
23
|
| { delete: boolean }
|
|
24
24
|
| { update: UpdateData<T['relations'][K]['model']> }
|
|
25
|
+
| {
|
|
26
|
+
create: InsertData<T['relations'][K]['nestedCreateQuery']>;
|
|
27
|
+
}
|
|
25
28
|
| (T['returnType'] extends 'one' | 'oneOrThrow'
|
|
26
|
-
?
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
update: UpdateData<T['relations'][K]['model']>;
|
|
35
|
-
create: InsertData<
|
|
36
|
-
T['relations'][K]['nestedCreateQuery']
|
|
37
|
-
>;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
29
|
+
? {
|
|
30
|
+
upsert: {
|
|
31
|
+
update: UpdateData<T['relations'][K]['model']>;
|
|
32
|
+
create: InsertData<
|
|
33
|
+
T['relations'][K]['nestedCreateQuery']
|
|
34
|
+
>;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
40
37
|
: never)
|
|
41
38
|
: T['relations'][K]['type'] extends 'hasOne'
|
|
42
39
|
?
|
package/src/sql/having.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AggregateItemOptions, HavingItem, SelectQueryData } from './types';
|
|
2
2
|
import { EMPTY_OBJECT, getRaw, isRaw, RawExpression } from '../common';
|
|
3
|
-
import { Operator } from '../
|
|
3
|
+
import { Operator } from '../columnsOperators';
|
|
4
4
|
import { aggregateToSql } from './aggregate';
|
|
5
5
|
import { Query } from '../query';
|
|
6
6
|
import { addValue, q } from './common';
|