@saltcorn/data 0.6.1-beta.2 → 0.6.2-beta.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.
@@ -16,7 +16,7 @@ const { traverseSync } = require("../../models/layout");
16
16
  const { structuredClone } = require("../../utils");
17
17
  const db = require("../../db");
18
18
 
19
- /**
19
+ /**
20
20
  * @function
21
21
  * @param {string} viewname
22
22
  * @param {Table|object} table
@@ -42,8 +42,8 @@ const action_url = contract(
42
42
  );
43
43
 
44
44
  /**
45
- * @param {string} url
46
- * @param {object} req
45
+ * @param {string} url
46
+ * @param {object} req
47
47
  * @param {object} opts
48
48
  * @param {string} opts.action_name
49
49
  * @param {string} opts.action_label
@@ -55,7 +55,7 @@ const action_url = contract(
55
55
  * @param {string} opts.action_bgcol
56
56
  * @param {string} opts.action_bordercol
57
57
  * @param {string} opts.action_textcol
58
- * @param {*} __
58
+ * @param {*} __
59
59
  * @returns {object}
60
60
  */
61
61
  const action_link = (
@@ -174,7 +174,7 @@ const make_link = contract(
174
174
  );
175
175
 
176
176
  /**
177
- * @param {string} s
177
+ * @param {string} s
178
178
  * @returns {object}
179
179
  */
180
180
  const parse_view_select = (s) => {
@@ -197,7 +197,7 @@ const parse_view_select = (s) => {
197
197
  };
198
198
 
199
199
  //todo: use above to simplify code
200
- /**
200
+ /**
201
201
  * @function
202
202
  * @param {object} opts
203
203
  * @param {string} opts.view,
@@ -338,7 +338,7 @@ const view_linker = contract(
338
338
  );
339
339
 
340
340
  /**
341
- * @param {string} nm
341
+ * @param {string} nm
342
342
  * @returns {boolean}
343
343
  */
344
344
  const action_requires_write = (nm) => {
@@ -347,7 +347,7 @@ const action_requires_write = (nm) => {
347
347
  if (nm.startsWith("Toggle")) return true;
348
348
  };
349
349
 
350
- /**
350
+ /**
351
351
  * @function
352
352
  * @param {string} viewname
353
353
  * @param {Table|object} table
@@ -527,8 +527,8 @@ const get_viewable_fields = contract(
527
527
  );
528
528
 
529
529
  /**
530
- * @param {string} fname
531
- * @param {object} req
530
+ * @param {string} fname
531
+ * @param {object} req
532
532
  * @returns {string}
533
533
  */
534
534
  const sortlinkForName = (fname, req) => {
@@ -543,10 +543,10 @@ const sortlinkForName = (fname, req) => {
543
543
  };
544
544
 
545
545
  /**
546
- * @param {object} column
547
- * @param {object} f
548
- * @param {object} req
549
- * @param {*} __
546
+ * @param {object} column
547
+ * @param {object} f
548
+ * @param {object} req
549
+ * @param {*} __
550
550
  * @returns {string}
551
551
  */
552
552
  const headerLabelForName = (column, f, req, __) => {
@@ -563,7 +563,7 @@ const headerLabelForName = (column, f, req, __) => {
563
563
  return label + arrow;
564
564
  };
565
565
 
566
- /**
566
+ /**
567
567
  * @function
568
568
  * @param {Field[]} fields
569
569
  * @param {object} state
@@ -597,12 +597,12 @@ const splitUniques = contract(
597
597
  );
598
598
 
599
599
  /**
600
- * @param {object} table
601
- * @param {string} viewname
602
- * @param {object[]} [columns]
603
- * @param {object} layout0
604
- * @param {boolean} id
605
- * @param {object} req
600
+ * @param {object} table
601
+ * @param {string} viewname
602
+ * @param {object[]} [columns]
603
+ * @param {object} layout0
604
+ * @param {boolean} id
605
+ * @param {object} req
606
606
  * @returns {Promise<Form>}
607
607
  */
608
608
  const getForm = async (table, viewname, columns, layout0, id, req) => {
@@ -626,7 +626,7 @@ const getForm = async (table, viewname, columns, layout0, id, req) => {
626
626
  }
627
627
  if (f.calculated)
628
628
  f.sourceURL = `/field/show-calculated/${table.name}/${f.name}/${f.fieldview}`;
629
-
629
+ f.attributes = { ...column.configuration, ...f.attributes };
630
630
  return f;
631
631
  } else if (table.name === "users" && column.field_name === "password") {
632
632
  return new Field({
@@ -669,15 +669,14 @@ const getForm = async (table, viewname, columns, layout0, id, req) => {
669
669
  fields: tfields,
670
670
  layout,
671
671
  });
672
- await form.fill_fkey_options();
673
672
  if (id) form.hidden("id");
674
673
  return form;
675
674
  };
676
675
 
677
676
  /**
678
- * @param {object} table
679
- * @param {object} req
680
- * @param {object} fixed
677
+ * @param {object} table
678
+ * @param {object} req
679
+ * @param {object} fixed
681
680
  * @returns {Promise<object>}
682
681
  */
683
682
  const fill_presets = async (table, req, fixed) => {
@@ -711,4 +710,5 @@ module.exports = {
711
710
  splitUniques,
712
711
  getForm,
713
712
  fill_presets,
713
+ get_view_link_query,
714
714
  };
package/db/index.js CHANGED
@@ -24,9 +24,12 @@ var connectObj = getConnectObject();
24
24
  /** @type {db/sqlite|db/pg|null} */
25
25
  let dbmodule = null;
26
26
  try {
27
- dbmodule = is_sqlite(connectObj) ?
28
- require("@saltcorn/sqlite/sqlite")(getConnectObject) :
29
- require("@saltcorn/postgres/postgres")(getConnectObject);
27
+ if(is_sqlite(connectObj)) {
28
+ dbmodule = require("@saltcorn/sqlite/sqlite");
29
+ dbmodule.init(getConnectObject);
30
+ }
31
+ else
32
+ dbmodule = require("@saltcorn/postgres/postgres")(getConnectObject);
30
33
  } catch(e) {
31
34
  console.log("No database package found.")
32
35
  throw e;
@@ -9,7 +9,7 @@ const estraverse = require("estraverse");
9
9
  const astring = require("astring");
10
10
 
11
11
  /**
12
- * @param {string} s
12
+ * @param {string} s
13
13
  * @returns {boolean|void}
14
14
  */
15
15
  function expressionValidator(s) {
@@ -23,7 +23,7 @@ function expressionValidator(s) {
23
23
  }
24
24
 
25
25
  /**
26
- * @param {string} expression
26
+ * @param {string} expression
27
27
  * @returns {string}
28
28
  */
29
29
  function jsexprToSQL(expression) {
@@ -32,11 +32,11 @@ function jsexprToSQL(expression) {
32
32
  }
33
33
 
34
34
  /**
35
- * @param {string} expression
35
+ * @param {string} expression
36
36
  * @throws {Error}
37
37
  * @returns {object}
38
38
  */
39
- function jsexprToWhere(expression) {
39
+ function jsexprToWhere(expression, extraCtx = {}) {
40
40
  if (!expression) return {};
41
41
  try {
42
42
  const ast = acorn.parseExpressionAt(expression, 0, {
@@ -47,18 +47,26 @@ function jsexprToWhere(expression) {
47
47
  const compile = (node) =>
48
48
  ({
49
49
  BinaryExpression() {
50
+ const cleft = compile(node.left);
51
+ const cright = compile(node.right);
52
+ const cmp =
53
+ typeof cleft === "string" || cleft === null
54
+ ? { eq: [cleft, cright] }
55
+ : typeof cleft === "symbol"
56
+ ? { [cleft.description]: cright }
57
+ : { [cleft]: cright };
50
58
  return {
51
- "=="({ left, right }) {
52
- return { [compile(left)]: compile(right) };
59
+ "=="() {
60
+ return cmp;
53
61
  },
54
- "==="({ left, right }) {
55
- return { [compile(left)]: compile(right) };
62
+ "==="() {
63
+ return cmp;
56
64
  },
57
- "!="({ left, right }) {
58
- return { not: { [compile(left)]: compile(right) } };
65
+ "!="() {
66
+ return { not: cmp };
59
67
  },
60
- "!=="({ left, right }) {
61
- return { not: { [compile(left)]: compile(right) } };
68
+ "!=="() {
69
+ return { not: cmp };
62
70
  },
63
71
  }[node.operator](node);
64
72
  },
@@ -83,14 +91,18 @@ function jsexprToWhere(expression) {
83
91
  }[node.operator](node);
84
92
  },
85
93
  Identifier({ name }) {
86
- return name;
94
+ if (name[0] === "$") {
95
+ return extraCtx[name.substring(1)] || null;
96
+ }
97
+ return Symbol(name);
87
98
  },
88
99
  Literal({ value }) {
89
100
  return value;
90
101
  },
91
102
  }[node.type](node));
92
103
  return compile(ast);
93
- } catch {
104
+ } catch (e) {
105
+ console.error(e);
94
106
  throw new Error(
95
107
  `Expression "${expression}" is too complicated, I do not understand`
96
108
  );
@@ -98,8 +110,8 @@ function jsexprToWhere(expression) {
98
110
  }
99
111
 
100
112
  /**
101
- * @param {string} expression
102
- * @param {object[]} statefuns
113
+ * @param {string} expression
114
+ * @param {object[]} statefuns
103
115
  * @returns {object}
104
116
  */
105
117
  function transform_for_async(expression, statefuns) {
@@ -125,8 +137,8 @@ function transform_for_async(expression, statefuns) {
125
137
  }
126
138
 
127
139
  /**
128
- * @param {string} expression
129
- * @param {object[]} fields
140
+ * @param {string} expression
141
+ * @param {object[]} fields
130
142
  * @returns {any}
131
143
  */
132
144
  function get_expression_function(expression, fields) {
@@ -142,8 +154,8 @@ function get_expression_function(expression, fields) {
142
154
  }
143
155
 
144
156
  /**
145
- * @param {string} expression
146
- * @param {object[]} fields
157
+ * @param {string} expression
158
+ * @param {object[]} fields
147
159
  * @param {object} [extraContext = {}]
148
160
  * @returns {any}
149
161
  */
@@ -162,9 +174,9 @@ function get_async_expression_function(expression, fields, extraContext = {}) {
162
174
  }
163
175
 
164
176
  /**
165
- * @param {object[]} rows
166
- * @param {object[]} fields
167
- * @returns {object[]}
177
+ * @param {object[]} rows
178
+ * @param {object[]} fields
179
+ * @returns {object[]}
168
180
  */
169
181
  function apply_calculated_fields(rows, fields) {
170
182
  let hasExprs = false;
@@ -196,8 +208,8 @@ function apply_calculated_fields(rows, fields) {
196
208
  }
197
209
 
198
210
  /**
199
- * @param {*} row
200
- * @param {*} fields
211
+ * @param {*} row
212
+ * @param {*} fields
201
213
  * @returns {Promise<any>}
202
214
  */
203
215
  const apply_calculated_fields_stored = async (row, fields) => {
package/models/field.js CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  const db = require("../db");
9
9
  const { contract, is } = require("contractis");
10
- const { recalculate_for_stored } = require("./expression");
10
+ const { recalculate_for_stored, jsexprToWhere } = require("./expression");
11
11
  const { sqlsanitize } = require("@saltcorn/db-common/internal.js");
12
12
  const { InvalidAdminAction } = require("../utils");
13
13
  const { mkWhere } = require("../db");
@@ -153,7 +153,13 @@ class Field {
153
153
  * @param {object} where
154
154
  * @returns {Promise<void>}
155
155
  */
156
- async fill_fkey_options(force_allow_none = false, where) {
156
+ async fill_fkey_options(force_allow_none = false, where0, extraCtx) {
157
+ const where =
158
+ where0 ||
159
+ (this.attributes.where
160
+ ? jsexprToWhere(this.attributes.where, extraCtx)
161
+ : undefined);
162
+ //console.log(where);
157
163
  if (
158
164
  this.is_fkey &&
159
165
  (this.type !== "File" ||
@@ -288,7 +294,7 @@ class Field {
288
294
  }
289
295
 
290
296
  /**
291
- * @param {object} whole_rec
297
+ * @param {object} whole_rec
292
298
  * @returns {object}
293
299
  */
294
300
  validate(whole_rec) {
@@ -316,8 +322,8 @@ class Field {
316
322
  }
317
323
 
318
324
  /**
319
- *
320
- * @param {object} where
325
+ *
326
+ * @param {object} where
321
327
  * @param {object} [selectopts]
322
328
  * @returns {Field[]}
323
329
  */
@@ -327,7 +333,7 @@ class Field {
327
333
  }
328
334
 
329
335
  /**
330
- * @param {object} where
336
+ * @param {object} where
331
337
  * @returns {Promise<Field>}
332
338
  */
333
339
  static async findOne(where) {
@@ -352,8 +358,8 @@ class Field {
352
358
  }
353
359
 
354
360
  /**
355
- *
356
- * @param {boolean} not_null
361
+ *
362
+ * @param {boolean} not_null
357
363
  * @returns {Promise<void>}
358
364
  */
359
365
  async toggle_not_null(not_null) {
@@ -370,7 +376,7 @@ class Field {
370
376
  }
371
377
 
372
378
  /**
373
- * @param {object} new_field
379
+ * @param {object} new_field
374
380
  * @returns {Promise<void>}
375
381
  */
376
382
  async alter_sql_type(new_field) {
@@ -421,7 +427,7 @@ class Field {
421
427
  }
422
428
 
423
429
  /**
424
- * @param {object} v
430
+ * @param {object} v
425
431
  * @returns {Promise<void>}
426
432
  */
427
433
  async update(v) {
@@ -523,7 +529,7 @@ class Field {
523
529
  }
524
530
 
525
531
  /**
526
- * @param {object} table
532
+ * @param {object} table
527
533
  * @returns {Promise<void>}
528
534
  */
529
535
  async enable_fkey_constraint(table) {
@@ -542,7 +548,7 @@ class Field {
542
548
  }
543
549
 
544
550
  /**
545
- * @param {object} fld
551
+ * @param {object} fld
546
552
  * @param {boolean} [bare = false]
547
553
  * @returns {Promise<Field>}
548
554
  */
package/models/file.js CHANGED
@@ -50,7 +50,14 @@ class File {
50
50
  * @param selectopts
51
51
  * @returns {Promise<*>}
52
52
  */
53
- static async find(where, selectopts) {
53
+ static async find(where, selectopts = {}) {
54
+ if (selectopts.cached) {
55
+ const { getState } = require("../db/state");
56
+ const files = Object.values(getState().files).sort((a, b) =>
57
+ a.filename > b.filename ? 1 : -1
58
+ );
59
+ return files.map((t) => new File(t));
60
+ }
54
61
  const db_flds = await db.select("_sc_files", where, selectopts);
55
62
  return db_flds.map((dbf) => new File(dbf));
56
63
  }
@@ -157,7 +164,7 @@ class File {
157
164
  return { error: e.message };
158
165
  }
159
166
  }
160
-
167
+
161
168
  /**
162
169
  * MIME type of the file
163
170
  * @type {string}
package/models/form.js CHANGED
@@ -14,9 +14,9 @@ const Field = require("./field");
14
14
  */
15
15
  class Form {
16
16
  /**
17
- * Constructor
18
- * @param o
19
- */
17
+ * Constructor
18
+ * @param o
19
+ */
20
20
  constructor(o) {
21
21
  this.fields = o.fields.map((f) =>
22
22
  f.constructor.name === Object.name ? new Field(f) : f
@@ -48,7 +48,7 @@ class Form {
48
48
  }
49
49
 
50
50
  /**
51
- * @param {object} ks
51
+ * @param {object} ks
52
52
  */
53
53
  hidden(...ks) {
54
54
  ks.forEach((k) => {
@@ -66,8 +66,9 @@ class Form {
66
66
  * @param {boolean} [force_allow_none = false]
67
67
  */
68
68
  async fill_fkey_options(force_allow_none = false) {
69
+ //console.log(this.values);
69
70
  for (const f of this.fields) {
70
- await f.fill_fkey_options(force_allow_none);
71
+ await f.fill_fkey_options(force_allow_none, undefined, this.values);
71
72
  }
72
73
  }
73
74
 
@@ -98,7 +99,7 @@ class Form {
98
99
  }
99
100
 
100
101
  /**
101
- * @param {*} v
102
+ * @param {*} v
102
103
  * @returns {object}
103
104
  */
104
105
  validate(v) {
package/models/page.js CHANGED
@@ -32,7 +32,7 @@ const {
32
32
  */
33
33
  class Page {
34
34
  /**
35
- * @param {object} o
35
+ * @param {object} o
36
36
  */
37
37
  constructor(o) {
38
38
  this.name = o.name;
@@ -56,6 +56,10 @@ class Page {
56
56
  * @returns {Promise<*>}
57
57
  */
58
58
  static async find(where, selectopts = { orderBy: "name", nocase: true }) {
59
+ if (selectopts.cached) {
60
+ const { getState } = require("../db/state");
61
+ return getState().pages.map((t) => new Page(t));
62
+ }
59
63
  const db_flds = await db.select("_sc_pages", where, selectopts);
60
64
  return db_flds.map((dbf) => new Page(dbf));
61
65
  }
package/models/table.js CHANGED
@@ -5,7 +5,11 @@
5
5
  * @subcategory models
6
6
  */
7
7
  const db = require("../db");
8
- const { sqlsanitize, mkWhere, mkSelectOptions } = require("@saltcorn/db-common/internal.js");
8
+ const {
9
+ sqlsanitize,
10
+ mkWhere,
11
+ mkSelectOptions,
12
+ } = require("@saltcorn/db-common/internal.js");
9
13
  const Field = require("./field");
10
14
  const Trigger = require("./trigger");
11
15
  const {
@@ -87,7 +91,7 @@ const normalise_error_message = (msg) =>
87
91
  class Table {
88
92
  /**
89
93
  * Table constructor
90
- * @param {object} o
94
+ * @param {object} o
91
95
  */
92
96
  constructor(o) {
93
97
  this.name = o.name;
@@ -141,6 +145,10 @@ class Table {
141
145
  * @returns {Promise<Table[]>} table list
142
146
  */
143
147
  static async find(where, selectopts = { orderBy: "name", nocase: true }) {
148
+ if (selectopts.cached) {
149
+ const { getState } = require("../db/state");
150
+ return getState().tables.map((t) => new Table(t));
151
+ }
144
152
  const tbls = await db.select("_sc_tables", where, selectopts);
145
153
 
146
154
  return tbls.map((t) => new Table(t));
package/models/view.js CHANGED
@@ -28,7 +28,7 @@ const { renderForm } = require("@saltcorn/markup");
28
28
  class View {
29
29
  /**
30
30
  * View constructor
31
- * @param {object} o
31
+ * @param {object} o
32
32
  */
33
33
  constructor(o) {
34
34
  this.name = o.name;
@@ -55,7 +55,7 @@ class View {
55
55
  }
56
56
 
57
57
  /**
58
- * @param {object} where
58
+ * @param {object} where
59
59
  * @returns {View}
60
60
  */
61
61
  static findOne(where) {
@@ -78,6 +78,10 @@ class View {
78
78
  * @returns {Promise<View[]>}
79
79
  */
80
80
  static async find(where, selectopts = { orderBy: "name", nocase: true }) {
81
+ if (selectopts.cached) {
82
+ const { getState } = require("../db/state");
83
+ return getState().views.map((t) => new View(t));
84
+ }
81
85
  const views = await db.select("_sc_views", where, selectopts);
82
86
 
83
87
  return views.map((v) => new View(v));
@@ -159,7 +163,7 @@ class View {
159
163
  }
160
164
 
161
165
  /**
162
- * @param {function} pred
166
+ * @param {function} pred
163
167
  * @returns {Promise<object>}
164
168
  */
165
169
  static async find_all_views_where(pred) {
@@ -182,7 +186,7 @@ class View {
182
186
  }
183
187
 
184
188
  /**
185
- * @param {Table|object} table
189
+ * @param {Table|object} table
186
190
  * @returns {Promise<View[]>}
187
191
  */
188
192
  static async find_possible_links_to_table(table) {
@@ -275,7 +279,7 @@ class View {
275
279
  }
276
280
 
277
281
  /**
278
- * @param {*} arg
282
+ * @param {*} arg
279
283
  * @returns {Promise<object>}
280
284
  */
281
285
  async authorise_post(arg) {
@@ -284,7 +288,7 @@ class View {
284
288
  }
285
289
 
286
290
  /**
287
- * @param {*} arg
291
+ * @param {*} arg
288
292
  * @returns {Promise<object>}
289
293
  */
290
294
  async authorise_get(arg) {
@@ -334,9 +338,9 @@ class View {
334
338
  }
335
339
 
336
340
  /**
337
- * @param {*} query
338
- * @param {*} req
339
- * @param {*} res
341
+ * @param {*} query
342
+ * @param {*} req
343
+ * @param {*} res
340
344
  * @returns {Promise<object>}
341
345
  */
342
346
  async run_possibly_on_page(query, req, res) {
@@ -361,8 +365,8 @@ class View {
361
365
  }
362
366
 
363
367
  /**
364
- * @param {*} query
365
- * @param {*} extraArgs
368
+ * @param {*} query
369
+ * @param {*} extraArgs
366
370
  * @throws {InvalidConfiguration}
367
371
  * @returns {Promise<object>}
368
372
  */
@@ -405,9 +409,9 @@ class View {
405
409
  }
406
410
 
407
411
  /**
408
- * @param {*} query
409
- * @param {*} body
410
- * @param {*} extraArgs
412
+ * @param {*} query
413
+ * @param {*} body
414
+ * @param {*} extraArgs
411
415
  * @returns {Promise<object>}
412
416
  */
413
417
  async runPost(query, body, extraArgs) {
@@ -423,10 +427,10 @@ class View {
423
427
  }
424
428
 
425
429
  /**
426
- * @param {*} route
427
- * @param {*} body
428
- * @param {*} res
429
- * @param {*} extraArgs
430
+ * @param {*} route
431
+ * @param {*} body
432
+ * @param {*} res
433
+ * @param {*} extraArgs
430
434
  * @returns {Promise<void>}
431
435
  */
432
436
  async runRoute(route, body, res, extraArgs) {
@@ -444,7 +448,7 @@ class View {
444
448
  }
445
449
 
446
450
  /**
447
- * @param {object} req_query
451
+ * @param {object} req_query
448
452
  * @returns {object}
449
453
  */
450
454
  combine_state_and_default_state(req_query) {
@@ -463,8 +467,8 @@ class View {
463
467
  }
464
468
 
465
469
  /**
466
- * @param {object} query
467
- * @param {object} req
470
+ * @param {object} query
471
+ * @param {object} req
468
472
  * @returns {Promise<Form|null>}
469
473
  */
470
474
  async get_state_form(query, req) {
@@ -501,7 +505,7 @@ class View {
501
505
  }
502
506
 
503
507
  /**
504
- * @param {object} req
508
+ * @param {object} req
505
509
  * @returns {Promise<object>}
506
510
  */
507
511
  async get_config_flow(req) {