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.
- package/.browserslistrc +1 -0
- package/.nvmrc +1 -0
- package/LICENSE +674 -0
- package/MAIN.md +15 -0
- package/README.md +63 -0
- package/babel.config.js +3 -0
- package/build-tools.config.json +30 -0
- package/dist/data/introspect/create.d.ts +11 -0
- package/dist/data/introspect/create.js +45 -0
- package/dist/data/introspect/create.min.js +1 -0
- package/dist/data/introspect/definitions/field.d.ts +28 -0
- package/dist/data/introspect/definitions/field.js +36 -0
- package/dist/data/introspect/definitions/field.min.js +1 -0
- package/dist/data/introspect/definitions/key.d.ts +26 -0
- package/dist/data/introspect/definitions/key.js +53 -0
- package/dist/data/introspect/definitions/key.min.js +1 -0
- package/dist/data/introspect/definitions/keyReference.d.ts +18 -0
- package/dist/data/introspect/definitions/keyReference.js +20 -0
- package/dist/data/introspect/definitions/keyReference.min.js +1 -0
- package/dist/data/introspect/definitions/record.d.ts +26 -0
- package/dist/data/introspect/definitions/record.js +32 -0
- package/dist/data/introspect/definitions/record.min.js +1 -0
- package/dist/data/introspect/describe.d.ts +2 -0
- package/dist/data/introspect/describe.js +15 -0
- package/dist/data/introspect/describe.min.js +1 -0
- package/dist/data/introspect/drop.d.ts +2 -0
- package/dist/data/introspect/drop.js +37 -0
- package/dist/data/introspect/drop.min.js +1 -0
- package/dist/data/introspect/index.d.ts +8 -0
- package/dist/data/introspect/index.js +30 -0
- package/dist/data/introspect/index.min.js +1 -0
- package/dist/data/introspect/policies/foreignKeyExists.d.ts +9 -0
- package/dist/data/introspect/policies/foreignKeyExists.js +25 -0
- package/dist/data/introspect/policies/foreignKeyExists.min.js +1 -0
- package/dist/data/introspect/policies/keyGenerate.d.ts +2 -0
- package/dist/data/introspect/policies/keyGenerate.js +17 -0
- package/dist/data/introspect/policies/keyGenerate.min.js +1 -0
- package/dist/data/introspect/policies/keyUnique.d.ts +2 -0
- package/dist/data/introspect/policies/keyUnique.js +21 -0
- package/dist/data/introspect/policies/keyUnique.min.js +1 -0
- package/dist/data/introspect/policies/typeCheck.d.ts +12 -0
- package/dist/data/introspect/policies/typeCheck.js +57 -0
- package/dist/data/introspect/policies/typeCheck.min.js +1 -0
- package/dist/data/introspect/policies/typeEnum.d.ts +2 -0
- package/dist/data/introspect/policies/typeEnum.js +14 -0
- package/dist/data/introspect/policies/typeEnum.min.js +1 -0
- package/dist/data/introspect/policies/useDefault.d.ts +2 -0
- package/dist/data/introspect/policies/useDefault.js +13 -0
- package/dist/data/introspect/policies/useDefault.min.js +1 -0
- package/dist/data/queries/deleteEntity.d.ts +2 -0
- package/dist/data/queries/deleteEntity.js +79 -0
- package/dist/data/queries/deleteEntity.min.js +1 -0
- package/dist/data/queries/groupBy.d.ts +2 -0
- package/dist/data/queries/groupBy.js +22 -0
- package/dist/data/queries/groupBy.min.js +1 -0
- package/dist/data/queries/insertEntity.d.ts +2 -0
- package/dist/data/queries/insertEntity.js +115 -0
- package/dist/data/queries/insertEntity.min.js +1 -0
- package/dist/data/queries/joinEntity.d.ts +2 -0
- package/dist/data/queries/joinEntity.js +27 -0
- package/dist/data/queries/joinEntity.min.js +1 -0
- package/dist/data/queries/mergeJoins.d.ts +2 -0
- package/dist/data/queries/mergeJoins.js +19 -0
- package/dist/data/queries/mergeJoins.min.js +1 -0
- package/dist/data/queries/parser.d.ts +2 -0
- package/dist/data/queries/parser.js +30 -0
- package/dist/data/queries/parser.min.js +1 -0
- package/dist/data/queries/query.d.ts +2 -0
- package/dist/data/queries/query.js +18 -0
- package/dist/data/queries/query.min.js +1 -0
- package/dist/data/queries/readEntity.d.ts +2 -0
- package/dist/data/queries/readEntity.js +14 -0
- package/dist/data/queries/readEntity.min.js +1 -0
- package/dist/data/queries/sortBy.d.ts +2 -0
- package/dist/data/queries/sortBy.js +25 -0
- package/dist/data/queries/sortBy.min.js +1 -0
- package/dist/data/queries/updateEntity.d.ts +2 -0
- package/dist/data/queries/updateEntity.js +164 -0
- package/dist/data/queries/updateEntity.min.js +1 -0
- package/dist/data/queries/where.d.ts +7 -0
- package/dist/data/queries/where.js +56 -0
- package/dist/data/queries/where.min.js +1 -0
- package/dist/data/utilities/deregisterRecord.d.ts +8 -0
- package/dist/data/utilities/deregisterRecord.js +34 -0
- package/dist/data/utilities/deregisterRecord.min.js +1 -0
- package/dist/data/utilities/getConditionIndexedList.d.ts +3 -0
- package/dist/data/utilities/getConditionIndexedList.js +47 -0
- package/dist/data/utilities/getConditionIndexedList.min.js +1 -0
- package/dist/data/utilities/getJoinIndexedList.d.ts +3 -0
- package/dist/data/utilities/getJoinIndexedList.js +59 -0
- package/dist/data/utilities/getJoinIndexedList.min.js +1 -0
- package/dist/data/utilities/getMatchingIndexes.d.ts +2 -0
- package/dist/data/utilities/getMatchingIndexes.js +28 -0
- package/dist/data/utilities/getMatchingIndexes.min.js +1 -0
- package/dist/data/utilities/matchIndexedJoins.d.ts +2 -0
- package/dist/data/utilities/matchIndexedJoins.js +61 -0
- package/dist/data/utilities/matchIndexedJoins.min.js +1 -0
- package/dist/data/utilities/mergeJoin.d.ts +2 -0
- package/dist/data/utilities/mergeJoin.js +44 -0
- package/dist/data/utilities/mergeJoin.min.js +1 -0
- package/dist/data/utilities/parsers/findCommand.d.ts +10 -0
- package/dist/data/utilities/parsers/findCommand.js +25 -0
- package/dist/data/utilities/parsers/findCommand.min.js +1 -0
- package/dist/data/utilities/parsers/findConditions.d.ts +15 -0
- package/dist/data/utilities/parsers/findConditions.js +28 -0
- package/dist/data/utilities/parsers/findConditions.min.js +1 -0
- package/dist/data/utilities/parsers/findGroupBy.d.ts +9 -0
- package/dist/data/utilities/parsers/findGroupBy.js +22 -0
- package/dist/data/utilities/parsers/findGroupBy.min.js +1 -0
- package/dist/data/utilities/parsers/findInsertValues.d.ts +13 -0
- package/dist/data/utilities/parsers/findInsertValues.js +34 -0
- package/dist/data/utilities/parsers/findInsertValues.min.js +1 -0
- package/dist/data/utilities/parsers/findJoinClauses.d.ts +13 -0
- package/dist/data/utilities/parsers/findJoinClauses.js +27 -0
- package/dist/data/utilities/parsers/findJoinClauses.min.js +1 -0
- package/dist/data/utilities/parsers/findJoinEntity.d.ts +9 -0
- package/dist/data/utilities/parsers/findJoinEntity.js +22 -0
- package/dist/data/utilities/parsers/findJoinEntity.min.js +1 -0
- package/dist/data/utilities/parsers/findLimit.d.ts +9 -0
- package/dist/data/utilities/parsers/findLimit.js +22 -0
- package/dist/data/utilities/parsers/findLimit.min.js +1 -0
- package/dist/data/utilities/parsers/findMergeJoins.d.ts +12 -0
- package/dist/data/utilities/parsers/findMergeJoins.js +26 -0
- package/dist/data/utilities/parsers/findMergeJoins.min.js +1 -0
- package/dist/data/utilities/parsers/findOffset.d.ts +9 -0
- package/dist/data/utilities/parsers/findOffset.js +22 -0
- package/dist/data/utilities/parsers/findOffset.min.js +1 -0
- package/dist/data/utilities/parsers/findSelects.d.ts +12 -0
- package/dist/data/utilities/parsers/findSelects.js +30 -0
- package/dist/data/utilities/parsers/findSelects.min.js +1 -0
- package/dist/data/utilities/parsers/findSortClauses.d.ts +12 -0
- package/dist/data/utilities/parsers/findSortClauses.js +30 -0
- package/dist/data/utilities/parsers/findSortClauses.min.js +1 -0
- package/dist/data/utilities/parsers/findUpdateValues.d.ts +15 -0
- package/dist/data/utilities/parsers/findUpdateValues.js +34 -0
- package/dist/data/utilities/parsers/findUpdateValues.min.js +1 -0
- package/dist/data/utilities/parsers/isEntityProperty.d.ts +2 -0
- package/dist/data/utilities/parsers/isEntityProperty.js +8 -0
- package/dist/data/utilities/parsers/isEntityProperty.min.js +1 -0
- package/dist/data/utilities/parsers/parseLiteralValue.d.ts +12 -0
- package/dist/data/utilities/parsers/parseLiteralValue.js +34 -0
- package/dist/data/utilities/parsers/parseLiteralValue.min.js +1 -0
- package/dist/data/utilities/parsers/parseQuery.d.ts +33 -0
- package/dist/data/utilities/parsers/parseQuery.js +51 -0
- package/dist/data/utilities/parsers/parseQuery.min.js +1 -0
- package/dist/data/utilities/parsers/removeFromQuery.d.ts +2 -0
- package/dist/data/utilities/parsers/removeFromQuery.js +8 -0
- package/dist/data/utilities/parsers/removeFromQuery.min.js +1 -0
- package/dist/data/utilities/parsers/splitEntityProperty.d.ts +5 -0
- package/dist/data/utilities/parsers/splitEntityProperty.js +23 -0
- package/dist/data/utilities/parsers/splitEntityProperty.min.js +1 -0
- package/dist/data/utilities/parsers/splitQueries.d.ts +2 -0
- package/dist/data/utilities/parsers/splitQueries.js +11 -0
- package/dist/data/utilities/parsers/splitQueries.min.js +1 -0
- package/dist/data/utilities/reconcileJoin.d.ts +2 -0
- package/dist/data/utilities/reconcileJoin.js +26 -0
- package/dist/data/utilities/reconcileJoin.min.js +1 -0
- package/dist/data/utilities/reconcileJoins.d.ts +2 -0
- package/dist/data/utilities/reconcileJoins.js +27 -0
- package/dist/data/utilities/reconcileJoins.min.js +1 -0
- package/dist/data/utilities/registerRecord.d.ts +8 -0
- package/dist/data/utilities/registerRecord.js +67 -0
- package/dist/data/utilities/registerRecord.min.js +1 -0
- package/dist/data/utilities/removeFromIndex.d.ts +14 -0
- package/dist/data/utilities/removeFromIndex.js +46 -0
- package/dist/data/utilities/removeFromIndex.min.js +1 -0
- package/dist/data/utilities/restrictIndexedList.d.ts +2 -0
- package/dist/data/utilities/restrictIndexedList.js +45 -0
- package/dist/data/utilities/restrictIndexedList.min.js +1 -0
- package/dist/data/utilities/retrieveFile.d.ts +2 -0
- package/dist/data/utilities/retrieveFile.js +28 -0
- package/dist/data/utilities/retrieveFile.min.js +1 -0
- package/dist/data/utilities/retrieveIndexes.d.ts +2 -0
- package/dist/data/utilities/retrieveIndexes.js +24 -0
- package/dist/data/utilities/retrieveIndexes.min.js +1 -0
- package/dist/data/utilities/retrieveRecord.d.ts +2 -0
- package/dist/data/utilities/retrieveRecord.js +9 -0
- package/dist/data/utilities/retrieveRecord.min.js +1 -0
- package/dist/data/utilities/retrieveRecords.d.ts +2 -0
- package/dist/data/utilities/retrieveRecords.js +21 -0
- package/dist/data/utilities/retrieveRecords.min.js +1 -0
- package/dist/data/utilities/runQuery.d.ts +2 -0
- package/dist/data/utilities/runQuery.js +72 -0
- package/dist/data/utilities/runQuery.min.js +1 -0
- package/dist/data/utilities/updateIndex.d.ts +17 -0
- package/dist/data/utilities/updateIndex.js +56 -0
- package/dist/data/utilities/updateIndex.min.js +1 -0
- package/dist/data/utilities/useJoinClause.d.ts +6 -0
- package/dist/data/utilities/useJoinClause.js +51 -0
- package/dist/data/utilities/useJoinClause.min.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +35 -0
- package/dist/index.min.js +1 -0
- package/gulpfile.mjs +15 -0
- package/package.json +44 -0
- package/src/data/introspect/create.test.js +67 -0
- package/src/data/introspect/create.ts +29 -0
- package/src/data/introspect/definitions/field.test.js +23 -0
- package/src/data/introspect/definitions/field.ts +48 -0
- package/src/data/introspect/definitions/key.test.js +100 -0
- package/src/data/introspect/definitions/key.ts +64 -0
- package/src/data/introspect/definitions/keyReference.test.js +8 -0
- package/src/data/introspect/definitions/keyReference.ts +23 -0
- package/src/data/introspect/definitions/record.ts +42 -0
- package/src/data/introspect/describe.test.js +36 -0
- package/src/data/introspect/describe.ts +9 -0
- package/src/data/introspect/drop.test.js +77 -0
- package/src/data/introspect/drop.ts +20 -0
- package/src/data/introspect/index.ts +8 -0
- package/src/data/introspect/policies/foreignKeyExists.test.js +44 -0
- package/src/data/introspect/policies/foreignKeyExists.ts +15 -0
- package/src/data/introspect/policies/keyGenerate.ts +11 -0
- package/src/data/introspect/policies/keyUnique.ts +11 -0
- package/src/data/introspect/policies/typeCheck.test.js +84 -0
- package/src/data/introspect/policies/typeCheck.ts +49 -0
- package/src/data/introspect/policies/typeEnum.ts +6 -0
- package/src/data/introspect/policies/useDefault.ts +6 -0
- package/src/data/queries/deleteEntity.test.js +86 -0
- package/src/data/queries/deleteEntity.ts +56 -0
- package/src/data/queries/groupBy.test.js +135 -0
- package/src/data/queries/groupBy.ts +16 -0
- package/src/data/queries/insertEntity.test.js +156 -0
- package/src/data/queries/insertEntity.ts +113 -0
- package/src/data/queries/joinEntity.test.js +109 -0
- package/src/data/queries/joinEntity.ts +20 -0
- package/src/data/queries/mergeJoins.test.js +174 -0
- package/src/data/queries/mergeJoins.ts +13 -0
- package/src/data/queries/parser.test.js +343 -0
- package/src/data/queries/parser.ts +24 -0
- package/src/data/queries/query.test.js +233 -0
- package/src/data/queries/query.ts +12 -0
- package/src/data/queries/readEntity.test.js +85 -0
- package/src/data/queries/readEntity.ts +8 -0
- package/src/data/queries/sortBy.test.js +131 -0
- package/src/data/queries/sortBy.ts +18 -0
- package/src/data/queries/updateEntity.test.js +228 -0
- package/src/data/queries/updateEntity.ts +162 -0
- package/src/data/queries/where.test.js +129 -0
- package/src/data/queries/where.ts +54 -0
- package/src/data/utilities/deregisterRecord.test.js +26 -0
- package/src/data/utilities/deregisterRecord.ts +24 -0
- package/src/data/utilities/getConditionIndexedList.ts +43 -0
- package/src/data/utilities/getJoinIndexedList.ts +53 -0
- package/src/data/utilities/getMatchingIndexes.ts +21 -0
- package/src/data/utilities/matchIndexedJoins.ts +45 -0
- package/src/data/utilities/mergeJoin.test.js +102 -0
- package/src/data/utilities/mergeJoin.ts +28 -0
- package/src/data/utilities/parsers/findCommand.ts +25 -0
- package/src/data/utilities/parsers/findConditions.test.js +38 -0
- package/src/data/utilities/parsers/findConditions.ts +32 -0
- package/src/data/utilities/parsers/findGroupBy.ts +20 -0
- package/src/data/utilities/parsers/findInsertValues.test.js +26 -0
- package/src/data/utilities/parsers/findInsertValues.ts +32 -0
- package/src/data/utilities/parsers/findJoinClauses.ts +29 -0
- package/src/data/utilities/parsers/findJoinEntity.ts +20 -0
- package/src/data/utilities/parsers/findLimit.ts +20 -0
- package/src/data/utilities/parsers/findMergeJoins.ts +27 -0
- package/src/data/utilities/parsers/findOffset.ts +20 -0
- package/src/data/utilities/parsers/findSelects.ts +31 -0
- package/src/data/utilities/parsers/findSortClauses.ts +31 -0
- package/src/data/utilities/parsers/findUpdateValues.test.js +37 -0
- package/src/data/utilities/parsers/findUpdateValues.ts +32 -0
- package/src/data/utilities/parsers/isEntityProperty.ts +1 -0
- package/src/data/utilities/parsers/parseLiteralValue.test.js +41 -0
- package/src/data/utilities/parsers/parseLiteralValue.ts +29 -0
- package/src/data/utilities/parsers/parseQuery.ts +73 -0
- package/src/data/utilities/parsers/removeFromQuery.ts +1 -0
- package/src/data/utilities/parsers/splitEntityProperty.test.js +8 -0
- package/src/data/utilities/parsers/splitEntityProperty.ts +17 -0
- package/src/data/utilities/parsers/splitQueries.test.js +33 -0
- package/src/data/utilities/parsers/splitQueries.ts +4 -0
- package/src/data/utilities/reconcileJoin.test.js +38 -0
- package/src/data/utilities/reconcileJoin.ts +23 -0
- package/src/data/utilities/reconcileJoins.test.js +160 -0
- package/src/data/utilities/reconcileJoins.ts +15 -0
- package/src/data/utilities/registerRecord.test.js +44 -0
- package/src/data/utilities/registerRecord.ts +44 -0
- package/src/data/utilities/removeFromIndex.test.js +44 -0
- package/src/data/utilities/removeFromIndex.ts +34 -0
- package/src/data/utilities/restrictIndexedList.ts +38 -0
- package/src/data/utilities/retrieveFile.test.js +49 -0
- package/src/data/utilities/retrieveFile.ts +19 -0
- package/src/data/utilities/retrieveIndexes.ts +17 -0
- package/src/data/utilities/retrieveRecord.ts +3 -0
- package/src/data/utilities/retrieveRecords.test.js +223 -0
- package/src/data/utilities/retrieveRecords.ts +16 -0
- package/src/data/utilities/runQuery.test.js +88 -0
- package/src/data/utilities/runQuery.ts +75 -0
- package/src/data/utilities/updateIndex.test.js +44 -0
- package/src/data/utilities/updateIndex.ts +45 -0
- package/src/data/utilities/useJoinClause.test.js +94 -0
- package/src/data/utilities/useJoinClause.ts +44 -0
- package/src/global.d.ts +1 -0
- package/src/index.test.js +52 -0
- package/src/index.ts +17 -0
- package/test-data/__RECORDS.json +32 -0
- package/test-data/__indexes/exercise_muscle/fk_exercise_id.json +14 -0
- package/test-data/__indexes/exercise_muscle/fk_muscle_id.json +14 -0
- package/test-data/__indexes/exercise_muscle/pk__id.json +14 -0
- package/test-data/__indexes/exercises/pk__id.json +14 -0
- package/test-data/__indexes/muscles/pk__id.json +58 -0
- package/test-data/__indexes/workouts/fk_exercise_id.json +23 -0
- package/test-data/__indexes/workouts/index_date.json +18 -0
- package/test-data/__indexes/workouts/pk__id.json +26 -0
- package/test-data/copyDatabase.ts +64 -0
- package/test-data/exercise_muscle/bench_press_pectorals.json +6 -0
- package/test-data/exercise_muscle/deadlift_hamstrings.json +6 -0
- package/test-data/exercise_muscle/squat_quadriceps.json +6 -0
- package/test-data/exercise_muscle.json +69 -0
- package/test-data/exercises/bench_press.json +5 -0
- package/test-data/exercises/deadlift.json +5 -0
- package/test-data/exercises/squat.json +5 -0
- package/test-data/exercises.json +34 -0
- package/test-data/muscles/abdominal.json +6 -0
- package/test-data/muscles/biceps.json +6 -0
- package/test-data/muscles/calves.json +6 -0
- package/test-data/muscles/deltoids.json +6 -0
- package/test-data/muscles/erector_spinae.json +6 -0
- package/test-data/muscles/forearms.json +6 -0
- package/test-data/muscles/gluteus_maximus.json +6 -0
- package/test-data/muscles/hamstrings.json +6 -0
- package/test-data/muscles/latissimus_dorsi.json +6 -0
- package/test-data/muscles/obliques.json +6 -0
- package/test-data/muscles/pectorals.json +6 -0
- package/test-data/muscles/quadriceps.json +6 -0
- package/test-data/muscles/trapezius.json +6 -0
- package/test-data/muscles/triceps.json +6 -0
- package/test-data/muscles.json +50 -0
- package/test-data/workouts/2024-06-02-001.json +10 -0
- package/test-data/workouts/2024-06-02-002.json +10 -0
- package/test-data/workouts/2024-06-02-003.json +10 -0
- package/test-data/workouts/2024-06-05-001.json +10 -0
- package/test-data/workouts/2024-06-05-002.json +10 -0
- package/test-data/workouts/2024-06-05-003.json +10 -0
- package/test-data/workouts.json +84 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { registerRecord } from './registerRecord'
|
|
2
|
+
import { testHelpers } from 'js-build-tools/testHelpers'
|
|
3
|
+
|
|
4
|
+
const { readFile } = require('fs/promises')
|
|
5
|
+
|
|
6
|
+
const databasePath = 'register-record-database/'
|
|
7
|
+
const gulpConfig = testHelpers.gulpConfig
|
|
8
|
+
gulpConfig.set('databasePath', databasePath)
|
|
9
|
+
testHelpers.setDefaults(databasePath)
|
|
10
|
+
|
|
11
|
+
beforeEach(testHelpers.beforeEach)
|
|
12
|
+
|
|
13
|
+
afterEach(testHelpers.afterEach)
|
|
14
|
+
|
|
15
|
+
describe('registerRecord', () => {
|
|
16
|
+
test('creates a fresh registry the first time a record is registered', async () => {
|
|
17
|
+
const result = await registerRecord('foo')
|
|
18
|
+
expect(result).toEqual({
|
|
19
|
+
path: '',
|
|
20
|
+
definition: [
|
|
21
|
+
{ name: 'path', type: 'string', optional: false, autoGenerate: false },
|
|
22
|
+
{ name: 'definition', type: 'object', optional: false, autoGenerate: false },
|
|
23
|
+
{ name: 'keys', type: 'array', optional: false, autoGenerate: false },
|
|
24
|
+
{ name: 'entries', type: 'array', optional: false, autoGenerate: false }
|
|
25
|
+
],
|
|
26
|
+
keys: [],
|
|
27
|
+
entries: ['foo.json']
|
|
28
|
+
})
|
|
29
|
+
const onDisk = JSON.parse(await readFile(`${databasePath}__RECORDS.json`, 'utf8'))
|
|
30
|
+
expect(onDisk).toEqual(result)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('appends onto an existing registry without disturbing other entries', async () => {
|
|
34
|
+
await registerRecord('foo')
|
|
35
|
+
const result = await registerRecord('bar')
|
|
36
|
+
expect(result.entries).toEqual(['foo.json', 'bar.json'])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('registering the same record twice does not duplicate its entry', async () => {
|
|
40
|
+
await registerRecord('foo')
|
|
41
|
+
const result = await registerRecord('foo')
|
|
42
|
+
expect(result.entries).toEqual(['foo.json'])
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { recordDefinition } from '../introspect/definitions/record'
|
|
2
|
+
|
|
3
|
+
const gulpConfig = require('js-build-tools/gulp.config')
|
|
4
|
+
const { readFile, writeFile } = require('fs/promises')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* __RECORDS is itself an ordinary record - path/definition/keys/entries - whose entries are the
|
|
8
|
+
* filenames of every other record that has been created. This is the schema it's given the first
|
|
9
|
+
* time a record is registered and no __RECORDS.json exists yet, matching what a hand-authored
|
|
10
|
+
* database (see test-data/__RECORDS.json) already expects to find.
|
|
11
|
+
*/
|
|
12
|
+
const defaultRegistry = (): recordDefinition => ({
|
|
13
|
+
path: '',
|
|
14
|
+
definition: [
|
|
15
|
+
{ name: 'path', type: 'string', optional: false, autoGenerate: false },
|
|
16
|
+
{ name: 'definition', type: 'object', optional: false, autoGenerate: false },
|
|
17
|
+
{ name: 'keys', type: 'array', optional: false, autoGenerate: false },
|
|
18
|
+
{ name: 'entries', type: 'array', optional: false, autoGenerate: false }
|
|
19
|
+
],
|
|
20
|
+
keys: [],
|
|
21
|
+
entries: []
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Add a record's filename to the __RECORDS registry, creating the registry file the first time
|
|
26
|
+
* a record is created if it doesn't already exist.
|
|
27
|
+
* @param recordName
|
|
28
|
+
*/
|
|
29
|
+
export const registerRecord = async (recordName: string): Promise<recordDefinition> => {
|
|
30
|
+
const databasePath = gulpConfig.get('databasePath', 'database/')
|
|
31
|
+
const registryPath = `${databasePath}__RECORDS.json`
|
|
32
|
+
let registry: recordDefinition
|
|
33
|
+
try {
|
|
34
|
+
registry = JSON.parse(await readFile(registryPath, 'utf8'))
|
|
35
|
+
} catch (err) {
|
|
36
|
+
registry = defaultRegistry()
|
|
37
|
+
}
|
|
38
|
+
const fileName = `${recordName}.json`
|
|
39
|
+
if (!registry.entries.includes(fileName)) {
|
|
40
|
+
registry.entries.push(fileName)
|
|
41
|
+
}
|
|
42
|
+
await writeFile(registryPath, JSON.stringify(registry, null, 2))
|
|
43
|
+
return registry
|
|
44
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { updateIndex } from './updateIndex'
|
|
2
|
+
import { removeFromIndex } from './removeFromIndex'
|
|
3
|
+
import { testHelpers } from 'js-build-tools/testHelpers'
|
|
4
|
+
|
|
5
|
+
const { readFile } = require('fs/promises')
|
|
6
|
+
|
|
7
|
+
const databasePath = 'remove-from-index-database/'
|
|
8
|
+
const gulpConfig = testHelpers.gulpConfig
|
|
9
|
+
gulpConfig.set('databasePath', databasePath)
|
|
10
|
+
testHelpers.setDefaults(databasePath)
|
|
11
|
+
|
|
12
|
+
beforeEach(testHelpers.beforeEach)
|
|
13
|
+
|
|
14
|
+
afterEach(testHelpers.afterEach)
|
|
15
|
+
|
|
16
|
+
describe('removeFromIndex', () => {
|
|
17
|
+
test('does nothing for a key with no lookup', async () => {
|
|
18
|
+
const result = await removeFromIndex('foo', { type: 'multi', fields: ['a', 'b'], lookup: '' }, 'x', 'x.json')
|
|
19
|
+
expect(result).toBeNull()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('does nothing when the index file does not exist yet', async () => {
|
|
23
|
+
const key = { type: 'primary', fields: ['_id'], lookup: 'pk__id.json' }
|
|
24
|
+
const result = await removeFromIndex('foo', key, 1, '1.json')
|
|
25
|
+
expect(result).toBeNull()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('removes a file from its value, keeping other files on that value', async () => {
|
|
29
|
+
const key = { type: 'index', fields: ['date'], lookup: 'index_date.json' }
|
|
30
|
+
await updateIndex('foo', key, '2024-06-02', '1.json')
|
|
31
|
+
await updateIndex('foo', key, '2024-06-02', '2.json')
|
|
32
|
+
const result = await removeFromIndex('foo', key, '2024-06-02', '1.json')
|
|
33
|
+
expect(result).toEqual([{ value: '2024-06-02', record: ['2.json'] }])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('drops the whole value once its last file is removed', async () => {
|
|
37
|
+
const key = { type: 'primary', fields: ['_id'], lookup: 'pk__id.json' }
|
|
38
|
+
await updateIndex('foo', key, 1, '1.json')
|
|
39
|
+
const result = await removeFromIndex('foo', key, 1, '1.json')
|
|
40
|
+
expect(result).toEqual([])
|
|
41
|
+
const onDisk = JSON.parse(await readFile(`${databasePath}__indexes/foo/pk__id.json`, 'utf8'))
|
|
42
|
+
expect(onDisk).toEqual([])
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { keyDefinition } from '../introspect/definitions/key'
|
|
2
|
+
import { recordPath } from '../introspect/definitions/record'
|
|
3
|
+
import { indexEntry } from './updateIndex'
|
|
4
|
+
|
|
5
|
+
const gulpConfig = require('js-build-tools/gulp.config')
|
|
6
|
+
const { readFile, writeFile } = require('fs/promises')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Remove a single entry file from the index that backs one of a record's keys, dropping a value
|
|
10
|
+
* entirely once its last file is removed. A key with no lookup, or an index file that doesn't
|
|
11
|
+
* exist yet, is left untouched.
|
|
12
|
+
* @param path
|
|
13
|
+
* @param key
|
|
14
|
+
* @param value
|
|
15
|
+
* @param fileName
|
|
16
|
+
*/
|
|
17
|
+
export const removeFromIndex = async (path: recordPath = '', key: keyDefinition = null, value: any = undefined, fileName: string = ''): Promise<indexEntry[] | null> => {
|
|
18
|
+
if (!key || !key.lookup) {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
const databasePath = gulpConfig.get('databasePath', 'database/')
|
|
22
|
+
const indexPath = `${databasePath}__indexes/${path}/${key.lookup}`
|
|
23
|
+
let index: indexEntry[] = []
|
|
24
|
+
try {
|
|
25
|
+
index = JSON.parse(await readFile(indexPath, 'utf8'))
|
|
26
|
+
} catch (err) {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
index = index
|
|
30
|
+
.map(entry => entry.value == value ? { value: entry.value, record: entry.record.filter(record => record !== fileName) } : entry)
|
|
31
|
+
.filter(entry => entry.record.length)
|
|
32
|
+
await writeFile(indexPath, JSON.stringify(index, null, 2))
|
|
33
|
+
return index
|
|
34
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { splitEntityProperty } from './parsers/splitEntityProperty'
|
|
2
|
+
|
|
3
|
+
export const restrictIndexedList = (joinClause, filterJoins, indexedClone) => {
|
|
4
|
+
const { entity: entityA, property: propertyA } = splitEntityProperty(joinClause.propertyA)
|
|
5
|
+
const { entity: entityB, property: propertyB } = splitEntityProperty(joinClause.propertyB)
|
|
6
|
+
if (filterJoins.hasOwnProperty(entityA)) {
|
|
7
|
+
const existingEntities = filterJoins[entityA]
|
|
8
|
+
if (!indexedClone.hasOwnProperty(entityA)) {
|
|
9
|
+
indexedClone[entityA] = {}
|
|
10
|
+
}
|
|
11
|
+
if (!indexedClone[entityA].hasOwnProperty(propertyA)) {
|
|
12
|
+
indexedClone[entityA][propertyA] = {}
|
|
13
|
+
}
|
|
14
|
+
indexedClone[entityA][propertyA] = [
|
|
15
|
+
{
|
|
16
|
+
value: existingEntities[0][propertyA],
|
|
17
|
+
record: existingEntities
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
if (filterJoins.hasOwnProperty(entityB)) {
|
|
22
|
+
const existingEntities = filterJoins[entityB]
|
|
23
|
+
if (!indexedClone.hasOwnProperty(entityB)) {
|
|
24
|
+
indexedClone[entityB] = {}
|
|
25
|
+
}
|
|
26
|
+
if (!indexedClone[entityB].hasOwnProperty(propertyB)) {
|
|
27
|
+
indexedClone[entityB][propertyB] = {}
|
|
28
|
+
}
|
|
29
|
+
indexedClone[entityB][propertyB] = [
|
|
30
|
+
{
|
|
31
|
+
value: existingEntities[0][propertyB],
|
|
32
|
+
record: existingEntities
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return indexedClone
|
|
38
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { retrieveFile } from './retrieveFile'
|
|
2
|
+
import { testHelpers } from 'js-build-tools/testHelpers'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
|
|
5
|
+
const databasePath = 'retrieve-file-database/'
|
|
6
|
+
const gulpConfig = testHelpers.gulpConfig
|
|
7
|
+
gulpConfig.set('databasePath', databasePath)
|
|
8
|
+
testHelpers.setDefaults(databasePath)
|
|
9
|
+
|
|
10
|
+
beforeEach(testHelpers.beforeEach)
|
|
11
|
+
|
|
12
|
+
afterEach(testHelpers.afterEach)
|
|
13
|
+
|
|
14
|
+
describe('retrieveFile', () => {
|
|
15
|
+
test('should get the file if found', async () => {
|
|
16
|
+
const recordsContent = {
|
|
17
|
+
path: '',
|
|
18
|
+
definition: [
|
|
19
|
+
{
|
|
20
|
+
name: 'path',
|
|
21
|
+
type: 'string',
|
|
22
|
+
optional: false,
|
|
23
|
+
keys: []
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'definition',
|
|
27
|
+
type: 'object',
|
|
28
|
+
optional: false,
|
|
29
|
+
keys: []
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'entries',
|
|
33
|
+
type: 'array',
|
|
34
|
+
optional: false,
|
|
35
|
+
keys: []
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
entries: [
|
|
39
|
+
'exercise_muscle.json',
|
|
40
|
+
'exercises.json',
|
|
41
|
+
'muscles.json',
|
|
42
|
+
'workouts.json',
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
const fileName = '__RECORDS.json'
|
|
46
|
+
fs.writeFileSync(`${databasePath}${fileName}`, JSON.stringify(recordsContent))
|
|
47
|
+
expect(await retrieveFile(fileName)).toEqual(recordsContent)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as jsEnv from 'browser-or-node'
|
|
2
|
+
import * as gulpConfig from 'js-build-tools/gulp.config'
|
|
3
|
+
|
|
4
|
+
export const retrieveFile = async (path) => {
|
|
5
|
+
let retrieveFn = url => fetch(url).then(res => res.json())
|
|
6
|
+
let relativePath = jsEnv.isBrowser ? gulpConfig.get('relativePath', '') : ''
|
|
7
|
+
if (jsEnv.isNode) {
|
|
8
|
+
const { readFile } = require('fs/promises')
|
|
9
|
+
retrieveFn = url => readFile(url).then(res => JSON.parse(res))
|
|
10
|
+
}
|
|
11
|
+
const databasePath = gulpConfig.get('databasePath', 'database/')
|
|
12
|
+
const fetchUrl = `${relativePath}${databasePath}${path}`
|
|
13
|
+
const recordInfo = await retrieveFn(fetchUrl)
|
|
14
|
+
.catch(err => console.error(err))
|
|
15
|
+
if (!recordInfo) {
|
|
16
|
+
throw new Error(`Could not read ${path}`)
|
|
17
|
+
}
|
|
18
|
+
return recordInfo
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { retrieveFile } from './retrieveFile'
|
|
2
|
+
import { retrieveRecord } from './retrieveRecord'
|
|
3
|
+
|
|
4
|
+
export const retrieveIndexes = async (entity = '', properties = [], recordData = {}) => {
|
|
5
|
+
if (!recordData.hasOwnProperty(entity)) {
|
|
6
|
+
recordData[entity] = await retrieveRecord(entity)
|
|
7
|
+
}
|
|
8
|
+
const indexSet = {}
|
|
9
|
+
for (let property of properties) {
|
|
10
|
+
for (let key of recordData[entity].keys) {
|
|
11
|
+
if (key.fields.includes(property)) {
|
|
12
|
+
indexSet[property] = await retrieveFile(`__indexes/${entity}/${key.lookup}`)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return indexSet
|
|
17
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { retrieveRecords } from './retrieveRecords'
|
|
2
|
+
import { testHelpers } from 'js-build-tools/testHelpers'
|
|
3
|
+
import { copyDatabase } from '../../../test-data/copyDatabase'
|
|
4
|
+
|
|
5
|
+
const databasePath = 'retrieve-records-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('retrieveRecords', () => {
|
|
18
|
+
test('retrieve main RECORDS file', async () => {
|
|
19
|
+
expect(await retrieveRecords('__RECORDS')).toEqual([
|
|
20
|
+
{
|
|
21
|
+
path: 'exercise_muscle',
|
|
22
|
+
definition: [
|
|
23
|
+
{
|
|
24
|
+
name: '_id',
|
|
25
|
+
type: 'string',
|
|
26
|
+
optional: false
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'exercise_id',
|
|
30
|
+
type: 'string',
|
|
31
|
+
optional: false
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'muscle_id',
|
|
35
|
+
type: 'string',
|
|
36
|
+
optional: false
|
|
37
|
+
},
|
|
38
|
+
{ name: 'muscle_rank', type: 'int', optional: false }
|
|
39
|
+
],
|
|
40
|
+
keys: [
|
|
41
|
+
{ type: 'primary', fields: ['_id'], lookup: 'pk__id.json' },
|
|
42
|
+
{
|
|
43
|
+
type: 'foreign', fields: ['exercise_id'], lookup: 'fk_exercise_id.json', references: [
|
|
44
|
+
{
|
|
45
|
+
'fields': ['exercises._id'],
|
|
46
|
+
'lookup': 'exercises.json'
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'foreign', fields: ['muscle_id'], lookup: 'fk_muscle_id.json', references: [
|
|
52
|
+
{
|
|
53
|
+
'fields': ['muscles._id'],
|
|
54
|
+
'lookup': 'muscles.json'
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
entries: [
|
|
60
|
+
'bench_press_pectorals.json',
|
|
61
|
+
'deadlift_hamstrings.json',
|
|
62
|
+
'squat_quadriceps.json'
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
path: 'exercises',
|
|
67
|
+
definition: [
|
|
68
|
+
{
|
|
69
|
+
name: '_id',
|
|
70
|
+
type: 'string',
|
|
71
|
+
optional: false
|
|
72
|
+
},
|
|
73
|
+
{ name: 'name', type: 'string', optional: false },
|
|
74
|
+
{ name: 'description', type: 'string', optional: true }
|
|
75
|
+
],
|
|
76
|
+
keys: [
|
|
77
|
+
{ type: 'primary', fields: ['_id'], lookup: 'pk__id.json' },
|
|
78
|
+
],
|
|
79
|
+
entries: ['bench_press.json', 'deadlift.json', 'squat.json']
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
path: 'muscles',
|
|
83
|
+
definition: [
|
|
84
|
+
{
|
|
85
|
+
name: '_id',
|
|
86
|
+
type: 'string',
|
|
87
|
+
optional: false
|
|
88
|
+
},
|
|
89
|
+
{ name: 'name', type: 'string', optional: false },
|
|
90
|
+
{ name: 'alias', type: 'string', optional: true },
|
|
91
|
+
{ name: 'image', type: 'string', optional: true }
|
|
92
|
+
],
|
|
93
|
+
keys: [
|
|
94
|
+
{ type: 'primary', fields: ['_id'], lookup: 'pk__id.json' },
|
|
95
|
+
],
|
|
96
|
+
entries: [
|
|
97
|
+
'abdominal.json',
|
|
98
|
+
'biceps.json',
|
|
99
|
+
'calves.json',
|
|
100
|
+
'deltoids.json',
|
|
101
|
+
'erector_spinae.json',
|
|
102
|
+
'forearms.json',
|
|
103
|
+
'gluteus_maximus.json',
|
|
104
|
+
'hamstrings.json',
|
|
105
|
+
'latissimus_dorsi.json',
|
|
106
|
+
'obliques.json',
|
|
107
|
+
'pectorals.json',
|
|
108
|
+
'quadriceps.json',
|
|
109
|
+
'trapezius.json',
|
|
110
|
+
'triceps.json'
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
path: 'workouts',
|
|
115
|
+
definition: [
|
|
116
|
+
{
|
|
117
|
+
name: '_id',
|
|
118
|
+
type: 'string',
|
|
119
|
+
optional: false
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: 'exercise_id',
|
|
123
|
+
type: 'string',
|
|
124
|
+
optional: false
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'weight_effort',
|
|
128
|
+
type: 'string',
|
|
129
|
+
optional: true
|
|
130
|
+
},
|
|
131
|
+
{ name: 'reps', type: 'int', optional: true },
|
|
132
|
+
{ name: 'sets', type: 'int', optional: true },
|
|
133
|
+
{ name: 'note', type: 'string', optional: true },
|
|
134
|
+
{ name: 'order', type: 'int', optional: false },
|
|
135
|
+
{ name: 'date', type: 'string', optional: false }
|
|
136
|
+
],
|
|
137
|
+
keys: [
|
|
138
|
+
{ type: 'primary', fields: ['_id'], lookup: 'pk__id.json' },
|
|
139
|
+
{ type: 'index', fields: ['date'], lookup: 'index_date.json' },
|
|
140
|
+
{
|
|
141
|
+
type: 'foreign',
|
|
142
|
+
fields: ['exercise_id'],
|
|
143
|
+
lookup: 'fk_exercise_id.json',
|
|
144
|
+
references: [{ fields: ['exercises._id'], lookup: 'exercises.json' }]
|
|
145
|
+
}
|
|
146
|
+
],
|
|
147
|
+
entries: [
|
|
148
|
+
'2024-06-02-001.json',
|
|
149
|
+
'2024-06-02-002.json',
|
|
150
|
+
'2024-06-02-003.json',
|
|
151
|
+
'2024-06-05-001.json',
|
|
152
|
+
'2024-06-05-002.json',
|
|
153
|
+
'2024-06-05-003.json'
|
|
154
|
+
]
|
|
155
|
+
}
|
|
156
|
+
])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('retrieve workouts entries', async () => {
|
|
160
|
+
expect(await retrieveRecords('workouts')).toEqual([
|
|
161
|
+
{
|
|
162
|
+
_id: 1,
|
|
163
|
+
exercise_id: 1,
|
|
164
|
+
weight_effort: '115lbs',
|
|
165
|
+
reps: 10,
|
|
166
|
+
sets: 3,
|
|
167
|
+
note: '',
|
|
168
|
+
order: 0,
|
|
169
|
+
date: '2024-06-02'
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
_id: 2,
|
|
173
|
+
exercise_id: 2,
|
|
174
|
+
weight_effort: '155lbs',
|
|
175
|
+
reps: 10,
|
|
176
|
+
sets: 3,
|
|
177
|
+
note: '',
|
|
178
|
+
order: 1,
|
|
179
|
+
date: '2024-06-02'
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
_id: 3,
|
|
183
|
+
exercise_id: 3,
|
|
184
|
+
weight_effort: '155lbs',
|
|
185
|
+
reps: 10,
|
|
186
|
+
sets: 3,
|
|
187
|
+
note: '',
|
|
188
|
+
order: 2,
|
|
189
|
+
date: '2024-06-02'
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
_id: 4,
|
|
193
|
+
exercise_id: 1,
|
|
194
|
+
weight_effort: '120lbs',
|
|
195
|
+
reps: 10,
|
|
196
|
+
sets: 3,
|
|
197
|
+
note: '',
|
|
198
|
+
order: 0,
|
|
199
|
+
date: '2024-06-05'
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
_id: 5,
|
|
203
|
+
exercise_id: 2,
|
|
204
|
+
weight_effort: '160lbs',
|
|
205
|
+
reps: 10,
|
|
206
|
+
sets: 3,
|
|
207
|
+
note: '',
|
|
208
|
+
order: 1,
|
|
209
|
+
date: '2024-06-05'
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
_id: 6,
|
|
213
|
+
exercise_id: 3,
|
|
214
|
+
weight_effort: '160lbs',
|
|
215
|
+
reps: 10,
|
|
216
|
+
sets: 3,
|
|
217
|
+
note: '',
|
|
218
|
+
order: 2,
|
|
219
|
+
date: '2024-06-05'
|
|
220
|
+
}
|
|
221
|
+
])
|
|
222
|
+
})
|
|
223
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { retrieveFile } from './retrieveFile'
|
|
2
|
+
import { retrieveRecord } from './retrieveRecord'
|
|
3
|
+
|
|
4
|
+
export const retrieveRecords = async (entity = '') => {
|
|
5
|
+
const recordFile = await retrieveRecord(entity)
|
|
6
|
+
const dataSet = []
|
|
7
|
+
for (let file of recordFile.entries) {
|
|
8
|
+
const data = await retrieveFile(`${recordFile.path}/${file}`)
|
|
9
|
+
.catch(err => console.error(err))
|
|
10
|
+
if (!data) {
|
|
11
|
+
throw new Error(`Could not read entity ${file}`)
|
|
12
|
+
}
|
|
13
|
+
dataSet.push(data)
|
|
14
|
+
}
|
|
15
|
+
return dataSet
|
|
16
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { create } from '../introspect/create'
|
|
2
|
+
import { insertEntity } from '../queries/insertEntity'
|
|
3
|
+
import { runQuery } from './runQuery'
|
|
4
|
+
import { testHelpers } from 'js-build-tools/testHelpers'
|
|
5
|
+
|
|
6
|
+
const databasePath = 'run-query-database/'
|
|
7
|
+
const gulpConfig = testHelpers.gulpConfig
|
|
8
|
+
gulpConfig.set('databasePath', databasePath)
|
|
9
|
+
testHelpers.setDefaults(databasePath)
|
|
10
|
+
|
|
11
|
+
beforeEach(testHelpers.beforeEach)
|
|
12
|
+
|
|
13
|
+
afterEach(testHelpers.afterEach)
|
|
14
|
+
|
|
15
|
+
const definition = [
|
|
16
|
+
{ name: '_id', type: 'string', optional: false, autoGenerate: true },
|
|
17
|
+
{ name: 'bar', type: 'string', optional: false, autoGenerate: false }
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
const keys = [
|
|
21
|
+
{ type: 'primary', fields: ['_id'], lookup: 'pk__id.json' }
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const baseParsed = (overrides) => Object.assign({
|
|
25
|
+
entity: 'foo',
|
|
26
|
+
joinEntity: undefined,
|
|
27
|
+
conditions: [],
|
|
28
|
+
joinClauses: [],
|
|
29
|
+
mergeJoins: [],
|
|
30
|
+
sortClauses: [],
|
|
31
|
+
groupBy: undefined
|
|
32
|
+
}, overrides)
|
|
33
|
+
|
|
34
|
+
describe('runQuery', () => {
|
|
35
|
+
describe('delete', () => {
|
|
36
|
+
test('deletes only the rows matching a non-indexed condition', async () => {
|
|
37
|
+
await create('foo', definition, keys)
|
|
38
|
+
await insertEntity('foo', [{ bar: 'keep' }, { bar: 'remove' }, { bar: 'keep' }], {})
|
|
39
|
+
|
|
40
|
+
const result = await runQuery(baseParsed({
|
|
41
|
+
command: 'delete',
|
|
42
|
+
conditions: [{ property: 'bar', comparator: '=', value: 'remove' }]
|
|
43
|
+
}))
|
|
44
|
+
|
|
45
|
+
expect(result.foo.map(row => row.bar)).toEqual(['remove'])
|
|
46
|
+
const remaining = await runQuery(baseParsed({ command: 'read' }))
|
|
47
|
+
expect(remaining.foo.map(row => row.bar).sort()).toEqual(['keep', 'keep'])
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('with no conditions, deletes every row', async () => {
|
|
51
|
+
await create('foo', definition, keys)
|
|
52
|
+
await insertEntity('foo', [{ bar: 'one' }, { bar: 'two' }], {})
|
|
53
|
+
|
|
54
|
+
await runQuery(baseParsed({ command: 'delete' }))
|
|
55
|
+
|
|
56
|
+
const remaining = await runQuery(baseParsed({ command: 'read' }))
|
|
57
|
+
expect(remaining.foo).toEqual([])
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('update', () => {
|
|
62
|
+
test('updates only the rows matching a non-indexed condition', async () => {
|
|
63
|
+
await create('foo', definition, keys)
|
|
64
|
+
await insertEntity('foo', [{ bar: 'change-me' }, { bar: 'leave-me' }], {})
|
|
65
|
+
|
|
66
|
+
const result = await runQuery(baseParsed({
|
|
67
|
+
command: 'update',
|
|
68
|
+
conditions: [{ property: 'bar', comparator: '=', value: 'change-me' }],
|
|
69
|
+
updateValues: { bar: 'changed' }
|
|
70
|
+
}))
|
|
71
|
+
|
|
72
|
+
expect(result.foo).toHaveLength(1)
|
|
73
|
+
expect(result.foo[0].bar).toBe('changed')
|
|
74
|
+
const remaining = await runQuery(baseParsed({ command: 'read' }))
|
|
75
|
+
expect(remaining.foo.map(row => row.bar).sort()).toEqual(['changed', 'leave-me'])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('with no conditions, updates every row', async () => {
|
|
79
|
+
await create('foo', definition, keys)
|
|
80
|
+
await insertEntity('foo', [{ bar: 'one' }, { bar: 'two' }], {})
|
|
81
|
+
|
|
82
|
+
await runQuery(baseParsed({ command: 'update', updateValues: { bar: 'all-changed' } }))
|
|
83
|
+
|
|
84
|
+
const remaining = await runQuery(baseParsed({ command: 'read' }))
|
|
85
|
+
expect(remaining.foo.map(row => row.bar)).toEqual(['all-changed', 'all-changed'])
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
})
|