-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
init.lua
1028 lines (790 loc) · 25.1 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local cjson = require "cjson"
local setmetatable = setmetatable
local tostring = tostring
local require = require
local error = error
local pairs = pairs
local floor = math.floor
local null = ngx.null
local type = type
local next = next
local log = ngx.log
local fmt = string.format
local ERR = ngx.ERR
local new_tab
do
local ok
ok, new_tab = pcall(require, "table.new")
if not ok then
new_tab = function() return {} end
end
end
local _M = {}
local DAO = {}
DAO.__index = DAO
local function validate_size_type(size)
if type(size) ~= "number" then
error("size must be a number", 3)
end
return true
end
local function validate_size_value(size)
if floor(size) ~= size or
size < 1 or
size > 1000 then
return nil, "size must be an integer between 1 and 1000"
end
return true
end
local function validate_offset_type(offset)
if type(offset) ~= "string" then
error("offset must be a string", 3)
end
return true
end
local function validate_entity_type(entity)
if type(entity) ~= "table" then
error("entity must be a table", 3)
end
return true
end
local function validate_primary_key_type(primary_key)
if type(primary_key) ~= "table" then
error("primary_key must be a table", 3)
end
return true
end
local function validate_foreign_key_type(foreign_key)
if type(foreign_key) ~= "table" then
error("foreign_key must be a table", 3)
end
return true
end
local function validate_unique_type(unique_value, name, field)
if type(unique_value) ~= "table" and (field.type == "array" or
field.type == "set" or
field.type == "map" or
field.type == "record" or
field.type == "foreign") then
error(fmt("%s must be a table", name), 3)
elseif type(unique_value) ~= "string" and field.type == "string" then
error(fmt("%s must be a string", name), 3)
elseif type(unique_value) ~= "number" and (field.type == "number" or
field.type == "integer") then
error(fmt("%s must be a number", name), 3)
elseif type(unique_value) ~= "boolean" and field.type == "boolean" then
error(fmt("%s must be a boolean", name), 3)
end
return true
end
local function validate_options_type(options)
if type(options) ~= "table" then
error("options must be a table when specified", 3)
end
return true
end
local function validate_options_value(options, schema, context)
local errors = {}
if schema.ttl == true and options.ttl ~= nil then
if context ~= "insert" and
context ~= "update" and
context ~= "upsert" then
errors.ttl = fmt("option can only be used with inserts, updates and upserts, not with '%ss'",
tostring(context))
elseif floor(options.ttl) ~= options.ttl or
options.ttl < 0 or
options.ttl > 100000000 then
-- a bit over three years maximum to make it more safe against
-- integer overflow (time() + ttl)
errors.ttl = "must be an integer between 0 and 100000000"
end
elseif schema.ttl ~= true and options.ttl ~= nil then
errors.ttl = fmt("cannot be used with '%s'", schema.name)
end
if next(errors) then
return nil, errors
end
return true
end
local function iteration_failed(err, err_t)
local failed = false
return function()
if failed then
return nil
end
failed = true
return false, err, err_t
end
end
local function page_iterator(pager, size, options)
local page = 1
local i, rows, err, offset = 0, pager(size, nil, options)
return function()
if not rows then
return nil, err
end
i = i + 1
local row = rows[i]
if row then
return row, nil, page
end
if i > size and offset then
i, rows, err, offset = 1, pager(size, offset, options)
if not rows then
return nil, err
end
page = page + 1
return rows[i], nil, page
end
return nil
end
end
local function row_iterator(self, pager, size, options)
local next_row = page_iterator(pager, size, options)
local failed = false -- avoid infinite loop if error is not caught
return function()
local err_t
if failed then
return nil
end
local row, err, page = next_row()
if not row then
if err then
failed = true
if type(err) == "table" then
return false, tostring(err), err
end
err_t = self.errors:database_error(err)
return false, tostring(err_t), err_t
end
return nil
end
row, err, err_t = self:row_to_entity(row)
if not row then
failed = true
return false, err, err_t
end
return row, nil, page
end
end
local function check_update(self, key, entity, options, name)
local entity_to_update, err, read_before_write =
self.schema:process_auto_fields(entity, "update")
if not entity_to_update then
local err_t = self.errors:schema_violation(err)
return nil, tostring(err_t), err_t
end
if read_before_write then
local rbw_entity, err, err_t
if name then
rbw_entity, err, err_t = self.strategy:select_by_field(name, key, options)
else
rbw_entity, err, err_t = self.strategy:select(key, options)
end
if not rbw_entity then
return nil, err, err_t
end
entity_to_update = self.schema:merge_values(entity_to_update, rbw_entity)
end
local ok, errors = self.schema:validate_update(entity_to_update)
if not ok then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
end
if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "update")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
if self.schema.cache_key and #self.schema.cache_key > 1 then
entity_to_update.cache_key = self:cache_key(entity_to_update)
end
return entity_to_update
end
local function generate_foreign_key_methods(schema)
local methods = {}
for name, field in schema:each_field() do
if field.type == "foreign" then
local page_method_name = "page_for_" .. name
methods[page_method_name] = function(self, foreign_key, size, offset, options)
validate_foreign_key_type(foreign_key)
if size ~= nil then
validate_size_type(size)
end
if offset ~= nil then
validate_offset_type(offset)
end
if options ~= nil then
validate_options_type(options)
end
local ok, errors = self.schema:validate_field(field, foreign_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end
if size ~= nil then
local err
ok, err = validate_size_value(size)
if not ok then
local err_t = self.errors:invalid_size(err)
return nil, tostring(err_t), err_t
end
else
size = 100
end
if options ~= nil then
ok, errors = validate_options_value(options, schema, "select")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local strategy = self.strategy
local rows, err_t, new_offset = strategy[page_method_name](strategy,
foreign_key,
size, offset)
if not rows then
return nil, tostring(err_t), err_t
end
local entities, err
entities, err, err_t = self:rows_to_entities(rows, options)
if err then
return nil, err, err_t
end
return entities, nil, nil, new_offset
end
local each_method_name = "each_for_" .. name
methods[each_method_name] = function(self, foreign_key, size)
validate_foreign_key_type(foreign_key)
if size ~= nil then
validate_size_type(size)
end
if size ~= nil then
local ok, err = validate_size_value(size)
if not ok then
local err_t = self.errors:invalid_size(err)
return iteration_failed(tostring(err_t), err_t)
end
else
size = 100
end
local ok, errors = self.schema:validate_primary_key(foreign_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return iteration_failed(tostring(err_t), err_t)
end
local strategy = self.strategy
local pager = function(size, offset)
return strategy[page_method_name](strategy, foreign_key, size, offset)
end
return row_iterator(self, pager, size)
end
elseif field.unique or schema.endpoint_key == name then
methods["select_by_" .. name] = function(self, unique_value, options)
validate_unique_type(unique_value, name, field)
if options ~= nil then
validate_options_type(options)
end
local ok, err = schema:validate_field(field, unique_value)
if not ok then
local err_t = self.errors:invalid_unique(name, err)
return nil, tostring(err_t), err_t
end
if options ~= nil then
local errors
ok, errors = validate_options_value(options, schema, "select")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local row, err_t = self.strategy:select_by_field(name, unique_value)
if err_t then
return nil, tostring(err_t), err_t
end
if not row then
return nil
end
return self:row_to_entity(row, options)
end
methods["update_by_" .. name] = function(self, unique_value, entity, options)
validate_unique_type(unique_value, name, field)
validate_entity_type(entity)
if options ~= nil then
validate_options_type(options)
end
local ok, err = schema:validate_field(field, unique_value)
if not ok then
local err_t = self.errors:invalid_unique(name, err)
return nil, tostring(err_t), err_t
end
local entity_to_update, err, err_t = check_update(self, unique_value,
entity, options, name)
if not entity_to_update then
return nil, err, err_t
end
local row, err_t = self.strategy:update_by_field(name, unique_value,
entity_to_update, options)
if not row then
return nil, tostring(err_t), err_t
end
row, err, err_t = self:row_to_entity(row, options)
if not row then
return nil, err, err_t
end
self:post_crud_event("update", row)
return row
end
methods["upsert_by_" .. name] = function(self, unique_value, entity, options)
validate_unique_type(unique_value, name, field)
validate_entity_type(entity)
if options ~= nil then
validate_options_type(options)
end
local ok, err = schema:validate_field(field, unique_value)
if not ok then
local err_t = self.errors:invalid_unique(name, err)
return nil, tostring(err_t), err_t
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local entity_to_upsert, err = self.schema:process_auto_fields(entity, "upsert")
if not entity_to_upsert then
local err_t = self.errors:schema_violation(err)
return nil, tostring(err_t), err_t
end
entity_to_upsert[name] = unique_value
local errors
ok, errors = self.schema:validate_upsert(entity_to_upsert)
if not ok then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
end
entity_to_upsert[name] = nil
if options ~= nil then
ok, errors = validate_options_value(options, schema, "upsert")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
if self.schema.cache_key and #self.schema.cache_key > 1 then
entity_to_upsert.cache_key = self:cache_key(entity)
end
local row, err_t = self.strategy:upsert_by_field(name, unique_value,
entity_to_upsert, options)
if not row then
return nil, tostring(err_t), err_t
end
row, err, err_t = self:row_to_entity(row, options)
if not row then
return nil, err, err_t
end
self:post_crud_event("update", row)
return row
end
methods["delete_by_" .. name] = function(self, unique_value, options)
validate_unique_type(unique_value, name, field)
if options ~= nil then
validate_options_type(options)
end
local ok, err = schema:validate_field(field, unique_value)
if not ok then
local err_t = self.errors:invalid_unique(name, err)
return nil, tostring(err_t), err_t
end
if options ~= nil then
local errors
ok, errors = validate_options_value(options, schema, "delete")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local entity, err, err_t = self["select_by_" .. name](self, unique_value)
if err then
return nil, err, err_t
end
if not entity then
return true
end
local _
_, err_t = self.strategy:delete_by_field(name, unique_value, options)
if err_t then
return nil, tostring(err_t), err_t
end
self:post_crud_event("delete", entity)
return true
end
end
end
return methods
end
function _M.new(db, schema, strategy, errors)
local fk_methods = generate_foreign_key_methods(schema)
local super = setmetatable(fk_methods, DAO)
local self = {
db = db,
schema = schema,
strategy = strategy,
errors = errors,
super = super,
}
if schema.dao then
local custom_dao = require(schema.dao)
for name, method in pairs(custom_dao) do
self[name] = method
end
end
return setmetatable(self, { __index = super })
end
function DAO:truncate()
return self.strategy:truncate()
end
function DAO:select(primary_key, options)
validate_primary_key_type(primary_key)
if options ~= nil then
validate_options_type(options)
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local ok, errors = self.schema:validate_primary_key(primary_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end
if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "select")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local row, err_t = self.strategy:select(primary_key, options)
if err_t then
return nil, tostring(err_t), err_t
end
if not row then
return nil
end
return self:row_to_entity(row, options)
end
function DAO:page(size, offset, options)
if size ~= nil then
validate_size_type(size)
end
if offset ~= nil then
validate_offset_type(offset)
end
if options ~= nil then
validate_options_type(options)
end
if size ~= nil then
local ok, err = validate_size_value(size)
if not ok then
local err_t = self.errors:invalid_size(err)
return nil, tostring(err_t), err_t
end
else
size = 100
end
if options ~= nil then
local ok, errors = validate_options_value(options, self.schema, "select")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local rows, err_t, offset = self.strategy:page(size, offset, nil, options)
if err_t then
return nil, tostring(err_t), err_t
end
local entities, err
entities, err, err_t = self:rows_to_entities(rows, options)
if not entities then
return nil, err, err_t
end
return entities, err, err_t, offset
end
function DAO:each(size, options)
if size ~= nil then
validate_size_type(size)
end
if options ~= nil then
validate_options_type(options)
end
if size ~= nil then
local ok, err = validate_size_value(size)
if not ok then
local err_t = self.errors:invalid_size(err)
return nil, tostring(err_t), err_t
end
else
size = 100
end
if options ~= nil then
local ok, errors = validate_options_value(options, self.schema, "select")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local pager = function(size, offset, options)
return self.strategy:page(size, offset, options)
end
return row_iterator(self, pager, size, options)
end
function DAO:insert(entity, options)
validate_entity_type(entity)
if options ~= nil then
validate_options_type(options)
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local entity_to_insert, err = self.schema:process_auto_fields(entity, "insert")
if not entity_to_insert then
local err_t = self.errors:schema_violation(err)
return nil, tostring(err_t), err_t
end
local ok, errors = self.schema:validate_insert(entity_to_insert)
if not ok then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
end
if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "insert")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
if self.schema.cache_key and #self.schema.cache_key > 1 then
entity_to_insert.cache_key = self:cache_key(entity)
end
local row, err_t = self.strategy:insert(entity_to_insert, options)
if not row then
return nil, tostring(err_t), err_t
end
row, err, err_t = self:row_to_entity(row, options)
if not row then
return nil, err, err_t
end
self:post_crud_event("create", row)
return row
end
function DAO:update(primary_key, entity, options)
validate_primary_key_type(primary_key)
validate_entity_type(entity)
if options ~= nil then
validate_options_type(options)
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local ok, errors = self.schema:validate_primary_key(primary_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end
local entity_to_update, err, err_t = check_update(self, primary_key, entity,
options)
if not entity_to_update then
return nil, err, err_t
end
local row, err_t = self.strategy:update(primary_key, entity_to_update, options)
if not row then
return nil, tostring(err_t), err_t
end
row, err, err_t = self:row_to_entity(row, options)
if not row then
return nil, err, err_t
end
self:post_crud_event("update", row)
return row
end
function DAO:upsert(primary_key, entity, options)
validate_primary_key_type(primary_key)
validate_entity_type(entity)
if options ~= nil then
validate_options_type(options)
end
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local ok, errors = self.schema:validate_primary_key(primary_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end
local entity_to_upsert, err = self.schema:process_auto_fields(entity, "upsert")
if not entity_to_upsert then
local err_t = self.errors:schema_violation(err)
return nil, tostring(err_t), err_t
end
ok, errors = self.schema:validate_upsert(entity_to_upsert)
if not ok then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
end
if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "upsert")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
if self.schema.cache_key and #self.schema.cache_key > 1 then
entity_to_upsert.cache_key = self:cache_key(entity)
end
local row, err_t = self.strategy:upsert(primary_key, entity_to_upsert, options)
if not row then
return nil, tostring(err_t), err_t
end
row, err, err_t = self:row_to_entity(row, options)
if not row then
return nil, err, err_t
end
self:post_crud_event("update", row)
return row
end
function DAO:delete(primary_key, options)
validate_primary_key_type(primary_key)
if options ~= nil then
validate_options_type(options)
end
local ok, errors = self.schema:validate_primary_key(primary_key)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end
local entity, err, err_t = self:select(primary_key)
if err then
return nil, err, err_t
end
if not entity then
return true
end
if options ~= nil then
ok, errors = validate_options_value(options, self.schema, "delete")
if not ok then
local err_t = self.errors:invalid_options(errors)
return nil, tostring(err_t), err_t
end
end
local _
_, err_t = self.strategy:delete(primary_key, options)
if err_t then
return nil, tostring(err_t), err_t
end
self:post_crud_event("delete", entity)
return true
end
function DAO:select_by_cache_key(cache_key)
local ck_definition = self.schema.cache_key
if not ck_definition then
error("entity does not have a cache_key defined", 2)
end
if type(cache_key) ~= "string" then
cache_key = self:cache_key(cache_key)
end
if #ck_definition == 1 then
return self["select_by_" .. ck_definition[1]](self, cache_key)
end
local row, err_t = self.strategy:select_by_field("cache_key", cache_key)
if err_t then
return nil, tostring(err_t), err_t
end
if not row then
return nil
end
return self:row_to_entity(row)
end
function DAO:rows_to_entities(rows, options)
local count = #rows
if count == 0 then
return setmetatable(rows, cjson.empty_array_mt)
end
local entities = new_tab(count, 0)
for i = 1, count do
local entity, err, err_t = self:row_to_entity(rows[i], options)
if not entity then
return nil, err, err_t
end
entities[i] = entity
end
return entities
end
function DAO:row_to_entity(row, options)
if options ~= nil and type(options) ~= "table" then
error("options must be a table", 2)
end
local nulls = options and options.nulls
local entity, errors = self.schema:process_auto_fields(row, "select", nulls)
if not entity then
local err_t = self.errors:schema_violation(errors)
return nil, tostring(err_t), err_t
end
return entity
end
function DAO:post_crud_event(operation, entity)
if self.events then
local _, err = self.events.post_local("dao:crud", operation, {
operation = operation,
schema = self.schema,
entity = entity,
})
if err then
log(ERR, "[db] failed to propagate CRUD operation: ", err)
end
end
end
function DAO:cache_key(key, arg2, arg3, arg4, arg5)
-- Fast path: passing the cache_key/primary_key entries in
-- order as arguments, this produces the same result as
-- the generic code below, but building the cache key
-- becomes a single string.format operation
if type(key) == "string" then
return fmt("%s:%s:%s:%s:%s:%s", self.schema.name,
key == nil and "" or key,
arg2 == nil and "" or arg2,
arg3 == nil and "" or arg3,
arg4 == nil and "" or arg4,
arg5 == nil and "" or arg5)
end
-- Generic path: build the cache key from the fields
-- listed in cache_key or primary_key
if type(key) ~= "table" then
error("key must be a string or an entity table", 2)
end