@saltcorn/data 0.9.4-beta.2 → 0.9.4-beta.20

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 (129) hide show
  1. package/dist/base-plugin/actions.d.ts +156 -67
  2. package/dist/base-plugin/actions.d.ts.map +1 -1
  3. package/dist/base-plugin/actions.js +147 -27
  4. package/dist/base-plugin/actions.js.map +1 -1
  5. package/dist/base-plugin/fieldviews.d.ts.map +1 -1
  6. package/dist/base-plugin/fieldviews.js +27 -10
  7. package/dist/base-plugin/fieldviews.js.map +1 -1
  8. package/dist/base-plugin/fileviews.d.ts +13 -0
  9. package/dist/base-plugin/fileviews.js +42 -2
  10. package/dist/base-plugin/fileviews.js.map +1 -1
  11. package/dist/base-plugin/index.d.ts +5 -2
  12. package/dist/base-plugin/index.d.ts.map +1 -1
  13. package/dist/base-plugin/types.d.ts.map +1 -1
  14. package/dist/base-plugin/types.js +25 -7
  15. package/dist/base-plugin/types.js.map +1 -1
  16. package/dist/base-plugin/viewtemplates/edit.d.ts +2 -0
  17. package/dist/base-plugin/viewtemplates/edit.d.ts.map +1 -1
  18. package/dist/base-plugin/viewtemplates/edit.js +194 -58
  19. package/dist/base-plugin/viewtemplates/edit.js.map +1 -1
  20. package/dist/base-plugin/viewtemplates/filter.d.ts.map +1 -1
  21. package/dist/base-plugin/viewtemplates/filter.js +88 -8
  22. package/dist/base-plugin/viewtemplates/filter.js.map +1 -1
  23. package/dist/base-plugin/viewtemplates/list.d.ts +3 -2
  24. package/dist/base-plugin/viewtemplates/list.d.ts.map +1 -1
  25. package/dist/base-plugin/viewtemplates/list.js +276 -41
  26. package/dist/base-plugin/viewtemplates/list.js.map +1 -1
  27. package/dist/base-plugin/viewtemplates/show.d.ts.map +1 -1
  28. package/dist/base-plugin/viewtemplates/show.js +130 -64
  29. package/dist/base-plugin/viewtemplates/show.js.map +1 -1
  30. package/dist/base-plugin/viewtemplates/viewable_fields.d.ts +4 -3
  31. package/dist/base-plugin/viewtemplates/viewable_fields.d.ts.map +1 -1
  32. package/dist/base-plugin/viewtemplates/viewable_fields.js +160 -47
  33. package/dist/base-plugin/viewtemplates/viewable_fields.js.map +1 -1
  34. package/dist/db/fixtures.d.ts.map +1 -1
  35. package/dist/db/fixtures.js +196 -30
  36. package/dist/db/fixtures.js.map +1 -1
  37. package/dist/db/state.d.ts +5 -0
  38. package/dist/db/state.d.ts.map +1 -1
  39. package/dist/db/state.js +20 -0
  40. package/dist/db/state.js.map +1 -1
  41. package/dist/migrate.d.ts +1 -0
  42. package/dist/migrate.d.ts.map +1 -1
  43. package/dist/migrate.js +6 -3
  44. package/dist/migrate.js.map +1 -1
  45. package/dist/migrations/202402071125.d.ts +2 -0
  46. package/dist/migrations/202402071125.d.ts.map +1 -0
  47. package/dist/migrations/202402071125.js +4 -0
  48. package/dist/migrations/202402071125.js.map +1 -0
  49. package/dist/models/config.d.ts.map +1 -1
  50. package/dist/models/config.js +33 -0
  51. package/dist/models/config.js.map +1 -1
  52. package/dist/models/email.d.ts.map +1 -1
  53. package/dist/models/email.js +6 -2
  54. package/dist/models/email.js.map +1 -1
  55. package/dist/models/expression.d.ts +2 -0
  56. package/dist/models/expression.d.ts.map +1 -1
  57. package/dist/models/expression.js +9 -0
  58. package/dist/models/expression.js.map +1 -1
  59. package/dist/models/field.d.ts +1 -1
  60. package/dist/models/field.d.ts.map +1 -1
  61. package/dist/models/field.js +4 -4
  62. package/dist/models/field.js.map +1 -1
  63. package/dist/models/file.d.ts +2 -0
  64. package/dist/models/file.d.ts.map +1 -1
  65. package/dist/models/file.js +9 -0
  66. package/dist/models/file.js.map +1 -1
  67. package/dist/models/internal/query.d.ts +6 -0
  68. package/dist/models/internal/query.d.ts.map +1 -0
  69. package/dist/models/internal/query.js +77 -0
  70. package/dist/models/internal/query.js.map +1 -0
  71. package/dist/models/page.d.ts.map +1 -1
  72. package/dist/models/page.js +4 -0
  73. package/dist/models/page.js.map +1 -1
  74. package/dist/models/page_group.d.ts +1 -0
  75. package/dist/models/page_group.d.ts.map +1 -1
  76. package/dist/models/page_group.js +1 -0
  77. package/dist/models/page_group.js.map +1 -1
  78. package/dist/models/table.d.ts +18 -4
  79. package/dist/models/table.d.ts.map +1 -1
  80. package/dist/models/table.js +78 -50
  81. package/dist/models/table.js.map +1 -1
  82. package/dist/models/trigger.d.ts +3 -3
  83. package/dist/models/trigger.d.ts.map +1 -1
  84. package/dist/models/trigger.js +69 -7
  85. package/dist/models/trigger.js.map +1 -1
  86. package/dist/models/user.d.ts.map +1 -1
  87. package/dist/models/user.js +5 -4
  88. package/dist/models/user.js.map +1 -1
  89. package/dist/models/view.d.ts +1 -0
  90. package/dist/models/view.d.ts.map +1 -1
  91. package/dist/models/view.js +6 -0
  92. package/dist/models/view.js.map +1 -1
  93. package/dist/plugin-helper.d.ts +8 -20
  94. package/dist/plugin-helper.d.ts.map +1 -1
  95. package/dist/plugin-helper.js +108 -76
  96. package/dist/plugin-helper.js.map +1 -1
  97. package/dist/tests/actions.test.js +143 -2
  98. package/dist/tests/actions.test.js.map +1 -1
  99. package/dist/tests/auth.test.js +101 -1
  100. package/dist/tests/auth.test.js.map +1 -1
  101. package/dist/tests/auxtest.test.js +4 -0
  102. package/dist/tests/auxtest.test.js.map +1 -1
  103. package/dist/tests/calc.test.js +11 -1
  104. package/dist/tests/calc.test.js.map +1 -1
  105. package/dist/tests/edit.test.js +394 -0
  106. package/dist/tests/edit.test.js.map +1 -1
  107. package/dist/tests/filter.test.d.ts +2 -0
  108. package/dist/tests/filter.test.d.ts.map +1 -0
  109. package/dist/tests/filter.test.js +438 -0
  110. package/dist/tests/filter.test.js.map +1 -0
  111. package/dist/tests/list.test.d.ts +2 -0
  112. package/dist/tests/list.test.d.ts.map +1 -0
  113. package/dist/tests/list.test.js +735 -0
  114. package/dist/tests/list.test.js.map +1 -0
  115. package/dist/tests/show.test.d.ts +2 -0
  116. package/dist/tests/show.test.d.ts.map +1 -0
  117. package/dist/tests/show.test.js +325 -0
  118. package/dist/tests/show.test.js.map +1 -0
  119. package/dist/tests/table.test.js +184 -56
  120. package/dist/tests/table.test.js.map +1 -1
  121. package/dist/tests/table_history.test.js +51 -0
  122. package/dist/tests/table_history.test.js.map +1 -1
  123. package/dist/tests/view.test.js +21 -1
  124. package/dist/tests/view.test.js.map +1 -1
  125. package/dist/utils.d.ts +4 -17
  126. package/dist/utils.d.ts.map +1 -1
  127. package/dist/utils.js +33 -32
  128. package/dist/utils.js.map +1 -1
  129. package/package.json +8 -7
@@ -248,6 +248,24 @@ jest.setTimeout(30000);
248
248
  (0, globals_1.expect)(michaels.length).toStrictEqual(2);
249
249
  (0, globals_1.expect)(Math.round(michaels[0].avg_temp)).toBe(38);
250
250
  });
251
+ (0, globals_1.it)("should get joined rows with filtered aggregations", async () => {
252
+ const patients = table_1.default.findOne({ name: "patients" });
253
+ (0, assertions_1.assertIsSet)(patients);
254
+ const michaels = await patients.getJoinedRows({
255
+ orderBy: "id",
256
+ aggregations: {
257
+ avg_temp: {
258
+ table: "readings",
259
+ ref: "patient_id",
260
+ field: "temperature",
261
+ aggregate: "avg",
262
+ where: { normalised: true },
263
+ },
264
+ },
265
+ });
266
+ (0, globals_1.expect)(michaels.length).toStrictEqual(2);
267
+ (0, globals_1.expect)(Math.round(michaels[0].avg_temp)).toBe(37);
268
+ });
251
269
  (0, globals_1.it)("should get joined rows with unique count aggregations", async () => {
252
270
  const patients = table_1.default.findOne({ name: "patients" });
253
271
  (0, assertions_1.assertIsSet)(patients);
@@ -544,6 +562,139 @@ jest.setTimeout(30000);
544
562
  (0, globals_1.expect)(reads[0].pages).toBe(967);
545
563
  });
546
564
  });
565
+ (0, globals_1.describe)("Table aggregationQuery", () => {
566
+ (0, globals_1.it)("should get avg aggregations", async () => {
567
+ const readings = table_1.default.findOne({ name: "readings" });
568
+ (0, assertions_1.assertIsSet)(readings);
569
+ const aggs = await readings.aggregationQuery({
570
+ avg_temp: {
571
+ field: "temperature",
572
+ aggregate: "avg",
573
+ },
574
+ });
575
+ (0, globals_1.expect)(Math.round(aggs.avg_temp)).toBe(38);
576
+ });
577
+ (0, globals_1.it)("should get filtered avg aggregations", async () => {
578
+ const readings = table_1.default.findOne({ name: "readings" });
579
+ (0, assertions_1.assertIsSet)(readings);
580
+ const aggs = await readings.aggregationQuery({
581
+ avg_temp: {
582
+ field: "temperature",
583
+ aggregate: "avg",
584
+ },
585
+ }, { where: { normalised: true } });
586
+ (0, globals_1.expect)(Math.round(aggs.avg_temp)).toBe(37);
587
+ });
588
+ (0, globals_1.it)("should get array aggregations", async () => {
589
+ const readings = table_1.default.findOne({ name: "readings" });
590
+ (0, assertions_1.assertIsSet)(readings);
591
+ if (!db_1.default.isSQLite) {
592
+ const aggs = await readings.aggregationQuery({
593
+ ids: {
594
+ field: "id",
595
+ aggregate: "array_agg",
596
+ },
597
+ });
598
+ (0, globals_1.expect)(aggs.ids).toStrictEqual([1, 2, 3]);
599
+ }
600
+ });
601
+ (0, globals_1.it)("should get filtered array aggregations", async () => {
602
+ const readings = table_1.default.findOne({ name: "readings" });
603
+ (0, assertions_1.assertIsSet)(readings);
604
+ if (!db_1.default.isSQLite) {
605
+ const aggs = await readings.aggregationQuery({
606
+ ids: {
607
+ field: "id",
608
+ aggregate: "array_agg",
609
+ },
610
+ }, { where: { normalised: true } });
611
+ (0, globals_1.expect)(aggs.ids).toStrictEqual([1]);
612
+ }
613
+ });
614
+ (0, globals_1.it)("should get grouped aggregations", async () => {
615
+ const readings = table_1.default.findOne({ name: "readings" });
616
+ (0, assertions_1.assertIsSet)(readings);
617
+ if (!db_1.default.isSQLite) {
618
+ const aggs = await readings.aggregationQuery({
619
+ temps: {
620
+ field: "id",
621
+ aggregate: "count",
622
+ },
623
+ }, { groupBy: ["patient_id"] });
624
+ (0, globals_1.expect)(aggs).toStrictEqual([
625
+ { patient_id: 2, temps: "1" },
626
+ { patient_id: 1, temps: "2" },
627
+ ]);
628
+ }
629
+ });
630
+ (0, globals_1.it)("sets up new fields", async () => {
631
+ const table = table_1.default.findOne({ name: "books" });
632
+ (0, assertions_1.assertIsSet)(table);
633
+ await field_1.default.create({
634
+ table,
635
+ name: "published",
636
+ label: "Published",
637
+ type: "Date",
638
+ });
639
+ await table.updateRow({ published: new Date("1971-05.04") }, 1);
640
+ await table.updateRow({ published: new Date("1972-05.04") }, 2);
641
+ });
642
+ (0, globals_1.it)("should get latest by field", async () => {
643
+ const books = table_1.default.findOne({ name: "books" });
644
+ (0, assertions_1.assertIsSet)(books);
645
+ if (!db_1.default.isSQLite) {
646
+ const aggs = await books.aggregationQuery({
647
+ pages: {
648
+ field: "pages",
649
+ aggregate: "Latest published",
650
+ },
651
+ });
652
+ (0, globals_1.expect)(aggs).toStrictEqual({ pages: 728 });
653
+ }
654
+ });
655
+ (0, globals_1.it)("should get latest by field, qualified", async () => {
656
+ const books = table_1.default.findOne({ name: "books" });
657
+ (0, assertions_1.assertIsSet)(books);
658
+ if (!db_1.default.isSQLite) {
659
+ const aggs = await books.aggregationQuery({
660
+ pages: {
661
+ field: "pages",
662
+ aggregate: "Latest published",
663
+ },
664
+ }, { where: { author: "Herman Melville" } });
665
+ (0, globals_1.expect)(aggs).toStrictEqual({ pages: 967 });
666
+ }
667
+ });
668
+ (0, globals_1.it)("should get latest by field, grouped", async () => {
669
+ const books = table_1.default.findOne({ name: "books" });
670
+ (0, assertions_1.assertIsSet)(books);
671
+ if (!db_1.default.isSQLite) {
672
+ const aggs = await books.aggregationQuery({
673
+ pages: {
674
+ field: "pages",
675
+ aggregate: "Latest published",
676
+ },
677
+ }, { groupBy: "author" });
678
+ (0, globals_1.expect)(aggs).toStrictEqual([
679
+ { author: "Leo Tolstoy", pages: 728 },
680
+ { author: "Herman Melville", pages: 967 },
681
+ ]);
682
+ }
683
+ });
684
+ (0, globals_1.it)("should get latest by field, grouped and qualified", async () => {
685
+ const books = table_1.default.findOne({ name: "books" });
686
+ (0, assertions_1.assertIsSet)(books);
687
+ if (!db_1.default.isSQLite) {
688
+ const aggs = await books.aggregationQuery({
689
+ pages: {
690
+ field: "pages",
691
+ aggregate: "Latest published",
692
+ },
693
+ }, { groupBy: "author", where: { author: "Herman Melville" } });
694
+ (0, globals_1.expect)(aggs).toStrictEqual([{ author: "Herman Melville", pages: 967 }]);
695
+ }
696
+ });
697
+ });
547
698
  (0, globals_1.describe)("relations", () => {
548
699
  (0, globals_1.it)("get parent relations", async () => {
549
700
  const table = table_1.default.findOne({ name: "patients" });
@@ -597,54 +748,20 @@ jest.setTimeout(30000);
597
748
  const table = table_1.default.findOne({ name: "readings" });
598
749
  (0, assertions_1.assertIsSet)(table);
599
750
  const rels = await table.get_parent_relations(true);
600
- const expected = [
601
- "patient_id.favbook",
602
- "patient_id.favbook.author",
603
- "patient_id.favbook.id",
604
- "patient_id.favbook.pages",
605
- "patient_id.favbook.publisher",
606
- "patient_id.id",
607
- "patient_id.name",
608
- "patient_id.parent",
609
- "patient_id.parent.favbook",
610
- "patient_id.parent.id",
611
- "patient_id.parent.name",
612
- "patient_id.parent.parent",
613
- ];
614
- (0, globals_1.expect)(rels.parent_field_list).toHaveLength(expected.length);
615
- (0, globals_1.expect)(rels.parent_field_list).toEqual(globals_1.expect.arrayContaining(expected));
751
+ (0, globals_1.expect)(rels.parent_field_list.length).toBeGreaterThan(10);
752
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.favbook.publisher");
753
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.name");
616
754
  (0, globals_1.expect)(rels.parent_relations.length).toBe(3);
617
755
  });
618
756
  (0, globals_1.it)("get triple relations", async () => {
619
757
  const table = table_1.default.findOne({ name: "readings" });
620
758
  (0, assertions_1.assertIsSet)(table);
621
759
  const rels = await table.get_parent_relations(true, true);
622
- const expected = [
623
- "patient_id.favbook",
624
- "patient_id.favbook.author",
625
- "patient_id.favbook.id",
626
- "patient_id.favbook.pages",
627
- "patient_id.favbook.publisher",
628
- "patient_id.favbook.publisher.id",
629
- "patient_id.favbook.publisher.name",
630
- "patient_id.id",
631
- "patient_id.name",
632
- "patient_id.parent",
633
- "patient_id.parent.favbook",
634
- "patient_id.parent.favbook.author",
635
- "patient_id.parent.favbook.id",
636
- "patient_id.parent.favbook.pages",
637
- "patient_id.parent.favbook.publisher",
638
- "patient_id.parent.id",
639
- "patient_id.parent.name",
640
- "patient_id.parent.parent",
641
- "patient_id.parent.parent.favbook",
642
- "patient_id.parent.parent.id",
643
- "patient_id.parent.parent.name",
644
- "patient_id.parent.parent.parent",
645
- ];
646
- (0, globals_1.expect)(rels.parent_field_list).toHaveLength(expected.length);
647
- (0, globals_1.expect)(rels.parent_field_list).toEqual(globals_1.expect.arrayContaining(expected));
760
+ (0, globals_1.expect)(rels.parent_field_list.length).toBeGreaterThan(10);
761
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.parent.favbook.author");
762
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.favbook.publisher.name");
763
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.favbook.author");
764
+ (0, globals_1.expect)(rels.parent_field_list).toContain("patient_id.name");
648
765
  (0, globals_1.expect)(rels.parent_relations.length).toBe(3);
649
766
  });
650
767
  });
@@ -706,6 +823,29 @@ Peter Rossi, 212,9,200`;
706
823
  (0, globals_1.expect)(row?.pages).toBe(540);
707
824
  await table.updateRow({ author: "Herman Melville" }, 1);
708
825
  });
826
+ (0, globals_1.it)("should replace when id given in preview", async () => {
827
+ const csv = `id,author,Pages
828
+ 1, Noam Chomsky, 540
829
+ 18, Cornel West, 678`;
830
+ const fnm = "/tmp/testreplaceid.csv";
831
+ await (0, promises_1.writeFile)(fnm, csv);
832
+ const table = table_1.default.findOne({ name: "books" });
833
+ (0, assertions_1.assertIsSet)(table);
834
+ (0, globals_1.expect)(!!table).toBe(true);
835
+ const impres = await table.import_csv_file(fnm, { no_table_write: true });
836
+ (0, assertions_1.assertsIsSuccessMessage)(impres);
837
+ const rows = impres.rows;
838
+ (0, assertions_1.assertIsSet)(rows);
839
+ (0, globals_1.expect)(rows.length).toBe(2);
840
+ const row = rows.find((r) => r.id == 1);
841
+ (0, globals_1.expect)(row?.pages).toBe(540);
842
+ (0, globals_1.expect)(row.author).toBe("Noam Chomsky");
843
+ const row1 = rows.find((r) => r.id == 18);
844
+ (0, globals_1.expect)(row1?.pages).toBe(678);
845
+ const rowDB = await table.getRow({ id: 1 });
846
+ (0, assertions_1.assertIsSet)(rowDB);
847
+ (0, globals_1.expect)(rowDB.author).toBe("Herman Melville");
848
+ });
709
849
  (0, globals_1.it)("fail on required field", async () => {
710
850
  const csv = `author,Pagez
711
851
  Joe Celko, 856
@@ -1648,20 +1788,8 @@ Pencil, 0.5,2, t`;
1648
1788
  (0, globals_1.it)("should find one-level join fields", async () => {
1649
1789
  const table = table_1.default.findOne({ name: "patients" });
1650
1790
  const opts = await table?.field_options(1);
1651
- (0, globals_1.expect)(opts).toStrictEqual([
1652
- "favbook",
1653
- "id",
1654
- "name",
1655
- "parent",
1656
- "favbook.author",
1657
- "favbook.id",
1658
- "favbook.pages",
1659
- "favbook.publisher",
1660
- "parent.favbook",
1661
- "parent.id",
1662
- "parent.name",
1663
- "parent.parent",
1664
- ]);
1791
+ (0, globals_1.expect)(opts).toContain("parent.name");
1792
+ (0, globals_1.expect)(opts).toContain("favbook.pages");
1665
1793
  });
1666
1794
  (0, globals_1.it)("should find string fields", async () => {
1667
1795
  const table = table_1.default.findOne({ name: "patients" });