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