json-fs-query 1.0.0 → 1.0.2

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Slice a dataSet down to at most `limit` rows, starting after the first `offset` of them.
3
+ * @param dataSet
4
+ * @param offset
5
+ * @param limit
6
+ */
7
+ export declare const limitOffset: (dataSet: Object[], offset: number | undefined, limit: number | undefined) => Object[];
8
+ //# sourceMappingURL=limitOffset.d.ts.map
@@ -0,0 +1,21 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.limitOffset = void 0
7
+ /**
8
+ * Slice a dataSet down to at most `limit` rows, starting after the first `offset` of them.
9
+ * @param dataSet
10
+ * @param offset
11
+ * @param limit
12
+ */
13
+ const limitOffset = (dataSet = [], offset, limit) => {
14
+ if (typeof offset === 'undefined' && typeof limit === 'undefined') {
15
+ return dataSet
16
+ }
17
+ const start = offset ?? 0
18
+ const end = typeof limit === 'number' ? start + limit : undefined
19
+ return dataSet.slice(start, end)
20
+ }
21
+ exports.limitOffset = limitOffset
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.limitOffset=void 0;const limitOffset=(e=[],t,i)=>{if(void 0===t&&void 0===i)return e;const s=t??0,o="number"==typeof i?s+i:void 0;return e.slice(s,o)};exports.limitOffset=limitOffset;
@@ -0,0 +1,10 @@
1
+ import { ParsedSelect } from '../utilities/parsers/findSelects';
2
+ /**
3
+ * Shape each row down to only the selected properties (aliased, when given). dataSet is either a plain array of
4
+ * rows, or (once groupBy has run) a plain object keyed by group value, each value an array of rows - both are
5
+ * shaped the same way, row by row.
6
+ * @param dataSet
7
+ * @param selectClauses
8
+ */
9
+ export declare const selectFields: (dataSet?: Object[] | Object, selectClauses?: ParsedSelect[]) => Object[] | Object;
10
+ //# sourceMappingURL=selectFields.d.ts.map
@@ -0,0 +1,36 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ exports.selectFields = void 0
7
+ require('core-js/modules/esnext.iterator.constructor.js')
8
+ require('core-js/modules/esnext.iterator.map.js')
9
+ require('core-js/modules/esnext.iterator.reduce.js')
10
+ const shapeRow = (row, selectClauses) => selectClauses.reduce((shaped, {
11
+ property,
12
+ alias
13
+ }) => {
14
+ shaped[alias || property] = row[property]
15
+ return shaped
16
+ }, {})
17
+ /**
18
+ * Shape each row down to only the selected properties (aliased, when given). dataSet is either a plain array of
19
+ * rows, or (once groupBy has run) a plain object keyed by group value, each value an array of rows - both are
20
+ * shaped the same way, row by row.
21
+ * @param dataSet
22
+ * @param selectClauses
23
+ */
24
+ const selectFields = (dataSet = [], selectClauses = []) => {
25
+ if (!selectClauses.length) {
26
+ return dataSet
27
+ }
28
+ if (Array.isArray(dataSet)) {
29
+ return dataSet.map(row => shapeRow(row, selectClauses))
30
+ }
31
+ return Object.keys(dataSet).reduce((shaped, key) => {
32
+ shaped[key] = dataSet[key].map(row => shapeRow(row, selectClauses))
33
+ return shaped
34
+ }, {})
35
+ }
36
+ exports.selectFields = selectFields
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.selectFields=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.map.js"),require("core-js/modules/esnext.iterator.reduce.js");const shapeRow=(e,s)=>s.reduce(((s,{property:r,alias:t})=>(s[t||r]=e[r],s)),{}),selectFields=(e=[],s=[])=>s.length?Array.isArray(e)?e.map((e=>shapeRow(e,s))):Object.keys(e).reduce(((r,t)=>(r[t]=e[t].map((e=>shapeRow(e,s))),r)),{}):e;exports.selectFields=selectFields;
@@ -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;
@@ -15,6 +15,8 @@ const _mergeJoins = require('../queries/mergeJoins')
15
15
  const _where = require('../queries/where')
16
16
  const _sortBy = require('../queries/sortBy')
17
17
  const _groupBy = require('../queries/groupBy')
18
+ const _limitOffset = require('../queries/limitOffset')
19
+ const _selectFields = require('../queries/selectFields')
18
20
  const _retrieveRecord = require('./retrieveRecord')
19
21
  const _getConditionIndexedList = require('./getConditionIndexedList')
20
22
  const _getJoinIndexedList = require('./getJoinIndexedList')
@@ -66,7 +68,9 @@ const runQuery = async parsed => {
66
68
  }, dataSet[parsed.entity])
67
69
  }
68
70
  dataSet[parsed.entity] = (0, _sortBy.sortBy)(dataSet[parsed.entity], parsed.sortClauses)
71
+ dataSet[parsed.entity] = (0, _limitOffset.limitOffset)(dataSet[parsed.entity], parsed.offset, parsed.limit)
69
72
  dataSet[parsed.entity] = (0, _groupBy.groupBy)(dataSet[parsed.entity], parsed.groupBy)
73
+ dataSet[parsed.entity] = (0, _selectFields.selectFields)(dataSet[parsed.entity], parsed.selectClauses)
70
74
  return dataSet
71
75
  }
72
76
  exports.runQuery = runQuery
@@ -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"),_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]:[]};switch(e=await(0,_getConditionIndexedList.getConditionIndexedList)(e,t,i),"delete"!==(e=await(0,_getJoinIndexedList.getJoinIndexedList)(e,t,i)).command&&"update"!==e.command||(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":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),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,_groupBy.groupBy)(i[e.entity],e.groupBy),i};exports.runQuery=runQuery;
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]:[]};switch(e=await(0,_getConditionIndexedList.getConditionIndexedList)(e,t,i),"delete"!==(e=await(0,_getJoinIndexedList.getJoinIndexedList)(e,t,i)).command&&"update"!==e.command||(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":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),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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-fs-query",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Use json files to manage and read data.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",