@teamkeel/functions-runtime 0.411.0 → 0.412.0-next.1

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 (49) hide show
  1. package/dist/index.d.mts +340 -0
  2. package/dist/index.d.ts +340 -0
  3. package/dist/index.js +3093 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +3097 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +23 -5
  8. package/.env.test +0 -2
  9. package/compose.yaml +0 -10
  10. package/src/Duration.js +0 -40
  11. package/src/Duration.test.js +0 -34
  12. package/src/File.js +0 -295
  13. package/src/ModelAPI.js +0 -377
  14. package/src/ModelAPI.test.js +0 -1428
  15. package/src/QueryBuilder.js +0 -184
  16. package/src/QueryContext.js +0 -90
  17. package/src/RequestHeaders.js +0 -21
  18. package/src/TimePeriod.js +0 -89
  19. package/src/TimePeriod.test.js +0 -148
  20. package/src/applyAdditionalQueryConstraints.js +0 -22
  21. package/src/applyJoins.js +0 -67
  22. package/src/applyWhereConditions.js +0 -124
  23. package/src/auditing.js +0 -110
  24. package/src/auditing.test.js +0 -330
  25. package/src/camelCasePlugin.js +0 -52
  26. package/src/casing.js +0 -54
  27. package/src/casing.test.js +0 -56
  28. package/src/consts.js +0 -14
  29. package/src/database.js +0 -244
  30. package/src/errors.js +0 -160
  31. package/src/handleJob.js +0 -110
  32. package/src/handleJob.test.js +0 -270
  33. package/src/handleRequest.js +0 -153
  34. package/src/handleRequest.test.js +0 -463
  35. package/src/handleRoute.js +0 -112
  36. package/src/handleSubscriber.js +0 -105
  37. package/src/index.d.ts +0 -317
  38. package/src/index.js +0 -38
  39. package/src/parsing.js +0 -113
  40. package/src/parsing.test.js +0 -140
  41. package/src/permissions.js +0 -77
  42. package/src/permissions.test.js +0 -118
  43. package/src/tracing.js +0 -184
  44. package/src/tracing.test.js +0 -147
  45. package/src/tryExecuteFunction.js +0 -91
  46. package/src/tryExecuteJob.js +0 -29
  47. package/src/tryExecuteSubscriber.js +0 -17
  48. package/src/type-utils.js +0 -18
  49. package/vite.config.js +0 -7
@@ -1,1428 +0,0 @@
1
- import { test, expect, beforeEach, describe } from "vitest";
2
- const { InlineFile, File } = require("./File");
3
- const { ModelAPI } = require("./ModelAPI");
4
- const { sql } = require("kysely");
5
- const { useDatabase } = require("./database");
6
- const KSUID = require("ksuid");
7
-
8
- let personAPI;
9
- let postAPI;
10
- let authorAPI;
11
-
12
- const imgDataURL = `data:image/png;name=my-avatar.png;base64,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`;
13
-
14
- beforeEach(async () => {
15
- const db = useDatabase();
16
-
17
- await sql`
18
- DROP TABLE IF EXISTS post;
19
- DROP TABLE IF EXISTS person;
20
- DROP TABLE IF EXISTS author;
21
-
22
- CREATE TABLE person(
23
- id text PRIMARY KEY,
24
- name text UNIQUE,
25
- married boolean,
26
- favourite_number integer,
27
- avatar jsonb,
28
- date timestamp
29
- );
30
- CREATE TABLE post(
31
- id text PRIMARY KEY,
32
- title text,
33
- tags text[],
34
- rating numeric,
35
- author_id text references person(id)
36
- );
37
- CREATE TABLE author(
38
- id text PRIMARY KEY,
39
- name text NOT NULL
40
- );
41
- CREATE TABLE IF NOT EXISTS keel_storage(
42
- id text NOT NULL,
43
- filename text NOT NULL,
44
- content_type text NOT NULL,
45
- data bytea NOT NULL,
46
- created_at timestamptz NOT NULL DEFAULT now(),
47
- PRIMARY KEY (id)
48
- );`.execute(db);
49
-
50
- const tableConfigMap = {
51
- person: {
52
- posts: {
53
- relationshipType: "hasMany",
54
- foreignKey: "author_id",
55
- referencesTable: "post",
56
- },
57
- },
58
- post: {
59
- author: {
60
- relationshipType: "belongsTo",
61
- foreignKey: "author_id",
62
- referencesTable: "person",
63
- },
64
- },
65
- };
66
-
67
- personAPI = new ModelAPI("person", undefined, tableConfigMap);
68
-
69
- postAPI = new ModelAPI("post", undefined, tableConfigMap);
70
-
71
- authorAPI = new ModelAPI("author", undefined, tableConfigMap);
72
- });
73
-
74
- test("ModelAPI.create", async () => {
75
- var file = InlineFile.fromDataURL(imgDataURL);
76
- const row = await personAPI.create({
77
- id: KSUID.randomSync().string,
78
- name: "Jim",
79
- married: false,
80
- favouriteNumber: 10,
81
- avatar: file,
82
- });
83
- expect(row.name).toEqual("Jim");
84
- expect(row.married).toEqual(false);
85
- expect(row.favouriteNumber).toEqual(10);
86
- expect(row.avatar.filename).toEqual("my-avatar.png");
87
- expect(row.avatar.size).toEqual(2024);
88
- expect(KSUID.parse(row.id).string).toEqual(row.id);
89
- });
90
-
91
- test("ModelAPI.create - non-ksuid id", async () => {
92
- const row = await personAPI.create({
93
- id: "not-a-ksuid",
94
- name: "Jim",
95
- married: false,
96
- favouriteNumber: 10,
97
- });
98
- expect(row.name).toEqual("Jim");
99
- expect(row.married).toEqual(false);
100
- expect(row.favouriteNumber).toEqual(10);
101
- expect(row.id).toEqual(row.id);
102
- });
103
-
104
- test("ModelAPI.create - throws if not not null constraint violation", async () => {
105
- await expect(
106
- authorAPI.create({
107
- id: KSUID.randomSync().string,
108
- name: null,
109
- })
110
- ).rejects.toThrow(
111
- 'null value in column "name" of relation "author" violates not-null constraint'
112
- );
113
- });
114
-
115
- test("ModelAPI.create - throws if database constraint fails", async () => {
116
- const row = await personAPI.create({
117
- id: KSUID.randomSync().string,
118
- name: "Jim",
119
- married: false,
120
- favouriteNumber: 10,
121
- });
122
- const promise = personAPI.create({
123
- id: row.id,
124
- name: "Jim",
125
- married: false,
126
- favouriteNumber: 10,
127
- });
128
- await expect(promise).rejects.toThrow(
129
- `duplicate key value violates unique constraint "person_pkey"`
130
- );
131
- });
132
-
133
- test("ModelAPI.create - arrays", async () => {
134
- const person = await personAPI.create({
135
- id: KSUID.randomSync().string,
136
- name: "Jim",
137
- married: false,
138
- favouriteNumber: 10,
139
- });
140
-
141
- const row = await postAPI.create({
142
- id: "id",
143
- title: "My Post",
144
- tags: ["tag 1", "tag 2"],
145
- rating: 1.23,
146
- authorId: person.id,
147
- });
148
- expect(row.tags).toEqual(["tag 1", "tag 2"]);
149
- expect(row.rating).toEqual(1.23);
150
- });
151
-
152
- test("ModelAPI.findOne", async () => {
153
- const created = await personAPI.create({
154
- id: KSUID.randomSync().string,
155
- name: "Jim",
156
- married: false,
157
- favouriteNumber: 10,
158
- });
159
- const row = await personAPI.findOne({
160
- id: created.id,
161
- });
162
- expect(row).toEqual(created);
163
- });
164
-
165
- test("ModelAPI.findOne - relationships - one to many", async () => {
166
- const person = await personAPI.create({
167
- id: KSUID.randomSync().string,
168
- name: "Jim",
169
- married: false,
170
- favouriteNumber: 10,
171
- });
172
- const post = await postAPI.create({
173
- id: KSUID.randomSync().string,
174
- title: "My Post",
175
- authorId: person.id,
176
- });
177
- const row = await personAPI.findOne({
178
- posts: {
179
- id: post.id,
180
- },
181
- });
182
- expect(row.name).toEqual("Jim");
183
- expect(row.id).toEqual(person.id);
184
- });
185
-
186
- test("ModelAPI.findOne - return null if not found", async () => {
187
- const row = await personAPI.findOne({
188
- id: "doesntexist",
189
- });
190
- expect(row).toEqual(null);
191
- });
192
-
193
- test("ModelAPI.findMany", async () => {
194
- await personAPI.create({
195
- id: KSUID.randomSync().string,
196
- name: "Jim",
197
- married: false,
198
- favouriteNumber: 10,
199
- });
200
- const bob = await personAPI.create({
201
- id: KSUID.randomSync().string,
202
- name: "Bob",
203
- married: true,
204
- favouriteNumber: 11,
205
- });
206
- const sally = await personAPI.create({
207
- id: KSUID.randomSync().string,
208
- name: "Sally",
209
- married: true,
210
- favouriteNumber: 12,
211
- });
212
- const rows = await personAPI.findMany({
213
- where: {
214
- married: true,
215
- },
216
- });
217
- expect(rows.length).toEqual(2);
218
- expect(rows.map((x) => x.id).sort()).toEqual([bob.id, sally.id].sort());
219
- });
220
-
221
- test("ModelAPI.findMany - no where conditions", async () => {
222
- await personAPI.create({
223
- id: KSUID.randomSync().string,
224
- name: "Jim",
225
- });
226
- await personAPI.create({
227
- id: KSUID.randomSync().string,
228
- name: "Bob",
229
- });
230
-
231
- const rows = await personAPI.findMany({});
232
-
233
- expect(rows.length).toEqual(2);
234
- });
235
-
236
- test("ModelAPI.findMany - startsWith", async () => {
237
- const jim = await personAPI.create({
238
- id: KSUID.randomSync().string,
239
- name: "Jim",
240
- });
241
- await personAPI.create({
242
- id: KSUID.randomSync().string,
243
- name: "Bob",
244
- });
245
- const rows = await personAPI.findMany({
246
- where: {
247
- name: {
248
- startsWith: "Ji",
249
- },
250
- },
251
- });
252
- expect(rows.length).toEqual(1);
253
- expect(rows[0].id).toEqual(jim.id);
254
- });
255
-
256
- test("ModelAPI.findMany - endsWith", async () => {
257
- const jim = await personAPI.create({
258
- id: KSUID.randomSync().string,
259
- name: "Jim",
260
- });
261
- await personAPI.create({
262
- id: KSUID.randomSync().string,
263
- name: "Bob",
264
- });
265
- const rows = await personAPI.findMany({
266
- where: {
267
- name: {
268
- endsWith: "im",
269
- },
270
- },
271
- });
272
- expect(rows.length).toEqual(1);
273
- expect(rows[0].id).toEqual(jim.id);
274
- });
275
-
276
- test("ModelAPI.findMany - contains", async () => {
277
- const billy = await personAPI.create({
278
- id: KSUID.randomSync().string,
279
- name: "Billy",
280
- });
281
- const sally = await personAPI.create({
282
- id: KSUID.randomSync().string,
283
- name: "Sally",
284
- });
285
- await personAPI.create({
286
- id: KSUID.randomSync().string,
287
- name: "Jim",
288
- });
289
- const rows = await personAPI.findMany({
290
- where: {
291
- name: {
292
- contains: "ll",
293
- },
294
- },
295
- });
296
- expect(rows.length).toEqual(2);
297
- expect(rows.map((x) => x.id).sort()).toEqual([billy.id, sally.id].sort());
298
- });
299
-
300
- test("ModelAPI.findMany - oneOf", async () => {
301
- const billy = await personAPI.create({
302
- id: KSUID.randomSync().string,
303
- name: "Billy",
304
- });
305
- const sally = await personAPI.create({
306
- id: KSUID.randomSync().string,
307
- name: "Sally",
308
- });
309
- await personAPI.create({
310
- id: KSUID.randomSync().string,
311
- name: "Jim",
312
- });
313
- const rows = await personAPI.findMany({
314
- where: {
315
- name: {
316
- oneOf: ["Billy", "Sally"],
317
- },
318
- },
319
- });
320
- expect(rows.length).toEqual(2);
321
- expect(rows.map((x) => x.id).sort()).toEqual([billy.id, sally.id].sort());
322
- });
323
-
324
- test("ModelAPI.findMany - notEquals on id", async () => {
325
- const p1 = await personAPI.create({
326
- id: KSUID.randomSync().string,
327
- favouriteNumber: 1,
328
- });
329
- const p2 = await personAPI.create({
330
- id: KSUID.randomSync().string,
331
- favouriteNumber: 2,
332
- });
333
- const rows = await personAPI.findMany({
334
- where: {
335
- id: {
336
- notEquals: p1.id,
337
- },
338
- },
339
- });
340
- expect(rows.length).toEqual(1);
341
- expect(rows[0].id).toEqual(p2.id);
342
- });
343
-
344
- test("ModelAPI.findMany - greaterThan", async () => {
345
- await personAPI.create({
346
- id: KSUID.randomSync().string,
347
- favouriteNumber: 1,
348
- });
349
- const p = await personAPI.create({
350
- id: KSUID.randomSync().string,
351
- favouriteNumber: 2,
352
- });
353
- const rows = await personAPI.findMany({
354
- where: {
355
- favouriteNumber: {
356
- greaterThan: 1,
357
- },
358
- },
359
- });
360
- expect(rows.length).toEqual(1);
361
- expect(rows[0].id).toEqual(p.id);
362
- });
363
-
364
- test("ModelAPI.findMany - greaterThanOrEquals", async () => {
365
- await personAPI.create({
366
- id: KSUID.randomSync().string,
367
- favouriteNumber: 1,
368
- });
369
- const p = await personAPI.create({
370
- id: KSUID.randomSync().string,
371
- favouriteNumber: 2,
372
- });
373
- const p2 = await personAPI.create({
374
- id: KSUID.randomSync().string,
375
- favouriteNumber: 3,
376
- });
377
- const rows = await personAPI.findMany({
378
- where: {
379
- favouriteNumber: {
380
- greaterThanOrEquals: 2,
381
- },
382
- },
383
- });
384
- expect(rows.length).toEqual(2);
385
- expect(rows.map((x) => x.id).sort()).toEqual([p.id, p2.id].sort());
386
- });
387
-
388
- test("ModelAPI.findMany - lessThan", async () => {
389
- const p = await personAPI.create({
390
- id: KSUID.randomSync().string,
391
- favouriteNumber: 1,
392
- });
393
- await personAPI.create({
394
- id: KSUID.randomSync().string,
395
- favouriteNumber: 2,
396
- });
397
- const rows = await personAPI.findMany({
398
- where: {
399
- favouriteNumber: {
400
- lessThan: 2,
401
- },
402
- },
403
- });
404
- expect(rows.length).toEqual(1);
405
- expect(rows[0].id).toEqual(p.id);
406
- });
407
-
408
- test("ModelAPI.findMany - lessThanOrEquals", async () => {
409
- const p = await personAPI.create({
410
- id: KSUID.randomSync().string,
411
- favouriteNumber: 1,
412
- });
413
- const p2 = await personAPI.create({
414
- id: KSUID.randomSync().string,
415
- favouriteNumber: 2,
416
- });
417
- await personAPI.create({
418
- id: KSUID.randomSync().string,
419
- favouriteNumber: 3,
420
- });
421
- const rows = await personAPI.findMany({
422
- where: {
423
- favouriteNumber: {
424
- lessThanOrEquals: 2,
425
- },
426
- },
427
- });
428
- expect(rows.length).toEqual(2);
429
- expect(rows.map((x) => x.id).sort()).toEqual([p.id, p2.id].sort());
430
- });
431
-
432
- test("ModelAPI.findMany - before", async () => {
433
- const p = await personAPI.create({
434
- id: KSUID.randomSync().string,
435
- date: new Date("2022-01-01"),
436
- });
437
- await personAPI.create({
438
- id: KSUID.randomSync().string,
439
- date: new Date("2022-01-02"),
440
- });
441
- const rows = await personAPI.findMany({
442
- where: {
443
- date: {
444
- before: new Date("2022-01-02"),
445
- },
446
- },
447
- });
448
- expect(rows.length).toEqual(1);
449
- expect(rows[0].id).toEqual(p.id);
450
- });
451
-
452
- test("ModelAPI.findMany - empty where", async () => {
453
- const p = await personAPI.create({
454
- id: KSUID.randomSync().string,
455
- date: new Date("2022-01-01"),
456
- });
457
-
458
- const p2 = await personAPI.create({
459
- id: KSUID.randomSync().string,
460
- date: new Date("2022-01-02"),
461
- });
462
-
463
- // with no param specified at all
464
- const rows = await personAPI.findMany();
465
-
466
- expect(rows.map((r) => r.id).sort()).toEqual([p, p2].map((r) => r.id).sort());
467
-
468
- // with empty object
469
- const rows2 = await personAPI.findMany({});
470
-
471
- expect(rows2.map((r) => r.id).sort()).toEqual(
472
- [p, p2].map((r) => r.id).sort()
473
- );
474
- });
475
-
476
- test("ModelAPI.findMany - onOrBefore", async () => {
477
- const p = await personAPI.create({
478
- id: KSUID.randomSync().string,
479
- date: new Date("2022-01-01"),
480
- });
481
- const p2 = await personAPI.create({
482
- id: KSUID.randomSync().string,
483
- date: new Date("2022-01-02"),
484
- });
485
- await personAPI.create({
486
- id: KSUID.randomSync().string,
487
- date: new Date("2022-01-03"),
488
- });
489
- const rows = await personAPI.findMany({
490
- where: {
491
- date: {
492
- onOrBefore: new Date("2022-01-02"),
493
- },
494
- },
495
- });
496
- expect(rows.length).toEqual(2);
497
- expect(rows.map((x) => x.id).sort()).toEqual([p.id, p2.id].sort());
498
- });
499
-
500
- test("ModelAPI.findMany - limit", async () => {
501
- await personAPI.create({
502
- id: KSUID.randomSync().string,
503
- name: "Jim",
504
- married: false,
505
- favouriteNumber: 10,
506
- });
507
- await personAPI.create({
508
- id: KSUID.randomSync().string,
509
- name: "Bob",
510
- married: true,
511
- favouriteNumber: 11,
512
- });
513
- await personAPI.create({
514
- id: KSUID.randomSync().string,
515
- name: "Sally",
516
- married: true,
517
- favouriteNumber: 12,
518
- });
519
-
520
- const rows = await personAPI.findMany({
521
- limit: 2,
522
- orderBy: {
523
- favouriteNumber: "asc",
524
- },
525
- });
526
-
527
- expect(rows.map((r) => r.name)).toEqual(["Jim", "Bob"]);
528
- });
529
-
530
- test("ModelAPI.findMany - orderBy", async () => {
531
- await personAPI.create({
532
- id: KSUID.randomSync().string,
533
- name: "Jim",
534
- married: false,
535
- favouriteNumber: 10,
536
- date: new Date(2023, 12, 29),
537
- });
538
- await personAPI.create({
539
- id: KSUID.randomSync().string,
540
- name: "Bob",
541
- married: true,
542
- favouriteNumber: 11,
543
- date: new Date(2023, 12, 30),
544
- });
545
- await personAPI.create({
546
- id: KSUID.randomSync().string,
547
- name: "Sally",
548
- married: true,
549
- favouriteNumber: 12,
550
- date: new Date(2023, 12, 31),
551
- });
552
-
553
- const ascendingNames = await personAPI.findMany({
554
- orderBy: {
555
- name: "asc",
556
- },
557
- });
558
-
559
- expect(ascendingNames.map((r) => r.name)).toEqual(["Bob", "Jim", "Sally"]);
560
-
561
- const descendingNames = await personAPI.findMany({
562
- orderBy: {
563
- name: "desc",
564
- },
565
- });
566
-
567
- expect(descendingNames.map((r) => r.name)).toEqual(["Sally", "Jim", "Bob"]);
568
-
569
- const ascendingFavouriteNumbers = await personAPI.findMany({
570
- orderBy: {
571
- favouriteNumber: "asc",
572
- },
573
- });
574
-
575
- expect(ascendingFavouriteNumbers.map((r) => r.name)).toEqual([
576
- "Jim",
577
- "Bob",
578
- "Sally",
579
- ]);
580
-
581
- const descendingDates = await personAPI.findMany({
582
- orderBy: {
583
- date: "desc",
584
- },
585
- });
586
-
587
- expect(descendingDates.map((r) => r.name)).toEqual(["Sally", "Bob", "Jim"]);
588
- });
589
-
590
- test("ModelAPI.findMany - orderBy ASC and DESC capitalised", async () => {
591
- await personAPI.create({
592
- id: KSUID.randomSync().string,
593
- name: "Jim",
594
- married: false,
595
- favouriteNumber: 10,
596
- date: new Date(2023, 12, 29),
597
- });
598
- await personAPI.create({
599
- id: KSUID.randomSync().string,
600
- name: "Bob",
601
- married: true,
602
- favouriteNumber: 11,
603
- date: new Date(2023, 12, 30),
604
- });
605
- await personAPI.create({
606
- id: KSUID.randomSync().string,
607
- name: "Sally",
608
- married: true,
609
- favouriteNumber: 12,
610
- date: new Date(2023, 12, 31),
611
- });
612
-
613
- const ascendingNames = await personAPI.findMany({
614
- orderBy: {
615
- name: "ASC",
616
- },
617
- });
618
-
619
- expect(ascendingNames.map((r) => r.name)).toEqual(["Bob", "Jim", "Sally"]);
620
-
621
- const descendingNames = await personAPI.findMany({
622
- orderBy: {
623
- name: "DESC",
624
- },
625
- });
626
-
627
- expect(descendingNames.map((r) => r.name)).toEqual(["Sally", "Jim", "Bob"]);
628
- });
629
-
630
- test("ModelAPI.findMany - offset", async () => {
631
- await personAPI.create({
632
- id: KSUID.randomSync().string,
633
- name: "Jim",
634
- married: false,
635
- favouriteNumber: 10,
636
- date: new Date(2023, 12, 29),
637
- });
638
- await personAPI.create({
639
- id: KSUID.randomSync().string,
640
- name: "Bob",
641
- married: true,
642
- favouriteNumber: 11,
643
- date: new Date(2023, 12, 30),
644
- });
645
- await personAPI.create({
646
- id: KSUID.randomSync().string,
647
- name: "Sally",
648
- married: true,
649
- favouriteNumber: 12,
650
- date: new Date(2023, 12, 31),
651
- });
652
-
653
- const rows = await personAPI.findMany({
654
- offset: 1,
655
- limit: 2,
656
- orderBy: {
657
- name: "asc",
658
- },
659
- });
660
-
661
- expect(rows.map((r) => r.name)).toEqual(["Jim", "Sally"]);
662
-
663
- const rows2 = await personAPI.findMany({
664
- offset: 2,
665
- orderBy: {
666
- name: "asc",
667
- },
668
- });
669
-
670
- expect(rows2.map((r) => r.name)).toEqual(["Sally"]);
671
-
672
- const rows3 = await personAPI.findMany({
673
- offset: 1,
674
- orderBy: {
675
- name: "asc",
676
- },
677
- limit: 1,
678
- });
679
-
680
- expect(rows3.map((r) => r.name)).toEqual(["Jim"]);
681
- });
682
-
683
- test("ModelAPI.findMany - after", async () => {
684
- await personAPI.create({
685
- id: KSUID.randomSync().string,
686
- date: new Date("2022-01-01"),
687
- });
688
- const p = await personAPI.create({
689
- id: KSUID.randomSync().string,
690
- date: new Date("2022-01-02"),
691
- });
692
- const rows = await personAPI.findMany({
693
- where: {
694
- date: {
695
- after: new Date("2022-01-01"),
696
- },
697
- },
698
- });
699
- expect(rows.length).toEqual(1);
700
- expect(rows[0].id).toEqual(p.id);
701
- });
702
-
703
- test("ModelAPI.findMany - onOrAfter", async () => {
704
- await personAPI.create({
705
- id: KSUID.randomSync().string,
706
- date: new Date("2022-01-01"),
707
- });
708
- const p = await personAPI.create({
709
- id: KSUID.randomSync().string,
710
- date: new Date("2022-01-02"),
711
- });
712
- const p2 = await personAPI.create({
713
- id: KSUID.randomSync().string,
714
- date: new Date("2022-01-03"),
715
- });
716
- const rows = await personAPI.findMany({
717
- where: {
718
- date: {
719
- onOrAfter: new Date("2022-01-02"),
720
- },
721
- },
722
- });
723
- expect(rows.length).toEqual(2);
724
- expect(rows.map((x) => x.id).sort()).toEqual([p.id, p2.id].sort());
725
- });
726
-
727
- test("ModelAPI.findMany - equals", async () => {
728
- const p = await personAPI.create({
729
- id: KSUID.randomSync().string,
730
- name: "Jim",
731
- });
732
- await personAPI.create({
733
- id: KSUID.randomSync().string,
734
- name: "Sally",
735
- });
736
- const rows = await personAPI.findMany({
737
- where: {
738
- name: {
739
- equals: "Jim",
740
- },
741
- },
742
- });
743
- expect(rows.length).toEqual(1);
744
- expect(rows[0].id).toEqual(p.id);
745
- });
746
-
747
- test("ModelAPI.findMany - notEquals", async () => {
748
- const p = await personAPI.create({
749
- id: KSUID.randomSync().string,
750
- name: "Jim",
751
- });
752
- await personAPI.create({
753
- id: KSUID.randomSync().string,
754
- name: "Sally",
755
- });
756
- const rows = await personAPI.findMany({
757
- where: {
758
- name: {
759
- notEquals: "Sally",
760
- },
761
- },
762
- });
763
- expect(rows.length).toEqual(1);
764
- expect(rows[0].id).toEqual(p.id);
765
- });
766
-
767
- test("ModelAPI.findMany - relationships - one to many", async () => {
768
- const person = await personAPI.create({
769
- id: KSUID.randomSync().string,
770
- name: "Jim",
771
- });
772
- const person2 = await personAPI.create({
773
- id: KSUID.randomSync().string,
774
- name: "Bob",
775
- });
776
- const post1 = await postAPI.create({
777
- id: KSUID.randomSync().string,
778
- title: "My First Post",
779
- authorId: person.id,
780
- });
781
- const post2 = await postAPI.create({
782
- id: KSUID.randomSync().string,
783
- title: "My Second Post",
784
- authorId: person.id,
785
- });
786
- await postAPI.create({
787
- id: KSUID.randomSync().string,
788
- title: "My Third Post",
789
- authorId: person2.id,
790
- });
791
-
792
- const posts = await postAPI.findMany({
793
- where: {
794
- author: {
795
- name: "Jim",
796
- },
797
- },
798
- });
799
- expect(posts.length).toEqual(2);
800
- expect(posts.map((x) => x.id).sort()).toEqual([post1.id, post2.id].sort());
801
- });
802
-
803
- test("ModelAPI.findMany - relationships - many to one", async () => {
804
- const person = await personAPI.create({
805
- id: KSUID.randomSync().string,
806
- name: "Jim",
807
- });
808
- await postAPI.create({
809
- id: KSUID.randomSync().string,
810
- title: "My First Post",
811
- authorId: person.id,
812
- });
813
- await postAPI.create({
814
- id: KSUID.randomSync().string,
815
- title: "My Second Post",
816
- authorId: person.id,
817
- });
818
- await postAPI.create({
819
- id: KSUID.randomSync().string,
820
- title: "My Second Post",
821
- authorId: person.id,
822
- });
823
-
824
- const people = await personAPI.findMany({
825
- where: {
826
- posts: {
827
- title: {
828
- startsWith: "My ",
829
- endsWith: " Post",
830
- },
831
- },
832
- },
833
- });
834
-
835
- // This tests that many to one joins work for findMany() but also
836
- // that the same row is not returned more than once e.g. Jim has
837
- // three posts but should only be returned once
838
- expect(people.length).toEqual(1);
839
- expect(people[0].id).toEqual(person.id);
840
- });
841
-
842
- test("ModelAPI.findMany - relationships - duplicate joins handled", async () => {
843
- const person = await personAPI.create({
844
- id: KSUID.randomSync().string,
845
- name: "Jim",
846
- });
847
- const person2 = await personAPI.create({
848
- id: KSUID.randomSync().string,
849
- name: "Bob",
850
- });
851
- const post1 = await postAPI.create({
852
- id: KSUID.randomSync().string,
853
- title: "My First Post",
854
- authorId: person.id,
855
- });
856
- const post2 = await postAPI.create({
857
- id: KSUID.randomSync().string,
858
- title: "My Second Post",
859
- authorId: person2.id,
860
- });
861
-
862
- const posts = await postAPI
863
- .where({
864
- author: {
865
- name: "Jim",
866
- },
867
- })
868
- .findMany();
869
-
870
- expect(posts.length).toEqual(1);
871
- expect(posts.map((x) => x.id).sort()).toEqual([post1.id].sort());
872
- });
873
-
874
- test("ModelAPI.update", async () => {
875
- let jim = await personAPI.create({
876
- id: KSUID.randomSync().string,
877
- name: "Jim",
878
- married: false,
879
- favouriteNumber: 10,
880
- });
881
- let bob = await personAPI.create({
882
- id: KSUID.randomSync().string,
883
- name: "Bob",
884
- married: false,
885
- favouriteNumber: 11,
886
- });
887
- jim = await personAPI.update(
888
- {
889
- id: jim.id,
890
- },
891
- {
892
- married: true,
893
- }
894
- );
895
- expect(jim.married).toEqual(true);
896
- expect(jim.name).toEqual("Jim");
897
-
898
- bob = await personAPI.findOne({ id: bob.id });
899
- expect(bob.married).toEqual(false);
900
- });
901
-
902
- test("ModelAPI.update - throws if not found", async () => {
903
- const result = personAPI.update(
904
- {
905
- id: "doesntexist",
906
- },
907
- {
908
- married: true,
909
- }
910
- );
911
- await expect(result).rejects.toThrow("no result");
912
- });
913
-
914
- test("ModelAPI.update - throws if not not null constraint violation", async () => {
915
- const jim = await authorAPI.create({
916
- id: KSUID.randomSync().string,
917
- name: "jim",
918
- });
919
-
920
- const result = authorAPI.update(
921
- {
922
- id: jim.id,
923
- },
924
- {
925
- name: null,
926
- }
927
- );
928
-
929
- await expect(result).rejects.toThrow(
930
- 'null value in column "name" of relation "author" violates not-null constraint'
931
- );
932
- });
933
-
934
- test("ModelAPI.delete", async () => {
935
- const jim = await personAPI.create({
936
- id: KSUID.randomSync().string,
937
- name: "Jim",
938
- });
939
- const id = jim.id;
940
- const deletedId = await personAPI.delete({
941
- name: "Jim",
942
- });
943
-
944
- expect(deletedId).toEqual(id);
945
- await expect(personAPI.findOne({ id })).resolves.toEqual(null);
946
- });
947
-
948
- describe("QueryBuilder", () => {
949
- test("ModelAPI chained findMany with offset/limit/order by", async () => {
950
- await postAPI.create({
951
- id: KSUID.randomSync().string,
952
- title: "adam",
953
- });
954
- await postAPI.create({
955
- id: KSUID.randomSync().string,
956
- title: "dave",
957
- });
958
- const three = await postAPI.create({
959
- id: KSUID.randomSync().string,
960
- title: "jon",
961
- });
962
- const four = await postAPI.create({
963
- id: KSUID.randomSync().string,
964
- title: "jon bretman",
965
- });
966
-
967
- const results = await postAPI
968
- .where({ title: { startsWith: "jon" } })
969
- .findMany({
970
- limit: 1,
971
- offset: 1,
972
- orderBy: {
973
- title: "asc",
974
- },
975
- });
976
-
977
- // because we've offset by 1, adam should not appear in the results even though
978
- // the query constraints match adam
979
- expect(results).toEqual([four]);
980
- });
981
-
982
- test("ModelAPI.findMany - complex query", async () => {
983
- const p = await personAPI.create({
984
- id: KSUID.randomSync().string,
985
- name: "Jake",
986
- favouriteNumber: 8,
987
- date: new Date("2021-12-31"),
988
- });
989
- await personAPI.create({
990
- id: KSUID.randomSync().string,
991
- name: "Jane",
992
- favouriteNumber: 12,
993
- date: new Date("2022-01-11"),
994
- });
995
- const p2 = await personAPI.create({
996
- id: KSUID.randomSync().string,
997
- name: "Billy",
998
- favouriteNumber: 16,
999
- date: new Date("2022-01-05"),
1000
- });
1001
-
1002
- const rows = await personAPI
1003
- // Will match Jane
1004
- .where({
1005
- name: {
1006
- startsWith: "J",
1007
- endsWith: "e",
1008
- },
1009
- favouriteNumber: {
1010
- lessThan: 10,
1011
- },
1012
- })
1013
- .findMany();
1014
- expect(rows.length).toEqual(1);
1015
- expect(rows.map((x) => x.id).sort()).toEqual([p.id].sort());
1016
- });
1017
-
1018
- test("ModelAPI chained delete", async () => {
1019
- const p = await personAPI.create({
1020
- id: KSUID.randomSync().string,
1021
- name: "Jake",
1022
- favouriteNumber: 8,
1023
- date: new Date("2021-12-31"),
1024
- });
1025
-
1026
- const deletedId = await personAPI.where({ id: p.id }).delete();
1027
-
1028
- expect(deletedId).toEqual(p.id);
1029
- });
1030
-
1031
- test("ModelAPI chained delete - non existent id", async () => {
1032
- const fakeId = "xxx";
1033
-
1034
- // the error message returned from the runtime will actually be 'record not found'
1035
- // but this is handled at handleRequest level
1036
- // no result is the error msg returned by kysely.
1037
- await expect(personAPI.where({ id: fakeId }).delete()).rejects.toThrow(
1038
- "no result"
1039
- );
1040
- });
1041
-
1042
- test("ModelAPI chained findOne", async () => {
1043
- const p = await personAPI.create({
1044
- id: KSUID.randomSync().string,
1045
- name: "Jake",
1046
- favouriteNumber: 8,
1047
- date: new Date("2021-12-31"),
1048
- });
1049
-
1050
- const jake = await personAPI.where({ id: p.id }).findOne();
1051
-
1052
- expect(jake).toEqual(p);
1053
- });
1054
-
1055
- test("ModelAPI chained update", async () => {
1056
- const p1 = await postAPI.create({
1057
- id: KSUID.randomSync().string,
1058
- title: "adam",
1059
- });
1060
- const p2 = await postAPI.create({
1061
- id: KSUID.randomSync().string,
1062
- title: "adam",
1063
- });
1064
- const p3 = await postAPI.create({
1065
- id: KSUID.randomSync().string,
1066
- title: "adam",
1067
- });
1068
-
1069
- const updatedRow = await postAPI
1070
- .where({ id: p2.id })
1071
- .update({ title: "adam 2" });
1072
-
1073
- expect(updatedRow.title).toEqual("adam 2");
1074
- expect(updatedRow.id).toEqual(p2.id);
1075
-
1076
- // will fail because there is more than 1 row matching the constraints (p1 and p3)
1077
- await expect(
1078
- postAPI
1079
- .where({
1080
- title: "adam",
1081
- })
1082
- .update({ title: "bob" })
1083
- ).rejects.toThrowError(
1084
- "more than one row matched update constraints - only unique fields should be used when updating."
1085
- );
1086
-
1087
- // will fail because there are no rows to update
1088
- await expect(
1089
- postAPI
1090
- .where({
1091
- title: "no match",
1092
- })
1093
- .update({ title: "bob" })
1094
- ).resolves.toEqual(null);
1095
- });
1096
-
1097
- test("ModelAPI.findMany - array equals", async () => {
1098
- const person = await personAPI.create({
1099
- id: KSUID.randomSync().string,
1100
- name: "Jake",
1101
- favouriteNumber: 8,
1102
- date: new Date("2021-12-31"),
1103
- });
1104
- const p1 = await postAPI.create({
1105
- id: KSUID.randomSync().string,
1106
- title: "Post 1",
1107
- tags: ["Tag 1", "Tag 2"],
1108
- authorId: person.id,
1109
- });
1110
- const p2 = await postAPI.create({
1111
- id: KSUID.randomSync().string,
1112
- title: "Post 2",
1113
- tags: ["Tag 2", "Tag 3"],
1114
- authorId: person.id,
1115
- });
1116
- const p3 = await postAPI.create({
1117
- id: KSUID.randomSync().string,
1118
- title: "Post 2",
1119
- tags: [],
1120
- authorId: person.id,
1121
- });
1122
- const p4 = await postAPI.create({
1123
- id: KSUID.randomSync().string,
1124
- title: "Post 2",
1125
- tags: null,
1126
- authorId: person.id,
1127
- });
1128
-
1129
- const rows = await postAPI.findMany({
1130
- where: {
1131
- tags: {
1132
- equals: ["Tag 1", "Tag 2"],
1133
- },
1134
- },
1135
- });
1136
-
1137
- expect(rows.length).toEqual(1);
1138
- expect(rows.map((x) => x.id).sort()).toEqual([p1.id].sort());
1139
- });
1140
-
1141
- test("ModelAPI.findMany - array equals implicit", async () => {
1142
- const person = await personAPI.create({
1143
- id: KSUID.randomSync().string,
1144
- name: "Jake",
1145
- favouriteNumber: 8,
1146
- date: new Date("2021-12-31"),
1147
- });
1148
- const p1 = await postAPI.create({
1149
- id: KSUID.randomSync().string,
1150
- title: "Post 1",
1151
- tags: ["Tag 1", "Tag 2"],
1152
- authorId: person.id,
1153
- });
1154
- const p2 = await postAPI.create({
1155
- id: KSUID.randomSync().string,
1156
- title: "Post 2",
1157
- tags: ["Tag 2", "Tag 3"],
1158
- authorId: person.id,
1159
- });
1160
- const p3 = await postAPI.create({
1161
- id: KSUID.randomSync().string,
1162
- title: "Post 2",
1163
- tags: [],
1164
- authorId: person.id,
1165
- });
1166
-
1167
- const rows = await postAPI.findMany({
1168
- where: {
1169
- tags: ["Tag 1", "Tag 2"],
1170
- },
1171
- });
1172
-
1173
- expect(rows.length).toEqual(1);
1174
- expect(rows.map((x) => x.id).sort()).toEqual([p1.id].sort());
1175
- });
1176
-
1177
- test("ModelAPI.findMany - array not equals", async () => {
1178
- const person = await personAPI.create({
1179
- id: KSUID.randomSync().string,
1180
- name: "Jake",
1181
- favouriteNumber: 8,
1182
- date: new Date("2021-12-31"),
1183
- });
1184
- const p1 = await postAPI.create({
1185
- id: KSUID.randomSync().string,
1186
- title: "Post 1",
1187
- tags: ["Tag 1", "Tag 2"],
1188
- authorId: person.id,
1189
- });
1190
- const p2 = await postAPI.create({
1191
- id: KSUID.randomSync().string,
1192
- title: "Post 2",
1193
- tags: ["Tag 2", "Tag 3"],
1194
- authorId: person.id,
1195
- });
1196
- const p3 = await postAPI.create({
1197
- id: KSUID.randomSync().string,
1198
- title: "Post 2",
1199
- tags: [],
1200
- authorId: person.id,
1201
- });
1202
- const p4 = await postAPI.create({
1203
- id: KSUID.randomSync().string,
1204
- title: "Post 2",
1205
- tags: null,
1206
- authorId: person.id,
1207
- });
1208
-
1209
- const rows = await postAPI.findMany({
1210
- where: {
1211
- tags: {
1212
- notEquals: ["Tag 1", "Tag 2"],
1213
- },
1214
- },
1215
- });
1216
-
1217
- expect(rows.length).toEqual(3);
1218
- expect(rows.map((x) => x.id).sort()).toEqual([p4.id, p3.id, p2.id].sort());
1219
- });
1220
-
1221
- test("ModelAPI.findMany - array any equals", async () => {
1222
- const person = await personAPI.create({
1223
- id: KSUID.randomSync().string,
1224
- name: "Jake",
1225
- favouriteNumber: 8,
1226
- date: new Date("2021-12-31"),
1227
- });
1228
- const p1 = await postAPI.create({
1229
- id: KSUID.randomSync().string,
1230
- title: "Post 1",
1231
- tags: ["Tag 1", "Tag 2"],
1232
- authorId: person.id,
1233
- });
1234
- const p2 = await postAPI.create({
1235
- id: KSUID.randomSync().string,
1236
- title: "Post 2",
1237
- tags: ["Tag 2", "Tag 3"],
1238
- authorId: person.id,
1239
- });
1240
- const p3 = await postAPI.create({
1241
- id: KSUID.randomSync().string,
1242
- title: "Post 2",
1243
- tags: [],
1244
- authorId: person.id,
1245
- });
1246
- const p4 = await postAPI.create({
1247
- id: KSUID.randomSync().string,
1248
- title: "Post 2",
1249
- tags: null,
1250
- authorId: person.id,
1251
- });
1252
- const p5 = await postAPI.create({
1253
- id: KSUID.randomSync().string,
1254
- title: "Post 2",
1255
- tags: ["Tag 3"],
1256
- authorId: person.id,
1257
- });
1258
-
1259
- const rows = await postAPI.findMany({
1260
- where: {
1261
- tags: {
1262
- any: {
1263
- equals: "Tag 2",
1264
- },
1265
- },
1266
- },
1267
- });
1268
-
1269
- expect(rows.length).toEqual(2);
1270
- expect(rows.map((x) => x.id).sort()).toEqual([p1.id, p2.id].sort());
1271
- });
1272
-
1273
- test("ModelAPI.findMany - array any not equals", async () => {
1274
- const person = await personAPI.create({
1275
- id: KSUID.randomSync().string,
1276
- name: "Jake",
1277
- favouriteNumber: 8,
1278
- date: new Date("2021-12-31"),
1279
- });
1280
- const p1 = await postAPI.create({
1281
- id: KSUID.randomSync().string,
1282
- title: "Post 1",
1283
- tags: ["Tag 1", "Tag 2"],
1284
- authorId: person.id,
1285
- });
1286
- const p2 = await postAPI.create({
1287
- id: KSUID.randomSync().string,
1288
- title: "Post 2",
1289
- tags: ["Tag 2", "Tag 3"],
1290
- authorId: person.id,
1291
- });
1292
- const p3 = await postAPI.create({
1293
- id: KSUID.randomSync().string,
1294
- title: "Post 2",
1295
- tags: [],
1296
- authorId: person.id,
1297
- });
1298
- const p4 = await postAPI.create({
1299
- id: KSUID.randomSync().string,
1300
- title: "Post 2",
1301
- tags: null,
1302
- authorId: person.id,
1303
- });
1304
- const p5 = await postAPI.create({
1305
- id: KSUID.randomSync().string,
1306
- title: "Post 2",
1307
- tags: ["Tag 3"],
1308
- authorId: person.id,
1309
- });
1310
-
1311
- const rows = await postAPI.findMany({
1312
- where: {
1313
- tags: {
1314
- any: {
1315
- notEquals: "Tag 3",
1316
- },
1317
- },
1318
- },
1319
- });
1320
-
1321
- expect(rows.length).toEqual(2);
1322
- expect(rows.map((x) => x.id).sort()).toEqual([p1.id, p3.id].sort());
1323
- });
1324
-
1325
- test("ModelAPI.findMany - array all equals", async () => {
1326
- const person = await personAPI.create({
1327
- id: KSUID.randomSync().string,
1328
- name: "Jake",
1329
- favouriteNumber: 8,
1330
- date: new Date("2021-12-31"),
1331
- });
1332
- const p1 = await postAPI.create({
1333
- id: KSUID.randomSync().string,
1334
- title: "Post 1",
1335
- tags: ["Tag 1", "Tag 2"],
1336
- authorId: person.id,
1337
- });
1338
- const p2 = await postAPI.create({
1339
- id: KSUID.randomSync().string,
1340
- title: "Post 2",
1341
- tags: ["Tag 2", "Tag 3"],
1342
- authorId: person.id,
1343
- });
1344
- const p3 = await postAPI.create({
1345
- id: KSUID.randomSync().string,
1346
- title: "Post 2",
1347
- tags: [],
1348
- authorId: person.id,
1349
- });
1350
- const p4 = await postAPI.create({
1351
- id: KSUID.randomSync().string,
1352
- title: "Post 2",
1353
- tags: null,
1354
- authorId: person.id,
1355
- });
1356
- const p5 = await postAPI.create({
1357
- id: KSUID.randomSync().string,
1358
- title: "Post 2",
1359
- tags: ["Tag 2", "Tag 2"],
1360
- authorId: person.id,
1361
- });
1362
-
1363
- const rows = await postAPI.findMany({
1364
- where: {
1365
- tags: {
1366
- all: {
1367
- equals: "Tag 2",
1368
- },
1369
- },
1370
- },
1371
- });
1372
-
1373
- expect(rows.length).toEqual(2);
1374
- expect(rows.map((x) => x.id).sort()).toEqual([p3.id, p5.id].sort());
1375
- });
1376
-
1377
- test("ModelAPI.findMany - array all not equals", async () => {
1378
- const person = await personAPI.create({
1379
- id: KSUID.randomSync().string,
1380
- name: "Jake",
1381
- favouriteNumber: 8,
1382
- date: new Date("2021-12-31"),
1383
- });
1384
- const p1 = await postAPI.create({
1385
- id: KSUID.randomSync().string,
1386
- title: "Post 1",
1387
- tags: ["Tag 1", "Tag 2"],
1388
- authorId: person.id,
1389
- });
1390
- const p2 = await postAPI.create({
1391
- id: KSUID.randomSync().string,
1392
- title: "Post 2",
1393
- tags: ["Tag 2", "Tag 3"],
1394
- authorId: person.id,
1395
- });
1396
- const p3 = await postAPI.create({
1397
- id: KSUID.randomSync().string,
1398
- title: "Post 2",
1399
- tags: [],
1400
- authorId: person.id,
1401
- });
1402
- const p4 = await postAPI.create({
1403
- id: KSUID.randomSync().string,
1404
- title: "Post 2",
1405
- tags: null,
1406
- authorId: person.id,
1407
- });
1408
- const p5 = await postAPI.create({
1409
- id: KSUID.randomSync().string,
1410
- title: "Post 2",
1411
- tags: ["Tag 2", "Tag 2"],
1412
- authorId: person.id,
1413
- });
1414
-
1415
- const rows = await postAPI.findMany({
1416
- where: {
1417
- tags: {
1418
- all: {
1419
- notEquals: "Tag 2",
1420
- },
1421
- },
1422
- },
1423
- });
1424
-
1425
- expect(rows.length).toEqual(2);
1426
- expect(rows.map((x) => x.id).sort()).toEqual([p1.id, p2.id].sort());
1427
- });
1428
- });