@instantdb/core
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -1,2 +0,2 @@ | ||
export default function query(store: any, q: any, join: any): {}; | ||
export default function query(store: any, q: any): {}; | ||
//# sourceMappingURL=instaql.d.ts.map |
@@ -117,9 +117,9 @@ "use strict"; | ||
// ----------------- | ||
function makeJoin(store, ns, ck, eid) { | ||
const [_etype, pat] = refAttrPat(store, ns, ck); | ||
const actualized = replaceInAttrPat(pat, makeVar(ns), eid); | ||
return [actualized]; | ||
function makeJoin(store, etype, label, eid) { | ||
const [nextEtype, pat] = refAttrPat(store, etype, label); | ||
const actualized = replaceInAttrPat(pat, makeVar(etype), eid); | ||
return [nextEtype, actualized]; | ||
} | ||
function withJoin(where, join) { | ||
return join ? join.concat(where) : where; | ||
return join ? [join].concat(where) : where; | ||
} | ||
@@ -140,12 +140,11 @@ // Query | ||
} | ||
function queryParents(store, q, k, join) { | ||
function queryParents(store, { etype, form, join }) { | ||
var _a; | ||
const ns = getNamespace(k); | ||
const where = withJoin(makeWhere(store, ns, (_a = q[k].$) === null || _a === void 0 ? void 0 : _a.where), join); | ||
const find = makeFind(ns); | ||
const where = withJoin(makeWhere(store, etype, (_a = form.$) === null || _a === void 0 ? void 0 : _a.where), join); | ||
const find = makeFind(etype); | ||
return runDatalog(store, { where, find }); | ||
} | ||
function guaredQueryParents(store, q, k, join) { | ||
function guaredQueryParents(store, opts) { | ||
try { | ||
return queryParents(store, q, k, join); | ||
return queryParents(store, opts); | ||
} | ||
@@ -159,4 +158,4 @@ catch (e) { | ||
} | ||
function extendParents(store, q, k, parents) { | ||
const children = Object.keys(q[k]).filter((c) => c !== "$"); | ||
function extendParents(store, { etype, form }, parents) { | ||
const children = Object.keys(form).filter((c) => c !== "$"); | ||
if (!children.length) { | ||
@@ -170,12 +169,11 @@ return Object.values(parents); | ||
.map(([eid, parent]) => { | ||
const childResults = children.map((ck) => { | ||
const childResults = children.map((label) => { | ||
try { | ||
const ns = getNamespace(k); | ||
const join = makeJoin(store, ns, ck, eid); | ||
const child = query(store, { [ck]: q[k][ck] }, join); | ||
return child; | ||
const [nextEtype, join] = makeJoin(store, etype, label, eid); | ||
const child = queryOne(store, { etype: nextEtype, form: form[label], join }); | ||
return { [label]: child }; | ||
} | ||
catch (e) { | ||
if (e instanceof AttrNotFoundError) { | ||
return { [ck]: [] }; | ||
return { [label]: [] }; | ||
} | ||
@@ -190,6 +188,9 @@ throw e; | ||
} | ||
function query(store, q, join) { | ||
function queryOne(store, opts) { | ||
const parents = guaredQueryParents(store, opts); | ||
return extendParents(store, opts, parents); | ||
} | ||
function query(store, q) { | ||
return Object.keys(q).reduce((res, k) => { | ||
const parents = guaredQueryParents(store, q, k, join); | ||
res[k] = extendParents(store, q, k, parents); | ||
res[k] = queryOne(store, { etype: k, form: q[k] }); | ||
return res; | ||
@@ -196,0 +197,0 @@ }, {}); |
@@ -1,2 +0,2 @@ | ||
export default function query(store: any, q: any, join: any): {}; | ||
export default function query(store: any, q: any): {}; | ||
//# sourceMappingURL=instaql.d.ts.map |
@@ -115,9 +115,9 @@ import { query as datalogQuery } from "./datalog"; | ||
// ----------------- | ||
function makeJoin(store, ns, ck, eid) { | ||
const [_etype, pat] = refAttrPat(store, ns, ck); | ||
const actualized = replaceInAttrPat(pat, makeVar(ns), eid); | ||
return [actualized]; | ||
function makeJoin(store, etype, label, eid) { | ||
const [nextEtype, pat] = refAttrPat(store, etype, label); | ||
const actualized = replaceInAttrPat(pat, makeVar(etype), eid); | ||
return [nextEtype, actualized]; | ||
} | ||
function withJoin(where, join) { | ||
return join ? join.concat(where) : where; | ||
return join ? [join].concat(where) : where; | ||
} | ||
@@ -138,12 +138,11 @@ // Query | ||
} | ||
function queryParents(store, q, k, join) { | ||
function queryParents(store, { etype, form, join }) { | ||
var _a; | ||
const ns = getNamespace(k); | ||
const where = withJoin(makeWhere(store, ns, (_a = q[k].$) === null || _a === void 0 ? void 0 : _a.where), join); | ||
const find = makeFind(ns); | ||
const where = withJoin(makeWhere(store, etype, (_a = form.$) === null || _a === void 0 ? void 0 : _a.where), join); | ||
const find = makeFind(etype); | ||
return runDatalog(store, { where, find }); | ||
} | ||
function guaredQueryParents(store, q, k, join) { | ||
function guaredQueryParents(store, opts) { | ||
try { | ||
return queryParents(store, q, k, join); | ||
return queryParents(store, opts); | ||
} | ||
@@ -157,4 +156,4 @@ catch (e) { | ||
} | ||
function extendParents(store, q, k, parents) { | ||
const children = Object.keys(q[k]).filter((c) => c !== "$"); | ||
function extendParents(store, { etype, form }, parents) { | ||
const children = Object.keys(form).filter((c) => c !== "$"); | ||
if (!children.length) { | ||
@@ -168,12 +167,11 @@ return Object.values(parents); | ||
.map(([eid, parent]) => { | ||
const childResults = children.map((ck) => { | ||
const childResults = children.map((label) => { | ||
try { | ||
const ns = getNamespace(k); | ||
const join = makeJoin(store, ns, ck, eid); | ||
const child = query(store, { [ck]: q[k][ck] }, join); | ||
return child; | ||
const [nextEtype, join] = makeJoin(store, etype, label, eid); | ||
const child = queryOne(store, { etype: nextEtype, form: form[label], join }); | ||
return { [label]: child }; | ||
} | ||
catch (e) { | ||
if (e instanceof AttrNotFoundError) { | ||
return { [ck]: [] }; | ||
return { [label]: [] }; | ||
} | ||
@@ -188,6 +186,9 @@ throw e; | ||
} | ||
export default function query(store, q, join) { | ||
function queryOne(store, opts) { | ||
const parents = guaredQueryParents(store, opts); | ||
return extendParents(store, opts, parents); | ||
} | ||
export default function query(store, q) { | ||
return Object.keys(q).reduce((res, k) => { | ||
const parents = guaredQueryParents(store, q, k, join); | ||
res[k] = extendParents(store, q, k, parents); | ||
res[k] = queryOne(store, { etype: k, form: q[k] }); | ||
return res; | ||
@@ -194,0 +195,0 @@ }, {}); |
{ | ||
"name": "@instantdb/core", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Instant's core local abstraction", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -152,10 +152,10 @@ import { query as datalogQuery } from "./datalog"; | ||
function makeJoin(store, ns, ck, eid) { | ||
const [_etype, pat] = refAttrPat(store, ns, ck); | ||
const actualized = replaceInAttrPat(pat, makeVar(ns), eid); | ||
return [actualized]; | ||
function makeJoin(store, etype, label, eid) { | ||
const [nextEtype, pat] = refAttrPat(store, etype, label); | ||
const actualized = replaceInAttrPat(pat, makeVar(etype), eid); | ||
return [nextEtype, actualized]; | ||
} | ||
function withJoin(where, join) { | ||
return join ? join.concat(where) : where; | ||
return join ? [join].concat(where) : where; | ||
} | ||
@@ -179,12 +179,11 @@ | ||
function queryParents(store, q, k, join) { | ||
const ns = getNamespace(k); | ||
const where = withJoin(makeWhere(store, ns, q[k].$?.where), join); | ||
const find = makeFind(ns); | ||
function queryParents(store, { etype, form, join }) { | ||
const where = withJoin(makeWhere(store, etype, form.$?.where), join); | ||
const find = makeFind(etype); | ||
return runDatalog(store, { where, find }); | ||
} | ||
function guaredQueryParents(store, q, k, join) { | ||
function guaredQueryParents(store, opts) { | ||
try { | ||
return queryParents(store, q, k, join); | ||
return queryParents(store, opts); | ||
} catch (e) { | ||
@@ -198,4 +197,4 @@ if (e instanceof AttrNotFoundError) { | ||
function extendParents(store, q, k, parents) { | ||
const children = Object.keys(q[k]).filter((c) => c !== "$"); | ||
function extendParents(store, { etype, form }, parents) { | ||
const children = Object.keys(form).filter((c) => c !== "$"); | ||
if (!children.length) { | ||
@@ -210,11 +209,10 @@ return Object.values(parents); | ||
.map(([eid, parent]) => { | ||
const childResults = children.map((ck) => { | ||
const childResults = children.map((label) => { | ||
try { | ||
const ns = getNamespace(k); | ||
const join = makeJoin(store, ns, ck, eid); | ||
const child = query(store, { [ck]: q[k][ck] }, join); | ||
return child; | ||
const [nextEtype, join] = makeJoin(store, etype, label, eid); | ||
const child = queryOne(store, { etype: nextEtype, form: form[label], join }); | ||
return { [label]: child }; | ||
} catch (e) { | ||
if (e instanceof AttrNotFoundError) { | ||
return { [ck]: [] }; | ||
return { [label]: [] }; | ||
} | ||
@@ -231,8 +229,12 @@ throw e; | ||
export default function query(store, q, join) { | ||
function queryOne(store, opts) { | ||
const parents = guaredQueryParents(store, opts); | ||
return extendParents(store, opts, parents); | ||
} | ||
export default function query(store, q) { | ||
return Object.keys(q).reduce((res, k) => { | ||
const parents = guaredQueryParents(store, q, k, join); | ||
res[k] = extendParents(store, q, k, parents); | ||
res[k] = queryOne(store, { etype: k, form: q[k] }); | ||
return res; | ||
}, {}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1027419
19751