json-fs-query 1.0.1 → 1.0.3
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/browser/jsonFsQuery.js +31 -5
- package/browser/jsonFsQuery.min.js +1 -1
- package/dist/data/queries/joinEntity.d.ts +1 -1
- package/dist/data/queries/joinEntity.js +7 -2
- package/dist/data/queries/joinEntity.min.js +1 -1
- package/dist/data/utilities/getJoinIndexedList.js +10 -0
- package/dist/data/utilities/getJoinIndexedList.min.js +1 -1
- package/dist/data/utilities/runQuery.js +14 -3
- package/dist/data/utilities/runQuery.min.js +1 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const joinEntity: (entity: any, joinEntity: any, dataSets?: {}, joinClauses?: any[]) => Promise<{}>;
|
|
1
|
+
export declare const joinEntity: (entity: any, joinEntity: any, dataSets?: {}, joinClauses?: any[], entityAlreadyResolved?: boolean) => Promise<{}>;
|
|
2
2
|
//# sourceMappingURL=joinEntity.d.ts.map
|
|
@@ -8,8 +8,13 @@ const _siFunciona = _interopRequireDefault(require('si-funciona'))
|
|
|
8
8
|
const _readEntity = require('./readEntity')
|
|
9
9
|
const _useJoinClause = require('../utilities/useJoinClause')
|
|
10
10
|
function _interopRequireDefault (e) { return e && e.__esModule ? e : { default: e } }
|
|
11
|
-
const joinEntity = async (entity, joinEntity, dataSets = {}, joinClauses = []) => {
|
|
12
|
-
|
|
11
|
+
const joinEntity = async (entity, joinEntity, dataSets = {}, joinClauses = [], entityAlreadyResolved = false) => {
|
|
12
|
+
// Same ambiguity readEntity() always has (can't tell "never loaded" from "loaded, zero rows
|
|
13
|
+
// matched") - runQuery already resolved entity's rows via an indexed condition in that second
|
|
14
|
+
// case, so entityAlreadyResolved tells this call not to reload and silently discard that result.
|
|
15
|
+
if (!entityAlreadyResolved) {
|
|
16
|
+
dataSets = await (0, _readEntity.readEntity)(entity, dataSets)
|
|
17
|
+
}
|
|
13
18
|
if (!joinClauses.length) {
|
|
14
19
|
return dataSets
|
|
15
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.joinEntity=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_readEntity=require("./readEntity"),_useJoinClause=require("../utilities/useJoinClause");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const joinEntity=async(e,t,i={},n=[])=>{if(i=await(0,_readEntity.readEntity)(e,i),!n.length)return i;i=await(0,_readEntity.readEntity)(t,i);for(const r in i[e]){let u=_siFunciona.default.cloneObject(i);u[e]=[i[e][r]];for(let e of n)u=await(0,_useJoinClause.useJoinClause)(e,i,u);i[e][r][t]=u[t]}return i};exports.joinEntity=joinEntity;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.joinEntity=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_readEntity=require("./readEntity"),_useJoinClause=require("../utilities/useJoinClause");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const joinEntity=async(e,t,i={},n=[],r=!1)=>{if(r||(i=await(0,_readEntity.readEntity)(e,i)),!n.length)return i;i=await(0,_readEntity.readEntity)(t,i);for(const r in i[e]){let u=_siFunciona.default.cloneObject(i);u[e]=[i[e][r]];for(let e of n)u=await(0,_useJoinClause.useJoinClause)(e,i,u);i[e][r][t]=u[t]}return i};exports.joinEntity=joinEntity;
|
|
@@ -18,6 +18,16 @@ const getJoinIndexedList = async (parsed, recordData, dataSets) => {
|
|
|
18
18
|
const entity = parsed.entity
|
|
19
19
|
const joinEntity = parsed.joinEntity
|
|
20
20
|
const joinClauses = parsed.joinClauses
|
|
21
|
+
if (!dataSets[entity].length) {
|
|
22
|
+
// Nothing indexed entity's own rows yet (no WHERE condition narrowed them down ahead of this -
|
|
23
|
+
// that only happens via getConditionIndexedList, called just before this). The loop below can't
|
|
24
|
+
// pre-resolve anything against zero rows, so leave parsed.joinClauses exactly as parsed: the
|
|
25
|
+
// later, unconditional joinEntity() call (once this command's own read actually runs) still
|
|
26
|
+
// needs the real clauses to do the join itself. Previously this fell through to the loop,
|
|
27
|
+
// which never ran, leaving reducedJoins (and so parsed.joinClauses) empty regardless of what
|
|
28
|
+
// was actually parsed - silently discarding every join clause whenever the query had no WHERE.
|
|
29
|
+
return parsed
|
|
30
|
+
}
|
|
21
31
|
for (const anEntity in dataSets[entity]) {
|
|
22
32
|
const currentEntity = dataSets[entity][anEntity]
|
|
23
33
|
// Loop over each of the main entities
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getJoinIndexedList=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_splitEntityProperty=require("./parsers/splitEntityProperty"),_retrieveRecord=require("./retrieveRecord"),_getMatchingIndexes=require("./getMatchingIndexes"),_matchIndexedJoins=require("./matchIndexedJoins"),_restrictIndexedList=require("./restrictIndexedList");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const getJoinIndexedList=async(e,t,r)=>{let i=[];const n={};let o=!0;const s=e.entity,c=e.joinEntity,d=e.joinClauses;for(const e in r[s]){const a=r[s][e];let p={};p[s]=[_siFunciona.default.cloneObject(a)];for(let e of d){let c=_siFunciona.default.cloneObject(n);const{entity:d,property:a}=(0,_splitEntityProperty.splitEntityProperty)(e.propertyA),{entity:l,property:u}=(0,_splitEntityProperty.splitEntityProperty)(e.propertyB);Object.prototype.hasOwnProperty.call(t,d)||(t[d]=await(0,_retrieveRecord.retrieveRecord)(d)),Object.prototype.hasOwnProperty.call(t,l)||(t[l]=await(0,_retrieveRecord.retrieveRecord)(l)),c=(0,_restrictIndexedList.restrictIndexedList)(e,p,c);const y=(0,_matchIndexedJoins.matchIndexedJoins)(s,c,r),_=await(0,_getMatchingIndexes.getMatchingIndexes)(d,a,t,e,p,n),x=await(0,_getMatchingIndexes.getMatchingIndexes)(l,u,t,e,p,n,y);!(_||x)&&o&&(i.push(e),o=!1)}r[s][e][c]=p[c]}return e.joinClauses=i,e};exports.getJoinIndexedList=getJoinIndexedList;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getJoinIndexedList=void 0;var _siFunciona=_interopRequireDefault(require("si-funciona")),_splitEntityProperty=require("./parsers/splitEntityProperty"),_retrieveRecord=require("./retrieveRecord"),_getMatchingIndexes=require("./getMatchingIndexes"),_matchIndexedJoins=require("./matchIndexedJoins"),_restrictIndexedList=require("./restrictIndexedList");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const getJoinIndexedList=async(e,t,r)=>{let i=[];const n={};let o=!0;const s=e.entity,c=e.joinEntity,d=e.joinClauses;if(!r[s].length)return e;for(const e in r[s]){const a=r[s][e];let p={};p[s]=[_siFunciona.default.cloneObject(a)];for(let e of d){let c=_siFunciona.default.cloneObject(n);const{entity:d,property:a}=(0,_splitEntityProperty.splitEntityProperty)(e.propertyA),{entity:l,property:u}=(0,_splitEntityProperty.splitEntityProperty)(e.propertyB);Object.prototype.hasOwnProperty.call(t,d)||(t[d]=await(0,_retrieveRecord.retrieveRecord)(d)),Object.prototype.hasOwnProperty.call(t,l)||(t[l]=await(0,_retrieveRecord.retrieveRecord)(l)),c=(0,_restrictIndexedList.restrictIndexedList)(e,p,c);const y=(0,_matchIndexedJoins.matchIndexedJoins)(s,c,r),_=await(0,_getMatchingIndexes.getMatchingIndexes)(d,a,t,e,p,n),x=await(0,_getMatchingIndexes.getMatchingIndexes)(l,u,t,e,p,n,y);!(_||x)&&o&&(i.push(e),o=!1)}r[s][e][c]=p[c]}return e.joinClauses=i,e};exports.getJoinIndexedList=getJoinIndexedList;
|
|
@@ -30,14 +30,23 @@ const runQuery = async parsed => {
|
|
|
30
30
|
let dataSet = {
|
|
31
31
|
[parsed.entity]: []
|
|
32
32
|
}
|
|
33
|
+
// A condition that's indexed but genuinely matches nothing leaves dataSet[entity] at [] - the
|
|
34
|
+
// same state it starts in before anything has looked at it at all. readEntity() (below) can't
|
|
35
|
+
// tell those two states apart on its own (it reloads whenever the array is empty, regardless of
|
|
36
|
+
// why), so capture here, before that ambiguity can happen, whether every condition actually got
|
|
37
|
+
// resolved this way - if so, dataSet[entity] is already correct and must not be reloaded.
|
|
38
|
+
const hadConditions = parsed.conditions.length > 0
|
|
33
39
|
parsed = await (0, _getConditionIndexedList.getConditionIndexedList)(parsed, recordData, dataSet)
|
|
40
|
+
const conditionsFullyIndexed = hadConditions && parsed.conditions.length === 0
|
|
34
41
|
parsed = await (0, _getJoinIndexedList.getJoinIndexedList)(parsed, recordData, dataSet)
|
|
35
42
|
// delete/update act on whatever is already in dataSet[entity] - unlike read, which loads
|
|
36
43
|
// everything and lets the where-reduce below narrow it down afterward, they never get a second
|
|
37
44
|
// pass. So the matching rows have to be fully resolved (indexed conditions already are, via
|
|
38
45
|
// getConditionIndexedList above; anything left in parsed.conditions is not) before either runs.
|
|
39
46
|
if (parsed.command === 'delete' || parsed.command === 'update') {
|
|
40
|
-
|
|
47
|
+
if (!conditionsFullyIndexed) {
|
|
48
|
+
dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
|
|
49
|
+
}
|
|
41
50
|
dataSet[parsed.entity] = parsed.conditions.reduce((remainingData, condition) => (0, _where.where)(remainingData, condition), dataSet[parsed.entity])
|
|
42
51
|
}
|
|
43
52
|
switch (parsed.command) {
|
|
@@ -48,7 +57,9 @@ const runQuery = async parsed => {
|
|
|
48
57
|
dataSet = await (0, _insertEntity.insertEntity)(parsed.entity, parsed.insertValues, dataSet)
|
|
49
58
|
break
|
|
50
59
|
case 'read':
|
|
51
|
-
|
|
60
|
+
if (!conditionsFullyIndexed) {
|
|
61
|
+
dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
|
|
62
|
+
}
|
|
52
63
|
break
|
|
53
64
|
case 'update':
|
|
54
65
|
dataSet = await (0, _updateEntity.updateEntity)(parsed.entity, parsed.updateValues, dataSet)
|
|
@@ -56,7 +67,7 @@ const runQuery = async parsed => {
|
|
|
56
67
|
default:
|
|
57
68
|
throw new Error(`Unknown command: ${parsed.command}`)
|
|
58
69
|
}
|
|
59
|
-
dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses)
|
|
70
|
+
dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses, conditionsFullyIndexed)
|
|
60
71
|
dataSet = await (0, _mergeJoins.mergeJoins)(dataSet, parsed.mergeJoins)
|
|
61
72
|
// delete/update already had parsed.conditions applied above, before they ran. Re-applying the
|
|
62
73
|
// same conditions here would be wrong, not just redundant, for update specifically: if the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.runQuery=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.reduce.js");var _deleteEntity=require("../queries/deleteEntity"),_insertEntity=require("../queries/insertEntity"),_readEntity=require("../queries/readEntity"),_updateEntity=require("../queries/updateEntity"),_joinEntity=require("../queries/joinEntity"),_mergeJoins=require("../queries/mergeJoins"),_where=require("../queries/where"),_sortBy=require("../queries/sortBy"),_groupBy=require("../queries/groupBy"),_limitOffset=require("../queries/limitOffset"),_selectFields=require("../queries/selectFields"),_retrieveRecord=require("./retrieveRecord"),_getConditionIndexedList=require("./getConditionIndexedList"),_getJoinIndexedList=require("./getJoinIndexedList");const runQuery=async e=>{const t={[e.entity]:await(0,_retrieveRecord.retrieveRecord)(e.entity)};void 0!==e.joinEntity&&(t[e.joinEntity]=await(0,_retrieveRecord.retrieveRecord)(e.joinEntity));let i={[e.entity]:[]};
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.runQuery=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.reduce.js");var _deleteEntity=require("../queries/deleteEntity"),_insertEntity=require("../queries/insertEntity"),_readEntity=require("../queries/readEntity"),_updateEntity=require("../queries/updateEntity"),_joinEntity=require("../queries/joinEntity"),_mergeJoins=require("../queries/mergeJoins"),_where=require("../queries/where"),_sortBy=require("../queries/sortBy"),_groupBy=require("../queries/groupBy"),_limitOffset=require("../queries/limitOffset"),_selectFields=require("../queries/selectFields"),_retrieveRecord=require("./retrieveRecord"),_getConditionIndexedList=require("./getConditionIndexedList"),_getJoinIndexedList=require("./getJoinIndexedList");const runQuery=async e=>{const t={[e.entity]:await(0,_retrieveRecord.retrieveRecord)(e.entity)};void 0!==e.joinEntity&&(t[e.joinEntity]=await(0,_retrieveRecord.retrieveRecord)(e.joinEntity));let i={[e.entity]:[]};const r=e.conditions.length>0;e=await(0,_getConditionIndexedList.getConditionIndexedList)(e,t,i);const n=r&&0===e.conditions.length;switch("delete"!==(e=await(0,_getJoinIndexedList.getJoinIndexedList)(e,t,i)).command&&"update"!==e.command||(n||(i=await(0,_readEntity.readEntity)(e.entity,i)),i[e.entity]=e.conditions.reduce(((e,t)=>(0,_where.where)(e,t)),i[e.entity])),e.command){case"delete":i=await(0,_deleteEntity.deleteEntity)(e.entity,i);break;case"insert":i=await(0,_insertEntity.insertEntity)(e.entity,e.insertValues,i);break;case"read":n||(i=await(0,_readEntity.readEntity)(e.entity,i));break;case"update":i=await(0,_updateEntity.updateEntity)(e.entity,e.updateValues,i);break;default:throw new Error(`Unknown command: ${e.command}`)}return i=await(0,_joinEntity.joinEntity)(e.entity,e.joinEntity,i,e.joinClauses,n),i=await(0,_mergeJoins.mergeJoins)(i,e.mergeJoins),"delete"!==e.command&&"update"!==e.command&&(i[e.entity]=e.conditions.reduce(((e,t)=>(0,_where.where)(e,t)),i[e.entity])),i[e.entity]=(0,_sortBy.sortBy)(i[e.entity],e.sortClauses),i[e.entity]=(0,_limitOffset.limitOffset)(i[e.entity],e.offset,e.limit),i[e.entity]=(0,_groupBy.groupBy)(i[e.entity],e.groupBy),i[e.entity]=(0,_selectFields.selectFields)(i[e.entity],e.selectClauses),i};exports.runQuery=runQuery;
|