json-fs-query 3.1.4 → 3.2.0

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.
Files changed (109) hide show
  1. package/browser/jsonFsQuery.js +2172 -5305
  2. package/browser/jsonFsQuery.min.js +1 -1
  3. package/dist/data/introspect/create.js +7 -22
  4. package/dist/data/introspect/create.min.js +1 -1
  5. package/dist/data/introspect/drop.js +5 -15
  6. package/dist/data/introspect/drop.min.js +1 -1
  7. package/dist/data/introspect/policies/foreignKeyExists.d.ts +2 -0
  8. package/dist/data/introspect/policies/foreignKeyExists.js +9 -3
  9. package/dist/data/introspect/policies/foreignKeyExists.min.js +1 -1
  10. package/dist/data/introspect/policies/hasDependents.d.ts +3 -3
  11. package/dist/data/introspect/policies/hasDependents.js +11 -3
  12. package/dist/data/introspect/policies/hasDependents.min.js +1 -1
  13. package/dist/data/introspect/policies/keyUnique.d.ts +3 -0
  14. package/dist/data/introspect/policies/keyUnique.js +10 -0
  15. package/dist/data/introspect/policies/keyUnique.min.js +1 -1
  16. package/dist/data/queries/deleteEntity.js +29 -18
  17. package/dist/data/queries/deleteEntity.min.js +1 -1
  18. package/dist/data/queries/insertEntity.js +4 -12
  19. package/dist/data/queries/insertEntity.min.js +1 -1
  20. package/dist/data/queries/joinEntity.d.ts +15 -0
  21. package/dist/data/queries/joinEntity.js +91 -15
  22. package/dist/data/queries/joinEntity.min.js +1 -1
  23. package/dist/data/queries/updateEntity.js +15 -10
  24. package/dist/data/queries/updateEntity.min.js +1 -1
  25. package/dist/data/storage/fileExists.d.ts +6 -0
  26. package/dist/data/storage/fileExists.js +18 -0
  27. package/dist/data/storage/fileExists.min.js +1 -0
  28. package/dist/data/storage/index.d.ts +16 -0
  29. package/dist/data/storage/index.js +61 -0
  30. package/dist/data/storage/index.min.js +1 -0
  31. package/dist/data/storage/makeDirectory.d.ts +6 -0
  32. package/dist/data/storage/makeDirectory.js +18 -0
  33. package/dist/data/storage/makeDirectory.min.js +1 -0
  34. package/dist/data/storage/nodeFiles.d.ts +6 -0
  35. package/dist/data/storage/nodeFiles.js +12 -0
  36. package/dist/data/storage/nodeFiles.min.js +1 -0
  37. package/dist/data/storage/readCache.d.ts +37 -0
  38. package/dist/data/storage/readCache.js +117 -0
  39. package/dist/data/storage/readCache.min.js +1 -0
  40. package/dist/data/storage/readJson.d.ts +8 -0
  41. package/dist/data/storage/readJson.js +25 -0
  42. package/dist/data/storage/readJson.min.js +1 -0
  43. package/dist/data/storage/removeDirectory.d.ts +6 -0
  44. package/dist/data/storage/removeDirectory.js +24 -0
  45. package/dist/data/storage/removeDirectory.min.js +1 -0
  46. package/dist/data/storage/removeFile.d.ts +7 -0
  47. package/dist/data/storage/removeFile.js +22 -0
  48. package/dist/data/storage/removeFile.min.js +1 -0
  49. package/dist/data/storage/storagePath.d.ts +11 -0
  50. package/dist/data/storage/storagePath.js +25 -0
  51. package/dist/data/storage/storagePath.min.js +1 -0
  52. package/dist/data/storage/writeJson.d.ts +8 -0
  53. package/dist/data/storage/writeJson.js +23 -0
  54. package/dist/data/storage/writeJson.min.js +1 -0
  55. package/dist/data/utilities/compareAs.d.ts +1 -0
  56. package/dist/data/utilities/compareAs.js +2 -2
  57. package/dist/data/utilities/compareAs.min.js +1 -1
  58. package/dist/data/utilities/databaseLock.d.ts +2 -0
  59. package/dist/data/utilities/databaseLock.js +5 -2
  60. package/dist/data/utilities/databaseLock.min.js +1 -1
  61. package/dist/data/utilities/deregisterRecord.js +4 -10
  62. package/dist/data/utilities/deregisterRecord.min.js +1 -1
  63. package/dist/data/utilities/indexHas.d.ts +13 -0
  64. package/dist/data/utilities/indexHas.js +48 -0
  65. package/dist/data/utilities/indexHas.min.js +1 -0
  66. package/dist/data/utilities/joinBases.d.ts +29 -0
  67. package/dist/data/utilities/joinBases.js +136 -0
  68. package/dist/data/utilities/joinBases.min.js +1 -0
  69. package/dist/data/utilities/joinKey.d.ts +17 -0
  70. package/dist/data/utilities/joinKey.js +31 -0
  71. package/dist/data/utilities/joinKey.min.js +1 -0
  72. package/dist/data/utilities/keyIndexFiles.d.ts +16 -0
  73. package/dist/data/utilities/keyIndexFiles.js +60 -0
  74. package/dist/data/utilities/keyIndexFiles.min.js +1 -0
  75. package/dist/data/utilities/recordExists.js +2 -7
  76. package/dist/data/utilities/recordExists.min.js +1 -1
  77. package/dist/data/utilities/registerRecord.js +4 -10
  78. package/dist/data/utilities/registerRecord.min.js +1 -1
  79. package/dist/data/utilities/removeFromIndex.js +4 -10
  80. package/dist/data/utilities/removeFromIndex.min.js +1 -1
  81. package/dist/data/utilities/retrieveFile.d.ts +5 -0
  82. package/dist/data/utilities/retrieveFile.js +7 -16
  83. package/dist/data/utilities/retrieveFile.min.js +1 -1
  84. package/dist/data/utilities/rowLookup.d.ts +38 -0
  85. package/dist/data/utilities/rowLookup.js +99 -0
  86. package/dist/data/utilities/rowLookup.min.js +1 -0
  87. package/dist/data/utilities/runQuery.js +1 -2
  88. package/dist/data/utilities/runQuery.min.js +1 -1
  89. package/dist/data/utilities/semiJoin.d.ts +27 -0
  90. package/dist/data/utilities/semiJoin.js +97 -0
  91. package/dist/data/utilities/semiJoin.min.js +1 -0
  92. package/dist/data/utilities/updateIndex.js +5 -14
  93. package/dist/data/utilities/updateIndex.min.js +1 -1
  94. package/package.json +4 -2
  95. package/dist/data/utilities/getJoinIndexedList.d.ts +0 -3
  96. package/dist/data/utilities/getJoinIndexedList.js +0 -76
  97. package/dist/data/utilities/getJoinIndexedList.min.js +0 -1
  98. package/dist/data/utilities/getMatchingIndexes.d.ts +0 -2
  99. package/dist/data/utilities/getMatchingIndexes.js +0 -28
  100. package/dist/data/utilities/getMatchingIndexes.min.js +0 -1
  101. package/dist/data/utilities/matchIndexedJoins.d.ts +0 -2
  102. package/dist/data/utilities/matchIndexedJoins.js +0 -63
  103. package/dist/data/utilities/matchIndexedJoins.min.js +0 -1
  104. package/dist/data/utilities/restrictIndexedList.d.ts +0 -2
  105. package/dist/data/utilities/restrictIndexedList.js +0 -45
  106. package/dist/data/utilities/restrictIndexedList.min.js +0 -1
  107. package/dist/data/utilities/useJoinClause.d.ts +0 -6
  108. package/dist/data/utilities/useJoinClause.js +0 -57
  109. package/dist/data/utilities/useJoinClause.min.js +0 -1
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.updateEntity = void 0;
7
- require("core-js/modules/es.json.stringify.js");
8
7
  require("core-js/modules/esnext.iterator.constructor.js");
9
8
  require("core-js/modules/esnext.iterator.find.js");
10
9
  require("core-js/modules/esnext.iterator.map.js");
@@ -22,21 +21,28 @@ var _foreignKeyExists = require("../introspect/policies/foreignKeyExists");
22
21
  var _hasDependents = require("../introspect/policies/hasDependents");
23
22
  var _splitEntityProperty = require("../utilities/parsers/splitEntityProperty");
24
23
  var _config = require("../utilities/config");
25
- const {
26
- writeFile
27
- } = require('fs/promises');
24
+ var _keyIndexFiles = require("../utilities/keyIndexFiles");
25
+ var _storage = require("../storage");
28
26
  /**
29
27
  * Same reasoning as deleteEntity: a record object carries no memory of which entry file it came
30
- * from, so re-scan entries[] and match by the record's own primary key value(s) - a single field,
31
- * or a composite of several for a multi-field primary key.
28
+ * from, so find the file whose primary key value(s) match - a single field, or a composite of
29
+ * several for a multi-field primary key. The primary key's index says which file that is; the file
30
+ * is read to confirm it, and without an index (or when it leads nowhere) every entry is looked at
31
+ * in turn, as before.
32
32
  * @param record
33
33
  * @param primaryKey
34
34
  * @param primaryValue
35
35
  */
36
36
  const findEntryFile = async (record, primaryKey, primaryValue) => {
37
+ const type = (0, _fieldType.keyValueType)(record, primaryKey);
38
+ const isMatch = async fileName => (0, _compareAs.sameValue)(type, (0, _computeKeyValue.computeKeyValue)(primaryKey.fields, await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`)), primaryValue);
39
+ for (const fileName of (await (0, _keyIndexFiles.keyIndexFiles)(record, primaryKey, value => (0, _compareAs.sameValue)(type, value, primaryValue))) ?? []) {
40
+ if (await isMatch(fileName)) {
41
+ return fileName;
42
+ }
43
+ }
37
44
  for (const fileName of record.entries) {
38
- const data = await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`);
39
- if ((0, _compareAs.sameValue)((0, _fieldType.keyValueType)(record, primaryKey), (0, _computeKeyValue.computeKeyValue)(primaryKey.fields, data), primaryValue)) {
45
+ if (await isMatch(fileName)) {
40
46
  return fileName;
41
47
  }
42
48
  }
@@ -199,7 +205,6 @@ const applyFieldTypes = (record, values) => {
199
205
  return typedValues;
200
206
  };
201
207
  const updateEntity = async (entity = '', values = {}, dataSet = {}) => {
202
- const databasePath = (0, _config.getSetting)('databasePath', 'database/');
203
208
  const record = await (0, _retrieveRecord.retrieveRecord)(entity);
204
209
  const primaryKey = record.keys.find(key => key.type === 'primary');
205
210
  if (!primaryKey) {
@@ -236,7 +241,7 @@ const updateEntity = async (entity = '', values = {}, dataSet = {}) => {
236
241
  newData
237
242
  } of planned) {
238
243
  await reconcileIndexes(record, oldData, newData, typedValues, fileName);
239
- await writeFile(`${databasePath}${record.path}/${fileName}`, JSON.stringify(newData, null, 2));
244
+ await (0, _storage.writeJson)(`${record.path}/${fileName}`, newData);
240
245
  }
241
246
  dataSet[entity] = planned.map(({
242
247
  newData
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateEntity=void 0,require("core-js/modules/es.json.stringify.js"),require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.find.js"),require("core-js/modules/esnext.iterator.map.js"),require("core-js/modules/esnext.iterator.some.js");var _retrieveRecord=require("../utilities/retrieveRecord"),_retrieveFile=require("../utilities/retrieveFile"),_updateIndex=require("../utilities/updateIndex"),_removeFromIndex=require("../utilities/removeFromIndex"),_computeKeyValue=require("../utilities/computeKeyValue"),_fieldType=require("../utilities/fieldType"),_compareAs=require("../utilities/compareAs"),_keyUnique=require("../introspect/policies/keyUnique"),_typeCheck=require("../introspect/policies/typeCheck"),_foreignKeyExists=require("../introspect/policies/foreignKeyExists"),_hasDependents=require("../introspect/policies/hasDependents"),_splitEntityProperty=require("../utilities/parsers/splitEntityProperty"),_config=require("../utilities/config");const{writeFile:writeFile}=require("fs/promises"),findEntryFile=async(e,t,i)=>{for(const r of e.entries){const o=await(0,_retrieveFile.retrieveFile)(`${e.path}/${r}`);if((0,_compareAs.sameValue)((0,_fieldType.keyValueType)(e,t),(0,_computeKeyValue.computeKeyValue)(t.fields,o),i))return r}return null},keyFieldsChanged=(e,t,i,r)=>t.fields.some((t=>Object.prototype.hasOwnProperty.call(r,t)&&!(0,_compareAs.sameValue)((0,_fieldType.fieldType)(e,t),r[t],i[t]))),assertUniqueForUpdate=async(e,t,i,r,o)=>{for(const s of t.keys){if("primary"!==s.type&&"unique"!==s.type||!keyFieldsChanged(t,s,i,o))continue;const n=s.fields.map((e=>r[e]));if(n.some((e=>null===e)))continue;if(!await(0,_keyUnique.keyUnique)(e,s.fields,n))throw new Error(`Duplicate value for ${s.type} key "${s.fields.join(", ")}" on ${e}`)}},assertForeignKeysForUpdate=async(e,t,i,r)=>{for(const o of t.keys){if("foreign"!==o.type||1!==o.fields.length||!o.references||1!==o.references.length)continue;const s=o.fields[0];if(!Object.prototype.hasOwnProperty.call(r,s)||null===r[s]||(0,_compareAs.sameValue)((0,_fieldType.fieldType)(t,s),r[s],i[s]))continue;const{entity:n,property:a}=(0,_splitEntityProperty.splitEntityProperty)(o.references[0].fields[0]);if(!await(0,_foreignKeyExists.foreignKeyExists)(n,a,r[s]))throw new Error(`Foreign key "${s}" on ${e} references a nonexistent ${n}.${a} = ${r[s]}`)}},assertNoDependentsForUpdate=async(e,t,i,r,o)=>{if(!(0,_config.getSetting)("enforceForeignKeys",!0)||1!==i.fields.length||!keyFieldsChanged(t,i,r,o))return;const s=i.fields[0];if(await(0,_hasDependents.hasDependents)(e,s,r[s]))throw new Error(`Cannot update "${e}" where "${s}" = ${r[s]} - other records still reference it`)},reconcileIndexes=async(e,t,i,r,o)=>{for(const s of e.keys)keyFieldsChanged(e,s,t,r)&&(await(0,_removeFromIndex.removeFromIndex)(e.path,s,(0,_computeKeyValue.computeKeyValue)(s.fields,t),o,(0,_fieldType.keyValueType)(e,s)),await(0,_updateIndex.updateIndex)(e.path,s,(0,_computeKeyValue.computeKeyValue)(s.fields,i),o,(0,_fieldType.keyValueType)(e,s)))},assertUniqueWithinUpdate=(e,t,i,r)=>{for(const o of t.keys){if("primary"!==o.type&&"unique"!==o.type)continue;const s=[];for(const{oldData:n,newData:a}of i){if(!keyFieldsChanged(t,o,n,r)||o.fields.some((e=>null===a[e])))continue;const i=(0,_computeKeyValue.computeKeyValue)(o.fields,a);if(s.some((e=>(0,_compareAs.sameValue)((0,_fieldType.keyValueType)(t,o),e,i))))throw new Error(`Duplicate value for ${o.type} key "${o.fields.join(", ")}" on ${e}`);s.push(i)}}},assertRequiredFields=(e,t,i)=>{for(const r of t.definition)if(!r.optional&&void 0===i[r.name])throw new Error(`Missing required field "${r.name}" for ${e}`)},applyFieldTypes=(e,t)=>{const i=Object.assign({},t);for(const t of e.definition)Object.prototype.hasOwnProperty.call(i,t.name)&&(i[t.name]=(0,_typeCheck.typeCheck)(t.type,i[t.name],t.nullable));return i},updateEntity=async(e="",t={},i={})=>{const r=(0,_config.getSetting)("databasePath","database/"),o=await(0,_retrieveRecord.retrieveRecord)(e),s=o.keys.find((e=>"primary"===e.type));if(!s)throw new Error(`Cannot update "${e}" without a primary key`);const n=applyFieldTypes(o,t),a=[];for(const t of i[e]||[]){const i=await findEntryFile(o,s,(0,_computeKeyValue.computeKeyValue)(s.fields,t));if(!i)throw new Error(`Could not find entry for ${e} where "${s.fields.join(", ")}" = ${(0,_computeKeyValue.computeKeyValue)(s.fields,t)}`);if(a.some((e=>e.fileName===i)))continue;const r=await(0,_retrieveFile.retrieveFile)(`${o.path}/${i}`),l=Object.assign({},r,n);await assertUniqueForUpdate(e,o,r,l,n),await assertForeignKeysForUpdate(e,o,r,n),await assertNoDependentsForUpdate(e,o,s,r,n),assertRequiredFields(e,o,l),a.push({fileName:i,oldData:r,newData:l})}assertUniqueWithinUpdate(e,o,a,n);for(const{fileName:e,oldData:t,newData:i}of a)await reconcileIndexes(o,t,i,n,e),await writeFile(`${r}${o.path}/${e}`,JSON.stringify(i,null,2));return i[e]=a.map((({newData:e})=>e)),i};exports.updateEntity=updateEntity;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateEntity=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.find.js"),require("core-js/modules/esnext.iterator.map.js"),require("core-js/modules/esnext.iterator.some.js");var _retrieveRecord=require("../utilities/retrieveRecord"),_retrieveFile=require("../utilities/retrieveFile"),_updateIndex=require("../utilities/updateIndex"),_removeFromIndex=require("../utilities/removeFromIndex"),_computeKeyValue=require("../utilities/computeKeyValue"),_fieldType=require("../utilities/fieldType"),_compareAs=require("../utilities/compareAs"),_keyUnique=require("../introspect/policies/keyUnique"),_typeCheck=require("../introspect/policies/typeCheck"),_foreignKeyExists=require("../introspect/policies/foreignKeyExists"),_hasDependents=require("../introspect/policies/hasDependents"),_splitEntityProperty=require("../utilities/parsers/splitEntityProperty"),_config=require("../utilities/config"),_keyIndexFiles=require("../utilities/keyIndexFiles"),_storage=require("../storage");const findEntryFile=async(e,t,i)=>{const r=(0,_fieldType.keyValueType)(e,t),o=async o=>(0,_compareAs.sameValue)(r,(0,_computeKeyValue.computeKeyValue)(t.fields,await(0,_retrieveFile.retrieveFile)(`${e.path}/${o}`)),i);for(const s of await(0,_keyIndexFiles.keyIndexFiles)(e,t,(e=>(0,_compareAs.sameValue)(r,e,i)))??[])if(await o(s))return s;for(const t of e.entries)if(await o(t))return t;return null},keyFieldsChanged=(e,t,i,r)=>t.fields.some((t=>Object.prototype.hasOwnProperty.call(r,t)&&!(0,_compareAs.sameValue)((0,_fieldType.fieldType)(e,t),r[t],i[t]))),assertUniqueForUpdate=async(e,t,i,r,o)=>{for(const s of t.keys){if("primary"!==s.type&&"unique"!==s.type||!keyFieldsChanged(t,s,i,o))continue;const n=s.fields.map((e=>r[e]));if(n.some((e=>null===e)))continue;if(!await(0,_keyUnique.keyUnique)(e,s.fields,n))throw new Error(`Duplicate value for ${s.type} key "${s.fields.join(", ")}" on ${e}`)}},assertForeignKeysForUpdate=async(e,t,i,r)=>{for(const o of t.keys){if("foreign"!==o.type||1!==o.fields.length||!o.references||1!==o.references.length)continue;const s=o.fields[0];if(!Object.prototype.hasOwnProperty.call(r,s)||null===r[s]||(0,_compareAs.sameValue)((0,_fieldType.fieldType)(t,s),r[s],i[s]))continue;const{entity:n,property:a}=(0,_splitEntityProperty.splitEntityProperty)(o.references[0].fields[0]);if(!await(0,_foreignKeyExists.foreignKeyExists)(n,a,r[s]))throw new Error(`Foreign key "${s}" on ${e} references a nonexistent ${n}.${a} = ${r[s]}`)}},assertNoDependentsForUpdate=async(e,t,i,r,o)=>{if(!(0,_config.getSetting)("enforceForeignKeys",!0)||1!==i.fields.length||!keyFieldsChanged(t,i,r,o))return;const s=i.fields[0];if(await(0,_hasDependents.hasDependents)(e,s,r[s]))throw new Error(`Cannot update "${e}" where "${s}" = ${r[s]} - other records still reference it`)},reconcileIndexes=async(e,t,i,r,o)=>{for(const s of e.keys)keyFieldsChanged(e,s,t,r)&&(await(0,_removeFromIndex.removeFromIndex)(e.path,s,(0,_computeKeyValue.computeKeyValue)(s.fields,t),o,(0,_fieldType.keyValueType)(e,s)),await(0,_updateIndex.updateIndex)(e.path,s,(0,_computeKeyValue.computeKeyValue)(s.fields,i),o,(0,_fieldType.keyValueType)(e,s)))},assertUniqueWithinUpdate=(e,t,i,r)=>{for(const o of t.keys){if("primary"!==o.type&&"unique"!==o.type)continue;const s=[];for(const{oldData:n,newData:a}of i){if(!keyFieldsChanged(t,o,n,r)||o.fields.some((e=>null===a[e])))continue;const i=(0,_computeKeyValue.computeKeyValue)(o.fields,a);if(s.some((e=>(0,_compareAs.sameValue)((0,_fieldType.keyValueType)(t,o),e,i))))throw new Error(`Duplicate value for ${o.type} key "${o.fields.join(", ")}" on ${e}`);s.push(i)}}},assertRequiredFields=(e,t,i)=>{for(const r of t.definition)if(!r.optional&&void 0===i[r.name])throw new Error(`Missing required field "${r.name}" for ${e}`)},applyFieldTypes=(e,t)=>{const i=Object.assign({},t);for(const t of e.definition)Object.prototype.hasOwnProperty.call(i,t.name)&&(i[t.name]=(0,_typeCheck.typeCheck)(t.type,i[t.name],t.nullable));return i},updateEntity=async(e="",t={},i={})=>{const r=await(0,_retrieveRecord.retrieveRecord)(e),o=r.keys.find((e=>"primary"===e.type));if(!o)throw new Error(`Cannot update "${e}" without a primary key`);const s=applyFieldTypes(r,t),n=[];for(const t of i[e]||[]){const i=await findEntryFile(r,o,(0,_computeKeyValue.computeKeyValue)(o.fields,t));if(!i)throw new Error(`Could not find entry for ${e} where "${o.fields.join(", ")}" = ${(0,_computeKeyValue.computeKeyValue)(o.fields,t)}`);if(n.some((e=>e.fileName===i)))continue;const a=await(0,_retrieveFile.retrieveFile)(`${r.path}/${i}`),l=Object.assign({},a,s);await assertUniqueForUpdate(e,r,a,l,s),await assertForeignKeysForUpdate(e,r,a,s),await assertNoDependentsForUpdate(e,r,o,a,s),assertRequiredFields(e,r,l),n.push({fileName:i,oldData:a,newData:l})}assertUniqueWithinUpdate(e,r,n,s);for(const{fileName:e,oldData:t,newData:i}of n)await reconcileIndexes(r,t,i,s,e),await(0,_storage.writeJson)(`${r.path}/${e}`,i);return i[e]=n.map((({newData:e})=>e)),i};exports.updateEntity=updateEntity;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Whether a file (or folder) of the database exists. Node only, like the schema statements that use it.
3
+ * @param path - A path inside the database, such as `users.json`.
4
+ */
5
+ export declare const fileExists: (path: string) => boolean;
6
+ //# sourceMappingURL=fileExists.d.ts.map
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.fileExists = void 0;
7
+ var _storagePath = require("./storagePath");
8
+ /**
9
+ * Whether a file (or folder) of the database exists. Node only, like the schema statements that use it.
10
+ * @param path - A path inside the database, such as `users.json`.
11
+ */
12
+ const fileExists = path => {
13
+ const {
14
+ existsSync
15
+ } = require('fs');
16
+ return existsSync((0, _storagePath.storagePath)(path));
17
+ };
18
+ exports.fileExists = fileExists;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.fileExists=void 0;var _storagePath=require("./storagePath");const fileExists=e=>{const{existsSync:s}=require("fs");return s((0,_storagePath.storagePath)(e))};exports.fileExists=fileExists;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The storage layer: every file json-fs-query reads, writes or deletes goes through these functions, and nothing else
3
+ * touches `fs` or `fetch()`. Paths given to them are inside the database (see storagePath).
4
+ *
5
+ * Today there is one backend, JSON files (on disk in Node, over fetch() in a browser). Keeping it behind these few
6
+ * functions is what lets a read cache, batched writes and other storage backends come later without touching queries.
7
+ */
8
+ export { storagePath } from './storagePath';
9
+ export { readJson } from './readJson';
10
+ export { writeJson } from './writeJson';
11
+ export { makeDirectory } from './makeDirectory';
12
+ export { removeFile } from './removeFile';
13
+ export { removeDirectory } from './removeDirectory';
14
+ export { fileExists } from './fileExists';
15
+ export { withReadCache } from './readCache';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "fileExists", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _fileExists.fileExists;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "makeDirectory", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _makeDirectory.makeDirectory;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "readJson", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _readJson.readJson;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "removeDirectory", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _removeDirectory.removeDirectory;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "removeFile", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _removeFile.removeFile;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "storagePath", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _storagePath.storagePath;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "withReadCache", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _readCache.withReadCache;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "writeJson", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _writeJson.writeJson;
52
+ }
53
+ });
54
+ var _storagePath = require("./storagePath");
55
+ var _readJson = require("./readJson");
56
+ var _writeJson = require("./writeJson");
57
+ var _makeDirectory = require("./makeDirectory");
58
+ var _removeFile = require("./removeFile");
59
+ var _removeDirectory = require("./removeDirectory");
60
+ var _fileExists = require("./fileExists");
61
+ var _readCache = require("./readCache");
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"fileExists",{enumerable:!0,get:function(){return _fileExists.fileExists}}),Object.defineProperty(exports,"makeDirectory",{enumerable:!0,get:function(){return _makeDirectory.makeDirectory}}),Object.defineProperty(exports,"readJson",{enumerable:!0,get:function(){return _readJson.readJson}}),Object.defineProperty(exports,"removeDirectory",{enumerable:!0,get:function(){return _removeDirectory.removeDirectory}}),Object.defineProperty(exports,"removeFile",{enumerable:!0,get:function(){return _removeFile.removeFile}}),Object.defineProperty(exports,"storagePath",{enumerable:!0,get:function(){return _storagePath.storagePath}}),Object.defineProperty(exports,"withReadCache",{enumerable:!0,get:function(){return _readCache.withReadCache}}),Object.defineProperty(exports,"writeJson",{enumerable:!0,get:function(){return _writeJson.writeJson}});var _storagePath=require("./storagePath"),_readJson=require("./readJson"),_writeJson=require("./writeJson"),_makeDirectory=require("./makeDirectory"),_removeFile=require("./removeFile"),_removeDirectory=require("./removeDirectory"),_fileExists=require("./fileExists"),_readCache=require("./readCache");
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Create a folder of the database, and any missing folders above it (doing nothing if it already exists). Node only.
3
+ * @param path - A path inside the database, such as `users`. Empty for the database folder itself.
4
+ */
5
+ export declare const makeDirectory: (path?: string) => Promise<void>;
6
+ //# sourceMappingURL=makeDirectory.d.ts.map
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeDirectory = void 0;
7
+ var _storagePath = require("./storagePath");
8
+ var _nodeFiles = require("./nodeFiles");
9
+ /**
10
+ * Create a folder of the database, and any missing folders above it (doing nothing if it already exists). Node only.
11
+ * @param path - A path inside the database, such as `users`. Empty for the database folder itself.
12
+ */
13
+ const makeDirectory = async (path = '') => {
14
+ await (0, _nodeFiles.nodeFiles)().mkdir((0, _storagePath.storagePath)(path), {
15
+ recursive: true
16
+ });
17
+ };
18
+ exports.makeDirectory = makeDirectory;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.makeDirectory=void 0;var _storagePath=require("./storagePath"),_nodeFiles=require("./nodeFiles");const makeDirectory=async(e="")=>{await(0,_nodeFiles.nodeFiles)().mkdir((0,_storagePath.storagePath)(e),{recursive:!0})};exports.makeDirectory=makeDirectory;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Node's `fs/promises`, loaded when a storage function runs rather than when the module is imported, so the browser
3
+ * bundle (which leaves `fs` out) can still import the storage module.
4
+ */
5
+ export declare const nodeFiles: () => any;
6
+ //# sourceMappingURL=nodeFiles.d.ts.map
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.nodeFiles = void 0;
7
+ /**
8
+ * Node's `fs/promises`, loaded when a storage function runs rather than when the module is imported, so the browser
9
+ * bundle (which leaves `fs` out) can still import the storage module.
10
+ */
11
+ const nodeFiles = () => require('fs/promises');
12
+ exports.nodeFiles = nodeFiles;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.nodeFiles=void 0;const nodeFiles=()=>require("fs/promises");exports.nodeFiles=nodeFiles;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Run `task` with a read cache for the configured database: while it runs, each file is read from storage once and
3
+ * later reads of it are answered from memory, so a join no longer reads the joined record's files again for every row.
4
+ * Writes and deletes made during the task update the cache (see cacheWrite, cacheRemove), so a query reads its own
5
+ * writes. Outside any task nothing is cached and every read goes to storage, as before.
6
+ *
7
+ * The cache holds text, not parsed objects: every read parses its own copy, so a caller changing what it read (a join
8
+ * attaches rows to the rows it read) never changes what the next reader gets.
9
+ *
10
+ * withDatabaseLock runs every public call (query, create, drop, describe) inside one, so the cache lasts one call. In
11
+ * Node calls on one database never overlap (the lock queues them). In a browser they can, and a read can't tell which
12
+ * call it belongs to, so each call that starts empties the cache: calls still running carry on (only reading their
13
+ * files again), and nothing is older than the newest call, however long calls keep overlapping.
14
+ * @param task
15
+ */
16
+ export declare const withReadCache: <T>(task: () => Promise<T>) => Promise<T>;
17
+ /**
18
+ * The text of the file at `location`: from the cache when a call is running and the file was read (or written) during
19
+ * it, otherwise from `read`, kept for the rest of the call. A failed read isn't kept, so it fails (or succeeds) afresh
20
+ * the next time.
21
+ * @param location - The file's full location (see storagePath).
22
+ * @param read - Reads the file's text from storage.
23
+ */
24
+ export declare const cachedRead: (location: string, read: () => Promise<string>) => Promise<string>;
25
+ /**
26
+ * Keep the text just written to `location`, so the rest of the call reads it without going back to storage.
27
+ * @param location - The file's full location (see storagePath).
28
+ * @param text
29
+ */
30
+ export declare const cacheWrite: (location: string, text: string) => void;
31
+ /**
32
+ * Forget a removed file, or (with `folder`) everything under a removed folder.
33
+ * @param location - The full location (see storagePath) of the file or folder.
34
+ * @param folder - Whether `location` is a folder.
35
+ */
36
+ export declare const cacheRemove: (location: string, folder?: boolean) => void;
37
+ //# sourceMappingURL=readCache.d.ts.map
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.withReadCache = exports.cachedRead = exports.cacheWrite = exports.cacheRemove = void 0;
7
+ require("core-js/modules/esnext.map.delete-all.js");
8
+ require("core-js/modules/esnext.map.every.js");
9
+ require("core-js/modules/esnext.map.filter.js");
10
+ require("core-js/modules/esnext.map.find.js");
11
+ require("core-js/modules/esnext.map.find-key.js");
12
+ require("core-js/modules/esnext.map.includes.js");
13
+ require("core-js/modules/esnext.map.key-of.js");
14
+ require("core-js/modules/esnext.map.map-keys.js");
15
+ require("core-js/modules/esnext.map.map-values.js");
16
+ require("core-js/modules/esnext.map.merge.js");
17
+ require("core-js/modules/esnext.map.reduce.js");
18
+ require("core-js/modules/esnext.map.some.js");
19
+ require("core-js/modules/esnext.map.update.js");
20
+ var _storagePath = require("./storagePath");
21
+ const scopes = new Map();
22
+ /**
23
+ * The scope of the database configured now (by its location, see storagePath), if a call on it is running.
24
+ */
25
+ const currentScope = () => scopes.get((0, _storagePath.storagePath)());
26
+ /**
27
+ * Run `task` with a read cache for the configured database: while it runs, each file is read from storage once and
28
+ * later reads of it are answered from memory, so a join no longer reads the joined record's files again for every row.
29
+ * Writes and deletes made during the task update the cache (see cacheWrite, cacheRemove), so a query reads its own
30
+ * writes. Outside any task nothing is cached and every read goes to storage, as before.
31
+ *
32
+ * The cache holds text, not parsed objects: every read parses its own copy, so a caller changing what it read (a join
33
+ * attaches rows to the rows it read) never changes what the next reader gets.
34
+ *
35
+ * withDatabaseLock runs every public call (query, create, drop, describe) inside one, so the cache lasts one call. In
36
+ * Node calls on one database never overlap (the lock queues them). In a browser they can, and a read can't tell which
37
+ * call it belongs to, so each call that starts empties the cache: calls still running carry on (only reading their
38
+ * files again), and nothing is older than the newest call, however long calls keep overlapping.
39
+ * @param task
40
+ */
41
+ const withReadCache = async task => {
42
+ const database = (0, _storagePath.storagePath)();
43
+ const scope = scopes.get(database) || {
44
+ calls: 0,
45
+ files: new Map()
46
+ };
47
+ scopes.set(database, scope);
48
+ scope.calls++;
49
+ if (scope.calls > 1) {
50
+ scope.files = new Map();
51
+ }
52
+ try {
53
+ return await task();
54
+ } finally {
55
+ scope.calls--;
56
+ if (!scope.calls && scopes.get(database) === scope) {
57
+ scopes.delete(database);
58
+ }
59
+ }
60
+ };
61
+ /**
62
+ * The text of the file at `location`: from the cache when a call is running and the file was read (or written) during
63
+ * it, otherwise from `read`, kept for the rest of the call. A failed read isn't kept, so it fails (or succeeds) afresh
64
+ * the next time.
65
+ * @param location - The file's full location (see storagePath).
66
+ * @param read - Reads the file's text from storage.
67
+ */
68
+ exports.withReadCache = withReadCache;
69
+ const cachedRead = (location, read) => {
70
+ const scope = currentScope();
71
+ if (!scope) {
72
+ return read();
73
+ }
74
+ const cached = scope.files.get(location);
75
+ if (cached) {
76
+ return cached;
77
+ }
78
+ const reading = read();
79
+ scope.files.set(location, reading);
80
+ reading.catch(() => {
81
+ if (scope.files.get(location) === reading) {
82
+ scope.files.delete(location);
83
+ }
84
+ });
85
+ return reading;
86
+ };
87
+ /**
88
+ * Keep the text just written to `location`, so the rest of the call reads it without going back to storage.
89
+ * @param location - The file's full location (see storagePath).
90
+ * @param text
91
+ */
92
+ exports.cachedRead = cachedRead;
93
+ const cacheWrite = (location, text) => {
94
+ currentScope()?.files.set(location, Promise.resolve(text));
95
+ };
96
+ /**
97
+ * Forget a removed file, or (with `folder`) everything under a removed folder.
98
+ * @param location - The full location (see storagePath) of the file or folder.
99
+ * @param folder - Whether `location` is a folder.
100
+ */
101
+ exports.cacheWrite = cacheWrite;
102
+ const cacheRemove = (location, folder = false) => {
103
+ const scope = currentScope();
104
+ if (!scope) {
105
+ return;
106
+ }
107
+ scope.files.delete(location);
108
+ if (folder) {
109
+ const prefix = location.endsWith('/') ? location : `${location}/`;
110
+ for (const cachedLocation of [...scope.files.keys()]) {
111
+ if (cachedLocation.startsWith(prefix)) {
112
+ scope.files.delete(cachedLocation);
113
+ }
114
+ }
115
+ }
116
+ };
117
+ exports.cacheRemove = cacheRemove;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.withReadCache=exports.cachedRead=exports.cacheWrite=exports.cacheRemove=void 0,require("core-js/modules/esnext.map.delete-all.js"),require("core-js/modules/esnext.map.every.js"),require("core-js/modules/esnext.map.filter.js"),require("core-js/modules/esnext.map.find.js"),require("core-js/modules/esnext.map.find-key.js"),require("core-js/modules/esnext.map.includes.js"),require("core-js/modules/esnext.map.key-of.js"),require("core-js/modules/esnext.map.map-keys.js"),require("core-js/modules/esnext.map.map-values.js"),require("core-js/modules/esnext.map.merge.js"),require("core-js/modules/esnext.map.reduce.js"),require("core-js/modules/esnext.map.some.js"),require("core-js/modules/esnext.map.update.js");var _storagePath=require("./storagePath");const scopes=new Map,currentScope=()=>scopes.get((0,_storagePath.storagePath)()),withReadCache=async e=>{const s=(0,_storagePath.storagePath)(),r=scopes.get(s)||{calls:0,files:new Map};scopes.set(s,r),r.calls++,r.calls>1&&(r.files=new Map);try{return await e()}finally{r.calls--,r.calls||scopes.get(s)!==r||scopes.delete(s)}};exports.withReadCache=withReadCache;const cachedRead=(e,s)=>{const r=currentScope();if(!r)return s();const t=r.files.get(e);if(t)return t;const c=s();return r.files.set(e,c),c.catch((()=>{r.files.get(e)===c&&r.files.delete(e)})),c};exports.cachedRead=cachedRead;const cacheWrite=(e,s)=>{currentScope()?.files.set(e,Promise.resolve(s))};exports.cacheWrite=cacheWrite;const cacheRemove=(e,s=!1)=>{const r=currentScope();if(r&&(r.files.delete(e),s)){const s=e.endsWith("/")?e:`${e}/`;for(const e of[...r.files.keys()])e.startsWith(s)&&r.files.delete(e)}};exports.cacheRemove=cacheRemove;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Read and parse one JSON file of the database: from disk in Node, over `fetch()` in a browser (see storagePath for
3
+ * where it's looked for). During a call, a file already read is answered from the read cache (see withReadCache).
4
+ * @param path - A path inside the database, such as `users.json`.
5
+ * @throws {Error} When the file can't be read or isn't valid JSON (the error from the file system, fetch or JSON.parse).
6
+ */
7
+ export declare const readJson: (path: string) => Promise<any>;
8
+ //# sourceMappingURL=readJson.d.ts.map
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ require("core-js/modules/esnext.weak-map.delete-all.js");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.readJson = void 0;
8
+ var jsEnv = _interopRequireWildcard(require("browser-or-node"));
9
+ var _storagePath = require("./storagePath");
10
+ var _nodeFiles = require("./nodeFiles");
11
+ var _readCache = require("./readCache");
12
+ var e;
13
+ function _interopRequireWildcard(t, r) { if (!r && t && t.__esModule) return t; var n, a, o = Object.defineProperty, f = { __proto__: null, default: t }; if (Object(t) !== t) return f; if (e || "function" != typeof WeakMap || (e = new WeakMap()), e) { if (e.has(t)) return e.get(t); e.set(t, f); } for (a in t) "default" !== a && {}.hasOwnProperty.call(t, a) && ((n = o && Object.getOwnPropertyDescriptor(t, a)) && (n.get || n.set) ? o(f, a, n) : f[a] = t[a]); return f; }
14
+ /**
15
+ * Read and parse one JSON file of the database: from disk in Node, over `fetch()` in a browser (see storagePath for
16
+ * where it's looked for). During a call, a file already read is answered from the read cache (see withReadCache).
17
+ * @param path - A path inside the database, such as `users.json`.
18
+ * @throws {Error} When the file can't be read or isn't valid JSON (the error from the file system, fetch or JSON.parse).
19
+ */
20
+ const readJson = async path => {
21
+ const location = (0, _storagePath.storagePath)(path);
22
+ const text = await (0, _readCache.cachedRead)(location, () => jsEnv.isNode ? (0, _nodeFiles.nodeFiles)().readFile(location, 'utf8') : fetch(location).then(response => response.text()));
23
+ return JSON.parse(text);
24
+ };
25
+ exports.readJson = readJson;
@@ -0,0 +1 @@
1
+ "use strict";require("core-js/modules/esnext.weak-map.delete-all.js"),Object.defineProperty(exports,"__esModule",{value:!0}),exports.readJson=void 0;var e,jsEnv=_interopRequireWildcard(require("browser-or-node")),_storagePath=require("./storagePath"),_nodeFiles=require("./nodeFiles"),_readCache=require("./readCache");function _interopRequireWildcard(r,t){if(!t&&r&&r.__esModule)return r;var a,o,s=Object.defineProperty,n={__proto__:null,default:r};if(Object(r)!==r)return n;if(e||"function"!=typeof WeakMap||(e=new WeakMap),e){if(e.has(r))return e.get(r);e.set(r,n)}for(o in r)"default"!==o&&{}.hasOwnProperty.call(r,o)&&((a=s&&Object.getOwnPropertyDescriptor(r,o))&&(a.get||a.set)?s(n,o,a):n[o]=r[o]);return n}const readJson=async e=>{const r=(0,_storagePath.storagePath)(e),t=await(0,_readCache.cachedRead)(r,(()=>jsEnv.isNode?(0,_nodeFiles.nodeFiles)().readFile(r,"utf8"):fetch(r).then((e=>e.text()))));return JSON.parse(t)};exports.readJson=readJson;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Delete a folder of the database and everything in it; a folder that doesn't exist is left alone. Node only.
3
+ * @param path - A path inside the database, such as `__indexes/users`.
4
+ */
5
+ export declare const removeDirectory: (path: string) => Promise<void>;
6
+ //# sourceMappingURL=removeDirectory.d.ts.map
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeDirectory = void 0;
7
+ var _storagePath = require("./storagePath");
8
+ var _nodeFiles = require("./nodeFiles");
9
+ var _readCache = require("./readCache");
10
+ /**
11
+ * Delete a folder of the database and everything in it; a folder that doesn't exist is left alone. Node only.
12
+ * @param path - A path inside the database, such as `__indexes/users`.
13
+ */
14
+ const removeDirectory = async path => {
15
+ const location = (0, _storagePath.storagePath)(path);
16
+ // Forgotten before and after: a read that starts while rm runs would otherwise keep a removed file's old text
17
+ (0, _readCache.cacheRemove)(location, true);
18
+ await (0, _nodeFiles.nodeFiles)().rm(location, {
19
+ recursive: true,
20
+ force: true
21
+ });
22
+ (0, _readCache.cacheRemove)(location, true);
23
+ };
24
+ exports.removeDirectory = removeDirectory;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.removeDirectory=void 0;var _storagePath=require("./storagePath"),_nodeFiles=require("./nodeFiles"),_readCache=require("./readCache");const removeDirectory=async e=>{const r=(0,_storagePath.storagePath)(e);(0,_readCache.cacheRemove)(r,!0),await(0,_nodeFiles.nodeFiles)().rm(r,{recursive:!0,force:!0}),(0,_readCache.cacheRemove)(r,!0)};exports.removeDirectory=removeDirectory;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Delete one file of the database. Node only.
3
+ * @param path - A path inside the database, such as `users/1.json`.
4
+ * @throws {Error} When the file doesn't exist.
5
+ */
6
+ export declare const removeFile: (path: string) => Promise<void>;
7
+ //# sourceMappingURL=removeFile.d.ts.map
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeFile = void 0;
7
+ var _storagePath = require("./storagePath");
8
+ var _nodeFiles = require("./nodeFiles");
9
+ var _readCache = require("./readCache");
10
+ /**
11
+ * Delete one file of the database. Node only.
12
+ * @param path - A path inside the database, such as `users/1.json`.
13
+ * @throws {Error} When the file doesn't exist.
14
+ */
15
+ const removeFile = async path => {
16
+ const location = (0, _storagePath.storagePath)(path);
17
+ // Forgotten before and after: a read that starts while rm runs would otherwise keep the file's old text
18
+ (0, _readCache.cacheRemove)(location);
19
+ await (0, _nodeFiles.nodeFiles)().rm(location);
20
+ (0, _readCache.cacheRemove)(location);
21
+ };
22
+ exports.removeFile = removeFile;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.removeFile=void 0;var _storagePath=require("./storagePath"),_nodeFiles=require("./nodeFiles"),_readCache=require("./readCache");const removeFile=async e=>{const r=(0,_storagePath.storagePath)(e);(0,_readCache.cacheRemove)(r),await(0,_nodeFiles.nodeFiles)().rm(r),(0,_readCache.cacheRemove)(r)};exports.removeFile=removeFile;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Turn a path inside the database (such as `users.json` or `__indexes/users/name.json`) into the location the files
3
+ * backend uses: the configured databasePath in front of it, and in a browser also the relativePath (the URL prefix the
4
+ * database is served from).
5
+ *
6
+ * Every other part of json-fs-query names files by their path inside the database only; this is the one place that
7
+ * knows where the database is.
8
+ * @param path - A path inside the database, without a leading slash. Empty for the database folder itself.
9
+ */
10
+ export declare const storagePath: (path?: string) => string;
11
+ //# sourceMappingURL=storagePath.d.ts.map
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ require("core-js/modules/esnext.weak-map.delete-all.js");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.storagePath = void 0;
8
+ var jsEnv = _interopRequireWildcard(require("browser-or-node"));
9
+ var _config = require("../utilities/config");
10
+ var e;
11
+ function _interopRequireWildcard(t, r) { if (!r && t && t.__esModule) return t; var n, a, o = Object.defineProperty, f = { __proto__: null, default: t }; if (Object(t) !== t) return f; if (e || "function" != typeof WeakMap || (e = new WeakMap()), e) { if (e.has(t)) return e.get(t); e.set(t, f); } for (a in t) "default" !== a && {}.hasOwnProperty.call(t, a) && ((n = o && Object.getOwnPropertyDescriptor(t, a)) && (n.get || n.set) ? o(f, a, n) : f[a] = t[a]); return f; }
12
+ /**
13
+ * Turn a path inside the database (such as `users.json` or `__indexes/users/name.json`) into the location the files
14
+ * backend uses: the configured databasePath in front of it, and in a browser also the relativePath (the URL prefix the
15
+ * database is served from).
16
+ *
17
+ * Every other part of json-fs-query names files by their path inside the database only; this is the one place that
18
+ * knows where the database is.
19
+ * @param path - A path inside the database, without a leading slash. Empty for the database folder itself.
20
+ */
21
+ const storagePath = (path = '') => {
22
+ const relativePath = jsEnv.isBrowser ? (0, _config.getSetting)('relativePath', '') : '';
23
+ return `${relativePath}${(0, _config.getSetting)('databasePath', 'database/')}${path}`;
24
+ };
25
+ exports.storagePath = storagePath;
@@ -0,0 +1 @@
1
+ "use strict";require("core-js/modules/esnext.weak-map.delete-all.js"),Object.defineProperty(exports,"__esModule",{value:!0}),exports.storagePath=void 0;var e,jsEnv=_interopRequireWildcard(require("browser-or-node")),_config=require("../utilities/config");function _interopRequireWildcard(t,r){if(!r&&t&&t.__esModule)return t;var a,o,i=Object.defineProperty,s={__proto__:null,default:t};if(Object(t)!==t)return s;if(e||"function"!=typeof WeakMap||(e=new WeakMap),e){if(e.has(t))return e.get(t);e.set(t,s)}for(o in t)"default"!==o&&{}.hasOwnProperty.call(t,o)&&((a=i&&Object.getOwnPropertyDescriptor(t,o))&&(a.get||a.set)?i(s,o,a):s[o]=t[o]);return s}const storagePath=(e="")=>`${jsEnv.isBrowser?(0,_config.getSetting)("relativePath",""):""}${(0,_config.getSetting)("databasePath","database/")}${e}`;exports.storagePath=storagePath;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Write `content` to one file of the database as JSON (indented by 2 spaces), replacing the file if it exists. Its
3
+ * folder must already exist (see makeDirectory). Node only.
4
+ * @param path - A path inside the database, such as `users.json`.
5
+ * @param content - Anything JSON.stringify accepts.
6
+ */
7
+ export declare const writeJson: (path: string, content: any) => Promise<void>;
8
+ //# sourceMappingURL=writeJson.d.ts.map
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.writeJson = void 0;
7
+ require("core-js/modules/es.json.stringify.js");
8
+ var _storagePath = require("./storagePath");
9
+ var _nodeFiles = require("./nodeFiles");
10
+ var _readCache = require("./readCache");
11
+ /**
12
+ * Write `content` to one file of the database as JSON (indented by 2 spaces), replacing the file if it exists. Its
13
+ * folder must already exist (see makeDirectory). Node only.
14
+ * @param path - A path inside the database, such as `users.json`.
15
+ * @param content - Anything JSON.stringify accepts.
16
+ */
17
+ const writeJson = async (path, content) => {
18
+ const location = (0, _storagePath.storagePath)(path);
19
+ const text = JSON.stringify(content, null, 2);
20
+ await (0, _nodeFiles.nodeFiles)().writeFile(location, text);
21
+ (0, _readCache.cacheWrite)(location, text);
22
+ };
23
+ exports.writeJson = writeJson;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.writeJson=void 0,require("core-js/modules/es.json.stringify.js");var _storagePath=require("./storagePath"),_nodeFiles=require("./nodeFiles"),_readCache=require("./readCache");const writeJson=async(e,r)=>{const s=(0,_storagePath.storagePath)(e),t=JSON.stringify(r,null,2);await(0,_nodeFiles.nodeFiles)().writeFile(s,t),(0,_readCache.cacheWrite)(s,t)};exports.writeJson=writeJson;
@@ -8,6 +8,7 @@
8
8
  * @param value
9
9
  */
10
10
  export declare const compareAs: (type: string | undefined, value: any) => any;
11
+ export declare const comparableTypes: string[];
11
12
  /**
12
13
  * Whether two values are equal for a field of the given declared type: both are normalized to
13
14
  * that type (see compareAs) and compared strictly. A field with no declared type, or one that