@fjell/lib-sequelize 4.4.0 → 4.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/AggregationBuilder.cjs +31 -0
- package/dist/cjs/Coordinate.cjs +37 -0
- package/dist/cjs/Definition.cjs +46 -0
- package/dist/cjs/EventCoordinator.cjs +54 -0
- package/dist/cjs/Instance.cjs +40 -0
- package/dist/cjs/KeyMaster.cjs +129 -0
- package/dist/cjs/Operations.cjs +29 -0
- package/dist/cjs/Options.cjs +39 -0
- package/dist/cjs/QueryBuilder.cjs +290 -0
- package/dist/cjs/ReferenceBuilder.cjs +34 -0
- package/dist/cjs/RowProcessor.cjs +41 -0
- package/dist/cjs/contained/Instance.cjs +21 -0
- package/dist/cjs/contained/index.cjs +10 -0
- package/dist/cjs/index.cjs +23 -0
- package/dist/cjs/logger.cjs +10 -0
- package/dist/cjs/ops/all.cjs +62 -0
- package/dist/cjs/ops/create.cjs +154 -0
- package/dist/cjs/ops/find.cjs +45 -0
- package/dist/cjs/ops/get.cjs +85 -0
- package/dist/cjs/ops/one.cjs +26 -0
- package/dist/cjs/ops/remove.cjs +111 -0
- package/dist/cjs/ops/update.cjs +61 -0
- package/dist/cjs/primary/Instance.cjs +31 -0
- package/dist/cjs/primary/index.cjs +10 -0
- package/dist/cjs/util/general.cjs +52 -0
- package/dist/cjs/util/relationshipUtils.cjs +117 -0
- package/dist/es/AggregationBuilder.js +27 -0
- package/dist/es/EventCoordinator.js +48 -0
- package/dist/{Instance.js → es/Instance.js} +6 -5
- package/dist/es/KeyMaster.js +124 -0
- package/dist/{Operations.js → es/Operations.js} +8 -8
- package/dist/{Options.js → es/Options.js} +8 -11
- package/dist/{QueryBuilder.js → es/QueryBuilder.js} +123 -21
- package/dist/es/ReferenceBuilder.js +30 -0
- package/dist/es/RowProcessor.js +37 -0
- package/dist/{contained → es/contained}/Instance.js +6 -5
- package/dist/{index.js → es/index.js} +1 -1
- package/dist/{ops → es/ops}/all.js +7 -4
- package/dist/es/ops/create.js +150 -0
- package/dist/{ops → es/ops}/find.js +16 -6
- package/dist/es/ops/get.js +81 -0
- package/dist/{ops → es/ops}/one.js +2 -2
- package/dist/{ops → es/ops}/remove.js +51 -11
- package/dist/{ops → es/ops}/update.js +15 -15
- package/dist/{primary → es/primary}/Instance.js +6 -5
- package/dist/es/util/general.js +47 -0
- package/dist/es/util/relationshipUtils.js +112 -0
- package/dist/index.cjs +1279 -0
- package/dist/index.cjs.map +1 -0
- package/dist/types/AggregationBuilder.d.ts +4 -0
- package/dist/{Definition.d.ts → types/Definition.d.ts} +1 -1
- package/dist/{EventCoordinator.d.ts → types/EventCoordinator.d.ts} +1 -0
- package/dist/{Instance.d.ts → types/Instance.d.ts} +1 -1
- package/dist/types/KeyMaster.d.ts +4 -0
- package/dist/{Operations.d.ts → types/Operations.d.ts} +2 -2
- package/dist/{Options.d.ts → types/Options.d.ts} +14 -3
- package/dist/{QueryBuilder.d.ts → types/QueryBuilder.d.ts} +1 -0
- package/dist/types/ReferenceBuilder.d.ts +3 -0
- package/dist/{RowProcessor.d.ts → types/RowProcessor.d.ts} +3 -1
- package/dist/{contained → types/contained}/Instance.d.ts +2 -1
- package/dist/{ops → types/ops}/all.d.ts +2 -1
- package/dist/{ops → types/ops}/create.d.ts +3 -2
- package/dist/{ops → types/ops}/find.d.ts +2 -1
- package/dist/{ops → types/ops}/get.d.ts +2 -1
- package/dist/{ops → types/ops}/one.d.ts +2 -1
- package/dist/{ops → types/ops}/remove.d.ts +2 -1
- package/dist/{ops → types/ops}/update.d.ts +2 -1
- package/dist/{primary → types/primary}/Instance.d.ts +2 -1
- package/dist/types/util/general.d.ts +4 -0
- package/dist/types/util/relationshipUtils.d.ts +21 -0
- package/package.json +37 -32
- package/dist/EventCoordinator.js +0 -30
- package/dist/KeyMaster.d.ts +0 -4
- package/dist/KeyMaster.js +0 -84
- package/dist/RowProcessor.js +0 -18
- package/dist/ops/create.js +0 -18
- package/dist/ops/get.js +0 -45
- package/src/Coordinate.ts +0 -16
- package/src/Definition.ts +0 -49
- package/src/EventCoordinator.ts +0 -103
- package/src/Instance.ts +0 -44
- package/src/KeyMaster.ts +0 -90
- package/src/Operations.ts +0 -42
- package/src/Options.ts +0 -41
- package/src/QueryBuilder.ts +0 -208
- package/src/RowProcessor.ts +0 -23
- package/src/contained/Instance.ts +0 -44
- package/src/contained/index.ts +0 -1
- package/src/index.ts +0 -7
- package/src/logger.ts +0 -5
- package/src/ops/all.ts +0 -76
- package/src/ops/create.ts +0 -40
- package/src/ops/find.ts +0 -49
- package/src/ops/get.ts +0 -67
- package/src/ops/one.ts +0 -37
- package/src/ops/remove.ts +0 -81
- package/src/ops/update.ts +0 -78
- package/src/primary/Instance.ts +0 -40
- package/src/primary/index.ts +0 -1
- /package/dist/{Coordinate.js → es/Coordinate.js} +0 -0
- /package/dist/{Definition.js → es/Definition.js} +0 -0
- /package/dist/{contained → es/contained}/index.js +0 -0
- /package/dist/{logger.js → es/logger.js} +0 -0
- /package/dist/{primary → es/primary}/index.js +0 -0
- /package/dist/{Coordinate.d.ts → types/Coordinate.d.ts} +0 -0
- /package/dist/{contained → types/contained}/index.d.ts +0 -0
- /package/dist/{index.d.ts → types/index.d.ts} +0 -0
- /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
- /package/dist/{primary → types/primary}/index.d.ts +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs');
|
|
7
|
+
const RowProcessor = require('../RowProcessor.cjs');
|
|
8
|
+
|
|
9
|
+
const logger = logger$1.default.get('sequelize', 'ops', 'find');
|
|
10
|
+
const getFindOperation = (models, definition, registry)=>{
|
|
11
|
+
const { options: { finders, references, aggregations } } = definition;
|
|
12
|
+
const find = async (finder, finderParams, locations)=>{
|
|
13
|
+
logger.default('Find', {
|
|
14
|
+
finder,
|
|
15
|
+
finderParams,
|
|
16
|
+
locations
|
|
17
|
+
});
|
|
18
|
+
// Note that we execute the createFinders function here because we want to make sure we're always getting the
|
|
19
|
+
// most up to date methods.
|
|
20
|
+
if (finders && finders[finder]) {
|
|
21
|
+
const finderMethod = finders[finder];
|
|
22
|
+
if (finderMethod) {
|
|
23
|
+
const results = await finderMethod(finderParams, locations);
|
|
24
|
+
if (results && results.length > 0) {
|
|
25
|
+
return await Promise.all(results.map(async (row)=>{
|
|
26
|
+
const processedRow = await RowProcessor.processRow(row, definition.coordinate.kta, references, aggregations, registry);
|
|
27
|
+
return core.validateKeys(processedRow, definition.coordinate.kta);
|
|
28
|
+
}));
|
|
29
|
+
} else {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
logger.error(`Finder %s not found`, finder);
|
|
34
|
+
throw new Error(`Finder ${finder} not found`);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
logger.error(`No finders have been defined for this lib`);
|
|
38
|
+
throw new Error(`No finders found`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return find;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
exports.getFindOperation = getFindOperation;
|
|
45
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmluZC5janMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs');
|
|
7
|
+
const RowProcessor = require('../RowProcessor.cjs');
|
|
8
|
+
const Library = require('@fjell/lib');
|
|
9
|
+
const relationshipUtils = require('../util/relationshipUtils.cjs');
|
|
10
|
+
|
|
11
|
+
const logger = logger$1.default.get('sequelize', 'ops', 'get');
|
|
12
|
+
// Helper function to process composite key and build query options
|
|
13
|
+
const processCompositeKey = (comKey, model, kta)=>{
|
|
14
|
+
const where = {
|
|
15
|
+
id: comKey.pk
|
|
16
|
+
};
|
|
17
|
+
const includes = [];
|
|
18
|
+
for (const locator of comKey.loc){
|
|
19
|
+
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locator.kt, kta);
|
|
20
|
+
if (!relationshipInfo.found) {
|
|
21
|
+
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
22
|
+
logger.error(errorMessage, {
|
|
23
|
+
key: comKey,
|
|
24
|
+
kta
|
|
25
|
+
});
|
|
26
|
+
throw new Error(errorMessage);
|
|
27
|
+
}
|
|
28
|
+
if (relationshipInfo.path) {
|
|
29
|
+
// This requires a relationship traversal
|
|
30
|
+
where[relationshipInfo.path] = locator.lk;
|
|
31
|
+
if (relationshipInfo.includes) {
|
|
32
|
+
includes.push(...relationshipInfo.includes);
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
// This is a direct field
|
|
36
|
+
const fieldName = `${locator.kt}Id`;
|
|
37
|
+
where[fieldName] = locator.lk;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const result = {
|
|
41
|
+
where
|
|
42
|
+
};
|
|
43
|
+
if (includes.length > 0) {
|
|
44
|
+
result.include = includes;
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
const getGetOperation = (models, definition, registry)=>{
|
|
49
|
+
const { coordinate, options: { references, aggregations } } = definition;
|
|
50
|
+
const { kta } = coordinate;
|
|
51
|
+
const get = async (key)=>{
|
|
52
|
+
logger.default('Get', {
|
|
53
|
+
key
|
|
54
|
+
});
|
|
55
|
+
if (!core.isValidItemKey(key)) {
|
|
56
|
+
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
57
|
+
throw new Error('Key for Get is not a valid ItemKey');
|
|
58
|
+
}
|
|
59
|
+
const itemKey = key;
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
const model = models[0];
|
|
62
|
+
let item;
|
|
63
|
+
if (core.isPriKey(itemKey)) {
|
|
64
|
+
// This is the easy case because we can just find the item by its primary key
|
|
65
|
+
item = await model.findByPk(itemKey.pk);
|
|
66
|
+
} else if (core.isComKey(itemKey)) {
|
|
67
|
+
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
68
|
+
const comKey = itemKey;
|
|
69
|
+
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
70
|
+
logger.default('Composite key query', {
|
|
71
|
+
queryOptions
|
|
72
|
+
});
|
|
73
|
+
item = await model.findOne(queryOptions);
|
|
74
|
+
}
|
|
75
|
+
if (!item) {
|
|
76
|
+
throw new Library.NotFoundError('get', coordinate, key);
|
|
77
|
+
} else {
|
|
78
|
+
return core.validateKeys(await RowProcessor.processRow(item, kta, references, aggregations, registry), kta);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
return get;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
exports.getGetOperation = getGetOperation;
|
|
85
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2V0LmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const logger$1 = require('../logger.cjs');
|
|
6
|
+
const all = require('./all.cjs');
|
|
7
|
+
|
|
8
|
+
const logger = logger$1.default.get('sequelize', 'ops', 'one');
|
|
9
|
+
const getOneOperation = (models, definition, registry)=>{
|
|
10
|
+
const one = async (itemQuery, locations = [])=>{
|
|
11
|
+
logger.default('One', {
|
|
12
|
+
itemQuery,
|
|
13
|
+
locations
|
|
14
|
+
});
|
|
15
|
+
const items = await all.getAllOperation(models, definition, registry)(itemQuery, locations);
|
|
16
|
+
if (items.length > 0) {
|
|
17
|
+
return items[0];
|
|
18
|
+
} else {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return one;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
exports.getOneOperation = getOneOperation;
|
|
26
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib25lLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const EventCoordinator = require('../EventCoordinator.cjs');
|
|
7
|
+
const KeyMaster = require('../KeyMaster.cjs');
|
|
8
|
+
const logger$1 = require('../logger.cjs');
|
|
9
|
+
const relationshipUtils = require('../util/relationshipUtils.cjs');
|
|
10
|
+
|
|
11
|
+
const logger = logger$1.default.get('sequelize', 'ops', 'remove');
|
|
12
|
+
// Helper function to process composite key and build query options
|
|
13
|
+
const processCompositeKey = (comKey, model, kta)=>{
|
|
14
|
+
const where = {
|
|
15
|
+
id: comKey.pk
|
|
16
|
+
};
|
|
17
|
+
const includes = [];
|
|
18
|
+
for (const locator of comKey.loc){
|
|
19
|
+
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locator.kt, kta);
|
|
20
|
+
if (!relationshipInfo.found) {
|
|
21
|
+
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
22
|
+
logger.error(errorMessage, {
|
|
23
|
+
key: comKey,
|
|
24
|
+
kta
|
|
25
|
+
});
|
|
26
|
+
throw new Error(errorMessage);
|
|
27
|
+
}
|
|
28
|
+
if (relationshipInfo.path) {
|
|
29
|
+
// This requires a relationship traversal
|
|
30
|
+
where[relationshipInfo.path] = locator.lk;
|
|
31
|
+
if (relationshipInfo.includes) {
|
|
32
|
+
includes.push(...relationshipInfo.includes);
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
// This is a direct field
|
|
36
|
+
const fieldName = `${locator.kt}Id`;
|
|
37
|
+
where[fieldName] = locator.lk;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const result = {
|
|
41
|
+
where
|
|
42
|
+
};
|
|
43
|
+
if (includes.length > 0) {
|
|
44
|
+
result.include = includes;
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
const getRemoveOperation = (models, definition, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
|
+
registry)=>{
|
|
50
|
+
const { coordinate, options } = definition;
|
|
51
|
+
const { kta } = coordinate;
|
|
52
|
+
const remove = async (key)=>{
|
|
53
|
+
logger.default('Remove', {
|
|
54
|
+
key
|
|
55
|
+
});
|
|
56
|
+
if (!core.isValidItemKey(key)) {
|
|
57
|
+
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
58
|
+
throw new Error('Key for Remove is not a valid ItemKey');
|
|
59
|
+
}
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
const model = models[0];
|
|
62
|
+
let item;
|
|
63
|
+
let returnItem;
|
|
64
|
+
logger.debug('remove: %s', core.abbrevIK(key));
|
|
65
|
+
if (core.isPriKey(key)) {
|
|
66
|
+
item = await model.findByPk(key.pk);
|
|
67
|
+
} else if (core.isComKey(key)) {
|
|
68
|
+
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
69
|
+
const comKey = key;
|
|
70
|
+
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
71
|
+
logger.default('Composite key query', {
|
|
72
|
+
queryOptions
|
|
73
|
+
});
|
|
74
|
+
item = await model.findOne(queryOptions);
|
|
75
|
+
}
|
|
76
|
+
if (!item) {
|
|
77
|
+
throw new Error(`Item not found for removal with key: ${core.abbrevIK(key)}`);
|
|
78
|
+
}
|
|
79
|
+
const isDeletedAttribute = model.getAttributes().isDeleted;
|
|
80
|
+
const deletedAtAttribute = model.getAttributes().deletedAt;
|
|
81
|
+
if (isDeletedAttribute || deletedAtAttribute) {
|
|
82
|
+
if (model.getAttributes().isDeleted) {
|
|
83
|
+
item.isDeleted = true;
|
|
84
|
+
}
|
|
85
|
+
if (model.getAttributes().deletedAt) {
|
|
86
|
+
item.deletedAt = new Date();
|
|
87
|
+
}
|
|
88
|
+
// Save the object
|
|
89
|
+
await (item === null || item === void 0 ? void 0 : item.save());
|
|
90
|
+
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
91
|
+
plain: true
|
|
92
|
+
});
|
|
93
|
+
returnItem = KeyMaster.addKey(item, returnItem, kta);
|
|
94
|
+
returnItem = EventCoordinator.populateEvents(returnItem);
|
|
95
|
+
} else if (options.deleteOnRemove) {
|
|
96
|
+
await (item === null || item === void 0 ? void 0 : item.destroy());
|
|
97
|
+
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
98
|
+
plain: true
|
|
99
|
+
});
|
|
100
|
+
returnItem = KeyMaster.addKey(item, returnItem, kta);
|
|
101
|
+
returnItem = EventCoordinator.populateEvents(returnItem);
|
|
102
|
+
} else {
|
|
103
|
+
throw new Error('No deletedAt or isDeleted attribute found in model, and deleteOnRemove is not set');
|
|
104
|
+
}
|
|
105
|
+
return returnItem;
|
|
106
|
+
};
|
|
107
|
+
return remove;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
exports.getRemoveOperation = getRemoveOperation;
|
|
111
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const EventCoordinator = require('../EventCoordinator.cjs');
|
|
7
|
+
const KeyMaster = require('../KeyMaster.cjs');
|
|
8
|
+
const logger$1 = require('../logger.cjs');
|
|
9
|
+
const RowProcessor = require('../RowProcessor.cjs');
|
|
10
|
+
const general = require('../util/general.cjs');
|
|
11
|
+
const Library = require('@fjell/lib');
|
|
12
|
+
|
|
13
|
+
const logger = logger$1.default.get('sequelize', 'ops', 'update');
|
|
14
|
+
const getUpdateOperation = (models, definition, registry)=>{
|
|
15
|
+
const { options: { references, aggregations } } = definition;
|
|
16
|
+
const update = async (key, item)=>{
|
|
17
|
+
const { coordinate } = definition;
|
|
18
|
+
const { kta } = coordinate;
|
|
19
|
+
logger.debug('update: %s, %j', core.abbrevIK(key), item);
|
|
20
|
+
// Find the object we're updating
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
const model = models[0];
|
|
23
|
+
let response;
|
|
24
|
+
if (core.isPriKey(key)) {
|
|
25
|
+
// Find the model by using the PK
|
|
26
|
+
const priKey = key;
|
|
27
|
+
response = await model.findByPk(priKey.pk);
|
|
28
|
+
} else if (core.isComKey(key)) {
|
|
29
|
+
var _comKey_loc_, _comKey_loc_1;
|
|
30
|
+
const comKey = key;
|
|
31
|
+
// Find the model by using both of the identifiers.
|
|
32
|
+
response = await model.findOne({
|
|
33
|
+
where: {
|
|
34
|
+
[(comKey === null || comKey === void 0 ? void 0 : (_comKey_loc_ = comKey.loc[0]) === null || _comKey_loc_ === void 0 ? void 0 : _comKey_loc_.kt) + 'Id']: comKey === null || comKey === void 0 ? void 0 : (_comKey_loc_1 = comKey.loc[0]) === null || _comKey_loc_1 === void 0 ? void 0 : _comKey_loc_1.lk,
|
|
35
|
+
id: comKey === null || comKey === void 0 ? void 0 : comKey.pk
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (response) {
|
|
40
|
+
// Remove the key and events
|
|
41
|
+
let updateProps = KeyMaster.removeKey(item);
|
|
42
|
+
// TODO: We need the opposite of processRow, something to step down from fjell to database.
|
|
43
|
+
updateProps = EventCoordinator.extractEvents(updateProps);
|
|
44
|
+
updateProps = EventCoordinator.removeEvents(updateProps);
|
|
45
|
+
logger.default('Response: %s', general.stringifyJSON(response));
|
|
46
|
+
logger.default('Update Properties: %s', general.stringifyJSON(updateProps));
|
|
47
|
+
// Update the object
|
|
48
|
+
response = await response.update(updateProps);
|
|
49
|
+
// Populate the key and events
|
|
50
|
+
const processedItem = await RowProcessor.processRow(response, kta, references, aggregations, registry);
|
|
51
|
+
const returnItem = core.validateKeys(processedItem, kta);
|
|
52
|
+
return returnItem;
|
|
53
|
+
} else {
|
|
54
|
+
throw new Library.NotFoundError('update', coordinate, key);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
return update;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exports.getUpdateOperation = getUpdateOperation;
|
|
61
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBkYXRlLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const Library = require('@fjell/lib');
|
|
6
|
+
const Definition = require('../Definition.cjs');
|
|
7
|
+
const Operations = require('../Operations.cjs');
|
|
8
|
+
const logger$1 = require('../logger.cjs');
|
|
9
|
+
|
|
10
|
+
const logger = logger$1.default.get('lib-sequelize', 'primary', 'instance');
|
|
11
|
+
function createInstance(keyType, models, libOptions = {}, scopes = [], registry) {
|
|
12
|
+
logger.debug('createInstance', {
|
|
13
|
+
keyType,
|
|
14
|
+
models,
|
|
15
|
+
libOptions,
|
|
16
|
+
scopes
|
|
17
|
+
});
|
|
18
|
+
const definition = Definition.createDefinition([
|
|
19
|
+
keyType
|
|
20
|
+
], scopes, libOptions);
|
|
21
|
+
const operations = Operations.createOperations(models, definition, registry);
|
|
22
|
+
return {
|
|
23
|
+
definition,
|
|
24
|
+
operations: Library.Primary.wrapOperations(operations, definition, registry),
|
|
25
|
+
models,
|
|
26
|
+
registry
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.createInstance = createInstance;
|
|
31
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSW5zdGFuY2UuY2pzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const Instance = require('./Instance.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.createInstance = Instance.createInstance;
|
|
10
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguY2pzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable no-undefined */ const clean = (obj)=>{
|
|
6
|
+
return Object.fromEntries(Object.entries(obj).filter(([_, v])=>v !== undefined));
|
|
7
|
+
};
|
|
8
|
+
//Recursive implementation of jSON.stringify;
|
|
9
|
+
const stringifyJSON = function(obj, visited = new Set()) {
|
|
10
|
+
const arrOfKeyVals = [];
|
|
11
|
+
const arrVals = [];
|
|
12
|
+
let objKeys = [];
|
|
13
|
+
/*********CHECK FOR PRIMITIVE TYPES**********/ if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) return '' + obj;
|
|
14
|
+
else if (typeof obj === 'string') return '"' + obj + '"';
|
|
15
|
+
/*********DETECT CIRCULAR REFERENCES**********/ if (obj instanceof Object && visited.has(obj)) {
|
|
16
|
+
return '"(circular)"';
|
|
17
|
+
} else if (Array.isArray(obj)) {
|
|
18
|
+
//check for empty array
|
|
19
|
+
if (obj[0] === undefined) return '[]';
|
|
20
|
+
else {
|
|
21
|
+
// Add array to visited before processing its elements
|
|
22
|
+
visited.add(obj);
|
|
23
|
+
obj.forEach(function(el) {
|
|
24
|
+
arrVals.push(stringifyJSON(el, visited));
|
|
25
|
+
});
|
|
26
|
+
return '[' + arrVals + ']';
|
|
27
|
+
}
|
|
28
|
+
} else if (obj instanceof Object) {
|
|
29
|
+
// Add object to visited before processing its properties
|
|
30
|
+
visited.add(obj);
|
|
31
|
+
//get object keys
|
|
32
|
+
objKeys = Object.keys(obj);
|
|
33
|
+
//set key output;
|
|
34
|
+
objKeys.forEach(function(key) {
|
|
35
|
+
const keyOut = '"' + key + '":';
|
|
36
|
+
const keyValOut = obj[key];
|
|
37
|
+
//skip functions and undefined properties
|
|
38
|
+
if (keyValOut instanceof Function || keyValOut === undefined) return; // Skip this entry entirely instead of pushing an empty string
|
|
39
|
+
else if (typeof keyValOut === 'string') arrOfKeyVals.push(keyOut + '"' + keyValOut + '"');
|
|
40
|
+
else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null) arrOfKeyVals.push(keyOut + keyValOut);
|
|
41
|
+
else if (keyValOut instanceof Object) {
|
|
42
|
+
arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut, visited));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return '{' + arrOfKeyVals + '}';
|
|
46
|
+
}
|
|
47
|
+
return '';
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
exports.clean = clean;
|
|
51
|
+
exports.stringifyJSON = stringifyJSON;
|
|
52
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhbC5janMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
/* eslint-disable indent */ /**
|
|
6
|
+
* Helper function to build relationship chain includes
|
|
7
|
+
*/ const buildRelationshipChain = (targetModel, kta, currentIndex, targetIndex)=>{
|
|
8
|
+
// Build the association path and validate relationships exist
|
|
9
|
+
const associationParts = [];
|
|
10
|
+
const modelChain = [
|
|
11
|
+
targetModel
|
|
12
|
+
];
|
|
13
|
+
let currentModel = targetModel;
|
|
14
|
+
// Validate that all associations exist and build model chain
|
|
15
|
+
for(let i = currentIndex + 1; i <= targetIndex; i++){
|
|
16
|
+
const intermediateType = kta[i];
|
|
17
|
+
const associationName = intermediateType;
|
|
18
|
+
if (!currentModel.associations || !currentModel.associations[associationName]) {
|
|
19
|
+
return {
|
|
20
|
+
success: false
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
associationParts.push(associationName);
|
|
24
|
+
currentModel = currentModel.associations[associationName].target;
|
|
25
|
+
modelChain.push(currentModel);
|
|
26
|
+
}
|
|
27
|
+
// Build the full association path for the target field
|
|
28
|
+
const targetType = kta[targetIndex];
|
|
29
|
+
const associationPath = `$${associationParts.join('.')}.${targetType}Id$`;
|
|
30
|
+
// Build nested includes structure iteratively (clearer than recursion)
|
|
31
|
+
let deepestInclude = null;
|
|
32
|
+
// Build from the deepest level back to the root
|
|
33
|
+
for(let i = targetIndex; i > currentIndex; i--){
|
|
34
|
+
const currentType = kta[i];
|
|
35
|
+
const modelIndex = i - currentIndex;
|
|
36
|
+
const includeObj = {
|
|
37
|
+
model: modelChain[modelIndex],
|
|
38
|
+
as: currentType,
|
|
39
|
+
required: true
|
|
40
|
+
};
|
|
41
|
+
if (deepestInclude) {
|
|
42
|
+
includeObj.include = [
|
|
43
|
+
deepestInclude
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
deepestInclude = includeObj;
|
|
47
|
+
}
|
|
48
|
+
const includes = deepestInclude ? [
|
|
49
|
+
deepestInclude
|
|
50
|
+
] : [];
|
|
51
|
+
return {
|
|
52
|
+
success: true,
|
|
53
|
+
path: associationPath,
|
|
54
|
+
includes
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Helper function to build relationship path for a locator
|
|
59
|
+
* @param includeIsDirect Whether to include the isDirect flag in the result
|
|
60
|
+
*/ const buildRelationshipPath = (targetModel, locatorType, kta, includeIsDirect = false)=>{
|
|
61
|
+
// First check if the field exists directly
|
|
62
|
+
const directFieldName = `${locatorType}Id`;
|
|
63
|
+
const attributes = targetModel.getAttributes();
|
|
64
|
+
if (attributes && attributes[directFieldName]) {
|
|
65
|
+
const result = {
|
|
66
|
+
found: true
|
|
67
|
+
};
|
|
68
|
+
if (includeIsDirect) {
|
|
69
|
+
result.isDirect = true;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
// If not direct, look for relationship path
|
|
74
|
+
const targetIndex = kta.indexOf(locatorType);
|
|
75
|
+
if (targetIndex === -1) {
|
|
76
|
+
const result = {
|
|
77
|
+
found: false
|
|
78
|
+
};
|
|
79
|
+
if (includeIsDirect) {
|
|
80
|
+
result.isDirect = false;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
const currentIndex = 0; // We're always looking from the base model
|
|
85
|
+
if (targetIndex <= currentIndex) {
|
|
86
|
+
const result = {
|
|
87
|
+
found: false
|
|
88
|
+
};
|
|
89
|
+
if (includeIsDirect) {
|
|
90
|
+
result.isDirect = false;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
const chainResult = buildRelationshipChain(targetModel, kta, currentIndex, targetIndex);
|
|
95
|
+
if (chainResult.success) {
|
|
96
|
+
const result = {
|
|
97
|
+
found: true,
|
|
98
|
+
path: chainResult.path,
|
|
99
|
+
includes: chainResult.includes
|
|
100
|
+
};
|
|
101
|
+
if (includeIsDirect) {
|
|
102
|
+
result.isDirect = false;
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
const result = {
|
|
107
|
+
found: false
|
|
108
|
+
};
|
|
109
|
+
if (includeIsDirect) {
|
|
110
|
+
result.isDirect = false;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
exports.buildRelationshipChain = buildRelationshipChain;
|
|
116
|
+
exports.buildRelationshipPath = buildRelationshipPath;
|
|
117
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVsYXRpb25zaGlwVXRpbHMuY2pzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ikToLKA } from '@fjell/core';
|
|
2
|
+
|
|
3
|
+
const buildAggregation = async (item, aggregationDefinition, registry)=>{
|
|
4
|
+
const location = ikToLKA(item.key);
|
|
5
|
+
// Get the library instance from the registry using the key type array
|
|
6
|
+
const libraryInstance = registry.get(aggregationDefinition.kta);
|
|
7
|
+
if (!libraryInstance) {
|
|
8
|
+
throw new Error(`Library instance not found for key type array: ${aggregationDefinition.kta.join(', ')}`);
|
|
9
|
+
}
|
|
10
|
+
// Based on cardinality, use either one or all operation
|
|
11
|
+
if (aggregationDefinition.cardinality === 'one') {
|
|
12
|
+
// For one-to-one relationship, use the one operation
|
|
13
|
+
return libraryInstance.operations.one({}, location).then((result)=>{
|
|
14
|
+
item[aggregationDefinition.property] = result;
|
|
15
|
+
return item;
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
// For one-to-many relationship, use the all operation
|
|
19
|
+
return libraryInstance.operations.all({}, location).then((results)=>{
|
|
20
|
+
item[aggregationDefinition.property] = results;
|
|
21
|
+
return item;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { buildAggregation };
|
|
27
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQWdncmVnYXRpb25CdWlsZGVyLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import 'deepmerge';
|
|
2
|
+
import LibLogger from './logger.js';
|
|
3
|
+
|
|
4
|
+
const logger = LibLogger.get("sequelize", "EventCoordinator");
|
|
5
|
+
//#endregion
|
|
6
|
+
const populateEvents = (item)=>{
|
|
7
|
+
const events = {
|
|
8
|
+
created: {
|
|
9
|
+
at: item.createdAt || null
|
|
10
|
+
},
|
|
11
|
+
updated: {
|
|
12
|
+
at: item.updatedAt || null
|
|
13
|
+
},
|
|
14
|
+
deleted: {
|
|
15
|
+
at: null
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
item.events = events;
|
|
19
|
+
return item;
|
|
20
|
+
};
|
|
21
|
+
const extractEvents = (item)=>{
|
|
22
|
+
logger.default('Extracting Events to database fields', {
|
|
23
|
+
item
|
|
24
|
+
});
|
|
25
|
+
if (item.events) {
|
|
26
|
+
var _item_events_created, _item_events_updated, _item_events_deleted;
|
|
27
|
+
if ((_item_events_created = item.events.created) === null || _item_events_created === void 0 ? void 0 : _item_events_created.at) {
|
|
28
|
+
item.createdAt = item.events.created.at;
|
|
29
|
+
}
|
|
30
|
+
if ((_item_events_updated = item.events.updated) === null || _item_events_updated === void 0 ? void 0 : _item_events_updated.at) {
|
|
31
|
+
item.updatedAt = item.events.updated.at;
|
|
32
|
+
}
|
|
33
|
+
if ((_item_events_deleted = item.events.deleted) === null || _item_events_deleted === void 0 ? void 0 : _item_events_deleted.at) {
|
|
34
|
+
item.deletedAt = item.events.deleted.at;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return item;
|
|
38
|
+
};
|
|
39
|
+
const removeEvents = (item)=>{
|
|
40
|
+
logger.default('Removing Events', {
|
|
41
|
+
item
|
|
42
|
+
});
|
|
43
|
+
delete item.events;
|
|
44
|
+
return item;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { extractEvents, populateEvents, removeEvents };
|
|
48
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRXZlbnRDb29yZGluYXRvci5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -2,15 +2,16 @@ import * as Library from '@fjell/lib';
|
|
|
2
2
|
import { createDefinition } from './Definition.js';
|
|
3
3
|
import { createOperations } from './Operations.js';
|
|
4
4
|
|
|
5
|
-
function createInstance(keyTypes, models, libOptions = {}, scopes = []) {
|
|
5
|
+
function createInstance(keyTypes, models, libOptions = {}, scopes = [], registry) {
|
|
6
6
|
const definition = createDefinition(keyTypes, scopes, libOptions);
|
|
7
|
-
const operations = createOperations(models, definition);
|
|
7
|
+
const operations = createOperations(models, definition, registry);
|
|
8
8
|
return {
|
|
9
9
|
definition,
|
|
10
|
-
operations: Library.wrapOperations(operations, definition),
|
|
11
|
-
models
|
|
10
|
+
operations: Library.wrapOperations(operations, definition, registry),
|
|
11
|
+
models,
|
|
12
|
+
registry
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export { createInstance };
|
|
16
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
17
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSW5zdGFuY2UuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|