json-fs-query 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/jsonFsQuery.js +2 -2
- package/browser/jsonFsQuery.min.js +1 -1
- package/dist/data/introspect/create.js +46 -46
- package/dist/data/introspect/definitions/field.js +35 -35
- package/dist/data/introspect/definitions/key.js +52 -52
- package/dist/data/introspect/definitions/keyReference.js +19 -19
- package/dist/data/introspect/definitions/record.js +31 -31
- package/dist/data/introspect/describe.js +14 -14
- package/dist/data/introspect/drop.js +36 -36
- package/dist/data/introspect/index.js +29 -29
- package/dist/data/introspect/policies/foreignKeyExists.js +24 -24
- package/dist/data/introspect/policies/hasDependents.js +51 -51
- package/dist/data/introspect/policies/keyGenerate.js +16 -16
- package/dist/data/introspect/policies/keyUnique.js +30 -30
- package/dist/data/introspect/policies/typeCheck.js +56 -56
- package/dist/data/introspect/policies/typeEnum.js +13 -13
- package/dist/data/introspect/policies/useDefault.js +12 -12
- package/dist/data/queries/deleteEntity.js +99 -99
- package/dist/data/queries/groupBy.js +21 -21
- package/dist/data/queries/insertEntity.js +116 -116
- package/dist/data/queries/joinEntity.js +31 -31
- package/dist/data/queries/limitOffset.js +20 -20
- package/dist/data/queries/mergeJoins.js +18 -18
- package/dist/data/queries/parser.js +29 -29
- package/dist/data/queries/query.js +17 -17
- package/dist/data/queries/readEntity.js +13 -13
- package/dist/data/queries/selectFields.js +35 -35
- package/dist/data/queries/sortBy.js +24 -24
- package/dist/data/queries/updateEntity.js +189 -189
- package/dist/data/queries/where.js +55 -55
- package/dist/data/utilities/computeKeyValue.js +25 -25
- package/dist/data/utilities/config.js +44 -44
- package/dist/data/utilities/deregisterRecord.js +33 -33
- package/dist/data/utilities/getConditionIndexedList.js +46 -46
- package/dist/data/utilities/getJoinIndexedList.js +75 -75
- package/dist/data/utilities/getMatchingIndexes.js +27 -27
- package/dist/data/utilities/matchIndexedJoins.js +60 -60
- package/dist/data/utilities/mergeJoin.js +43 -43
- package/dist/data/utilities/parsers/findCommand.js +24 -24
- package/dist/data/utilities/parsers/findConditions.js +27 -27
- package/dist/data/utilities/parsers/findGroupBy.js +21 -21
- package/dist/data/utilities/parsers/findInsertValues.js +33 -33
- package/dist/data/utilities/parsers/findJoinClauses.js +26 -26
- package/dist/data/utilities/parsers/findJoinEntity.js +21 -21
- package/dist/data/utilities/parsers/findLimit.js +21 -21
- package/dist/data/utilities/parsers/findMergeJoins.js +25 -25
- package/dist/data/utilities/parsers/findOffset.js +21 -21
- package/dist/data/utilities/parsers/findSelects.js +29 -29
- package/dist/data/utilities/parsers/findSortClauses.js +29 -29
- package/dist/data/utilities/parsers/findUpdateValues.js +33 -33
- package/dist/data/utilities/parsers/isEntityProperty.js +7 -7
- package/dist/data/utilities/parsers/parseLiteralValue.js +33 -33
- package/dist/data/utilities/parsers/parseQuery.js +50 -50
- package/dist/data/utilities/parsers/removeFromQuery.js +7 -7
- package/dist/data/utilities/parsers/splitEntityProperty.js +22 -22
- package/dist/data/utilities/parsers/splitQueries.js +10 -10
- package/dist/data/utilities/reconcileJoin.js +25 -25
- package/dist/data/utilities/reconcileJoins.js +25 -25
- package/dist/data/utilities/registerRecord.js +66 -66
- package/dist/data/utilities/removeFromIndex.js +43 -43
- package/dist/data/utilities/restrictIndexedList.js +44 -44
- package/dist/data/utilities/retrieveFile.js +27 -27
- package/dist/data/utilities/retrieveIndexes.js +23 -23
- package/dist/data/utilities/retrieveRecord.js +8 -8
- package/dist/data/utilities/retrieveRecords.js +20 -20
- package/dist/data/utilities/runQuery.js +86 -86
- package/dist/data/utilities/updateIndex.js +55 -55
- package/dist/data/utilities/useJoinClause.js +50 -50
- package/dist/index.js +41 -41
- package/package.json +9 -5
|
@@ -1,190 +1,190 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.updateEntity = void 0;
|
|
7
|
-
require("core-js/modules/es.json.stringify.js");
|
|
8
|
-
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
-
require("core-js/modules/esnext.iterator.find.js");
|
|
10
|
-
require("core-js/modules/esnext.iterator.map.js");
|
|
11
|
-
require("core-js/modules/esnext.iterator.some.js");
|
|
12
|
-
var _retrieveRecord = require("../utilities/retrieveRecord");
|
|
13
|
-
var _retrieveFile = require("../utilities/retrieveFile");
|
|
14
|
-
var _updateIndex = require("../utilities/updateIndex");
|
|
15
|
-
var _removeFromIndex = require("../utilities/removeFromIndex");
|
|
16
|
-
var _computeKeyValue = require("../utilities/computeKeyValue");
|
|
17
|
-
var _keyUnique = require("../introspect/policies/keyUnique");
|
|
18
|
-
var _typeCheck = require("../introspect/policies/typeCheck");
|
|
19
|
-
var _foreignKeyExists = require("../introspect/policies/foreignKeyExists");
|
|
20
|
-
var _hasDependents = require("../introspect/policies/hasDependents");
|
|
21
|
-
var _splitEntityProperty = require("../utilities/parsers/splitEntityProperty");
|
|
22
|
-
var _config = require("../utilities/config");
|
|
23
|
-
const {
|
|
24
|
-
writeFile
|
|
25
|
-
} = require('fs/promises');
|
|
26
|
-
/**
|
|
27
|
-
* Same reasoning as deleteEntity: a record object carries no memory of which entry file it came
|
|
28
|
-
* from, so re-scan entries[] and match by the record's own primary key value(s) - a single field,
|
|
29
|
-
* or a composite of several for a multi-field primary key.
|
|
30
|
-
* @param record
|
|
31
|
-
* @param primaryKey
|
|
32
|
-
* @param primaryValue
|
|
33
|
-
*/
|
|
34
|
-
const findEntryFile = async (record, primaryKey, primaryValue) => {
|
|
35
|
-
for (const fileName of record.entries) {
|
|
36
|
-
const data = await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`);
|
|
37
|
-
if ((0, _computeKeyValue.computeKeyValue)(primaryKey.fields, data) == primaryValue) {
|
|
38
|
-
return fileName;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return null;
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* True if the update actually changes at least one of this key's fields - a key none of whose
|
|
45
|
-
* fields appear in newValues, or whose given values are all unchanged, doesn't need re-checking.
|
|
46
|
-
* @param key
|
|
47
|
-
* @param oldData
|
|
48
|
-
* @param newValues
|
|
49
|
-
*/
|
|
50
|
-
const keyFieldsChanged = (key, oldData, newValues) => key.fields.some(field => Object.prototype.hasOwnProperty.call(newValues, field) && newValues[field] != oldData[field]);
|
|
51
|
-
/**
|
|
52
|
-
* Only check uniqueness for a primary/unique key (single-field, or composite) whose value is
|
|
53
|
-
* actually changing - checking an unchanged value would find the record's own existing entry and
|
|
54
|
-
* wrongly report it as a duplicate of itself.
|
|
55
|
-
* @param entity
|
|
56
|
-
* @param record
|
|
57
|
-
* @param oldData
|
|
58
|
-
* @param newData
|
|
59
|
-
* @param newValues
|
|
60
|
-
*/
|
|
61
|
-
const assertUniqueForUpdate = async (entity, record, oldData, newData, newValues) => {
|
|
62
|
-
for (const key of record.keys) {
|
|
63
|
-
if (key.type !== 'primary' && key.type !== 'unique' || !keyFieldsChanged(key, oldData, newValues)) {
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
const checkKeys = key.fields.map(field => newData[field]);
|
|
67
|
-
const isUnique = await (0, _keyUnique.keyUnique)(entity, key.fields, checkKeys);
|
|
68
|
-
if (!isUnique) {
|
|
69
|
-
throw new Error(`Duplicate value for ${key.type} key "${key.fields.join(', ')}" on ${entity}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* Same "only if it actually changed" scoping as assertUniqueForUpdate: re-checking an unchanged
|
|
75
|
-
* foreign key value would re-validate a reference this update never touches, which could fail an
|
|
76
|
-
* unrelated field's update if that parent record happened to be deleted since.
|
|
77
|
-
* @param entity
|
|
78
|
-
* @param record
|
|
79
|
-
* @param oldData
|
|
80
|
-
* @param newValues
|
|
81
|
-
*/
|
|
82
|
-
const assertForeignKeysForUpdate = async (entity, record, oldData, newValues) => {
|
|
83
|
-
for (const key of record.keys) {
|
|
84
|
-
if (key.type !== 'foreign' || key.fields.length !== 1 || !key.references || key.references.length !== 1) {
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
const field = key.fields[0];
|
|
88
|
-
if (!Object.prototype.hasOwnProperty.call(newValues, field) || newValues[field] == oldData[field]) {
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
const {
|
|
92
|
-
entity: refEntity,
|
|
93
|
-
property: refProperty
|
|
94
|
-
} = (0, _splitEntityProperty.splitEntityProperty)(key.references[0].fields[0]);
|
|
95
|
-
const exists = await (0, _foreignKeyExists.foreignKeyExists)(refEntity, refProperty, newValues[field]);
|
|
96
|
-
if (!exists) {
|
|
97
|
-
throw new Error(`Foreign key "${field}" on ${entity} references a nonexistent ${refEntity}.${refProperty} = ${newValues[field]}`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* RESTRICT: reject an update that would change a single-field primary key's value while another
|
|
103
|
-
* entity's foreign key still points at the old one - that reference would otherwise be left
|
|
104
|
-
* dangling, since the row itself isn't gone, just re-keyed. Only meaningful for a single-field
|
|
105
|
-
* primary key, matching foreign key support's own single-field-only scope.
|
|
106
|
-
* @param entity
|
|
107
|
-
* @param primaryKey
|
|
108
|
-
* @param oldData
|
|
109
|
-
* @param newValues
|
|
110
|
-
*/
|
|
111
|
-
const assertNoDependentsForUpdate = async (entity, primaryKey, oldData, newValues) => {
|
|
112
|
-
if (!(0, _config.getSetting)('enforceForeignKeys', true) || primaryKey.fields.length !== 1 || !keyFieldsChanged(primaryKey, oldData, newValues)) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const property = primaryKey.fields[0];
|
|
116
|
-
if (await (0, _hasDependents.hasDependents)(entity, property, oldData[property])) {
|
|
117
|
-
throw new Error(`Cannot update "${entity}" where "${property}" = ${oldData[property]} - other records still reference it`);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
/**
|
|
121
|
-
* Move a key's index entry (single-field, or a composite for a multi-field key) from the old
|
|
122
|
-
* value to the new one wherever the update actually changed one of its fields.
|
|
123
|
-
* @param record
|
|
124
|
-
* @param oldData
|
|
125
|
-
* @param newData
|
|
126
|
-
* @param newValues
|
|
127
|
-
* @param fileName
|
|
128
|
-
*/
|
|
129
|
-
const reconcileIndexes = async (record, oldData, newData, newValues, fileName) => {
|
|
130
|
-
for (const key of record.keys) {
|
|
131
|
-
if (!keyFieldsChanged(key, oldData, newValues)) {
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
await (0, _removeFromIndex.removeFromIndex)(record.path, key, (0, _computeKeyValue.computeKeyValue)(key.fields, oldData), fileName);
|
|
135
|
-
await (0, _updateIndex.updateIndex)(record.path, key, (0, _computeKeyValue.computeKeyValue)(key.fields, newData), fileName);
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
const assertRequiredFields = (entity, record, newData) => {
|
|
139
|
-
for (const field of record.definition) {
|
|
140
|
-
if (!field.optional && typeof newData[field.name] === 'undefined') {
|
|
141
|
-
throw new Error(`Missing required field "${field.name}" for ${entity}`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
/**
|
|
146
|
-
* Type-check (and coerce) only the fields actually present in an update's values, never the rest
|
|
147
|
-
* of the merged record - a field whose already-stored value doesn't match its declared type
|
|
148
|
-
* (nothing enforced this before typeCheck() existed) must not block an update that never touches
|
|
149
|
-
* that field.
|
|
150
|
-
* @param record
|
|
151
|
-
* @param values
|
|
152
|
-
*/
|
|
153
|
-
const applyFieldTypes = (record, values) => {
|
|
154
|
-
const typedValues = Object.assign({}, values);
|
|
155
|
-
for (const field of record.definition) {
|
|
156
|
-
if (Object.prototype.hasOwnProperty.call(typedValues, field.name)) {
|
|
157
|
-
typedValues[field.name] = (0, _typeCheck.typeCheck)(field.type, typedValues[field.name]);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return typedValues;
|
|
161
|
-
};
|
|
162
|
-
const updateEntity = async (entity = '', values = {}, dataSet = {}) => {
|
|
163
|
-
const databasePath = (0, _config.getSetting)('databasePath', 'database/');
|
|
164
|
-
const record = await (0, _retrieveRecord.retrieveRecord)(entity);
|
|
165
|
-
const primaryKey = record.keys.find(key => key.type === 'primary');
|
|
166
|
-
if (!primaryKey) {
|
|
167
|
-
throw new Error(`Cannot update "${entity}" without a primary key`);
|
|
168
|
-
}
|
|
169
|
-
const typedValues = applyFieldTypes(record, values);
|
|
170
|
-
const toUpdate = dataSet[entity] || [];
|
|
171
|
-
const updatedRows = [];
|
|
172
|
-
for (const row of toUpdate) {
|
|
173
|
-
const fileName = await findEntryFile(record, primaryKey, (0, _computeKeyValue.computeKeyValue)(primaryKey.fields, row));
|
|
174
|
-
if (!fileName) {
|
|
175
|
-
throw new Error(`Could not find entry for ${entity} where "${primaryKey.fields.join(', ')}" = ${(0, _computeKeyValue.computeKeyValue)(primaryKey.fields, row)}`);
|
|
176
|
-
}
|
|
177
|
-
const oldData = await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`);
|
|
178
|
-
const newData = Object.assign({}, oldData, typedValues);
|
|
179
|
-
await assertUniqueForUpdate(entity, record, oldData, newData, typedValues);
|
|
180
|
-
await assertForeignKeysForUpdate(entity, record, oldData, typedValues);
|
|
181
|
-
await assertNoDependentsForUpdate(entity, primaryKey, oldData, typedValues);
|
|
182
|
-
assertRequiredFields(entity, record, newData);
|
|
183
|
-
await reconcileIndexes(record, oldData, newData, typedValues, fileName);
|
|
184
|
-
await writeFile(`${databasePath}${record.path}/${fileName}`, JSON.stringify(newData, null, 2));
|
|
185
|
-
updatedRows.push(newData);
|
|
186
|
-
}
|
|
187
|
-
dataSet[entity] = updatedRows;
|
|
188
|
-
return dataSet;
|
|
189
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateEntity = void 0;
|
|
7
|
+
require("core-js/modules/es.json.stringify.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
+
require("core-js/modules/esnext.iterator.find.js");
|
|
10
|
+
require("core-js/modules/esnext.iterator.map.js");
|
|
11
|
+
require("core-js/modules/esnext.iterator.some.js");
|
|
12
|
+
var _retrieveRecord = require("../utilities/retrieveRecord");
|
|
13
|
+
var _retrieveFile = require("../utilities/retrieveFile");
|
|
14
|
+
var _updateIndex = require("../utilities/updateIndex");
|
|
15
|
+
var _removeFromIndex = require("../utilities/removeFromIndex");
|
|
16
|
+
var _computeKeyValue = require("../utilities/computeKeyValue");
|
|
17
|
+
var _keyUnique = require("../introspect/policies/keyUnique");
|
|
18
|
+
var _typeCheck = require("../introspect/policies/typeCheck");
|
|
19
|
+
var _foreignKeyExists = require("../introspect/policies/foreignKeyExists");
|
|
20
|
+
var _hasDependents = require("../introspect/policies/hasDependents");
|
|
21
|
+
var _splitEntityProperty = require("../utilities/parsers/splitEntityProperty");
|
|
22
|
+
var _config = require("../utilities/config");
|
|
23
|
+
const {
|
|
24
|
+
writeFile
|
|
25
|
+
} = require('fs/promises');
|
|
26
|
+
/**
|
|
27
|
+
* Same reasoning as deleteEntity: a record object carries no memory of which entry file it came
|
|
28
|
+
* from, so re-scan entries[] and match by the record's own primary key value(s) - a single field,
|
|
29
|
+
* or a composite of several for a multi-field primary key.
|
|
30
|
+
* @param record
|
|
31
|
+
* @param primaryKey
|
|
32
|
+
* @param primaryValue
|
|
33
|
+
*/
|
|
34
|
+
const findEntryFile = async (record, primaryKey, primaryValue) => {
|
|
35
|
+
for (const fileName of record.entries) {
|
|
36
|
+
const data = await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`);
|
|
37
|
+
if ((0, _computeKeyValue.computeKeyValue)(primaryKey.fields, data) == primaryValue) {
|
|
38
|
+
return fileName;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* True if the update actually changes at least one of this key's fields - a key none of whose
|
|
45
|
+
* fields appear in newValues, or whose given values are all unchanged, doesn't need re-checking.
|
|
46
|
+
* @param key
|
|
47
|
+
* @param oldData
|
|
48
|
+
* @param newValues
|
|
49
|
+
*/
|
|
50
|
+
const keyFieldsChanged = (key, oldData, newValues) => key.fields.some(field => Object.prototype.hasOwnProperty.call(newValues, field) && newValues[field] != oldData[field]);
|
|
51
|
+
/**
|
|
52
|
+
* Only check uniqueness for a primary/unique key (single-field, or composite) whose value is
|
|
53
|
+
* actually changing - checking an unchanged value would find the record's own existing entry and
|
|
54
|
+
* wrongly report it as a duplicate of itself.
|
|
55
|
+
* @param entity
|
|
56
|
+
* @param record
|
|
57
|
+
* @param oldData
|
|
58
|
+
* @param newData
|
|
59
|
+
* @param newValues
|
|
60
|
+
*/
|
|
61
|
+
const assertUniqueForUpdate = async (entity, record, oldData, newData, newValues) => {
|
|
62
|
+
for (const key of record.keys) {
|
|
63
|
+
if (key.type !== 'primary' && key.type !== 'unique' || !keyFieldsChanged(key, oldData, newValues)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const checkKeys = key.fields.map(field => newData[field]);
|
|
67
|
+
const isUnique = await (0, _keyUnique.keyUnique)(entity, key.fields, checkKeys);
|
|
68
|
+
if (!isUnique) {
|
|
69
|
+
throw new Error(`Duplicate value for ${key.type} key "${key.fields.join(', ')}" on ${entity}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Same "only if it actually changed" scoping as assertUniqueForUpdate: re-checking an unchanged
|
|
75
|
+
* foreign key value would re-validate a reference this update never touches, which could fail an
|
|
76
|
+
* unrelated field's update if that parent record happened to be deleted since.
|
|
77
|
+
* @param entity
|
|
78
|
+
* @param record
|
|
79
|
+
* @param oldData
|
|
80
|
+
* @param newValues
|
|
81
|
+
*/
|
|
82
|
+
const assertForeignKeysForUpdate = async (entity, record, oldData, newValues) => {
|
|
83
|
+
for (const key of record.keys) {
|
|
84
|
+
if (key.type !== 'foreign' || key.fields.length !== 1 || !key.references || key.references.length !== 1) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const field = key.fields[0];
|
|
88
|
+
if (!Object.prototype.hasOwnProperty.call(newValues, field) || newValues[field] == oldData[field]) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const {
|
|
92
|
+
entity: refEntity,
|
|
93
|
+
property: refProperty
|
|
94
|
+
} = (0, _splitEntityProperty.splitEntityProperty)(key.references[0].fields[0]);
|
|
95
|
+
const exists = await (0, _foreignKeyExists.foreignKeyExists)(refEntity, refProperty, newValues[field]);
|
|
96
|
+
if (!exists) {
|
|
97
|
+
throw new Error(`Foreign key "${field}" on ${entity} references a nonexistent ${refEntity}.${refProperty} = ${newValues[field]}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* RESTRICT: reject an update that would change a single-field primary key's value while another
|
|
103
|
+
* entity's foreign key still points at the old one - that reference would otherwise be left
|
|
104
|
+
* dangling, since the row itself isn't gone, just re-keyed. Only meaningful for a single-field
|
|
105
|
+
* primary key, matching foreign key support's own single-field-only scope.
|
|
106
|
+
* @param entity
|
|
107
|
+
* @param primaryKey
|
|
108
|
+
* @param oldData
|
|
109
|
+
* @param newValues
|
|
110
|
+
*/
|
|
111
|
+
const assertNoDependentsForUpdate = async (entity, primaryKey, oldData, newValues) => {
|
|
112
|
+
if (!(0, _config.getSetting)('enforceForeignKeys', true) || primaryKey.fields.length !== 1 || !keyFieldsChanged(primaryKey, oldData, newValues)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const property = primaryKey.fields[0];
|
|
116
|
+
if (await (0, _hasDependents.hasDependents)(entity, property, oldData[property])) {
|
|
117
|
+
throw new Error(`Cannot update "${entity}" where "${property}" = ${oldData[property]} - other records still reference it`);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Move a key's index entry (single-field, or a composite for a multi-field key) from the old
|
|
122
|
+
* value to the new one wherever the update actually changed one of its fields.
|
|
123
|
+
* @param record
|
|
124
|
+
* @param oldData
|
|
125
|
+
* @param newData
|
|
126
|
+
* @param newValues
|
|
127
|
+
* @param fileName
|
|
128
|
+
*/
|
|
129
|
+
const reconcileIndexes = async (record, oldData, newData, newValues, fileName) => {
|
|
130
|
+
for (const key of record.keys) {
|
|
131
|
+
if (!keyFieldsChanged(key, oldData, newValues)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
await (0, _removeFromIndex.removeFromIndex)(record.path, key, (0, _computeKeyValue.computeKeyValue)(key.fields, oldData), fileName);
|
|
135
|
+
await (0, _updateIndex.updateIndex)(record.path, key, (0, _computeKeyValue.computeKeyValue)(key.fields, newData), fileName);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const assertRequiredFields = (entity, record, newData) => {
|
|
139
|
+
for (const field of record.definition) {
|
|
140
|
+
if (!field.optional && typeof newData[field.name] === 'undefined') {
|
|
141
|
+
throw new Error(`Missing required field "${field.name}" for ${entity}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Type-check (and coerce) only the fields actually present in an update's values, never the rest
|
|
147
|
+
* of the merged record - a field whose already-stored value doesn't match its declared type
|
|
148
|
+
* (nothing enforced this before typeCheck() existed) must not block an update that never touches
|
|
149
|
+
* that field.
|
|
150
|
+
* @param record
|
|
151
|
+
* @param values
|
|
152
|
+
*/
|
|
153
|
+
const applyFieldTypes = (record, values) => {
|
|
154
|
+
const typedValues = Object.assign({}, values);
|
|
155
|
+
for (const field of record.definition) {
|
|
156
|
+
if (Object.prototype.hasOwnProperty.call(typedValues, field.name)) {
|
|
157
|
+
typedValues[field.name] = (0, _typeCheck.typeCheck)(field.type, typedValues[field.name]);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return typedValues;
|
|
161
|
+
};
|
|
162
|
+
const updateEntity = async (entity = '', values = {}, dataSet = {}) => {
|
|
163
|
+
const databasePath = (0, _config.getSetting)('databasePath', 'database/');
|
|
164
|
+
const record = await (0, _retrieveRecord.retrieveRecord)(entity);
|
|
165
|
+
const primaryKey = record.keys.find(key => key.type === 'primary');
|
|
166
|
+
if (!primaryKey) {
|
|
167
|
+
throw new Error(`Cannot update "${entity}" without a primary key`);
|
|
168
|
+
}
|
|
169
|
+
const typedValues = applyFieldTypes(record, values);
|
|
170
|
+
const toUpdate = dataSet[entity] || [];
|
|
171
|
+
const updatedRows = [];
|
|
172
|
+
for (const row of toUpdate) {
|
|
173
|
+
const fileName = await findEntryFile(record, primaryKey, (0, _computeKeyValue.computeKeyValue)(primaryKey.fields, row));
|
|
174
|
+
if (!fileName) {
|
|
175
|
+
throw new Error(`Could not find entry for ${entity} where "${primaryKey.fields.join(', ')}" = ${(0, _computeKeyValue.computeKeyValue)(primaryKey.fields, row)}`);
|
|
176
|
+
}
|
|
177
|
+
const oldData = await (0, _retrieveFile.retrieveFile)(`${record.path}/${fileName}`);
|
|
178
|
+
const newData = Object.assign({}, oldData, typedValues);
|
|
179
|
+
await assertUniqueForUpdate(entity, record, oldData, newData, typedValues);
|
|
180
|
+
await assertForeignKeysForUpdate(entity, record, oldData, typedValues);
|
|
181
|
+
await assertNoDependentsForUpdate(entity, primaryKey, oldData, typedValues);
|
|
182
|
+
assertRequiredFields(entity, record, newData);
|
|
183
|
+
await reconcileIndexes(record, oldData, newData, typedValues, fileName);
|
|
184
|
+
await writeFile(`${databasePath}${record.path}/${fileName}`, JSON.stringify(newData, null, 2));
|
|
185
|
+
updatedRows.push(newData);
|
|
186
|
+
}
|
|
187
|
+
dataSet[entity] = updatedRows;
|
|
188
|
+
return dataSet;
|
|
189
|
+
};
|
|
190
190
|
exports.updateEntity = updateEntity;
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.where = void 0;
|
|
7
|
-
require("core-js/modules/es.array.includes.js");
|
|
8
|
-
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
-
require("core-js/modules/esnext.iterator.filter.js");
|
|
10
|
-
const likeMatch = (dataValue, pattern) => {
|
|
11
|
-
if (typeof dataValue !== 'string' || typeof pattern !== 'string') {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
const regexPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/%/g, '.*').replace(/_/g, '.');
|
|
15
|
-
return new RegExp(`^${regexPattern}$`, 'i').test(dataValue);
|
|
16
|
-
};
|
|
17
|
-
const makeCondition = (data, property, comparator, value) => {
|
|
18
|
-
if (!Object.prototype.hasOwnProperty.call(data, property)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
const dataValue = data[property];
|
|
22
|
-
switch (comparator) {
|
|
23
|
-
case '=':
|
|
24
|
-
return dataValue == value;
|
|
25
|
-
case '!=':
|
|
26
|
-
case '<>':
|
|
27
|
-
return dataValue != value;
|
|
28
|
-
case '>':
|
|
29
|
-
return dataValue > value;
|
|
30
|
-
case '>=':
|
|
31
|
-
return dataValue >= value;
|
|
32
|
-
case '<':
|
|
33
|
-
return dataValue < value;
|
|
34
|
-
case '<=':
|
|
35
|
-
return dataValue <= value;
|
|
36
|
-
case 'in':
|
|
37
|
-
return Array.isArray(value) && value.includes(dataValue);
|
|
38
|
-
case 'between':
|
|
39
|
-
return Array.isArray(value) && value.length === 2 && dataValue >= value[0] && dataValue <= value[1];
|
|
40
|
-
case 'like':
|
|
41
|
-
return likeMatch(dataValue, value);
|
|
42
|
-
}
|
|
43
|
-
return false;
|
|
44
|
-
};
|
|
45
|
-
const where = (dataSet = [], {
|
|
46
|
-
property = '',
|
|
47
|
-
joinEntity = undefined,
|
|
48
|
-
comparator = '=',
|
|
49
|
-
value = null
|
|
50
|
-
} = {}) => {
|
|
51
|
-
return dataSet.filter(data => {
|
|
52
|
-
const testProperty = joinEntity ? `${joinEntity}.${property}` : property;
|
|
53
|
-
return makeCondition(data, testProperty, comparator, value);
|
|
54
|
-
});
|
|
55
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.where = void 0;
|
|
7
|
+
require("core-js/modules/es.array.includes.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
+
require("core-js/modules/esnext.iterator.filter.js");
|
|
10
|
+
const likeMatch = (dataValue, pattern) => {
|
|
11
|
+
if (typeof dataValue !== 'string' || typeof pattern !== 'string') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const regexPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/%/g, '.*').replace(/_/g, '.');
|
|
15
|
+
return new RegExp(`^${regexPattern}$`, 'i').test(dataValue);
|
|
16
|
+
};
|
|
17
|
+
const makeCondition = (data, property, comparator, value) => {
|
|
18
|
+
if (!Object.prototype.hasOwnProperty.call(data, property)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const dataValue = data[property];
|
|
22
|
+
switch (comparator) {
|
|
23
|
+
case '=':
|
|
24
|
+
return dataValue == value;
|
|
25
|
+
case '!=':
|
|
26
|
+
case '<>':
|
|
27
|
+
return dataValue != value;
|
|
28
|
+
case '>':
|
|
29
|
+
return dataValue > value;
|
|
30
|
+
case '>=':
|
|
31
|
+
return dataValue >= value;
|
|
32
|
+
case '<':
|
|
33
|
+
return dataValue < value;
|
|
34
|
+
case '<=':
|
|
35
|
+
return dataValue <= value;
|
|
36
|
+
case 'in':
|
|
37
|
+
return Array.isArray(value) && value.includes(dataValue);
|
|
38
|
+
case 'between':
|
|
39
|
+
return Array.isArray(value) && value.length === 2 && dataValue >= value[0] && dataValue <= value[1];
|
|
40
|
+
case 'like':
|
|
41
|
+
return likeMatch(dataValue, value);
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
const where = (dataSet = [], {
|
|
46
|
+
property = '',
|
|
47
|
+
joinEntity = undefined,
|
|
48
|
+
comparator = '=',
|
|
49
|
+
value = null
|
|
50
|
+
} = {}) => {
|
|
51
|
+
return dataSet.filter(data => {
|
|
52
|
+
const testProperty = joinEntity ? `${joinEntity}.${property}` : property;
|
|
53
|
+
return makeCondition(data, testProperty, comparator, value);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
56
|
exports.where = where;
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.computeKeyValue = void 0;
|
|
7
|
-
require("core-js/modules/es.json.stringify.js");
|
|
8
|
-
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
-
require("core-js/modules/esnext.iterator.map.js");
|
|
10
|
-
/**
|
|
11
|
-
* The value a key (of any field count) indexes and checks uniqueness by. A single-field key uses
|
|
12
|
-
* the field's own raw value directly - this is the pre-existing, already-shipped index file
|
|
13
|
-
* format (e.g. a plain "2024-06-02" or 1), and stays exactly as it was for backward compatibility
|
|
14
|
-
* with real fixture data. A multi-field key has no single value to use, so its fields' values are
|
|
15
|
-
* combined into one deterministic composite (a JSON array), in the same field order as key.fields
|
|
16
|
-
* - two records only share a composite value if every field matches.
|
|
17
|
-
* @param fields
|
|
18
|
-
* @param values
|
|
19
|
-
*/
|
|
20
|
-
const computeKeyValue = (fields, values) => {
|
|
21
|
-
if (fields.length === 1) {
|
|
22
|
-
return values[fields[0]];
|
|
23
|
-
}
|
|
24
|
-
return JSON.stringify(fields.map(field => values[field]));
|
|
25
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.computeKeyValue = void 0;
|
|
7
|
+
require("core-js/modules/es.json.stringify.js");
|
|
8
|
+
require("core-js/modules/esnext.iterator.constructor.js");
|
|
9
|
+
require("core-js/modules/esnext.iterator.map.js");
|
|
10
|
+
/**
|
|
11
|
+
* The value a key (of any field count) indexes and checks uniqueness by. A single-field key uses
|
|
12
|
+
* the field's own raw value directly - this is the pre-existing, already-shipped index file
|
|
13
|
+
* format (e.g. a plain "2024-06-02" or 1), and stays exactly as it was for backward compatibility
|
|
14
|
+
* with real fixture data. A multi-field key has no single value to use, so its fields' values are
|
|
15
|
+
* combined into one deterministic composite (a JSON array), in the same field order as key.fields
|
|
16
|
+
* - two records only share a composite value if every field matches.
|
|
17
|
+
* @param fields
|
|
18
|
+
* @param values
|
|
19
|
+
*/
|
|
20
|
+
const computeKeyValue = (fields, values) => {
|
|
21
|
+
if (fields.length === 1) {
|
|
22
|
+
return values[fields[0]];
|
|
23
|
+
}
|
|
24
|
+
return JSON.stringify(fields.map(field => values[field]));
|
|
25
|
+
};
|
|
26
26
|
exports.computeKeyValue = computeKeyValue;
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.getSetting = exports.configure = void 0;
|
|
7
|
-
const defaults = {
|
|
8
|
-
databasePath: 'database/',
|
|
9
|
-
relativePath: '',
|
|
10
|
-
enforceForeignKeys: true
|
|
11
|
-
};
|
|
12
|
-
let settings = {
|
|
13
|
-
...defaults
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Set where this package's data lives: databasePath (a filesystem path, used in Node) and/or
|
|
17
|
-
* relativePath (a URL prefix prepended to databasePath, used when reading over fetch() in a
|
|
18
|
-
* browser). Call this once before using create()/drop()/query() - a fresh consumer has no other
|
|
19
|
-
* way to point this package at their own data directory.
|
|
20
|
-
*
|
|
21
|
-
* enforceForeignKeys (default true) controls whether deleteEntity/updateEntity block an operation
|
|
22
|
-
* that would leave another record's foreign key pointing at a value that no longer exists -
|
|
23
|
-
* mirroring MySQL's own foreign_key_checks setting, which is also toggleable rather than always
|
|
24
|
-
* on, since strict enforcement isn't always what every consumer wants.
|
|
25
|
-
* @param newSettings
|
|
26
|
-
*/
|
|
27
|
-
const configure = newSettings => {
|
|
28
|
-
settings = {
|
|
29
|
-
...settings,
|
|
30
|
-
...newSettings
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Read one runtime setting, falling back to the given default if it was never configured. This is
|
|
35
|
-
* the package's own lightweight replacement for reading settings via js-build-tools/gulp.config -
|
|
36
|
-
* that module is a dev-time build tool, not something a published package should require at
|
|
37
|
-
* runtime, and it isn't available at all once a consumer installs this package on its own.
|
|
38
|
-
* @param key
|
|
39
|
-
* @param defaultValue
|
|
40
|
-
*/
|
|
41
|
-
exports.configure = configure;
|
|
42
|
-
const getSetting = (key, defaultValue) => {
|
|
43
|
-
return typeof settings[key] !== 'undefined' ? settings[key] : defaultValue;
|
|
44
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getSetting = exports.configure = void 0;
|
|
7
|
+
const defaults = {
|
|
8
|
+
databasePath: 'database/',
|
|
9
|
+
relativePath: '',
|
|
10
|
+
enforceForeignKeys: true
|
|
11
|
+
};
|
|
12
|
+
let settings = {
|
|
13
|
+
...defaults
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Set where this package's data lives: databasePath (a filesystem path, used in Node) and/or
|
|
17
|
+
* relativePath (a URL prefix prepended to databasePath, used when reading over fetch() in a
|
|
18
|
+
* browser). Call this once before using create()/drop()/query() - a fresh consumer has no other
|
|
19
|
+
* way to point this package at their own data directory.
|
|
20
|
+
*
|
|
21
|
+
* enforceForeignKeys (default true) controls whether deleteEntity/updateEntity block an operation
|
|
22
|
+
* that would leave another record's foreign key pointing at a value that no longer exists -
|
|
23
|
+
* mirroring MySQL's own foreign_key_checks setting, which is also toggleable rather than always
|
|
24
|
+
* on, since strict enforcement isn't always what every consumer wants.
|
|
25
|
+
* @param newSettings
|
|
26
|
+
*/
|
|
27
|
+
const configure = newSettings => {
|
|
28
|
+
settings = {
|
|
29
|
+
...settings,
|
|
30
|
+
...newSettings
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Read one runtime setting, falling back to the given default if it was never configured. This is
|
|
35
|
+
* the package's own lightweight replacement for reading settings via js-build-tools/gulp.config -
|
|
36
|
+
* that module is a dev-time build tool, not something a published package should require at
|
|
37
|
+
* runtime, and it isn't available at all once a consumer installs this package on its own.
|
|
38
|
+
* @param key
|
|
39
|
+
* @param defaultValue
|
|
40
|
+
*/
|
|
41
|
+
exports.configure = configure;
|
|
42
|
+
const getSetting = (key, defaultValue) => {
|
|
43
|
+
return typeof settings[key] !== 'undefined' ? settings[key] : defaultValue;
|
|
44
|
+
};
|
|
45
45
|
exports.getSetting = getSetting;
|