@domql/utils 3.6.4 → 3.6.7
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/LICENSE +25 -12
- package/dist/cjs/keys.js +6 -1
- package/dist/cjs/methods.js +226 -0
- package/dist/cjs/state.js +2 -5
- package/dist/esm/keys.js +6 -1
- package/dist/esm/methods.js +216 -0
- package/dist/esm/state.js +2 -7
- package/dist/iife/index.js +1738 -14
- package/keys.js +6 -1
- package/methods.js +233 -0
- package/package.json +10 -2
- package/state.js +3 -8
package/LICENSE
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
Creative Commons Attribution-NonCommercial 4.0 International License
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2023 symbo.ls
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
5
|
+
This work is licensed under the Creative Commons Attribution-NonCommercial
|
|
6
|
+
4.0 International License. To view a copy of this license, visit
|
|
7
|
+
https://creativecommons.org/licenses/by-nc/4.0/ or send a letter to
|
|
8
|
+
Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
copies or substantial portions of the Software.
|
|
10
|
+
You are free to:
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
Share — copy and redistribute the material in any medium or format
|
|
13
|
+
Adapt — remix, transform, and build upon the material
|
|
14
|
+
|
|
15
|
+
Under the following terms:
|
|
16
|
+
|
|
17
|
+
Attribution — You must give appropriate credit, provide a link to the
|
|
18
|
+
license, and indicate if changes were made. You may do so in any
|
|
19
|
+
reasonable manner, but not in any way that suggests the licensor endorses
|
|
20
|
+
you or your use.
|
|
21
|
+
|
|
22
|
+
NonCommercial — You may not use the material for commercial purposes.
|
|
23
|
+
|
|
24
|
+
No additional restrictions — You may not apply legal terms or
|
|
25
|
+
technological measures that legally restrict others from doing anything
|
|
26
|
+
the license permits.
|
|
27
|
+
|
|
28
|
+
THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
29
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
30
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
31
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
32
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE
|
|
21
|
-
|
|
33
|
+
OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN THE
|
|
34
|
+
WORK.
|
package/dist/cjs/keys.js
CHANGED
|
@@ -63,6 +63,7 @@ const DOMQ_PROPERTIES = /* @__PURE__ */ new Set([
|
|
|
63
63
|
"node",
|
|
64
64
|
"variables",
|
|
65
65
|
"on",
|
|
66
|
+
"fetch",
|
|
66
67
|
"component",
|
|
67
68
|
"context"
|
|
68
69
|
]);
|
|
@@ -161,6 +162,8 @@ const METHODS = /* @__PURE__ */ new Set([
|
|
|
161
162
|
"getRootData",
|
|
162
163
|
"getRootContext",
|
|
163
164
|
"getContext",
|
|
165
|
+
"getQuery",
|
|
166
|
+
"getDB",
|
|
164
167
|
"getChildren"
|
|
165
168
|
]);
|
|
166
169
|
const METHODS_EXL = /* @__PURE__ */ new Set([
|
|
@@ -188,5 +191,7 @@ const DOMQL_EVENTS = /* @__PURE__ */ new Set([
|
|
|
188
191
|
"create",
|
|
189
192
|
"complete",
|
|
190
193
|
"frame",
|
|
191
|
-
"update"
|
|
194
|
+
"update",
|
|
195
|
+
"fetchError",
|
|
196
|
+
"fetchComplete"
|
|
192
197
|
]);
|
package/dist/cjs/methods.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
var methods_exports = {};
|
|
20
30
|
__export(methods_exports, {
|
|
@@ -24,7 +34,9 @@ __export(methods_exports, {
|
|
|
24
34
|
get: () => get,
|
|
25
35
|
getChildren: () => getChildren,
|
|
26
36
|
getContext: () => getContext,
|
|
37
|
+
getDB: () => getDB,
|
|
27
38
|
getPath: () => getPath,
|
|
39
|
+
getQuery: () => getQuery,
|
|
28
40
|
getRef: () => getRef,
|
|
29
41
|
getRoot: () => getRoot,
|
|
30
42
|
getRootContext: () => getRootContext,
|
|
@@ -55,6 +67,7 @@ var import_types = require("./types.js");
|
|
|
55
67
|
var import_object = require("./object.js");
|
|
56
68
|
var import_env = require("./env.js");
|
|
57
69
|
var import_array = require("./array.js");
|
|
70
|
+
var import_cache = require("./cache.js");
|
|
58
71
|
const ENV = process.env.NODE_ENV;
|
|
59
72
|
function spotByPath(path) {
|
|
60
73
|
const element = this;
|
|
@@ -368,6 +381,219 @@ function call(fnKey, ...args) {
|
|
|
368
381
|
if (context?.strictMode) throw err;
|
|
369
382
|
}
|
|
370
383
|
}
|
|
384
|
+
async function getDB() {
|
|
385
|
+
const element = this;
|
|
386
|
+
const db = element.context?.db;
|
|
387
|
+
if (!db) return null;
|
|
388
|
+
if (typeof db.select === "function") {
|
|
389
|
+
if (!db.__authInitialized && db.getSession) {
|
|
390
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
391
|
+
await initAdapterAuth(db, element.context);
|
|
392
|
+
}
|
|
393
|
+
return db;
|
|
394
|
+
}
|
|
395
|
+
if (db.__resolved) {
|
|
396
|
+
if (!db.__resolved.__authInitialized && db.__resolved.getSession) {
|
|
397
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
398
|
+
await initAdapterAuth(db.__resolved, element.context);
|
|
399
|
+
}
|
|
400
|
+
return db.__resolved;
|
|
401
|
+
}
|
|
402
|
+
if (db.__resolving) return db.__resolving;
|
|
403
|
+
const resolvePromise = import("@symbo.ls/fetch").then(({ resolveDb }) => resolveDb(db));
|
|
404
|
+
db.__resolving = resolvePromise;
|
|
405
|
+
const resolved = await resolvePromise;
|
|
406
|
+
db.__resolved = resolved;
|
|
407
|
+
element.context.db = resolved;
|
|
408
|
+
delete db.__resolving;
|
|
409
|
+
if (resolved.getSession) {
|
|
410
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
411
|
+
await initAdapterAuth(resolved, element.context);
|
|
412
|
+
}
|
|
413
|
+
return resolved;
|
|
414
|
+
}
|
|
415
|
+
function getQuery(format) {
|
|
416
|
+
const element = this;
|
|
417
|
+
const useStateQuery = format || import_cache.OPTIONS.useStateQuery || element.context?.useStateQuery;
|
|
418
|
+
if (!useStateQuery) return null;
|
|
419
|
+
const query = {};
|
|
420
|
+
buildQueryFromElement(element, query);
|
|
421
|
+
if (useStateQuery === true) return query;
|
|
422
|
+
const formatter = QUERY_FORMATTERS[useStateQuery];
|
|
423
|
+
return formatter ? formatter(query) : query;
|
|
424
|
+
}
|
|
425
|
+
const buildQueryFromElement = (element, query) => {
|
|
426
|
+
const ref = element.__ref;
|
|
427
|
+
if (!ref) return;
|
|
428
|
+
const stateKey = ref.__state;
|
|
429
|
+
if (stateKey && typeof stateKey === "string") {
|
|
430
|
+
setQueryPath(query, stateKey);
|
|
431
|
+
}
|
|
432
|
+
const children = ref.__children;
|
|
433
|
+
if (children) {
|
|
434
|
+
for (let i = 0; i < children.length; i++) {
|
|
435
|
+
const child = element[children[i]];
|
|
436
|
+
if (child && child.__ref) {
|
|
437
|
+
buildQueryFromElement(child, query);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const contentKey = ref.contentElementKey || "content";
|
|
442
|
+
const content = element[contentKey];
|
|
443
|
+
if (content && content.__ref) {
|
|
444
|
+
buildQueryFromElement(content, query);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
const setQueryPath = (query, path) => {
|
|
448
|
+
const clean = path.replaceAll("../", "").replaceAll("~/", "");
|
|
449
|
+
const parts = clean.split("/");
|
|
450
|
+
let current = query;
|
|
451
|
+
for (let i = 0; i < parts.length; i++) {
|
|
452
|
+
const part = parts[i];
|
|
453
|
+
if (i === parts.length - 1) {
|
|
454
|
+
if (!current[part]) current[part] = true;
|
|
455
|
+
} else {
|
|
456
|
+
if (current[part] === true) current[part] = {};
|
|
457
|
+
if (!current[part]) current[part] = {};
|
|
458
|
+
current = current[part];
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
const queryToGraphQL = (query, indent = 0) => {
|
|
463
|
+
const pad = " ".repeat(indent);
|
|
464
|
+
const entries = Object.entries(query);
|
|
465
|
+
if (!entries.length) return "";
|
|
466
|
+
const fields = entries.map(([key, value]) => {
|
|
467
|
+
if (value === true) return `${pad}${key}`;
|
|
468
|
+
return `${pad}${key} {
|
|
469
|
+
${queryToGraphQL(value, indent + 1)}
|
|
470
|
+
${pad}}`;
|
|
471
|
+
});
|
|
472
|
+
return fields.join("\n");
|
|
473
|
+
};
|
|
474
|
+
const formatGraphQL = (query) => {
|
|
475
|
+
return `{
|
|
476
|
+
${queryToGraphQL(query, 1)}
|
|
477
|
+
}`;
|
|
478
|
+
};
|
|
479
|
+
const formatJsonApi = (query) => {
|
|
480
|
+
const result = { fields: {}, include: [] };
|
|
481
|
+
const walk = (obj, prefix) => {
|
|
482
|
+
const fields = [];
|
|
483
|
+
for (const key in obj) {
|
|
484
|
+
if (obj[key] === true) {
|
|
485
|
+
fields.push(key);
|
|
486
|
+
} else {
|
|
487
|
+
const nested = prefix ? `${prefix}.${key}` : key;
|
|
488
|
+
result.include.push(nested);
|
|
489
|
+
walk(obj[key], nested);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (fields.length) {
|
|
493
|
+
result.fields[prefix || "_root"] = fields;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
walk(query, "");
|
|
497
|
+
return result;
|
|
498
|
+
};
|
|
499
|
+
const formatOData = (query) => {
|
|
500
|
+
const build = (obj) => {
|
|
501
|
+
const select2 = [];
|
|
502
|
+
const expand2 = [];
|
|
503
|
+
for (const key in obj) {
|
|
504
|
+
if (obj[key] === true) {
|
|
505
|
+
select2.push(key);
|
|
506
|
+
} else {
|
|
507
|
+
const nested = build(obj[key]);
|
|
508
|
+
const parts2 = [];
|
|
509
|
+
if (nested.select) parts2.push(`$select=${nested.select}`);
|
|
510
|
+
if (nested.expand) parts2.push(`$expand=${nested.expand}`);
|
|
511
|
+
expand2.push(parts2.length ? `${key}(${parts2.join(";")})` : key);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
select: select2.length ? select2.join(",") : "",
|
|
516
|
+
expand: expand2.length ? expand2.join(",") : ""
|
|
517
|
+
};
|
|
518
|
+
};
|
|
519
|
+
const { select, expand } = build(query);
|
|
520
|
+
const parts = [];
|
|
521
|
+
if (select) parts.push(`$select=${select}`);
|
|
522
|
+
if (expand) parts.push(`$expand=${expand}`);
|
|
523
|
+
return parts.join("&");
|
|
524
|
+
};
|
|
525
|
+
const formatSQL = (query, options) => {
|
|
526
|
+
const columns = [];
|
|
527
|
+
const joins = [];
|
|
528
|
+
const tables = /* @__PURE__ */ new Set();
|
|
529
|
+
const walk = (obj, table, parentTable) => {
|
|
530
|
+
tables.add(table);
|
|
531
|
+
if (parentTable) {
|
|
532
|
+
joins.push(
|
|
533
|
+
`LEFT JOIN ${table} ON ${table}.${parentTable}_id = ${parentTable}.id`
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
for (const key in obj) {
|
|
537
|
+
if (obj[key] === true) {
|
|
538
|
+
columns.push(`${table}.${key}`);
|
|
539
|
+
} else {
|
|
540
|
+
walk(obj[key], key, table);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
const roots = Object.keys(query);
|
|
545
|
+
if (roots.length === 1 && query[roots[0]] !== true) {
|
|
546
|
+
walk(query[roots[0]], roots[0], null);
|
|
547
|
+
} else {
|
|
548
|
+
walk(query, "_root", null);
|
|
549
|
+
}
|
|
550
|
+
const from = [...tables][0] || "_root";
|
|
551
|
+
const select = columns.length ? columns.join(", ") : "*";
|
|
552
|
+
const joinStr = joins.length ? "\n" + joins.join("\n") : "";
|
|
553
|
+
return `SELECT ${select}
|
|
554
|
+
FROM ${from}${joinStr}`;
|
|
555
|
+
};
|
|
556
|
+
const formatPaths = (query) => {
|
|
557
|
+
const paths = [];
|
|
558
|
+
const walk = (obj, prefix) => {
|
|
559
|
+
for (const key in obj) {
|
|
560
|
+
const path = prefix ? `${prefix}/${key}` : key;
|
|
561
|
+
if (obj[key] === true) {
|
|
562
|
+
paths.push(path);
|
|
563
|
+
} else {
|
|
564
|
+
walk(obj[key], path);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
walk(query, "");
|
|
569
|
+
return paths;
|
|
570
|
+
};
|
|
571
|
+
const formatSupabase = (query) => {
|
|
572
|
+
const build = (obj) => {
|
|
573
|
+
const parts = [];
|
|
574
|
+
for (const key in obj) {
|
|
575
|
+
if (obj[key] === true) {
|
|
576
|
+
parts.push(key);
|
|
577
|
+
} else {
|
|
578
|
+
parts.push(`${key}(${build(obj[key])})`);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return parts.join(", ");
|
|
582
|
+
};
|
|
583
|
+
const roots = Object.keys(query);
|
|
584
|
+
if (roots.length === 1 && query[roots[0]] !== true) {
|
|
585
|
+
return { from: roots[0], select: build(query[roots[0]]) };
|
|
586
|
+
}
|
|
587
|
+
return { from: null, select: build(query) };
|
|
588
|
+
};
|
|
589
|
+
const QUERY_FORMATTERS = {
|
|
590
|
+
graphql: formatGraphQL,
|
|
591
|
+
"json-api": formatJsonApi,
|
|
592
|
+
odata: formatOData,
|
|
593
|
+
sql: formatSQL,
|
|
594
|
+
supabase: formatSupabase,
|
|
595
|
+
paths: formatPaths
|
|
596
|
+
};
|
|
371
597
|
function isMethod(param, element) {
|
|
372
598
|
return Boolean(import_keys.METHODS.has(param) || element?.context?.methods?.[param]);
|
|
373
599
|
}
|
package/dist/cjs/state.js
CHANGED
|
@@ -38,10 +38,7 @@ var import_types = require("./types.js");
|
|
|
38
38
|
const checkForStateTypes = (element) => {
|
|
39
39
|
const { state: orig, props, __ref: ref } = element;
|
|
40
40
|
const state = props?.state || orig;
|
|
41
|
-
if ((0, import_types.
|
|
42
|
-
ref.__state = state;
|
|
43
|
-
return (0, import_object.exec)(state, element);
|
|
44
|
-
} else if ((0, import_types.is)(state)("string", "number")) {
|
|
41
|
+
if ((0, import_types.is)(state)("string", "number")) {
|
|
45
42
|
ref.__state = state;
|
|
46
43
|
return { value: state };
|
|
47
44
|
} else if (state === true) {
|
|
@@ -144,7 +141,7 @@ const createNestedObjectByKeyPath = (path, value) => {
|
|
|
144
141
|
};
|
|
145
142
|
const applyDependentState = (element, state) => {
|
|
146
143
|
const { __element } = state;
|
|
147
|
-
const origState =
|
|
144
|
+
const origState = __element?.state;
|
|
148
145
|
if (!origState) return;
|
|
149
146
|
const dependentState = (0, import_object.deepClone)(origState, import_keys.STATE_METHODS);
|
|
150
147
|
const newDepends = { [element.key]: dependentState };
|
package/dist/esm/keys.js
CHANGED
|
@@ -33,6 +33,7 @@ const DOMQ_PROPERTIES = /* @__PURE__ */ new Set([
|
|
|
33
33
|
"node",
|
|
34
34
|
"variables",
|
|
35
35
|
"on",
|
|
36
|
+
"fetch",
|
|
36
37
|
"component",
|
|
37
38
|
"context"
|
|
38
39
|
]);
|
|
@@ -131,6 +132,8 @@ const METHODS = /* @__PURE__ */ new Set([
|
|
|
131
132
|
"getRootData",
|
|
132
133
|
"getRootContext",
|
|
133
134
|
"getContext",
|
|
135
|
+
"getQuery",
|
|
136
|
+
"getDB",
|
|
134
137
|
"getChildren"
|
|
135
138
|
]);
|
|
136
139
|
const METHODS_EXL = /* @__PURE__ */ new Set([
|
|
@@ -158,7 +161,9 @@ const DOMQL_EVENTS = /* @__PURE__ */ new Set([
|
|
|
158
161
|
"create",
|
|
159
162
|
"complete",
|
|
160
163
|
"frame",
|
|
161
|
-
"update"
|
|
164
|
+
"update",
|
|
165
|
+
"fetchError",
|
|
166
|
+
"fetchComplete"
|
|
162
167
|
]);
|
|
163
168
|
export {
|
|
164
169
|
DOMQL_EVENTS,
|
package/dist/esm/methods.js
CHANGED
|
@@ -4,6 +4,7 @@ import { isDefined, isFunction, isObject, isObjectLike } from "./types.js";
|
|
|
4
4
|
import { deepClone } from "./object.js";
|
|
5
5
|
import { isProduction } from "./env.js";
|
|
6
6
|
import { removeValueFromArray } from "./array.js";
|
|
7
|
+
import { OPTIONS } from "./cache.js";
|
|
7
8
|
const ENV = process.env.NODE_ENV;
|
|
8
9
|
function spotByPath(path) {
|
|
9
10
|
const element = this;
|
|
@@ -317,6 +318,219 @@ function call(fnKey, ...args) {
|
|
|
317
318
|
if (context?.strictMode) throw err;
|
|
318
319
|
}
|
|
319
320
|
}
|
|
321
|
+
async function getDB() {
|
|
322
|
+
const element = this;
|
|
323
|
+
const db = element.context?.db;
|
|
324
|
+
if (!db) return null;
|
|
325
|
+
if (typeof db.select === "function") {
|
|
326
|
+
if (!db.__authInitialized && db.getSession) {
|
|
327
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
328
|
+
await initAdapterAuth(db, element.context);
|
|
329
|
+
}
|
|
330
|
+
return db;
|
|
331
|
+
}
|
|
332
|
+
if (db.__resolved) {
|
|
333
|
+
if (!db.__resolved.__authInitialized && db.__resolved.getSession) {
|
|
334
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
335
|
+
await initAdapterAuth(db.__resolved, element.context);
|
|
336
|
+
}
|
|
337
|
+
return db.__resolved;
|
|
338
|
+
}
|
|
339
|
+
if (db.__resolving) return db.__resolving;
|
|
340
|
+
const resolvePromise = import("@symbo.ls/fetch").then(({ resolveDb }) => resolveDb(db));
|
|
341
|
+
db.__resolving = resolvePromise;
|
|
342
|
+
const resolved = await resolvePromise;
|
|
343
|
+
db.__resolved = resolved;
|
|
344
|
+
element.context.db = resolved;
|
|
345
|
+
delete db.__resolving;
|
|
346
|
+
if (resolved.getSession) {
|
|
347
|
+
const { initAdapterAuth } = await import("@symbo.ls/fetch");
|
|
348
|
+
await initAdapterAuth(resolved, element.context);
|
|
349
|
+
}
|
|
350
|
+
return resolved;
|
|
351
|
+
}
|
|
352
|
+
function getQuery(format) {
|
|
353
|
+
const element = this;
|
|
354
|
+
const useStateQuery = format || OPTIONS.useStateQuery || element.context?.useStateQuery;
|
|
355
|
+
if (!useStateQuery) return null;
|
|
356
|
+
const query = {};
|
|
357
|
+
buildQueryFromElement(element, query);
|
|
358
|
+
if (useStateQuery === true) return query;
|
|
359
|
+
const formatter = QUERY_FORMATTERS[useStateQuery];
|
|
360
|
+
return formatter ? formatter(query) : query;
|
|
361
|
+
}
|
|
362
|
+
const buildQueryFromElement = (element, query) => {
|
|
363
|
+
const ref = element.__ref;
|
|
364
|
+
if (!ref) return;
|
|
365
|
+
const stateKey = ref.__state;
|
|
366
|
+
if (stateKey && typeof stateKey === "string") {
|
|
367
|
+
setQueryPath(query, stateKey);
|
|
368
|
+
}
|
|
369
|
+
const children = ref.__children;
|
|
370
|
+
if (children) {
|
|
371
|
+
for (let i = 0; i < children.length; i++) {
|
|
372
|
+
const child = element[children[i]];
|
|
373
|
+
if (child && child.__ref) {
|
|
374
|
+
buildQueryFromElement(child, query);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const contentKey = ref.contentElementKey || "content";
|
|
379
|
+
const content = element[contentKey];
|
|
380
|
+
if (content && content.__ref) {
|
|
381
|
+
buildQueryFromElement(content, query);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
const setQueryPath = (query, path) => {
|
|
385
|
+
const clean = path.replaceAll("../", "").replaceAll("~/", "");
|
|
386
|
+
const parts = clean.split("/");
|
|
387
|
+
let current = query;
|
|
388
|
+
for (let i = 0; i < parts.length; i++) {
|
|
389
|
+
const part = parts[i];
|
|
390
|
+
if (i === parts.length - 1) {
|
|
391
|
+
if (!current[part]) current[part] = true;
|
|
392
|
+
} else {
|
|
393
|
+
if (current[part] === true) current[part] = {};
|
|
394
|
+
if (!current[part]) current[part] = {};
|
|
395
|
+
current = current[part];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
const queryToGraphQL = (query, indent = 0) => {
|
|
400
|
+
const pad = " ".repeat(indent);
|
|
401
|
+
const entries = Object.entries(query);
|
|
402
|
+
if (!entries.length) return "";
|
|
403
|
+
const fields = entries.map(([key, value]) => {
|
|
404
|
+
if (value === true) return `${pad}${key}`;
|
|
405
|
+
return `${pad}${key} {
|
|
406
|
+
${queryToGraphQL(value, indent + 1)}
|
|
407
|
+
${pad}}`;
|
|
408
|
+
});
|
|
409
|
+
return fields.join("\n");
|
|
410
|
+
};
|
|
411
|
+
const formatGraphQL = (query) => {
|
|
412
|
+
return `{
|
|
413
|
+
${queryToGraphQL(query, 1)}
|
|
414
|
+
}`;
|
|
415
|
+
};
|
|
416
|
+
const formatJsonApi = (query) => {
|
|
417
|
+
const result = { fields: {}, include: [] };
|
|
418
|
+
const walk = (obj, prefix) => {
|
|
419
|
+
const fields = [];
|
|
420
|
+
for (const key in obj) {
|
|
421
|
+
if (obj[key] === true) {
|
|
422
|
+
fields.push(key);
|
|
423
|
+
} else {
|
|
424
|
+
const nested = prefix ? `${prefix}.${key}` : key;
|
|
425
|
+
result.include.push(nested);
|
|
426
|
+
walk(obj[key], nested);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (fields.length) {
|
|
430
|
+
result.fields[prefix || "_root"] = fields;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
walk(query, "");
|
|
434
|
+
return result;
|
|
435
|
+
};
|
|
436
|
+
const formatOData = (query) => {
|
|
437
|
+
const build = (obj) => {
|
|
438
|
+
const select2 = [];
|
|
439
|
+
const expand2 = [];
|
|
440
|
+
for (const key in obj) {
|
|
441
|
+
if (obj[key] === true) {
|
|
442
|
+
select2.push(key);
|
|
443
|
+
} else {
|
|
444
|
+
const nested = build(obj[key]);
|
|
445
|
+
const parts2 = [];
|
|
446
|
+
if (nested.select) parts2.push(`$select=${nested.select}`);
|
|
447
|
+
if (nested.expand) parts2.push(`$expand=${nested.expand}`);
|
|
448
|
+
expand2.push(parts2.length ? `${key}(${parts2.join(";")})` : key);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
select: select2.length ? select2.join(",") : "",
|
|
453
|
+
expand: expand2.length ? expand2.join(",") : ""
|
|
454
|
+
};
|
|
455
|
+
};
|
|
456
|
+
const { select, expand } = build(query);
|
|
457
|
+
const parts = [];
|
|
458
|
+
if (select) parts.push(`$select=${select}`);
|
|
459
|
+
if (expand) parts.push(`$expand=${expand}`);
|
|
460
|
+
return parts.join("&");
|
|
461
|
+
};
|
|
462
|
+
const formatSQL = (query, options) => {
|
|
463
|
+
const columns = [];
|
|
464
|
+
const joins = [];
|
|
465
|
+
const tables = /* @__PURE__ */ new Set();
|
|
466
|
+
const walk = (obj, table, parentTable) => {
|
|
467
|
+
tables.add(table);
|
|
468
|
+
if (parentTable) {
|
|
469
|
+
joins.push(
|
|
470
|
+
`LEFT JOIN ${table} ON ${table}.${parentTable}_id = ${parentTable}.id`
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
for (const key in obj) {
|
|
474
|
+
if (obj[key] === true) {
|
|
475
|
+
columns.push(`${table}.${key}`);
|
|
476
|
+
} else {
|
|
477
|
+
walk(obj[key], key, table);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
const roots = Object.keys(query);
|
|
482
|
+
if (roots.length === 1 && query[roots[0]] !== true) {
|
|
483
|
+
walk(query[roots[0]], roots[0], null);
|
|
484
|
+
} else {
|
|
485
|
+
walk(query, "_root", null);
|
|
486
|
+
}
|
|
487
|
+
const from = [...tables][0] || "_root";
|
|
488
|
+
const select = columns.length ? columns.join(", ") : "*";
|
|
489
|
+
const joinStr = joins.length ? "\n" + joins.join("\n") : "";
|
|
490
|
+
return `SELECT ${select}
|
|
491
|
+
FROM ${from}${joinStr}`;
|
|
492
|
+
};
|
|
493
|
+
const formatPaths = (query) => {
|
|
494
|
+
const paths = [];
|
|
495
|
+
const walk = (obj, prefix) => {
|
|
496
|
+
for (const key in obj) {
|
|
497
|
+
const path = prefix ? `${prefix}/${key}` : key;
|
|
498
|
+
if (obj[key] === true) {
|
|
499
|
+
paths.push(path);
|
|
500
|
+
} else {
|
|
501
|
+
walk(obj[key], path);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
walk(query, "");
|
|
506
|
+
return paths;
|
|
507
|
+
};
|
|
508
|
+
const formatSupabase = (query) => {
|
|
509
|
+
const build = (obj) => {
|
|
510
|
+
const parts = [];
|
|
511
|
+
for (const key in obj) {
|
|
512
|
+
if (obj[key] === true) {
|
|
513
|
+
parts.push(key);
|
|
514
|
+
} else {
|
|
515
|
+
parts.push(`${key}(${build(obj[key])})`);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return parts.join(", ");
|
|
519
|
+
};
|
|
520
|
+
const roots = Object.keys(query);
|
|
521
|
+
if (roots.length === 1 && query[roots[0]] !== true) {
|
|
522
|
+
return { from: roots[0], select: build(query[roots[0]]) };
|
|
523
|
+
}
|
|
524
|
+
return { from: null, select: build(query) };
|
|
525
|
+
};
|
|
526
|
+
const QUERY_FORMATTERS = {
|
|
527
|
+
graphql: formatGraphQL,
|
|
528
|
+
"json-api": formatJsonApi,
|
|
529
|
+
odata: formatOData,
|
|
530
|
+
sql: formatSQL,
|
|
531
|
+
supabase: formatSupabase,
|
|
532
|
+
paths: formatPaths
|
|
533
|
+
};
|
|
320
534
|
function isMethod(param, element) {
|
|
321
535
|
return Boolean(METHODS.has(param) || element?.context?.methods?.[param]);
|
|
322
536
|
}
|
|
@@ -327,7 +541,9 @@ export {
|
|
|
327
541
|
get,
|
|
328
542
|
getChildren,
|
|
329
543
|
getContext,
|
|
544
|
+
getDB,
|
|
330
545
|
getPath,
|
|
546
|
+
getQuery,
|
|
331
547
|
getRef,
|
|
332
548
|
getRoot,
|
|
333
549
|
getRootContext,
|
package/dist/esm/state.js
CHANGED
|
@@ -3,13 +3,11 @@ import { STATE_METHODS } from "./keys.js";
|
|
|
3
3
|
import {
|
|
4
4
|
deepClone,
|
|
5
5
|
deepMerge,
|
|
6
|
-
exec,
|
|
7
6
|
overwriteDeep,
|
|
8
7
|
overwriteShallow
|
|
9
8
|
} from "./object.js";
|
|
10
9
|
import {
|
|
11
10
|
is,
|
|
12
|
-
isFunction,
|
|
13
11
|
isObject,
|
|
14
12
|
isObjectLike,
|
|
15
13
|
isString
|
|
@@ -17,10 +15,7 @@ import {
|
|
|
17
15
|
const checkForStateTypes = (element) => {
|
|
18
16
|
const { state: orig, props, __ref: ref } = element;
|
|
19
17
|
const state = props?.state || orig;
|
|
20
|
-
if (
|
|
21
|
-
ref.__state = state;
|
|
22
|
-
return exec(state, element);
|
|
23
|
-
} else if (is(state)("string", "number")) {
|
|
18
|
+
if (is(state)("string", "number")) {
|
|
24
19
|
ref.__state = state;
|
|
25
20
|
return { value: state };
|
|
26
21
|
} else if (state === true) {
|
|
@@ -123,7 +118,7 @@ const createNestedObjectByKeyPath = (path, value) => {
|
|
|
123
118
|
};
|
|
124
119
|
const applyDependentState = (element, state) => {
|
|
125
120
|
const { __element } = state;
|
|
126
|
-
const origState =
|
|
121
|
+
const origState = __element?.state;
|
|
127
122
|
if (!origState) return;
|
|
128
123
|
const dependentState = deepClone(origState, STATE_METHODS);
|
|
129
124
|
const newDepends = { [element.key]: dependentState };
|