@apisr/drizzle-model 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/dist/index.d.mts +25 -0
- package/dist/index.mjs +5 -0
- package/dist/model/builder.d.mts +20 -0
- package/dist/model/builder.mjs +18 -0
- package/dist/model/config.d.mts +23 -0
- package/dist/model/core/joins.mjs +184 -0
- package/dist/model/core/projection.mjs +28 -0
- package/dist/model/core/runtime.mjs +198 -0
- package/dist/model/core/thenable.mjs +64 -0
- package/dist/model/core/transform.mjs +39 -0
- package/dist/model/core/where.mjs +130 -0
- package/dist/model/core/with.mjs +19 -0
- package/dist/model/dialect.d.mts +12 -0
- package/dist/model/foreigns.d.mts +7 -0
- package/dist/model/format.d.mts +4 -0
- package/dist/model/index.d.mts +23 -0
- package/dist/model/index.mjs +4 -0
- package/dist/model/methods/exclude.d.mts +8 -0
- package/dist/model/methods/include.d.mts +6 -0
- package/dist/model/methods/index.d.mts +10 -0
- package/dist/model/methods/insert.d.mts +8 -0
- package/dist/model/methods/levels.d.mts +5 -0
- package/dist/model/methods/query/where.d.mts +14 -0
- package/dist/model/methods/return.d.mts +12 -0
- package/dist/model/methods/select.d.mts +8 -0
- package/dist/model/methods/update.d.mts +7 -0
- package/dist/model/methods/upsert.d.mts +26 -0
- package/dist/model/methods/with.d.mts +16 -0
- package/dist/model/model.d.mts +105 -0
- package/dist/model/options.d.mts +28 -0
- package/dist/model/query/operations.d.mts +123 -0
- package/dist/model/query/operations.mjs +12 -0
- package/dist/model/relation.d.mts +68 -0
- package/dist/model/result.d.mts +34 -0
- package/dist/model/shape.d.mts +8 -0
- package/dist/model/table.d.mts +81 -0
- package/dist/types.d.mts +12 -0
- package/drizzle.config.ts +6 -6
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/model/builder.ts +37 -37
- package/src/model/config.ts +28 -25
- package/src/model/core/joins.ts +337 -252
- package/src/model/core/projection.ts +49 -35
- package/src/model/core/runtime.ts +302 -221
- package/src/model/core/thenable.ts +70 -61
- package/src/model/core/transform.ts +53 -33
- package/src/model/core/where.ts +228 -162
- package/src/model/core/with.ts +21 -21
- package/src/model/dialect.ts +12 -2
- package/src/model/foreigns.ts +6 -10
- package/src/model/format.ts +8 -8
- package/src/model/index.ts +14 -0
- package/src/model/methods/include.ts +1 -1
- package/src/model/methods/index.ts +10 -0
- package/src/model/methods/insert.ts +13 -10
- package/src/model/methods/levels.ts +7 -1
- package/src/model/methods/query/where.ts +49 -36
- package/src/model/methods/return.ts +13 -12
- package/src/model/methods/select.ts +29 -29
- package/src/model/methods/update.ts +3 -1
- package/src/model/methods/upsert.ts +35 -36
- package/src/model/model.ts +115 -107
- package/src/model/options.ts +44 -37
- package/src/model/query/operations.ts +73 -72
- package/src/model/relation.ts +47 -46
- package/src/model/result.ts +79 -63
- package/src/model/shape.ts +5 -4
- package/src/model/table.ts +34 -33
- package/src/types.ts +3 -3
- package/tests/builder-v2-mysql.type-test.ts +31 -20
- package/tests/builder-v2.type-test.ts +246 -253
- package/tests/builder.test.ts +1 -1
- package/tests/db.ts +3 -3
- package/tests/find.test.ts +149 -138
- package/tests/insert.test.ts +217 -203
- package/tests/relations.ts +34 -34
- package/tests/schema.ts +34 -35
|
@@ -5,81 +5,90 @@ import type { MethodWithValue } from "../methods/with.ts";
|
|
|
5
5
|
type AnyObj = Record<string, any>;
|
|
6
6
|
|
|
7
7
|
type QueryState = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
where?: unknown;
|
|
9
|
+
with?: unknown;
|
|
10
|
+
raw?: boolean;
|
|
11
|
+
select?: AnyObj;
|
|
12
|
+
exclude?: AnyObj;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
type MutateKind = "insert" | "update" | "delete" | "upsert";
|
|
16
16
|
|
|
17
17
|
type MutateState = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
kind: MutateKind;
|
|
19
|
+
where?: unknown;
|
|
20
|
+
value?: unknown;
|
|
21
|
+
returnSelect?: AnyObj;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
export class ThenableResult<T> implements PromiseLike<T> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
protected _execute: () => Promise<T>;
|
|
26
|
+
|
|
27
|
+
constructor(execute: () => Promise<T>) {
|
|
28
|
+
this._execute = execute;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
then<TResult1 = T, TResult2 = never>(
|
|
32
|
+
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
|
|
33
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
|
|
34
|
+
): Promise<TResult1 | TResult2> {
|
|
35
|
+
return this._execute().then(onfulfilled as any, onrejected as any);
|
|
36
|
+
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export class QueryResult<T> extends ThenableResult<T> {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
40
|
+
private state: QueryState;
|
|
41
|
+
private runner: (state: QueryState) => Promise<T>;
|
|
42
|
+
|
|
43
|
+
constructor(state: QueryState, runner: (state: QueryState) => Promise<T>) {
|
|
44
|
+
super(() => runner(state));
|
|
45
|
+
this.state = state;
|
|
46
|
+
this.runner = runner;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
with(value: MethodWithValue<any, any>): any {
|
|
50
|
+
return new QueryResult({ ...this.state, with: value }, this.runner) as any;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
select(value: MethodSelectValue<any>): any {
|
|
54
|
+
return new QueryResult(
|
|
55
|
+
{ ...this.state, select: value as any },
|
|
56
|
+
this.runner
|
|
57
|
+
) as any;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
exclude(value: MethodExcludeValue<any>): any {
|
|
61
|
+
return new QueryResult(
|
|
62
|
+
{ ...this.state, exclude: value as any },
|
|
63
|
+
this.runner
|
|
64
|
+
) as any;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
raw(): any {
|
|
68
|
+
return new QueryResult({ ...this.state, raw: true }, this.runner) as any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
debug(): any {
|
|
72
|
+
return this.state;
|
|
73
|
+
}
|
|
68
74
|
}
|
|
69
75
|
|
|
70
76
|
export class MutateResult<T> extends ThenableResult<T> {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
private state: MutateState;
|
|
78
|
+
private runner: (state: MutateState) => Promise<T>;
|
|
79
|
+
|
|
80
|
+
constructor(state: MutateState, runner: (state: MutateState) => Promise<T>) {
|
|
81
|
+
super(() => runner(state));
|
|
82
|
+
this.state = state;
|
|
83
|
+
this.runner = runner;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return(value?: AnyObj): any {
|
|
87
|
+
return new MutateResult(
|
|
88
|
+
{ ...this.state, returnSelect: value },
|
|
89
|
+
this.runner
|
|
90
|
+
) as any;
|
|
91
|
+
}
|
|
83
92
|
}
|
|
84
93
|
|
|
85
94
|
export type { QueryState, MutateState, MutateKind };
|
|
@@ -1,45 +1,65 @@
|
|
|
1
1
|
type AnyObj = Record<string, any>;
|
|
2
2
|
|
|
3
3
|
export function applySelect(value: any, select: AnyObj): any {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
if (value == null) {
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
return value.map((v) => applySelect(v, select));
|
|
9
|
+
}
|
|
10
|
+
if (typeof value !== "object") {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
const out: AnyObj = {};
|
|
15
|
+
for (const [key, sel] of Object.entries(select)) {
|
|
16
|
+
if (sel === true) {
|
|
17
|
+
out[key] = (value as any)[key];
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (sel && typeof sel === "object") {
|
|
21
|
+
out[key] = applySelect((value as any)[key], sel as AnyObj);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
export function applyExclude(value: any, exclude: AnyObj): any {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
if (value == null) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
return value.map((v) => applyExclude(v, exclude));
|
|
33
|
+
}
|
|
34
|
+
if (typeof value !== "object") {
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
const out: AnyObj = { ...(value as AnyObj) };
|
|
39
|
+
for (const [key, ex] of Object.entries(exclude)) {
|
|
40
|
+
if (ex === true) {
|
|
41
|
+
delete out[key];
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (ex && typeof ex === "object" && key in out) {
|
|
45
|
+
out[key] = applyExclude(out[key], ex as AnyObj);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
37
49
|
}
|
|
38
50
|
|
|
39
51
|
export function applyFormat(value: any, format: any): any {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
if (!format) {
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
if (value == null) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
return value.map((v) => applyFormat(v, format));
|
|
60
|
+
}
|
|
61
|
+
if (typeof value !== "object") {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
return format(value);
|
|
45
65
|
}
|
package/src/model/core/where.ts
CHANGED
|
@@ -1,183 +1,249 @@
|
|
|
1
|
+
import type { SQL } from "drizzle-orm";
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
and,
|
|
4
|
+
between,
|
|
5
|
+
eq,
|
|
6
|
+
gt,
|
|
7
|
+
gte,
|
|
8
|
+
ilike,
|
|
9
|
+
inArray,
|
|
10
|
+
isNull,
|
|
11
|
+
like,
|
|
12
|
+
lt,
|
|
13
|
+
lte,
|
|
14
|
+
ne,
|
|
15
|
+
notBetween,
|
|
16
|
+
notInArray,
|
|
17
|
+
or,
|
|
17
18
|
} from "drizzle-orm";
|
|
18
|
-
import type { SQL } from "drizzle-orm";
|
|
19
19
|
import type { EscapedValue } from "../query/operations.ts";
|
|
20
20
|
|
|
21
21
|
type AnyObj = Record<string, any>;
|
|
22
22
|
|
|
23
23
|
function isPromiseLike(value: any): value is PromiseLike<any> {
|
|
24
|
-
|
|
24
|
+
return (
|
|
25
|
+
value &&
|
|
26
|
+
(typeof value === "object" || typeof value === "function") &&
|
|
27
|
+
typeof value.then === "function"
|
|
28
|
+
);
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
function isEscapedValue(value: any): value is EscapedValue<any> {
|
|
28
|
-
|
|
32
|
+
return (
|
|
33
|
+
value &&
|
|
34
|
+
typeof value === "object" &&
|
|
35
|
+
("equal" in value || value.__kind === "esc-op")
|
|
36
|
+
);
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
function unwrapEscapedValue(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
function unwrapEscapedValue(
|
|
40
|
+
column: any,
|
|
41
|
+
value: any
|
|
42
|
+
): { sql?: SQL; value?: any } {
|
|
43
|
+
if (!isEscapedValue(value)) {
|
|
44
|
+
return { value };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if ((value as any).__kind === "esc-op") {
|
|
48
|
+
return {
|
|
49
|
+
sql: (value as any).op(column, (value as any).value),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
value: (value as any).equal,
|
|
55
|
+
};
|
|
43
56
|
}
|
|
44
57
|
|
|
45
58
|
function compileColumnValue(column: any, value: any): SQL | undefined {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
59
|
+
if (isEscapedValue(value)) {
|
|
60
|
+
if ((value as any).__kind === "esc-op") {
|
|
61
|
+
return (value as any).op(column, (value as any).value);
|
|
62
|
+
}
|
|
63
|
+
return eq(column, (value as any).equal);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
67
|
+
const parts: SQL[] = [];
|
|
68
|
+
|
|
69
|
+
const pushIf = (sql: SQL | undefined) => {
|
|
70
|
+
if (sql) {
|
|
71
|
+
parts.push(sql);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const v: AnyObj = value;
|
|
76
|
+
if ("eq" in v) {
|
|
77
|
+
const u = unwrapEscapedValue(column, v.eq);
|
|
78
|
+
pushIf(u.sql ?? eq(column, u.value));
|
|
79
|
+
}
|
|
80
|
+
if ("equal" in v) {
|
|
81
|
+
const u = unwrapEscapedValue(column, v.equal);
|
|
82
|
+
pushIf(u.sql ?? eq(column, u.value));
|
|
83
|
+
}
|
|
84
|
+
if ("not" in v) {
|
|
85
|
+
const u = unwrapEscapedValue(column, v.not);
|
|
86
|
+
pushIf(u.sql ?? ne(column, u.value));
|
|
87
|
+
}
|
|
88
|
+
if ("in" in v) {
|
|
89
|
+
const arr = (v.in ?? []).map((item: any) =>
|
|
90
|
+
unwrapEscapedValue(column, item)
|
|
91
|
+
);
|
|
92
|
+
const sqls = arr
|
|
93
|
+
.map((x: { sql?: SQL; value?: any }) => x.sql)
|
|
94
|
+
.filter(Boolean) as SQL[];
|
|
95
|
+
const values = arr
|
|
96
|
+
.map((x: { sql?: SQL; value?: any }) => x.value)
|
|
97
|
+
.filter((x: any) => x !== undefined);
|
|
98
|
+
if (sqls.length) {
|
|
99
|
+
pushIf(or(...sqls));
|
|
100
|
+
}
|
|
101
|
+
if (values.length) {
|
|
102
|
+
pushIf(inArray(column, values));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if ("nin" in v) {
|
|
106
|
+
const arr = (v.nin ?? []).map((item: any) =>
|
|
107
|
+
unwrapEscapedValue(column, item)
|
|
108
|
+
);
|
|
109
|
+
const sqls = arr
|
|
110
|
+
.map((x: { sql?: SQL; value?: any }) => x.sql)
|
|
111
|
+
.filter(Boolean) as SQL[];
|
|
112
|
+
const values = arr
|
|
113
|
+
.map((x: { sql?: SQL; value?: any }) => x.value)
|
|
114
|
+
.filter((x: any) => x !== undefined);
|
|
115
|
+
if (sqls.length) {
|
|
116
|
+
pushIf(or(...sqls));
|
|
117
|
+
}
|
|
118
|
+
if (values.length) {
|
|
119
|
+
pushIf(notInArray(column, values));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if ("isNull" in v) {
|
|
123
|
+
pushIf(v.isNull ? isNull(column) : undefined);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if ("gt" in v) {
|
|
127
|
+
const u = unwrapEscapedValue(column, v.gt);
|
|
128
|
+
pushIf(u.sql ?? gt(column, u.value));
|
|
129
|
+
}
|
|
130
|
+
if ("gte" in v) {
|
|
131
|
+
const u = unwrapEscapedValue(column, v.gte);
|
|
132
|
+
pushIf(u.sql ?? gte(column, u.value));
|
|
133
|
+
}
|
|
134
|
+
if ("lt" in v) {
|
|
135
|
+
const u = unwrapEscapedValue(column, v.lt);
|
|
136
|
+
pushIf(u.sql ?? lt(column, u.value));
|
|
137
|
+
}
|
|
138
|
+
if ("lte" in v) {
|
|
139
|
+
const u = unwrapEscapedValue(column, v.lte);
|
|
140
|
+
pushIf(u.sql ?? lte(column, u.value));
|
|
141
|
+
}
|
|
142
|
+
if ("between" in v) {
|
|
143
|
+
const a = unwrapEscapedValue(column, v.between?.[0]);
|
|
144
|
+
const b = unwrapEscapedValue(column, v.between?.[1]);
|
|
145
|
+
if (a.sql) {
|
|
146
|
+
pushIf(a.sql);
|
|
147
|
+
}
|
|
148
|
+
if (b.sql) {
|
|
149
|
+
pushIf(b.sql);
|
|
150
|
+
}
|
|
151
|
+
pushIf(between(column, a.value, b.value));
|
|
152
|
+
}
|
|
153
|
+
if ("notBetween" in v) {
|
|
154
|
+
const a = unwrapEscapedValue(column, v.notBetween?.[0]);
|
|
155
|
+
const b = unwrapEscapedValue(column, v.notBetween?.[1]);
|
|
156
|
+
if (a.sql) {
|
|
157
|
+
pushIf(a.sql);
|
|
158
|
+
}
|
|
159
|
+
if (b.sql) {
|
|
160
|
+
pushIf(b.sql);
|
|
161
|
+
}
|
|
162
|
+
pushIf(notBetween(column, a.value, b.value));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if ("like" in v) {
|
|
166
|
+
const u = unwrapEscapedValue(column, v.like);
|
|
167
|
+
pushIf(u.sql ?? like(column, u.value));
|
|
168
|
+
}
|
|
169
|
+
if ("ilike" in v) {
|
|
170
|
+
const u = unwrapEscapedValue(column, v.ilike);
|
|
171
|
+
pushIf(u.sql ?? ilike(column, u.value));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (Array.isArray(v.or)) {
|
|
175
|
+
const sub = v.or
|
|
176
|
+
.map((item: any) => compileColumnValue(column, item))
|
|
177
|
+
.filter(Boolean) as SQL[];
|
|
178
|
+
if (sub.length) {
|
|
179
|
+
pushIf(or(...sub));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (Array.isArray(v.and)) {
|
|
184
|
+
const sub = v.and
|
|
185
|
+
.map((item: any) => compileColumnValue(column, item))
|
|
186
|
+
.filter(Boolean) as SQL[];
|
|
187
|
+
if (sub.length) {
|
|
188
|
+
pushIf(and(...sub));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (!parts.length) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
return parts.length === 1 ? parts[0] : and(...parts);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return eq(column, value);
|
|
148
199
|
}
|
|
149
200
|
|
|
150
|
-
export function compileWhereObject(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
201
|
+
export function compileWhereObject(
|
|
202
|
+
fields: AnyObj,
|
|
203
|
+
where: AnyObj
|
|
204
|
+
): SQL | undefined {
|
|
205
|
+
const parts: SQL[] = [];
|
|
206
|
+
for (const [key, value] of Object.entries(where)) {
|
|
207
|
+
if (value === undefined) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const col = (fields as any)[key];
|
|
212
|
+
if (col) {
|
|
213
|
+
const sql = compileColumnValue(col, value);
|
|
214
|
+
if (sql) {
|
|
215
|
+
parts.push(sql);
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (value && typeof value === "object") {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Relation where is not implemented yet for key '${key}'.`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (!parts.length) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
return parts.length === 1 ? parts[0] : and(...parts);
|
|
169
231
|
}
|
|
170
232
|
|
|
171
233
|
export function compileWhere(fields: AnyObj, where: unknown): SQL | undefined {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
234
|
+
if (!where) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (typeof where === "object" && where && !isPromiseLike(where)) {
|
|
239
|
+
if ((where as any).$model === "model") {
|
|
240
|
+
throw new Error("Model-as-where is not implemented yet.");
|
|
241
|
+
}
|
|
242
|
+
if ((where as any).getSQL) {
|
|
243
|
+
return where as any;
|
|
244
|
+
}
|
|
245
|
+
return compileWhereObject(fields, where as AnyObj);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return where as any;
|
|
183
249
|
}
|