@expressive/sql 0.1.0 → 0.2.0

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.
@@ -1,115 +0,0 @@
1
- import { Query, Str, Type } from '..';
2
-
3
- class Foo extends Type {
4
- bar = Str();
5
- baz = Str();
6
- }
7
-
8
- it("will count query by default", () => {
9
- class Foo extends Type {}
10
-
11
- const query = Query(where => void where(Foo));
12
-
13
- expect(query).toMatchInlineSnapshot(`
14
- SELECT
15
- COUNT(*)
16
- FROM
17
- foo
18
- `);
19
- })
20
-
21
- it("will select without tables", () => {
22
- const query = Query(() => {
23
- return {
24
- foo: 5,
25
- bar: "hello",
26
- }
27
- });
28
-
29
- expect(query).toMatchInlineSnapshot(`
30
- SELECT
31
- 5 AS "foo",
32
- 'hello' AS "bar"
33
- `);
34
- })
35
-
36
- it("will select via object", () => {
37
- const query = Query(where => {
38
- const { bar, baz } = where(Foo);
39
-
40
- return { bar, baz }
41
- })
42
-
43
- type Returns = Query.Selects<{
44
- bar: string;
45
- baz: string;
46
- }>
47
-
48
- expect<Returns>(query).toMatchInlineSnapshot(`
49
- SELECT
50
- foo.bar AS "bar",
51
- foo.baz AS "baz"
52
- FROM
53
- foo
54
- `);
55
- })
56
-
57
- it("will output nested object", () => {
58
- const query = Query(where => {
59
- const { bar, baz } = where(Foo);
60
-
61
- return {
62
- bar: { value: bar },
63
- baz: { value: baz }
64
- }
65
- })
66
-
67
- type Returns = Query.Selects<{
68
- bar: { value: string };
69
- baz: { value: string };
70
- }>
71
-
72
- expect<Returns>(query).toMatchInlineSnapshot(`
73
- SELECT
74
- foo.bar AS "bar.value",
75
- foo.baz AS "baz.value"
76
- FROM
77
- foo
78
- `);
79
- })
80
-
81
- it("will select a field directly", () => {
82
- const query = Query(where => {
83
- return where(Foo).bar;
84
- })
85
-
86
- type Returns = Query.Selects<string>;
87
-
88
- expect<Returns>(query).toMatchInlineSnapshot(`
89
- SELECT
90
- foo.bar
91
- FROM
92
- foo
93
- `);
94
- })
95
-
96
- it("will select a entire entity", () => {
97
- const query = Query(where => {
98
- return where(Foo);
99
- })
100
-
101
- type Returns = Query.Selects<{
102
- id: number;
103
- bar: string;
104
- baz: string;
105
- }>
106
-
107
- expect<Returns>(query).toMatchInlineSnapshot(`
108
- SELECT
109
- foo.id AS "id",
110
- foo.bar AS "bar",
111
- foo.baz AS "baz"
112
- FROM
113
- foo
114
- `);
115
- })
@@ -1,16 +0,0 @@
1
- import { Query, Type, Time } from '..';
2
-
3
- it("will preprocess params", async () => {
4
- class Thing extends Type {
5
- created = Time();
6
- }
7
-
8
- const template = Query(where => (created: Date) => {
9
- const thing = where(Thing);
10
- where(thing.created).is(created);
11
- });
12
-
13
- const query = template(new Date("2020-01-01"));
14
-
15
- expect(query.params).toEqual(['2020-01-01 00:00:00']);
16
- });
@@ -1,85 +0,0 @@
1
- import { Query, Str, Type } from '..';
2
-
3
- class Foo extends Type {
4
- value = Str();
5
- color = Str();
6
- }
7
-
8
- class Bar extends Type {
9
- value = Str();
10
- color = Str();
11
- }
12
-
13
- it("will generate query", () => {
14
- const query = Query(where => {
15
- const foo = where(Foo);
16
-
17
- where(foo.color).is("red");
18
- where(foo).update({
19
- value: "new!",
20
- color: "blue"
21
- })
22
- });
23
-
24
- expect(query).toMatchInlineSnapshot(`
25
- UPDATE
26
- foo
27
- SET
28
- value = 'new!',
29
- color = 'blue'
30
- WHERE
31
- foo.color = 'red'
32
- `);
33
- })
34
-
35
- it("will update with joins", () => {
36
- const query = Query(where => {
37
- const foo = where(Foo);
38
- const bar = where(Bar);
39
-
40
- where(bar.color).is(foo.color);
41
- where(foo.color).is("red");
42
- where(foo).update({ value: bar.value });
43
- });
44
-
45
- expect(query).toMatchInlineSnapshot(`
46
- UPDATE
47
- foo
48
- INNER JOIN bar ON bar.color = foo.color
49
- SET
50
- value = bar.value
51
- WHERE
52
- foo.color = 'red'
53
- `);
54
- })
55
-
56
- it("will complain about nullable mismatch", () => {
57
- class Baz extends Type {
58
- nullable = Str({ nullable: true });
59
- nonNullable = Str();
60
- }
61
-
62
- const query = Query(where => {
63
- const baz = where(Baz);
64
- where(baz).update({ nullable: null });
65
- });
66
-
67
- expect(query).toMatchInlineSnapshot(`
68
- UPDATE
69
- baz
70
- SET
71
- nullable = NULL
72
- `);
73
-
74
- const badQuery = () => Query(where => {
75
- const baz = where(Baz);
76
- // @ts-expect-error
77
- where(baz).update({ nonNullable: null });
78
- })
79
-
80
- expect(badQuery).toThrowErrorMatchingInlineSnapshot(
81
- `Column baz.non_nullable is not nullable.`
82
- );
83
- })
84
-
85
- it.todo('will update multiple tables')
@@ -1,347 +0,0 @@
1
- import { Type, Num, Query, Str, Bool } from '..';
2
-
3
- class Item extends Type {
4
- number = Num();
5
- }
6
-
7
- it("will limit results", async () => {
8
- const results = Query(where => {
9
- const item = where(Item);
10
-
11
- where(item.number).over(3);
12
- where(3);
13
-
14
- return item.number;
15
- });
16
-
17
- expect(results).toMatchInlineSnapshot(`
18
- SELECT
19
- item.number
20
- FROM
21
- item
22
- WHERE
23
- item.number > 3
24
- LIMIT
25
- 3
26
- `);
27
- });
28
-
29
- it("will add operator clauses", () => {
30
- const results = Query(where => {
31
- const item = where(Item);
32
-
33
- where(item.number).is(5);
34
- where(item.number).not(0);
35
- where(item.number).over(4);
36
- where(item.number).over(5, true);
37
- where(item.number).under(6);
38
- where(item.number).under(5, true);
39
- });
40
-
41
- expect(results).toMatchInlineSnapshot(`
42
- SELECT
43
- COUNT(*)
44
- FROM
45
- item
46
- WHERE
47
- item.number = 5
48
- AND item.number <> 0
49
- AND item.number > 4
50
- AND item.number >= 5
51
- AND item.number < 6
52
- AND item.number <= 5
53
- `);
54
- });
55
-
56
- it("will add IN clause", () => {
57
- const results = Query(where => {
58
- const item = where(Item);
59
-
60
- where(item.number).in([1, 2, 3]);
61
- });
62
-
63
- expect(results).toMatchInlineSnapshot(`
64
- SELECT
65
- COUNT(*)
66
- FROM
67
- item
68
- WHERE
69
- item.number IN (1, 2, 3)
70
- `);
71
- });
72
-
73
- describe("grouping", () => {
74
- it("will chain clauses", () => {
75
- class Test extends Type {
76
- a = Num();
77
- b = Num();
78
- c = Num();
79
- d = Num();
80
- }
81
-
82
- const query = Query(where => {
83
- const test = where(Test);
84
-
85
- where(test.a).is(1);
86
- where(test.b).not(2);
87
- where(test.c).over(3);
88
- where(test.d).under(4);
89
- });
90
-
91
- expect(query).toMatchInlineSnapshot(`
92
- SELECT
93
- COUNT(*)
94
- FROM
95
- test
96
- WHERE
97
- test.a = 1
98
- AND test.b <> 2
99
- AND test.c > 3
100
- AND test.d < 4
101
- `);
102
- })
103
-
104
- it("will group clauses", () => {
105
- class Foo extends Type {
106
- name = Str();
107
- color = Str();
108
- }
109
-
110
- const query = Query(where => {
111
- const foo = where(Foo);
112
-
113
- where(
114
- where(foo.name).is("Gabe"),
115
- where(foo.color).is("purple"),
116
- where(
117
- where(foo.color).not("orange"),
118
- where(foo.color).not("yellow"),
119
- )
120
- )
121
- });
122
-
123
- expect(query).toMatchInlineSnapshot(`
124
- SELECT
125
- COUNT(*)
126
- FROM
127
- foo
128
- WHERE
129
- foo.name = 'Gabe'
130
- OR foo.color = 'purple'
131
- OR (
132
- foo.color <> 'orange'
133
- AND foo.color <> 'yellow'
134
- )
135
- `);
136
- })
137
-
138
- it("will group recursively", () => {
139
- class Foo extends Type {
140
- name = Str();
141
- color = Str();
142
- }
143
-
144
- const query = Query(where => {
145
- const foo = where(Foo);
146
-
147
- where(foo.id).over(1);
148
-
149
- where(
150
- where(
151
- where(foo.name).is("Gabe"),
152
- where(foo.color).is("red"),
153
- ),
154
- where(
155
- where(foo.name).is("Bob"),
156
- where(
157
- where(foo.color).is("blue"),
158
- where(foo.color).is("green")
159
- )
160
- )
161
- )
162
- });
163
-
164
- expect(query).toMatchInlineSnapshot(`
165
- SELECT
166
- COUNT(*)
167
- FROM
168
- foo
169
- WHERE
170
- foo.id > 1
171
- AND (
172
- foo.name = 'Gabe'
173
- AND foo.color = 'red'
174
- OR (
175
- foo.name = 'Bob'
176
- AND (
177
- foo.color = 'blue'
178
- OR foo.color = 'green'
179
- )
180
- )
181
- )
182
- `);
183
- })
184
- });
185
-
186
- describe("complex grouping", () => {
187
- class Item extends Type {
188
- name = Str();
189
- price = Num();
190
- color = Str();
191
- size = Str();
192
- inStock = Bool();
193
- }
194
-
195
- it("will alternate between OR and AND groups", () => {
196
- const query = Query(where => {
197
- const item = where(Item);
198
-
199
- where(
200
- where(
201
- where(item.color).is("red"),
202
- where(
203
- where(item.size).is("small"),
204
- where(item.size).is("medium")
205
- )
206
- ),
207
- where(
208
- where(item.color).is("blue"),
209
- where(item.size).is("large")
210
- )
211
- );
212
- });
213
-
214
- expect(query).toMatchInlineSnapshot(`
215
- SELECT
216
- COUNT(*)
217
- FROM
218
- item
219
- WHERE
220
- item.color = 'red'
221
- AND (
222
- item.size = 'small'
223
- OR item.size = 'medium'
224
- )
225
- OR (
226
- item.color = 'blue'
227
- AND item.size = 'large'
228
- )
229
- `);
230
- });
231
-
232
- it("will combine standalone and grouped conditions", () => {
233
- const query = Query(where => {
234
- const item = where(Item);
235
-
236
- // Standalone condition at query level = AND
237
- where(item.id).over(0);
238
-
239
- // Group of conditions = OR between siblings
240
- where(
241
- where(item.price).over(50),
242
- where(
243
- where(item.color).is("red"),
244
- where(item.size).is("large")
245
- ),
246
- where(item.inStock).is(true)
247
- );
248
- });
249
-
250
- expect(query).toMatchInlineSnapshot(`
251
- SELECT
252
- COUNT(*)
253
- FROM
254
- item
255
- WHERE
256
- item.id > 0
257
- AND (
258
- item.price > 50
259
- OR (
260
- item.color = 'red'
261
- AND item.size = 'large'
262
- )
263
- OR item.in_stock = 1
264
- )
265
- `);
266
- });
267
-
268
- it("will collapse unnecessary nesting", () => {
269
- const query = Query(where => {
270
- const item = where(Item);
271
-
272
- where(
273
- where(
274
- where(item.price).over(100)
275
- ),
276
- where(item.color).is("red"),
277
- where(
278
- where(item.size).is("large"),
279
- where(item.inStock).is(true)
280
- )
281
- );
282
- });
283
-
284
- expect(query).toMatchInlineSnapshot(`
285
- SELECT
286
- COUNT(*)
287
- FROM
288
- item
289
- WHERE
290
- item.price > 100
291
- OR item.color = 'red'
292
- OR (
293
- item.size = 'large'
294
- AND item.in_stock = 1
295
- )
296
- `);
297
- });
298
- });
299
-
300
- describe("sort", () => {
301
- class Test extends Type {
302
- name = Str();
303
- rating = Num();
304
- }
305
-
306
- it("will add order clause", async () => {
307
- const query = Query(where => {
308
- const test = where(Test);
309
-
310
- where(test.id).desc()
311
-
312
- return test.name;
313
- });
314
-
315
- expect(query).toMatchInlineSnapshot(`
316
- SELECT
317
- test.name
318
- FROM
319
- test
320
- ORDER BY
321
- test.id desc
322
- `);
323
- })
324
-
325
- it("will sort multiple columns", async () => {
326
- const query = Query(where => {
327
- const { name, rating } = where(Test);
328
-
329
- where(rating).asc()
330
- where(name).desc()
331
-
332
- return name;
333
- });
334
-
335
- expect(query).toMatchInlineSnapshot(`
336
- SELECT
337
- test.name
338
- FROM
339
- test
340
- ORDER BY
341
- test.rating asc,
342
- test.name desc
343
- `);
344
-
345
- // SQLite may not support multiple column sorting
346
- })
347
- })