json-fs-query 0.1.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 (336) hide show
  1. package/.browserslistrc +1 -0
  2. package/.nvmrc +1 -0
  3. package/LICENSE +674 -0
  4. package/MAIN.md +15 -0
  5. package/README.md +63 -0
  6. package/babel.config.js +3 -0
  7. package/build-tools.config.json +30 -0
  8. package/dist/data/introspect/create.d.ts +11 -0
  9. package/dist/data/introspect/create.js +45 -0
  10. package/dist/data/introspect/create.min.js +1 -0
  11. package/dist/data/introspect/definitions/field.d.ts +28 -0
  12. package/dist/data/introspect/definitions/field.js +36 -0
  13. package/dist/data/introspect/definitions/field.min.js +1 -0
  14. package/dist/data/introspect/definitions/key.d.ts +26 -0
  15. package/dist/data/introspect/definitions/key.js +53 -0
  16. package/dist/data/introspect/definitions/key.min.js +1 -0
  17. package/dist/data/introspect/definitions/keyReference.d.ts +18 -0
  18. package/dist/data/introspect/definitions/keyReference.js +20 -0
  19. package/dist/data/introspect/definitions/keyReference.min.js +1 -0
  20. package/dist/data/introspect/definitions/record.d.ts +26 -0
  21. package/dist/data/introspect/definitions/record.js +32 -0
  22. package/dist/data/introspect/definitions/record.min.js +1 -0
  23. package/dist/data/introspect/describe.d.ts +2 -0
  24. package/dist/data/introspect/describe.js +15 -0
  25. package/dist/data/introspect/describe.min.js +1 -0
  26. package/dist/data/introspect/drop.d.ts +2 -0
  27. package/dist/data/introspect/drop.js +37 -0
  28. package/dist/data/introspect/drop.min.js +1 -0
  29. package/dist/data/introspect/index.d.ts +8 -0
  30. package/dist/data/introspect/index.js +30 -0
  31. package/dist/data/introspect/index.min.js +1 -0
  32. package/dist/data/introspect/policies/foreignKeyExists.d.ts +9 -0
  33. package/dist/data/introspect/policies/foreignKeyExists.js +25 -0
  34. package/dist/data/introspect/policies/foreignKeyExists.min.js +1 -0
  35. package/dist/data/introspect/policies/keyGenerate.d.ts +2 -0
  36. package/dist/data/introspect/policies/keyGenerate.js +17 -0
  37. package/dist/data/introspect/policies/keyGenerate.min.js +1 -0
  38. package/dist/data/introspect/policies/keyUnique.d.ts +2 -0
  39. package/dist/data/introspect/policies/keyUnique.js +21 -0
  40. package/dist/data/introspect/policies/keyUnique.min.js +1 -0
  41. package/dist/data/introspect/policies/typeCheck.d.ts +12 -0
  42. package/dist/data/introspect/policies/typeCheck.js +57 -0
  43. package/dist/data/introspect/policies/typeCheck.min.js +1 -0
  44. package/dist/data/introspect/policies/typeEnum.d.ts +2 -0
  45. package/dist/data/introspect/policies/typeEnum.js +14 -0
  46. package/dist/data/introspect/policies/typeEnum.min.js +1 -0
  47. package/dist/data/introspect/policies/useDefault.d.ts +2 -0
  48. package/dist/data/introspect/policies/useDefault.js +13 -0
  49. package/dist/data/introspect/policies/useDefault.min.js +1 -0
  50. package/dist/data/queries/deleteEntity.d.ts +2 -0
  51. package/dist/data/queries/deleteEntity.js +79 -0
  52. package/dist/data/queries/deleteEntity.min.js +1 -0
  53. package/dist/data/queries/groupBy.d.ts +2 -0
  54. package/dist/data/queries/groupBy.js +22 -0
  55. package/dist/data/queries/groupBy.min.js +1 -0
  56. package/dist/data/queries/insertEntity.d.ts +2 -0
  57. package/dist/data/queries/insertEntity.js +115 -0
  58. package/dist/data/queries/insertEntity.min.js +1 -0
  59. package/dist/data/queries/joinEntity.d.ts +2 -0
  60. package/dist/data/queries/joinEntity.js +27 -0
  61. package/dist/data/queries/joinEntity.min.js +1 -0
  62. package/dist/data/queries/mergeJoins.d.ts +2 -0
  63. package/dist/data/queries/mergeJoins.js +19 -0
  64. package/dist/data/queries/mergeJoins.min.js +1 -0
  65. package/dist/data/queries/parser.d.ts +2 -0
  66. package/dist/data/queries/parser.js +30 -0
  67. package/dist/data/queries/parser.min.js +1 -0
  68. package/dist/data/queries/query.d.ts +2 -0
  69. package/dist/data/queries/query.js +18 -0
  70. package/dist/data/queries/query.min.js +1 -0
  71. package/dist/data/queries/readEntity.d.ts +2 -0
  72. package/dist/data/queries/readEntity.js +14 -0
  73. package/dist/data/queries/readEntity.min.js +1 -0
  74. package/dist/data/queries/sortBy.d.ts +2 -0
  75. package/dist/data/queries/sortBy.js +25 -0
  76. package/dist/data/queries/sortBy.min.js +1 -0
  77. package/dist/data/queries/updateEntity.d.ts +2 -0
  78. package/dist/data/queries/updateEntity.js +164 -0
  79. package/dist/data/queries/updateEntity.min.js +1 -0
  80. package/dist/data/queries/where.d.ts +7 -0
  81. package/dist/data/queries/where.js +56 -0
  82. package/dist/data/queries/where.min.js +1 -0
  83. package/dist/data/utilities/deregisterRecord.d.ts +8 -0
  84. package/dist/data/utilities/deregisterRecord.js +34 -0
  85. package/dist/data/utilities/deregisterRecord.min.js +1 -0
  86. package/dist/data/utilities/getConditionIndexedList.d.ts +3 -0
  87. package/dist/data/utilities/getConditionIndexedList.js +47 -0
  88. package/dist/data/utilities/getConditionIndexedList.min.js +1 -0
  89. package/dist/data/utilities/getJoinIndexedList.d.ts +3 -0
  90. package/dist/data/utilities/getJoinIndexedList.js +59 -0
  91. package/dist/data/utilities/getJoinIndexedList.min.js +1 -0
  92. package/dist/data/utilities/getMatchingIndexes.d.ts +2 -0
  93. package/dist/data/utilities/getMatchingIndexes.js +28 -0
  94. package/dist/data/utilities/getMatchingIndexes.min.js +1 -0
  95. package/dist/data/utilities/matchIndexedJoins.d.ts +2 -0
  96. package/dist/data/utilities/matchIndexedJoins.js +61 -0
  97. package/dist/data/utilities/matchIndexedJoins.min.js +1 -0
  98. package/dist/data/utilities/mergeJoin.d.ts +2 -0
  99. package/dist/data/utilities/mergeJoin.js +44 -0
  100. package/dist/data/utilities/mergeJoin.min.js +1 -0
  101. package/dist/data/utilities/parsers/findCommand.d.ts +10 -0
  102. package/dist/data/utilities/parsers/findCommand.js +25 -0
  103. package/dist/data/utilities/parsers/findCommand.min.js +1 -0
  104. package/dist/data/utilities/parsers/findConditions.d.ts +15 -0
  105. package/dist/data/utilities/parsers/findConditions.js +28 -0
  106. package/dist/data/utilities/parsers/findConditions.min.js +1 -0
  107. package/dist/data/utilities/parsers/findGroupBy.d.ts +9 -0
  108. package/dist/data/utilities/parsers/findGroupBy.js +22 -0
  109. package/dist/data/utilities/parsers/findGroupBy.min.js +1 -0
  110. package/dist/data/utilities/parsers/findInsertValues.d.ts +13 -0
  111. package/dist/data/utilities/parsers/findInsertValues.js +34 -0
  112. package/dist/data/utilities/parsers/findInsertValues.min.js +1 -0
  113. package/dist/data/utilities/parsers/findJoinClauses.d.ts +13 -0
  114. package/dist/data/utilities/parsers/findJoinClauses.js +27 -0
  115. package/dist/data/utilities/parsers/findJoinClauses.min.js +1 -0
  116. package/dist/data/utilities/parsers/findJoinEntity.d.ts +9 -0
  117. package/dist/data/utilities/parsers/findJoinEntity.js +22 -0
  118. package/dist/data/utilities/parsers/findJoinEntity.min.js +1 -0
  119. package/dist/data/utilities/parsers/findLimit.d.ts +9 -0
  120. package/dist/data/utilities/parsers/findLimit.js +22 -0
  121. package/dist/data/utilities/parsers/findLimit.min.js +1 -0
  122. package/dist/data/utilities/parsers/findMergeJoins.d.ts +12 -0
  123. package/dist/data/utilities/parsers/findMergeJoins.js +26 -0
  124. package/dist/data/utilities/parsers/findMergeJoins.min.js +1 -0
  125. package/dist/data/utilities/parsers/findOffset.d.ts +9 -0
  126. package/dist/data/utilities/parsers/findOffset.js +22 -0
  127. package/dist/data/utilities/parsers/findOffset.min.js +1 -0
  128. package/dist/data/utilities/parsers/findSelects.d.ts +12 -0
  129. package/dist/data/utilities/parsers/findSelects.js +30 -0
  130. package/dist/data/utilities/parsers/findSelects.min.js +1 -0
  131. package/dist/data/utilities/parsers/findSortClauses.d.ts +12 -0
  132. package/dist/data/utilities/parsers/findSortClauses.js +30 -0
  133. package/dist/data/utilities/parsers/findSortClauses.min.js +1 -0
  134. package/dist/data/utilities/parsers/findUpdateValues.d.ts +15 -0
  135. package/dist/data/utilities/parsers/findUpdateValues.js +34 -0
  136. package/dist/data/utilities/parsers/findUpdateValues.min.js +1 -0
  137. package/dist/data/utilities/parsers/isEntityProperty.d.ts +2 -0
  138. package/dist/data/utilities/parsers/isEntityProperty.js +8 -0
  139. package/dist/data/utilities/parsers/isEntityProperty.min.js +1 -0
  140. package/dist/data/utilities/parsers/parseLiteralValue.d.ts +12 -0
  141. package/dist/data/utilities/parsers/parseLiteralValue.js +34 -0
  142. package/dist/data/utilities/parsers/parseLiteralValue.min.js +1 -0
  143. package/dist/data/utilities/parsers/parseQuery.d.ts +33 -0
  144. package/dist/data/utilities/parsers/parseQuery.js +51 -0
  145. package/dist/data/utilities/parsers/parseQuery.min.js +1 -0
  146. package/dist/data/utilities/parsers/removeFromQuery.d.ts +2 -0
  147. package/dist/data/utilities/parsers/removeFromQuery.js +8 -0
  148. package/dist/data/utilities/parsers/removeFromQuery.min.js +1 -0
  149. package/dist/data/utilities/parsers/splitEntityProperty.d.ts +5 -0
  150. package/dist/data/utilities/parsers/splitEntityProperty.js +23 -0
  151. package/dist/data/utilities/parsers/splitEntityProperty.min.js +1 -0
  152. package/dist/data/utilities/parsers/splitQueries.d.ts +2 -0
  153. package/dist/data/utilities/parsers/splitQueries.js +11 -0
  154. package/dist/data/utilities/parsers/splitQueries.min.js +1 -0
  155. package/dist/data/utilities/reconcileJoin.d.ts +2 -0
  156. package/dist/data/utilities/reconcileJoin.js +26 -0
  157. package/dist/data/utilities/reconcileJoin.min.js +1 -0
  158. package/dist/data/utilities/reconcileJoins.d.ts +2 -0
  159. package/dist/data/utilities/reconcileJoins.js +27 -0
  160. package/dist/data/utilities/reconcileJoins.min.js +1 -0
  161. package/dist/data/utilities/registerRecord.d.ts +8 -0
  162. package/dist/data/utilities/registerRecord.js +67 -0
  163. package/dist/data/utilities/registerRecord.min.js +1 -0
  164. package/dist/data/utilities/removeFromIndex.d.ts +14 -0
  165. package/dist/data/utilities/removeFromIndex.js +46 -0
  166. package/dist/data/utilities/removeFromIndex.min.js +1 -0
  167. package/dist/data/utilities/restrictIndexedList.d.ts +2 -0
  168. package/dist/data/utilities/restrictIndexedList.js +45 -0
  169. package/dist/data/utilities/restrictIndexedList.min.js +1 -0
  170. package/dist/data/utilities/retrieveFile.d.ts +2 -0
  171. package/dist/data/utilities/retrieveFile.js +28 -0
  172. package/dist/data/utilities/retrieveFile.min.js +1 -0
  173. package/dist/data/utilities/retrieveIndexes.d.ts +2 -0
  174. package/dist/data/utilities/retrieveIndexes.js +24 -0
  175. package/dist/data/utilities/retrieveIndexes.min.js +1 -0
  176. package/dist/data/utilities/retrieveRecord.d.ts +2 -0
  177. package/dist/data/utilities/retrieveRecord.js +9 -0
  178. package/dist/data/utilities/retrieveRecord.min.js +1 -0
  179. package/dist/data/utilities/retrieveRecords.d.ts +2 -0
  180. package/dist/data/utilities/retrieveRecords.js +21 -0
  181. package/dist/data/utilities/retrieveRecords.min.js +1 -0
  182. package/dist/data/utilities/runQuery.d.ts +2 -0
  183. package/dist/data/utilities/runQuery.js +72 -0
  184. package/dist/data/utilities/runQuery.min.js +1 -0
  185. package/dist/data/utilities/updateIndex.d.ts +17 -0
  186. package/dist/data/utilities/updateIndex.js +56 -0
  187. package/dist/data/utilities/updateIndex.min.js +1 -0
  188. package/dist/data/utilities/useJoinClause.d.ts +6 -0
  189. package/dist/data/utilities/useJoinClause.js +51 -0
  190. package/dist/data/utilities/useJoinClause.min.js +1 -0
  191. package/dist/index.d.ts +4 -0
  192. package/dist/index.js +35 -0
  193. package/dist/index.min.js +1 -0
  194. package/gulpfile.mjs +15 -0
  195. package/package.json +44 -0
  196. package/src/data/introspect/create.test.js +67 -0
  197. package/src/data/introspect/create.ts +29 -0
  198. package/src/data/introspect/definitions/field.test.js +23 -0
  199. package/src/data/introspect/definitions/field.ts +48 -0
  200. package/src/data/introspect/definitions/key.test.js +100 -0
  201. package/src/data/introspect/definitions/key.ts +64 -0
  202. package/src/data/introspect/definitions/keyReference.test.js +8 -0
  203. package/src/data/introspect/definitions/keyReference.ts +23 -0
  204. package/src/data/introspect/definitions/record.ts +42 -0
  205. package/src/data/introspect/describe.test.js +36 -0
  206. package/src/data/introspect/describe.ts +9 -0
  207. package/src/data/introspect/drop.test.js +77 -0
  208. package/src/data/introspect/drop.ts +20 -0
  209. package/src/data/introspect/index.ts +8 -0
  210. package/src/data/introspect/policies/foreignKeyExists.test.js +44 -0
  211. package/src/data/introspect/policies/foreignKeyExists.ts +15 -0
  212. package/src/data/introspect/policies/keyGenerate.ts +11 -0
  213. package/src/data/introspect/policies/keyUnique.ts +11 -0
  214. package/src/data/introspect/policies/typeCheck.test.js +84 -0
  215. package/src/data/introspect/policies/typeCheck.ts +49 -0
  216. package/src/data/introspect/policies/typeEnum.ts +6 -0
  217. package/src/data/introspect/policies/useDefault.ts +6 -0
  218. package/src/data/queries/deleteEntity.test.js +86 -0
  219. package/src/data/queries/deleteEntity.ts +56 -0
  220. package/src/data/queries/groupBy.test.js +135 -0
  221. package/src/data/queries/groupBy.ts +16 -0
  222. package/src/data/queries/insertEntity.test.js +156 -0
  223. package/src/data/queries/insertEntity.ts +113 -0
  224. package/src/data/queries/joinEntity.test.js +109 -0
  225. package/src/data/queries/joinEntity.ts +20 -0
  226. package/src/data/queries/mergeJoins.test.js +174 -0
  227. package/src/data/queries/mergeJoins.ts +13 -0
  228. package/src/data/queries/parser.test.js +343 -0
  229. package/src/data/queries/parser.ts +24 -0
  230. package/src/data/queries/query.test.js +233 -0
  231. package/src/data/queries/query.ts +12 -0
  232. package/src/data/queries/readEntity.test.js +85 -0
  233. package/src/data/queries/readEntity.ts +8 -0
  234. package/src/data/queries/sortBy.test.js +131 -0
  235. package/src/data/queries/sortBy.ts +18 -0
  236. package/src/data/queries/updateEntity.test.js +228 -0
  237. package/src/data/queries/updateEntity.ts +162 -0
  238. package/src/data/queries/where.test.js +129 -0
  239. package/src/data/queries/where.ts +54 -0
  240. package/src/data/utilities/deregisterRecord.test.js +26 -0
  241. package/src/data/utilities/deregisterRecord.ts +24 -0
  242. package/src/data/utilities/getConditionIndexedList.ts +43 -0
  243. package/src/data/utilities/getJoinIndexedList.ts +53 -0
  244. package/src/data/utilities/getMatchingIndexes.ts +21 -0
  245. package/src/data/utilities/matchIndexedJoins.ts +45 -0
  246. package/src/data/utilities/mergeJoin.test.js +102 -0
  247. package/src/data/utilities/mergeJoin.ts +28 -0
  248. package/src/data/utilities/parsers/findCommand.ts +25 -0
  249. package/src/data/utilities/parsers/findConditions.test.js +38 -0
  250. package/src/data/utilities/parsers/findConditions.ts +32 -0
  251. package/src/data/utilities/parsers/findGroupBy.ts +20 -0
  252. package/src/data/utilities/parsers/findInsertValues.test.js +26 -0
  253. package/src/data/utilities/parsers/findInsertValues.ts +32 -0
  254. package/src/data/utilities/parsers/findJoinClauses.ts +29 -0
  255. package/src/data/utilities/parsers/findJoinEntity.ts +20 -0
  256. package/src/data/utilities/parsers/findLimit.ts +20 -0
  257. package/src/data/utilities/parsers/findMergeJoins.ts +27 -0
  258. package/src/data/utilities/parsers/findOffset.ts +20 -0
  259. package/src/data/utilities/parsers/findSelects.ts +31 -0
  260. package/src/data/utilities/parsers/findSortClauses.ts +31 -0
  261. package/src/data/utilities/parsers/findUpdateValues.test.js +37 -0
  262. package/src/data/utilities/parsers/findUpdateValues.ts +32 -0
  263. package/src/data/utilities/parsers/isEntityProperty.ts +1 -0
  264. package/src/data/utilities/parsers/parseLiteralValue.test.js +41 -0
  265. package/src/data/utilities/parsers/parseLiteralValue.ts +29 -0
  266. package/src/data/utilities/parsers/parseQuery.ts +73 -0
  267. package/src/data/utilities/parsers/removeFromQuery.ts +1 -0
  268. package/src/data/utilities/parsers/splitEntityProperty.test.js +8 -0
  269. package/src/data/utilities/parsers/splitEntityProperty.ts +17 -0
  270. package/src/data/utilities/parsers/splitQueries.test.js +33 -0
  271. package/src/data/utilities/parsers/splitQueries.ts +4 -0
  272. package/src/data/utilities/reconcileJoin.test.js +38 -0
  273. package/src/data/utilities/reconcileJoin.ts +23 -0
  274. package/src/data/utilities/reconcileJoins.test.js +160 -0
  275. package/src/data/utilities/reconcileJoins.ts +15 -0
  276. package/src/data/utilities/registerRecord.test.js +44 -0
  277. package/src/data/utilities/registerRecord.ts +44 -0
  278. package/src/data/utilities/removeFromIndex.test.js +44 -0
  279. package/src/data/utilities/removeFromIndex.ts +34 -0
  280. package/src/data/utilities/restrictIndexedList.ts +38 -0
  281. package/src/data/utilities/retrieveFile.test.js +49 -0
  282. package/src/data/utilities/retrieveFile.ts +19 -0
  283. package/src/data/utilities/retrieveIndexes.ts +17 -0
  284. package/src/data/utilities/retrieveRecord.ts +3 -0
  285. package/src/data/utilities/retrieveRecords.test.js +223 -0
  286. package/src/data/utilities/retrieveRecords.ts +16 -0
  287. package/src/data/utilities/runQuery.test.js +88 -0
  288. package/src/data/utilities/runQuery.ts +75 -0
  289. package/src/data/utilities/updateIndex.test.js +44 -0
  290. package/src/data/utilities/updateIndex.ts +45 -0
  291. package/src/data/utilities/useJoinClause.test.js +94 -0
  292. package/src/data/utilities/useJoinClause.ts +44 -0
  293. package/src/global.d.ts +1 -0
  294. package/src/index.test.js +52 -0
  295. package/src/index.ts +17 -0
  296. package/test-data/__RECORDS.json +32 -0
  297. package/test-data/__indexes/exercise_muscle/fk_exercise_id.json +14 -0
  298. package/test-data/__indexes/exercise_muscle/fk_muscle_id.json +14 -0
  299. package/test-data/__indexes/exercise_muscle/pk__id.json +14 -0
  300. package/test-data/__indexes/exercises/pk__id.json +14 -0
  301. package/test-data/__indexes/muscles/pk__id.json +58 -0
  302. package/test-data/__indexes/workouts/fk_exercise_id.json +23 -0
  303. package/test-data/__indexes/workouts/index_date.json +18 -0
  304. package/test-data/__indexes/workouts/pk__id.json +26 -0
  305. package/test-data/copyDatabase.ts +64 -0
  306. package/test-data/exercise_muscle/bench_press_pectorals.json +6 -0
  307. package/test-data/exercise_muscle/deadlift_hamstrings.json +6 -0
  308. package/test-data/exercise_muscle/squat_quadriceps.json +6 -0
  309. package/test-data/exercise_muscle.json +69 -0
  310. package/test-data/exercises/bench_press.json +5 -0
  311. package/test-data/exercises/deadlift.json +5 -0
  312. package/test-data/exercises/squat.json +5 -0
  313. package/test-data/exercises.json +34 -0
  314. package/test-data/muscles/abdominal.json +6 -0
  315. package/test-data/muscles/biceps.json +6 -0
  316. package/test-data/muscles/calves.json +6 -0
  317. package/test-data/muscles/deltoids.json +6 -0
  318. package/test-data/muscles/erector_spinae.json +6 -0
  319. package/test-data/muscles/forearms.json +6 -0
  320. package/test-data/muscles/gluteus_maximus.json +6 -0
  321. package/test-data/muscles/hamstrings.json +6 -0
  322. package/test-data/muscles/latissimus_dorsi.json +6 -0
  323. package/test-data/muscles/obliques.json +6 -0
  324. package/test-data/muscles/pectorals.json +6 -0
  325. package/test-data/muscles/quadriceps.json +6 -0
  326. package/test-data/muscles/trapezius.json +6 -0
  327. package/test-data/muscles/triceps.json +6 -0
  328. package/test-data/muscles.json +50 -0
  329. package/test-data/workouts/2024-06-02-001.json +10 -0
  330. package/test-data/workouts/2024-06-02-002.json +10 -0
  331. package/test-data/workouts/2024-06-02-003.json +10 -0
  332. package/test-data/workouts/2024-06-05-001.json +10 -0
  333. package/test-data/workouts/2024-06-05-002.json +10 -0
  334. package/test-data/workouts/2024-06-05-003.json +10 -0
  335. package/test-data/workouts.json +84 -0
  336. package/tsconfig.json +15 -0
@@ -0,0 +1,75 @@
1
+ import { deleteEntity } from '../queries/deleteEntity'
2
+ import { insertEntity } from '../queries/insertEntity'
3
+ import { readEntity } from '../queries/readEntity'
4
+ import { updateEntity } from '../queries/updateEntity'
5
+ import { joinEntity } from '../queries/joinEntity'
6
+ import { mergeJoins } from '../queries/mergeJoins'
7
+ import { where } from '../queries/where'
8
+ import { sortBy } from '../queries/sortBy'
9
+ import { groupBy } from '../queries/groupBy'
10
+ import { retrieveRecord } from './retrieveRecord'
11
+ import { getConditionIndexedList } from './getConditionIndexedList'
12
+ import { getJoinIndexedList } from './getJoinIndexedList'
13
+
14
+ export const runQuery = async (parsed) => {
15
+ const recordData = {
16
+ [parsed.entity]: await retrieveRecord(parsed.entity)
17
+ }
18
+ if (typeof parsed.joinEntity !== 'undefined') {
19
+ recordData[parsed.joinEntity] = await retrieveRecord(parsed.joinEntity)
20
+ }
21
+
22
+ let dataSet: any = { [parsed.entity]: [] }
23
+
24
+ parsed = await getConditionIndexedList(parsed, recordData, dataSet)
25
+ parsed = await getJoinIndexedList(parsed, recordData, dataSet)
26
+
27
+ // delete/update act on whatever is already in dataSet[entity] - unlike read, which loads
28
+ // everything and lets the where-reduce below narrow it down afterward, they never get a second
29
+ // pass. So the matching rows have to be fully resolved (indexed conditions already are, via
30
+ // getConditionIndexedList above; anything left in parsed.conditions is not) before either runs.
31
+ if (parsed.command === 'delete' || parsed.command === 'update') {
32
+ dataSet = await readEntity(parsed.entity, dataSet)
33
+ dataSet[parsed.entity] = parsed.conditions.reduce(
34
+ (remainingData, condition) => where(remainingData, condition),
35
+ dataSet[parsed.entity]
36
+ )
37
+ }
38
+
39
+ switch (parsed.command) {
40
+ case 'delete':
41
+ dataSet = await deleteEntity(parsed.entity, dataSet)
42
+ break
43
+ case 'insert':
44
+ dataSet = await insertEntity(parsed.entity, parsed.insertValues, dataSet)
45
+ break
46
+ case 'read':
47
+ dataSet = await readEntity(parsed.entity, dataSet)
48
+ break
49
+ case 'update':
50
+ dataSet = await updateEntity(parsed.entity, parsed.updateValues, dataSet)
51
+ break
52
+ default:
53
+ throw new Error(`Unknown command: ${parsed.command}`)
54
+ }
55
+ dataSet = await joinEntity(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses)
56
+ dataSet = await mergeJoins(dataSet, parsed.mergeJoins)
57
+
58
+ // delete/update already had parsed.conditions applied above, before they ran. Re-applying the
59
+ // same conditions here would be wrong, not just redundant, for update specifically: if the
60
+ // update just changed the very field a condition filters on, re-checking that condition against
61
+ // the now-changed data would filter the just-updated rows back out.
62
+ if (parsed.command !== 'delete' && parsed.command !== 'update') {
63
+ dataSet[parsed.entity] = parsed.conditions.reduce(
64
+ (remainingData, condition) => {
65
+ return where(remainingData, condition)
66
+ },
67
+ dataSet[parsed.entity]
68
+ )
69
+ }
70
+
71
+ dataSet[parsed.entity] = sortBy(dataSet[parsed.entity], parsed.sortClauses)
72
+ dataSet[parsed.entity] = groupBy(dataSet[parsed.entity], parsed.groupBy)
73
+
74
+ return dataSet
75
+ }
@@ -0,0 +1,44 @@
1
+ import { updateIndex } from './updateIndex'
2
+ import { testHelpers } from 'js-build-tools/testHelpers'
3
+
4
+ const databasePath = 'update-index-database/'
5
+ const gulpConfig = testHelpers.gulpConfig
6
+ gulpConfig.set('databasePath', databasePath)
7
+ testHelpers.setDefaults(databasePath)
8
+
9
+ beforeEach(testHelpers.beforeEach)
10
+
11
+ afterEach(testHelpers.afterEach)
12
+
13
+ describe('updateIndex', () => {
14
+ test('does nothing for a key with no lookup', async () => {
15
+ const result = await updateIndex('foo', { type: 'multi', fields: ['a', 'b'], lookup: '' }, 'x', 'x.json')
16
+ expect(result).toBeNull()
17
+ })
18
+
19
+ test('creates a new index file the first time a value is indexed', async () => {
20
+ const key = { type: 'primary', fields: ['_id'], lookup: 'pk__id.json' }
21
+ const result = await updateIndex('foo', key, 1, '1.json')
22
+ expect(result).toEqual([{ value: 1, record: ['1.json'] }])
23
+ expect(testHelpers.fileExists(`${databasePath}__indexes/foo/pk__id.json`)).toBeTruthy()
24
+ })
25
+
26
+ test('appends a new value onto an existing index file', async () => {
27
+ const key = { type: 'index', fields: ['date'], lookup: 'index_date.json' }
28
+ await updateIndex('foo', key, '2024-06-02', '1.json')
29
+ const result = await updateIndex('foo', key, '2024-06-05', '2.json')
30
+ expect(result).toEqual([
31
+ { value: '2024-06-02', record: ['1.json'] },
32
+ { value: '2024-06-05', record: ['2.json'] }
33
+ ])
34
+ })
35
+
36
+ test('adds a matching file onto an existing value instead of duplicating the value', async () => {
37
+ const key = { type: 'index', fields: ['date'], lookup: 'index_date.json' }
38
+ await updateIndex('foo', key, '2024-06-02', '1.json')
39
+ const result = await updateIndex('foo', key, '2024-06-02', '2.json')
40
+ expect(result).toEqual([
41
+ { value: '2024-06-02', record: ['1.json', '2.json'] }
42
+ ])
43
+ })
44
+ })
@@ -0,0 +1,45 @@
1
+ import { keyDefinition } from '../introspect/definitions/key'
2
+ import { recordPath } from '../introspect/definitions/record'
3
+
4
+ const gulpConfig = require('js-build-tools/gulp.config')
5
+ const { readFile, writeFile, mkdir } = require('fs/promises')
6
+
7
+ export type indexEntry = {
8
+ value: any
9
+ record: string[]
10
+ }
11
+
12
+ /**
13
+ * Add a single entry file to the index that backs one of a record's keys, creating the index
14
+ * file (and its __indexes/{record} directory) the first time a value is indexed. Keys with no
15
+ * lookup (multi-field keys are not indexed here) are left untouched.
16
+ * @param path
17
+ * @param key
18
+ * @param value
19
+ * @param fileName
20
+ */
21
+ export const updateIndex = async (path: recordPath = '', key: keyDefinition = null, value: any = undefined, fileName: string = ''): Promise<indexEntry[] | null> => {
22
+ if (!key || !key.lookup) {
23
+ return null
24
+ }
25
+ const databasePath = gulpConfig.get('databasePath', 'database/')
26
+ const indexDir = `${databasePath}__indexes/${path}`
27
+ const indexPath = `${indexDir}/${key.lookup}`
28
+ let index: indexEntry[] = []
29
+ try {
30
+ index = JSON.parse(await readFile(indexPath, 'utf8'))
31
+ } catch (err) {
32
+ index = []
33
+ }
34
+ const existingEntry = index.find(entry => entry.value == value)
35
+ if (existingEntry) {
36
+ if (!existingEntry.record.includes(fileName)) {
37
+ existingEntry.record.push(fileName)
38
+ }
39
+ } else {
40
+ index.push({ value, record: [fileName] })
41
+ }
42
+ await mkdir(indexDir, { recursive: true })
43
+ await writeFile(indexPath, JSON.stringify(index, null, 2))
44
+ return index
45
+ }
@@ -0,0 +1,94 @@
1
+ import { useJoinClause } from './useJoinClause'
2
+ import { testHelpers } from 'js-build-tools/testHelpers'
3
+ import { copyDatabase } from '../../../test-data/copyDatabase'
4
+
5
+ const databasePath = 'use-join-clause-database/'
6
+ const gulpConfig = testHelpers.gulpConfig
7
+ gulpConfig.set('databasePath', databasePath)
8
+ testHelpers.setDefaults(databasePath)
9
+
10
+ beforeEach(
11
+ () => testHelpers.beforeEach()
12
+ .then(() => copyDatabase(databasePath))
13
+ )
14
+
15
+ afterEach(testHelpers.afterEach)
16
+
17
+ describe('useJoinClause', () => {
18
+ test('it will find the joins', async () => {
19
+ const result = await useJoinClause({
20
+ propertyA: 'workouts.exercise_id',
21
+ comparator: '=',
22
+ propertyB: 'exercises._id'
23
+ })
24
+ expect(result).toEqual({
25
+ workouts: [
26
+ {
27
+ _id: 1,
28
+ exercise_id: 1,
29
+ weight_effort: '115lbs',
30
+ reps: 10,
31
+ sets: 3,
32
+ note: '',
33
+ order: 0,
34
+ date: '2024-06-02'
35
+ },
36
+ {
37
+ _id: 4,
38
+ exercise_id: 1,
39
+ weight_effort: '120lbs',
40
+ reps: 10,
41
+ sets: 3,
42
+ note: '',
43
+ order: 0,
44
+ date: '2024-06-05'
45
+ },
46
+ {
47
+ _id: 2,
48
+ exercise_id: 2,
49
+ weight_effort: '155lbs',
50
+ reps: 10,
51
+ sets: 3,
52
+ note: '',
53
+ order: 1,
54
+ date: '2024-06-02'
55
+ },
56
+ {
57
+ _id: 5,
58
+ exercise_id: 2,
59
+ weight_effort: '160lbs',
60
+ reps: 10,
61
+ sets: 3,
62
+ note: '',
63
+ order: 1,
64
+ date: '2024-06-05'
65
+ },
66
+ {
67
+ _id: 3,
68
+ exercise_id: 3,
69
+ weight_effort: '155lbs',
70
+ reps: 10,
71
+ sets: 3,
72
+ note: '',
73
+ order: 2,
74
+ date: '2024-06-02'
75
+ },
76
+ {
77
+ _id: 6,
78
+ exercise_id: 3,
79
+ weight_effort: '160lbs',
80
+ reps: 10,
81
+ sets: 3,
82
+ note: '',
83
+ order: 2,
84
+ date: '2024-06-05'
85
+ }
86
+ ],
87
+ exercises: [
88
+ { _id: 1, name: 'Bench Press', description: '' },
89
+ { _id: 2, name: 'Deadlift', description: '' },
90
+ { _id: 3, name: 'Squat', description: '' }
91
+ ]
92
+ })
93
+ })
94
+ })
@@ -0,0 +1,44 @@
1
+ import { readEntity } from '../queries/readEntity'
2
+ import { where } from '../queries/where'
3
+ import { splitEntityProperty } from './parsers/splitEntityProperty'
4
+ import siFunciona from 'si-funciona'
5
+
6
+ export const useJoinClause = async (
7
+ { propertyA = null, comparator = '=', propertyB = null } = {},
8
+ dataSets = {},
9
+ filterJoins = {}
10
+ ) => {
11
+ const { entity: entityA, property: propA } = splitEntityProperty(propertyA)
12
+ const { entity: entityB, property: propB } = splitEntityProperty(propertyB)
13
+
14
+ if (!dataSets.hasOwnProperty(entityA)) {
15
+ dataSets = await readEntity(entityA, dataSets)
16
+ }
17
+ if (!dataSets.hasOwnProperty(entityB)) {
18
+ dataSets = await readEntity(entityB, dataSets)
19
+ }
20
+
21
+ if (!filterJoins.hasOwnProperty(entityA)) {
22
+ filterJoins[entityA] = siFunciona.cloneObject(dataSets[entityA])
23
+ }
24
+ if (!filterJoins.hasOwnProperty(entityB)) {
25
+ filterJoins[entityB] = siFunciona.cloneObject(dataSets[entityB])
26
+ }
27
+
28
+ filterJoins[entityA] = filterJoins[entityB].reduce(
29
+ (dataA, dataB) => siFunciona.mergeArrays(
30
+ dataA,
31
+ where(filterJoins[entityA], { property: propA, comparator: comparator, value: dataB[propB] })
32
+ ),
33
+ []
34
+ )
35
+
36
+ filterJoins[entityB] = filterJoins[entityA].reduce(
37
+ (dataB, dataA) => siFunciona.mergeArrays(
38
+ dataB,
39
+ where(filterJoins[entityB], { property: propB, comparator: comparator, value: dataA[propA] })
40
+ ),
41
+ []
42
+ )
43
+ return filterJoins
44
+ }
@@ -0,0 +1 @@
1
+ declare function require (id: string): any
@@ -0,0 +1,52 @@
1
+ import { query, create, drop, describe as describeRecord } from './index'
2
+ import { testHelpers } from 'js-build-tools/testHelpers'
3
+
4
+ const databasePath = 'public-api-database/'
5
+ const gulpConfig = testHelpers.gulpConfig
6
+ gulpConfig.set('databasePath', databasePath)
7
+ testHelpers.setDefaults(databasePath)
8
+
9
+ beforeEach(testHelpers.beforeEach)
10
+
11
+ afterEach(testHelpers.afterEach)
12
+
13
+ describe('the public package entry point', () => {
14
+ test('create, insert, read, update, delete, and drop all work through the exported functions', async () => {
15
+ const definition = [
16
+ { name: '_id', type: 'string', optional: false, autoGenerate: true },
17
+ { name: 'note', type: 'string', optional: true, autoGenerate: false }
18
+ ]
19
+ const keys = [
20
+ { type: 'primary', fields: ['_id'], lookup: 'pk__id.json' }
21
+ ]
22
+
23
+ await create('foo', definition, keys)
24
+
25
+ const inserted = await query("insert foo values note = 'hello'")
26
+ expect(inserted[0]).toHaveLength(1)
27
+
28
+ const read = await query('read foo')
29
+ expect(read[0]).toHaveLength(1)
30
+ expect(read[0][0].note).toBe('hello')
31
+
32
+ const updated = await query("update foo set note = 'updated' where note = 'hello'")
33
+ expect(updated[0][0].note).toBe('updated')
34
+
35
+ const deleted = await query("delete foo where note = 'updated'")
36
+ expect(deleted[0]).toHaveLength(1)
37
+
38
+ const afterDelete = await query('read foo')
39
+ expect(afterDelete[0]).toEqual([])
40
+
41
+ await drop('foo')
42
+ })
43
+
44
+ test('describe reports a record\'s field definition', async () => {
45
+ const definition = [
46
+ { name: '_id', type: 'string', optional: false, autoGenerate: true }
47
+ ]
48
+ await create('bar', definition, [])
49
+ const result = await describeRecord('bar')
50
+ expect(result).toEqual(definition)
51
+ })
52
+ })
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ export { query } from './data/queries/query'
2
+ export { create, drop, describe } from './data/introspect'
3
+ export {
4
+ fieldProperties,
5
+ fieldDefinition,
6
+ fieldName,
7
+ keyProperties,
8
+ keyDefinition,
9
+ indexType,
10
+ indexLocation,
11
+ referenceProperties,
12
+ reference,
13
+ recordProperties,
14
+ recordDefinition,
15
+ recordPath,
16
+ entityPath
17
+ } from './data/introspect'
@@ -0,0 +1,32 @@
1
+ {
2
+ "path": "",
3
+ "definition": [
4
+ {
5
+ "name": "path",
6
+ "type": "string",
7
+ "optional": false
8
+ },
9
+ {
10
+ "name": "definition",
11
+ "type": "object",
12
+ "optional": false
13
+ },
14
+ {
15
+ "name": "keys",
16
+ "type": "array",
17
+ "optional": false
18
+ },
19
+ {
20
+ "name": "entries",
21
+ "type": "array",
22
+ "optional": false
23
+ }
24
+ ],
25
+ "keys": [],
26
+ "entries": [
27
+ "exercise_muscle.json",
28
+ "exercises.json",
29
+ "muscles.json",
30
+ "workouts.json"
31
+ ]
32
+ }
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": ["bench_press_pectorals.json"]
5
+ },
6
+ {
7
+ "value": 2,
8
+ "record": ["deadlift_hamstrings.json"]
9
+ },
10
+ {
11
+ "value": 3,
12
+ "record": ["squat_quadriceps.json"]
13
+ }
14
+ ]
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "value": 8,
4
+ "record": ["deadlift_hamstrings.json"]
5
+ },
6
+ {
7
+ "value": 11,
8
+ "record": ["bench_press_pectorals.json"]
9
+ },
10
+ {
11
+ "value": 12,
12
+ "record": ["squat_quadriceps.json"]
13
+ }
14
+ ]
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": ["bench_press_pectorals.json"]
5
+ },
6
+ {
7
+ "value": 2,
8
+ "record": ["deadlift_hamstrings.json"]
9
+ },
10
+ {
11
+ "value": 3,
12
+ "record": ["squat_quadriceps.json"]
13
+ }
14
+ ]
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": ["bench_press.json"]
5
+ },
6
+ {
7
+ "value": 2,
8
+ "record": ["deadlift.json"]
9
+ },
10
+ {
11
+ "value": 3,
12
+ "record": ["squat.json"]
13
+ }
14
+ ]
@@ -0,0 +1,58 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": ["abdominal.json"]
5
+ },
6
+ {
7
+ "value": 2,
8
+ "record": ["biceps.json"]
9
+ },
10
+ {
11
+ "value": 3,
12
+ "record": ["calves.json"]
13
+ },
14
+ {
15
+ "value": 4,
16
+ "record": ["deltoids.json"]
17
+ },
18
+ {
19
+ "value": 5,
20
+ "record": ["erector_spinae.json"]
21
+ },
22
+ {
23
+ "value": 6,
24
+ "record": ["forearms.json"]
25
+ },
26
+ {
27
+ "value": 7,
28
+ "record": ["gluteas_maximus.json"]
29
+ },
30
+ {
31
+ "value": 8,
32
+ "record": ["hamstrings.json"]
33
+ },
34
+ {
35
+ "value": 9,
36
+ "record": ["latissimus_dorsi.json"]
37
+ },
38
+ {
39
+ "value": 10,
40
+ "record": ["obliques.json"]
41
+ },
42
+ {
43
+ "value": 11,
44
+ "record": ["pectorals.json"]
45
+ },
46
+ {
47
+ "value": 12,
48
+ "record": ["quadriceps.json"]
49
+ },
50
+ {
51
+ "value": 13,
52
+ "record": ["trapezius.json"]
53
+ },
54
+ {
55
+ "value": 14,
56
+ "record": ["triceps.json"]
57
+ }
58
+ ]
@@ -0,0 +1,23 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": [
5
+ "2024-06-02-001.json",
6
+ "2024-06-05-001.json"
7
+ ]
8
+ },
9
+ {
10
+ "value": 2,
11
+ "record": [
12
+ "2024-06-02-002.json",
13
+ "2024-06-05-002.json"
14
+ ]
15
+ },
16
+ {
17
+ "value": 3,
18
+ "record": [
19
+ "2024-06-02-003.json",
20
+ "2024-06-05-003.json"
21
+ ]
22
+ }
23
+ ]
@@ -0,0 +1,18 @@
1
+ [
2
+ {
3
+ "value": "2024-06-02",
4
+ "record": [
5
+ "2024-06-02-001.json",
6
+ "2024-06-02-002.json",
7
+ "2024-06-02-003.json"
8
+ ]
9
+ },
10
+ {
11
+ "value": "2024-06-05",
12
+ "record": [
13
+ "2024-06-05-001.json",
14
+ "2024-06-05-002.json",
15
+ "2024-06-05-003.json"
16
+ ]
17
+ }
18
+ ]
@@ -0,0 +1,26 @@
1
+ [
2
+ {
3
+ "value": 1,
4
+ "record": ["2024-06-02-001.json"]
5
+ },
6
+ {
7
+ "value": 2,
8
+ "record": ["2024-06-02-001.json"]
9
+ },
10
+ {
11
+ "value": 3,
12
+ "record": ["2024-06-02-001.json"]
13
+ },
14
+ {
15
+ "value": 4,
16
+ "record": ["2024-06-05-001.json"]
17
+ },
18
+ {
19
+ "value": 5,
20
+ "record": ["2024-06-05-001.json"]
21
+ },
22
+ {
23
+ "value": 6,
24
+ "record": ["2024-06-05-001.json"]
25
+ }
26
+ ]
@@ -0,0 +1,64 @@
1
+ // @ts-ignore
2
+ import fs from 'fs/promises'
3
+
4
+ export const copyDatabase = async (destination: string = 'test-database'): Promise<Array<string>> => {
5
+ const makeDirs: string[] = [
6
+ '__indexes',
7
+ '__indexes/exercise_muscle',
8
+ '__indexes/exercises',
9
+ '__indexes/muscles',
10
+ '__indexes/workouts',
11
+ 'exercise_muscle',
12
+ 'exercises',
13
+ 'muscles',
14
+ 'workouts',
15
+ ]
16
+ for (let dir of makeDirs) {
17
+ await fs.mkdir(`${destination}/${dir}`)
18
+ }
19
+ const copyFiles: string[] = [
20
+ '__RECORDS.json',
21
+ 'exercise_muscle.json',
22
+ 'exercise_muscle/bench_press_pectorals.json',
23
+ 'exercise_muscle/deadlift_hamstrings.json',
24
+ 'exercise_muscle/squat_quadriceps.json',
25
+ '__indexes/exercise_muscle/fk_exercise_id.json',
26
+ '__indexes/exercise_muscle/fk_muscle_id.json',
27
+ '__indexes/exercise_muscle/pk__id.json',
28
+ 'exercises.json',
29
+ 'exercises/bench_press.json',
30
+ 'exercises/deadlift.json',
31
+ 'exercises/squat.json',
32
+ '__indexes/exercises/pk__id.json',
33
+ 'muscles.json',
34
+ 'muscles/abdominal.json',
35
+ 'muscles/biceps.json',
36
+ 'muscles/calves.json',
37
+ 'muscles/deltoids.json',
38
+ 'muscles/erector_spinae.json',
39
+ 'muscles/forearms.json',
40
+ 'muscles/gluteus_maximus.json',
41
+ 'muscles/hamstrings.json',
42
+ 'muscles/latissimus_dorsi.json',
43
+ 'muscles/obliques.json',
44
+ 'muscles/pectorals.json',
45
+ 'muscles/quadriceps.json',
46
+ 'muscles/trapezius.json',
47
+ 'muscles/triceps.json',
48
+ '__indexes/muscles/pk__id.json',
49
+ 'workouts.json',
50
+ 'workouts/2024-06-02-001.json',
51
+ 'workouts/2024-06-02-002.json',
52
+ 'workouts/2024-06-02-003.json',
53
+ 'workouts/2024-06-05-001.json',
54
+ 'workouts/2024-06-05-002.json',
55
+ 'workouts/2024-06-05-003.json',
56
+ '__indexes/workouts/fk_exercise_id.json',
57
+ '__indexes/workouts/index_date.json',
58
+ '__indexes/workouts/pk__id.json',
59
+ ]
60
+ for (let file of copyFiles) {
61
+ await fs.copyFile(`test-data/${file}`, `${destination}/${file}`)
62
+ }
63
+ return copyFiles
64
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "_id": 1,
3
+ "exercise_id": 1,
4
+ "muscle_id": 11,
5
+ "muscle_rank": 100
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "_id": 2,
3
+ "exercise_id": 2,
4
+ "muscle_id": 8,
5
+ "muscle_rank": 100
6
+ }