@teamkeel/functions-runtime 0.337.0 → 0.338.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamkeel/functions-runtime",
3
- "version": "0.337.0",
3
+ "version": "0.338.0",
4
4
  "description": "Internal package used by @teamkeel/sdk",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/ModelAPI.js CHANGED
@@ -44,11 +44,10 @@ class DatabaseError extends Error {
44
44
  class ModelAPI {
45
45
  /**
46
46
  * @param {string} tableName The name of the table this API is for
47
- * @param {Function} defaultValues A function that returns the default values for a row in this table
47
+ * @param {Function} _ Used to be a function that returns the default values for a row in this table. No longer used.
48
48
  * @param {TableConfigMap} tableConfigMap
49
49
  */
50
- constructor(tableName, defaultValues, tableConfigMap = {}) {
51
- this._defaultValues = defaultValues;
50
+ constructor(tableName, _, tableConfigMap = {}) {
52
51
  this._tableName = tableName;
53
52
  this._tableConfigMap = tableConfigMap;
54
53
  this._modelName = upperCamelCase(this._tableName);
@@ -60,12 +59,10 @@ class ModelAPI {
60
59
 
61
60
  return tracing.withSpan(name, async (span) => {
62
61
  try {
63
- const defaults = this._defaultValues();
64
62
  const query = db
65
63
  .insertInto(this._tableName)
66
64
  .values(
67
65
  snakeCaseObject({
68
- ...defaults,
69
66
  ...values,
70
67
  })
71
68
  )
@@ -54,47 +54,22 @@ beforeEach(async () => {
54
54
  },
55
55
  };
56
56
 
57
- personAPI = new ModelAPI(
58
- "person",
59
- () => {
60
- return {
61
- id: KSUID.randomSync().string,
62
- date: new Date("2022-01-01"),
63
- };
64
- },
65
- tableConfigMap
66
- );
57
+ personAPI = new ModelAPI("person", undefined, tableConfigMap);
67
58
 
68
- postAPI = new ModelAPI(
69
- "post",
70
- () => {
71
- return {
72
- id: KSUID.randomSync().string,
73
- };
74
- },
75
- tableConfigMap
76
- );
59
+ postAPI = new ModelAPI("post", undefined, tableConfigMap);
77
60
 
78
- authorAPI = new ModelAPI(
79
- "author",
80
- () => {
81
- return {
82
- id: KSUID.randomSync().string,
83
- };
84
- },
85
- tableConfigMap
86
- );
61
+ authorAPI = new ModelAPI("author", undefined, tableConfigMap);
87
62
  });
88
63
 
89
64
  test("ModelAPI.create", async () => {
90
65
  const row = await personAPI.create({
66
+ id: KSUID.randomSync().string,
91
67
  name: "Jim",
92
68
  married: false,
93
69
  favouriteNumber: 10,
94
70
  });
95
71
  expect(row.name).toEqual("Jim");
96
72
  expect(row.married).toEqual(false);
97
- expect(row.date).toEqual(new Date("2022-01-01"));
98
73
  expect(row.favouriteNumber).toEqual(10);
99
74
  expect(KSUID.parse(row.id).string).toEqual(row.id);
100
75
  });
@@ -102,6 +77,7 @@ test("ModelAPI.create", async () => {
102
77
  test("ModelAPI.create - throws if not not null constraint violation", async () => {
103
78
  await expect(
104
79
  authorAPI.create({
80
+ id: KSUID.randomSync().string,
105
81
  name: null,
106
82
  })
107
83
  ).rejects.toThrow('null value in column "name" violates not-null constraint');
@@ -109,11 +85,13 @@ test("ModelAPI.create - throws if not not null constraint violation", async () =
109
85
 
110
86
  test("ModelAPI.create - throws if database constraint fails", async () => {
111
87
  const row = await personAPI.create({
88
+ id: KSUID.randomSync().string,
112
89
  name: "Jim",
113
90
  married: false,
114
91
  favouriteNumber: 10,
115
92
  });
116
93
  const promise = personAPI.create({
94
+ id: KSUID.randomSync().string,
117
95
  id: row.id,
118
96
  name: "Jim",
119
97
  married: false,
@@ -126,6 +104,7 @@ test("ModelAPI.create - throws if database constraint fails", async () => {
126
104
 
127
105
  test("ModelAPI.findOne", async () => {
128
106
  const created = await personAPI.create({
107
+ id: KSUID.randomSync().string,
129
108
  name: "Jim",
130
109
  married: false,
131
110
  favouriteNumber: 10,
@@ -138,11 +117,13 @@ test("ModelAPI.findOne", async () => {
138
117
 
139
118
  test("ModelAPI.findOne - relationships - one to many", async () => {
140
119
  const person = await personAPI.create({
120
+ id: KSUID.randomSync().string,
141
121
  name: "Jim",
142
122
  married: false,
143
123
  favouriteNumber: 10,
144
124
  });
145
125
  const post = await postAPI.create({
126
+ id: KSUID.randomSync().string,
146
127
  title: "My Post",
147
128
  authorId: person.id,
148
129
  });
@@ -164,16 +145,19 @@ test("ModelAPI.findOne - return null if not found", async () => {
164
145
 
165
146
  test("ModelAPI.findMany", async () => {
166
147
  await personAPI.create({
148
+ id: KSUID.randomSync().string,
167
149
  name: "Jim",
168
150
  married: false,
169
151
  favouriteNumber: 10,
170
152
  });
171
153
  const bob = await personAPI.create({
154
+ id: KSUID.randomSync().string,
172
155
  name: "Bob",
173
156
  married: true,
174
157
  favouriteNumber: 11,
175
158
  });
176
159
  const sally = await personAPI.create({
160
+ id: KSUID.randomSync().string,
177
161
  name: "Sally",
178
162
  married: true,
179
163
  favouriteNumber: 12,
@@ -189,9 +173,11 @@ test("ModelAPI.findMany", async () => {
189
173
 
190
174
  test("ModelAPI.findMany - no where conditions", async () => {
191
175
  await personAPI.create({
176
+ id: KSUID.randomSync().string,
192
177
  name: "Jim",
193
178
  });
194
179
  await personAPI.create({
180
+ id: KSUID.randomSync().string,
195
181
  name: "Bob",
196
182
  });
197
183
 
@@ -202,9 +188,11 @@ test("ModelAPI.findMany - no where conditions", async () => {
202
188
 
203
189
  test("ModelAPI.findMany - startsWith", async () => {
204
190
  const jim = await personAPI.create({
191
+ id: KSUID.randomSync().string,
205
192
  name: "Jim",
206
193
  });
207
194
  await personAPI.create({
195
+ id: KSUID.randomSync().string,
208
196
  name: "Bob",
209
197
  });
210
198
  const rows = await personAPI.findMany({
@@ -220,9 +208,11 @@ test("ModelAPI.findMany - startsWith", async () => {
220
208
 
221
209
  test("ModelAPI.findMany - endsWith", async () => {
222
210
  const jim = await personAPI.create({
211
+ id: KSUID.randomSync().string,
223
212
  name: "Jim",
224
213
  });
225
214
  await personAPI.create({
215
+ id: KSUID.randomSync().string,
226
216
  name: "Bob",
227
217
  });
228
218
  const rows = await personAPI.findMany({
@@ -238,12 +228,15 @@ test("ModelAPI.findMany - endsWith", async () => {
238
228
 
239
229
  test("ModelAPI.findMany - contains", async () => {
240
230
  const billy = await personAPI.create({
231
+ id: KSUID.randomSync().string,
241
232
  name: "Billy",
242
233
  });
243
234
  const sally = await personAPI.create({
235
+ id: KSUID.randomSync().string,
244
236
  name: "Sally",
245
237
  });
246
238
  await personAPI.create({
239
+ id: KSUID.randomSync().string,
247
240
  name: "Jim",
248
241
  });
249
242
  const rows = await personAPI.findMany({
@@ -259,12 +252,15 @@ test("ModelAPI.findMany - contains", async () => {
259
252
 
260
253
  test("ModelAPI.findMany - oneOf", async () => {
261
254
  const billy = await personAPI.create({
255
+ id: KSUID.randomSync().string,
262
256
  name: "Billy",
263
257
  });
264
258
  const sally = await personAPI.create({
259
+ id: KSUID.randomSync().string,
265
260
  name: "Sally",
266
261
  });
267
262
  await personAPI.create({
263
+ id: KSUID.randomSync().string,
268
264
  name: "Jim",
269
265
  });
270
266
  const rows = await personAPI.findMany({
@@ -280,9 +276,11 @@ test("ModelAPI.findMany - oneOf", async () => {
280
276
 
281
277
  test("ModelAPI.findMany - greaterThan", async () => {
282
278
  await personAPI.create({
279
+ id: KSUID.randomSync().string,
283
280
  favouriteNumber: 1,
284
281
  });
285
282
  const p = await personAPI.create({
283
+ id: KSUID.randomSync().string,
286
284
  favouriteNumber: 2,
287
285
  });
288
286
  const rows = await personAPI.findMany({
@@ -298,12 +296,15 @@ test("ModelAPI.findMany - greaterThan", async () => {
298
296
 
299
297
  test("ModelAPI.findMany - greaterThanOrEquals", async () => {
300
298
  await personAPI.create({
299
+ id: KSUID.randomSync().string,
301
300
  favouriteNumber: 1,
302
301
  });
303
302
  const p = await personAPI.create({
303
+ id: KSUID.randomSync().string,
304
304
  favouriteNumber: 2,
305
305
  });
306
306
  const p2 = await personAPI.create({
307
+ id: KSUID.randomSync().string,
307
308
  favouriteNumber: 3,
308
309
  });
309
310
  const rows = await personAPI.findMany({
@@ -319,9 +320,11 @@ test("ModelAPI.findMany - greaterThanOrEquals", async () => {
319
320
 
320
321
  test("ModelAPI.findMany - lessThan", async () => {
321
322
  const p = await personAPI.create({
323
+ id: KSUID.randomSync().string,
322
324
  favouriteNumber: 1,
323
325
  });
324
326
  await personAPI.create({
327
+ id: KSUID.randomSync().string,
325
328
  favouriteNumber: 2,
326
329
  });
327
330
  const rows = await personAPI.findMany({
@@ -337,12 +340,15 @@ test("ModelAPI.findMany - lessThan", async () => {
337
340
 
338
341
  test("ModelAPI.findMany - lessThanOrEquals", async () => {
339
342
  const p = await personAPI.create({
343
+ id: KSUID.randomSync().string,
340
344
  favouriteNumber: 1,
341
345
  });
342
346
  const p2 = await personAPI.create({
347
+ id: KSUID.randomSync().string,
343
348
  favouriteNumber: 2,
344
349
  });
345
350
  await personAPI.create({
351
+ id: KSUID.randomSync().string,
346
352
  favouriteNumber: 3,
347
353
  });
348
354
  const rows = await personAPI.findMany({
@@ -358,9 +364,11 @@ test("ModelAPI.findMany - lessThanOrEquals", async () => {
358
364
 
359
365
  test("ModelAPI.findMany - before", async () => {
360
366
  const p = await personAPI.create({
367
+ id: KSUID.randomSync().string,
361
368
  date: new Date("2022-01-01"),
362
369
  });
363
370
  await personAPI.create({
371
+ id: KSUID.randomSync().string,
364
372
  date: new Date("2022-01-02"),
365
373
  });
366
374
  const rows = await personAPI.findMany({
@@ -376,10 +384,12 @@ test("ModelAPI.findMany - before", async () => {
376
384
 
377
385
  test("ModelAPI.findMany - empty where", async () => {
378
386
  const p = await personAPI.create({
387
+ id: KSUID.randomSync().string,
379
388
  date: new Date("2022-01-01"),
380
389
  });
381
390
 
382
391
  const p2 = await personAPI.create({
392
+ id: KSUID.randomSync().string,
383
393
  date: new Date("2022-01-02"),
384
394
  });
385
395
 
@@ -398,12 +408,15 @@ test("ModelAPI.findMany - empty where", async () => {
398
408
 
399
409
  test("ModelAPI.findMany - onOrBefore", async () => {
400
410
  const p = await personAPI.create({
411
+ id: KSUID.randomSync().string,
401
412
  date: new Date("2022-01-01"),
402
413
  });
403
414
  const p2 = await personAPI.create({
415
+ id: KSUID.randomSync().string,
404
416
  date: new Date("2022-01-02"),
405
417
  });
406
418
  await personAPI.create({
419
+ id: KSUID.randomSync().string,
407
420
  date: new Date("2022-01-03"),
408
421
  });
409
422
  const rows = await personAPI.findMany({
@@ -419,19 +432,19 @@ test("ModelAPI.findMany - onOrBefore", async () => {
419
432
 
420
433
  test("ModelAPI.findMany - limit", async () => {
421
434
  await personAPI.create({
422
- id: "1",
435
+ id: KSUID.randomSync().string,
423
436
  name: "Jim",
424
437
  married: false,
425
438
  favouriteNumber: 10,
426
439
  });
427
440
  await personAPI.create({
428
- id: "2",
441
+ id: KSUID.randomSync().string,
429
442
  name: "Bob",
430
443
  married: true,
431
444
  favouriteNumber: 11,
432
445
  });
433
446
  await personAPI.create({
434
- id: "3",
447
+ id: KSUID.randomSync().string,
435
448
  name: "Sally",
436
449
  married: true,
437
450
  favouriteNumber: 12,
@@ -439,6 +452,9 @@ test("ModelAPI.findMany - limit", async () => {
439
452
 
440
453
  const rows = await personAPI.findMany({
441
454
  limit: 2,
455
+ orderBy: {
456
+ favouriteNumber: "asc",
457
+ },
442
458
  });
443
459
 
444
460
  expect(rows.map((r) => r.name)).toEqual(["Jim", "Bob"]);
@@ -446,21 +462,21 @@ test("ModelAPI.findMany - limit", async () => {
446
462
 
447
463
  test("ModelAPI.findMany - orderBy", async () => {
448
464
  await personAPI.create({
449
- id: "1",
465
+ id: KSUID.randomSync().string,
450
466
  name: "Jim",
451
467
  married: false,
452
468
  favouriteNumber: 10,
453
469
  date: new Date(2023, 12, 29),
454
470
  });
455
471
  await personAPI.create({
456
- id: "2",
472
+ id: KSUID.randomSync().string,
457
473
  name: "Bob",
458
474
  married: true,
459
475
  favouriteNumber: 11,
460
476
  date: new Date(2023, 12, 30),
461
477
  });
462
478
  await personAPI.create({
463
- id: "3",
479
+ id: KSUID.randomSync().string,
464
480
  name: "Sally",
465
481
  married: true,
466
482
  favouriteNumber: 12,
@@ -506,21 +522,21 @@ test("ModelAPI.findMany - orderBy", async () => {
506
522
 
507
523
  test("ModelAPI.findMany - offset", async () => {
508
524
  await personAPI.create({
509
- id: "1",
525
+ id: KSUID.randomSync().string,
510
526
  name: "Jim",
511
527
  married: false,
512
528
  favouriteNumber: 10,
513
529
  date: new Date(2023, 12, 29),
514
530
  });
515
531
  await personAPI.create({
516
- id: "2",
532
+ id: KSUID.randomSync().string,
517
533
  name: "Bob",
518
534
  married: true,
519
535
  favouriteNumber: 11,
520
536
  date: new Date(2023, 12, 30),
521
537
  });
522
538
  await personAPI.create({
523
- id: "3",
539
+ id: KSUID.randomSync().string,
524
540
  name: "Sally",
525
541
  married: true,
526
542
  favouriteNumber: 12,
@@ -559,9 +575,11 @@ test("ModelAPI.findMany - offset", async () => {
559
575
 
560
576
  test("ModelAPI.findMany - after", async () => {
561
577
  await personAPI.create({
578
+ id: KSUID.randomSync().string,
562
579
  date: new Date("2022-01-01"),
563
580
  });
564
581
  const p = await personAPI.create({
582
+ id: KSUID.randomSync().string,
565
583
  date: new Date("2022-01-02"),
566
584
  });
567
585
  const rows = await personAPI.findMany({
@@ -577,12 +595,15 @@ test("ModelAPI.findMany - after", async () => {
577
595
 
578
596
  test("ModelAPI.findMany - onOrAfter", async () => {
579
597
  await personAPI.create({
598
+ id: KSUID.randomSync().string,
580
599
  date: new Date("2022-01-01"),
581
600
  });
582
601
  const p = await personAPI.create({
602
+ id: KSUID.randomSync().string,
583
603
  date: new Date("2022-01-02"),
584
604
  });
585
605
  const p2 = await personAPI.create({
606
+ id: KSUID.randomSync().string,
586
607
  date: new Date("2022-01-03"),
587
608
  });
588
609
  const rows = await personAPI.findMany({
@@ -598,9 +619,11 @@ test("ModelAPI.findMany - onOrAfter", async () => {
598
619
 
599
620
  test("ModelAPI.findMany - equals", async () => {
600
621
  const p = await personAPI.create({
622
+ id: KSUID.randomSync().string,
601
623
  name: "Jim",
602
624
  });
603
625
  await personAPI.create({
626
+ id: KSUID.randomSync().string,
604
627
  name: "Sally",
605
628
  });
606
629
  const rows = await personAPI.findMany({
@@ -616,9 +639,11 @@ test("ModelAPI.findMany - equals", async () => {
616
639
 
617
640
  test("ModelAPI.findMany - notEquals", async () => {
618
641
  const p = await personAPI.create({
642
+ id: KSUID.randomSync().string,
619
643
  name: "Jim",
620
644
  });
621
645
  await personAPI.create({
646
+ id: KSUID.randomSync().string,
622
647
  name: "Sally",
623
648
  });
624
649
  const rows = await personAPI.findMany({
@@ -634,16 +659,19 @@ test("ModelAPI.findMany - notEquals", async () => {
634
659
 
635
660
  test("ModelAPI.findMany - complex query", async () => {
636
661
  const p = await personAPI.create({
662
+ id: KSUID.randomSync().string,
637
663
  name: "Jake",
638
664
  favouriteNumber: 8,
639
665
  date: new Date("2021-12-31"),
640
666
  });
641
667
  await personAPI.create({
668
+ id: KSUID.randomSync().string,
642
669
  name: "Jane",
643
670
  favouriteNumber: 12,
644
671
  date: new Date("2022-01-11"),
645
672
  });
646
673
  const p2 = await personAPI.create({
674
+ id: KSUID.randomSync().string,
647
675
  name: "Billy",
648
676
  favouriteNumber: 16,
649
677
  date: new Date("2022-01-05"),
@@ -674,20 +702,25 @@ test("ModelAPI.findMany - complex query", async () => {
674
702
 
675
703
  test("ModelAPI.findMany - relationships - one to many", async () => {
676
704
  const person = await personAPI.create({
705
+ id: KSUID.randomSync().string,
677
706
  name: "Jim",
678
707
  });
679
708
  const person2 = await personAPI.create({
709
+ id: KSUID.randomSync().string,
680
710
  name: "Bob",
681
711
  });
682
712
  const post1 = await postAPI.create({
713
+ id: KSUID.randomSync().string,
683
714
  title: "My First Post",
684
715
  authorId: person.id,
685
716
  });
686
717
  const post2 = await postAPI.create({
718
+ id: KSUID.randomSync().string,
687
719
  title: "My Second Post",
688
720
  authorId: person.id,
689
721
  });
690
722
  await postAPI.create({
723
+ id: KSUID.randomSync().string,
691
724
  title: "My Third Post",
692
725
  authorId: person2.id,
693
726
  });
@@ -705,17 +738,21 @@ test("ModelAPI.findMany - relationships - one to many", async () => {
705
738
 
706
739
  test("ModelAPI.findMany - relationships - many to one", async () => {
707
740
  const person = await personAPI.create({
741
+ id: KSUID.randomSync().string,
708
742
  name: "Jim",
709
743
  });
710
744
  await postAPI.create({
745
+ id: KSUID.randomSync().string,
711
746
  title: "My First Post",
712
747
  authorId: person.id,
713
748
  });
714
749
  await postAPI.create({
750
+ id: KSUID.randomSync().string,
715
751
  title: "My Second Post",
716
752
  authorId: person.id,
717
753
  });
718
754
  await postAPI.create({
755
+ id: KSUID.randomSync().string,
719
756
  title: "My Second Post",
720
757
  authorId: person.id,
721
758
  });
@@ -740,16 +777,20 @@ test("ModelAPI.findMany - relationships - many to one", async () => {
740
777
 
741
778
  test("ModelAPI.findMany - relationships - duplicate joins handled", async () => {
742
779
  const person = await personAPI.create({
780
+ id: KSUID.randomSync().string,
743
781
  name: "Jim",
744
782
  });
745
783
  const person2 = await personAPI.create({
784
+ id: KSUID.randomSync().string,
746
785
  name: "Bob",
747
786
  });
748
787
  const post1 = await postAPI.create({
788
+ id: KSUID.randomSync().string,
749
789
  title: "My First Post",
750
790
  authorId: person.id,
751
791
  });
752
792
  const post2 = await postAPI.create({
793
+ id: KSUID.randomSync().string,
753
794
  title: "My Second Post",
754
795
  authorId: person2.id,
755
796
  });
@@ -773,11 +814,13 @@ test("ModelAPI.findMany - relationships - duplicate joins handled", async () =>
773
814
 
774
815
  test("ModelAPI.update", async () => {
775
816
  let jim = await personAPI.create({
817
+ id: KSUID.randomSync().string,
776
818
  name: "Jim",
777
819
  married: false,
778
820
  favouriteNumber: 10,
779
821
  });
780
822
  let bob = await personAPI.create({
823
+ id: KSUID.randomSync().string,
781
824
  name: "Bob",
782
825
  married: false,
783
826
  favouriteNumber: 11,
@@ -811,6 +854,7 @@ test("ModelAPI.update - throws if not found", async () => {
811
854
 
812
855
  test("ModelAPI.update - throws if not not null constraint violation", async () => {
813
856
  const jim = await authorAPI.create({
857
+ id: KSUID.randomSync().string,
814
858
  name: "jim",
815
859
  });
816
860
 
@@ -830,6 +874,7 @@ test("ModelAPI.update - throws if not not null constraint violation", async () =
830
874
 
831
875
  test("ModelAPI.delete", async () => {
832
876
  const jim = await personAPI.create({
877
+ id: KSUID.randomSync().string,
833
878
  name: "Jim",
834
879
  });
835
880
  const id = jim.id;
@@ -843,15 +888,19 @@ test("ModelAPI.delete", async () => {
843
888
 
844
889
  test("ModelAPI chained findMany with offset/limit/order by", async () => {
845
890
  await postAPI.create({
891
+ id: KSUID.randomSync().string,
846
892
  title: "adam",
847
893
  });
848
894
  await postAPI.create({
895
+ id: KSUID.randomSync().string,
849
896
  title: "dave",
850
897
  });
851
898
  const three = await postAPI.create({
899
+ id: KSUID.randomSync().string,
852
900
  title: "jon",
853
901
  });
854
902
  const four = await postAPI.create({
903
+ id: KSUID.randomSync().string,
855
904
  title: "jon bretman",
856
905
  });
857
906
 
@@ -211,23 +211,15 @@ describe("ModelAPI error handling", () => {
211
211
  `.execute(db);
212
212
 
213
213
  const models = {
214
- post: new ModelAPI(
215
- "post",
216
- () => {
217
- return {
218
- id: KSUID.randomSync().string,
219
- };
220
- },
221
- {
222
- post: {
223
- author: {
224
- relationshipType: "belongsTo",
225
- foreignKey: "author_id",
226
- referencesTable: "person",
227
- },
214
+ post: new ModelAPI("post", undefined, {
215
+ post: {
216
+ author: {
217
+ relationshipType: "belongsTo",
218
+ foreignKey: "author_id",
219
+ referencesTable: "person",
228
220
  },
229
- }
230
- ),
221
+ },
222
+ }),
231
223
  };
232
224
 
233
225
  functionConfig = {
@@ -240,7 +232,10 @@ describe("ModelAPI error handling", () => {
240
232
  createPost: async (ctx, inputs) => {
241
233
  new Permissions().allow();
242
234
 
243
- const post = await models.post.create(inputs);
235
+ const post = await models.post.create({
236
+ id: KSUID.randomSync().string,
237
+ ...inputs,
238
+ });
244
239
 
245
240
  return post;
246
241
  },