@fjell/lib-sequelize 4.4.15 → 4.4.17
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/{types/Operations.d.ts → Operations.d.ts} +5 -5
- package/dist/{types/SequelizeLibraryFactory.d.ts → SequelizeLibraryFactory.d.ts} +4 -4
- package/dist/{types/contained → contained}/SequelizeLibrary.d.ts +4 -4
- package/dist/index.js +1488 -0
- package/dist/index.js.map +7 -0
- package/dist/logger.d.ts +2 -0
- package/dist/{types/ops → ops}/all.d.ts +3 -3
- package/dist/{types/ops → ops}/create.d.ts +3 -3
- package/dist/{types/ops → ops}/find.d.ts +3 -3
- package/dist/{types/ops → ops}/get.d.ts +1 -1
- package/dist/{types/ops → ops}/one.d.ts +3 -3
- package/dist/{types/ops → ops}/remove.d.ts +4 -3
- package/dist/{types/ops → ops}/update.d.ts +3 -3
- package/dist/{types/primary → primary}/SequelizeLibrary.d.ts +4 -4
- package/package.json +17 -20
- package/dist/cjs/AggregationBuilder.cjs +0 -65
- package/dist/cjs/Coordinate.cjs +0 -24
- package/dist/cjs/Definition.cjs +0 -25
- package/dist/cjs/EventCoordinator.cjs +0 -54
- package/dist/cjs/KeyMaster.cjs +0 -151
- package/dist/cjs/OperationContext.cjs +0 -161
- package/dist/cjs/Operations.cjs +0 -34
- package/dist/cjs/Options.cjs +0 -46
- package/dist/cjs/QueryBuilder.cjs +0 -296
- package/dist/cjs/ReferenceBuilder.cjs +0 -76
- package/dist/cjs/RowProcessor.cjs +0 -56
- package/dist/cjs/SequelizeLibrary.cjs +0 -56
- package/dist/cjs/SequelizeLibraryFactory.cjs +0 -25
- package/dist/cjs/contained/SequelizeLibrary.cjs +0 -31
- package/dist/cjs/contained/index.cjs +0 -11
- package/dist/cjs/index.cjs +0 -26
- package/dist/cjs/logger.cjs +0 -10
- package/dist/cjs/ops/all.cjs +0 -145
- package/dist/cjs/ops/create.cjs +0 -252
- package/dist/cjs/ops/find.cjs +0 -47
- package/dist/cjs/ops/get.cjs +0 -92
- package/dist/cjs/ops/one.cjs +0 -27
- package/dist/cjs/ops/remove.cjs +0 -114
- package/dist/cjs/ops/update.cjs +0 -120
- package/dist/cjs/primary/SequelizeLibrary.cjs +0 -41
- package/dist/cjs/primary/index.cjs +0 -11
- package/dist/cjs/util/general.cjs +0 -48
- package/dist/cjs/util/relationshipUtils.cjs +0 -117
- package/dist/es/AggregationBuilder.js +0 -61
- package/dist/es/Coordinate.js +0 -19
- package/dist/es/Definition.js +0 -21
- package/dist/es/EventCoordinator.js +0 -48
- package/dist/es/KeyMaster.js +0 -146
- package/dist/es/OperationContext.js +0 -155
- package/dist/es/Operations.js +0 -30
- package/dist/es/Options.js +0 -23
- package/dist/es/QueryBuilder.js +0 -290
- package/dist/es/ReferenceBuilder.js +0 -72
- package/dist/es/RowProcessor.js +0 -52
- package/dist/es/SequelizeLibrary.js +0 -32
- package/dist/es/SequelizeLibraryFactory.js +0 -21
- package/dist/es/contained/SequelizeLibrary.js +0 -26
- package/dist/es/contained/index.js +0 -2
- package/dist/es/index.js +0 -11
- package/dist/es/logger.js +0 -6
- package/dist/es/ops/all.js +0 -141
- package/dist/es/ops/create.js +0 -248
- package/dist/es/ops/find.js +0 -43
- package/dist/es/ops/get.js +0 -88
- package/dist/es/ops/one.js +0 -23
- package/dist/es/ops/remove.js +0 -110
- package/dist/es/ops/update.js +0 -116
- package/dist/es/primary/SequelizeLibrary.js +0 -36
- package/dist/es/primary/index.js +0 -2
- package/dist/es/util/general.js +0 -44
- package/dist/es/util/relationshipUtils.js +0 -112
- package/dist/index.cjs +0 -1853
- package/dist/index.cjs.map +0 -1
- package/dist/types/AggregationBuilder.d.ts +0 -5
- package/dist/types/EventCoordinator.d.ts +0 -6
- package/dist/types/KeyMaster.d.ts +0 -4
- package/dist/types/OperationContext.d.ts +0 -72
- package/dist/types/QueryBuilder.d.ts +0 -12
- package/dist/types/ReferenceBuilder.d.ts +0 -4
- package/dist/types/RowProcessor.d.ts +0 -6
- package/dist/types/logger.d.ts +0 -2
- package/dist/types/util/general.d.ts +0 -4
- package/dist/types/util/relationshipUtils.d.ts +0 -21
- package/dist/{types/Coordinate.d.ts → Coordinate.d.ts} +0 -0
- package/dist/{types/Definition.d.ts → Definition.d.ts} +0 -0
- package/dist/{types/Options.d.ts → Options.d.ts} +1 -1
- package/dist/{types/Registry.d.ts → Registry.d.ts} +0 -0
- package/dist/{types/SequelizeLibrary.d.ts → SequelizeLibrary.d.ts} +1 -1
- /package/dist/{types/contained → contained}/index.d.ts +0 -0
- /package/dist/{types/index.d.ts → index.d.ts} +0 -0
- /package/dist/{types/primary → primary}/index.d.ts +0 -0
package/dist/es/ops/find.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { validateKeys } from '@fjell/core';
|
|
2
|
-
import LibLogger from '../logger.js';
|
|
3
|
-
import { processRow } from '../RowProcessor.js';
|
|
4
|
-
import { stringifyJSON } from '../util/general.js';
|
|
5
|
-
|
|
6
|
-
const logger = LibLogger.get('sequelize', 'ops', 'find');
|
|
7
|
-
const getFindOperation = (models, definition, registry)=>{
|
|
8
|
-
const { options: { finders, references, aggregations } } = definition;
|
|
9
|
-
const find = async (finder, finderParams, locations)=>{
|
|
10
|
-
logger.debug(`FIND operation called on ${models[0].name} with finder '${finder}' and ${(locations === null || locations === void 0 ? void 0 : locations.length) || 0} location filters: ${(locations === null || locations === void 0 ? void 0 : locations.map((loc)=>`${loc.kt}=${loc.lk}`).join(', ')) || 'none'}`);
|
|
11
|
-
logger.default(`Find configured for ${models[0].name} using finder '${finder}' with ${Object.keys(finderParams).length} params`);
|
|
12
|
-
// Note that we execute the createFinders function here because we want to make sure we're always getting the
|
|
13
|
-
// most up to date methods.
|
|
14
|
-
if (finders && finders[finder]) {
|
|
15
|
-
const finderMethod = finders[finder];
|
|
16
|
-
if (finderMethod) {
|
|
17
|
-
logger.trace(`[FIND] Executing finder '${finder}' on ${models[0].name} with params: ${stringifyJSON(finderParams)}, locations: ${stringifyJSON(locations)}`);
|
|
18
|
-
const results = await finderMethod(finderParams, locations);
|
|
19
|
-
if (results && results.length > 0) {
|
|
20
|
-
const processedResults = await Promise.all(results.map(async (row)=>{
|
|
21
|
-
const processedRow = await processRow(row, definition.coordinate.kta, references, aggregations, registry);
|
|
22
|
-
return validateKeys(processedRow, definition.coordinate.kta);
|
|
23
|
-
}));
|
|
24
|
-
logger.debug(`[FIND] Found ${processedResults.length} ${models[0].name} records using finder '${finder}'`);
|
|
25
|
-
return processedResults;
|
|
26
|
-
} else {
|
|
27
|
-
logger.debug(`[FIND] Found 0 ${models[0].name} records using finder '${finder}'`);
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
logger.error(`Finder %s not found`, finder);
|
|
32
|
-
throw new Error(`Finder ${finder} not found`);
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
logger.error(`No finders have been defined for this lib`);
|
|
36
|
-
throw new Error(`No finders found`);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
return find;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export { getFindOperation };
|
|
43
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmluZC5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
package/dist/es/ops/get.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { isValidItemKey, isPriKey, isComKey, validateKeys } from '@fjell/core';
|
|
2
|
-
import LibLogger from '../logger.js';
|
|
3
|
-
import { processRow } from '../RowProcessor.js';
|
|
4
|
-
import { NotFoundError } from '@fjell/lib';
|
|
5
|
-
import { buildRelationshipPath } from '../util/relationshipUtils.js';
|
|
6
|
-
import { contextManager } from '../OperationContext.js';
|
|
7
|
-
import { stringifyJSON } from '../util/general.js';
|
|
8
|
-
|
|
9
|
-
const logger = LibLogger.get('sequelize', 'ops', 'get');
|
|
10
|
-
// Helper function to process composite key and build query options
|
|
11
|
-
const processCompositeKey = (comKey, model, kta)=>{
|
|
12
|
-
const where = {
|
|
13
|
-
id: comKey.pk
|
|
14
|
-
};
|
|
15
|
-
const includes = [];
|
|
16
|
-
for (const locator of comKey.loc){
|
|
17
|
-
const relationshipInfo = buildRelationshipPath(model, locator.kt, kta);
|
|
18
|
-
if (!relationshipInfo.found) {
|
|
19
|
-
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
20
|
-
logger.error(errorMessage, {
|
|
21
|
-
key: comKey,
|
|
22
|
-
kta
|
|
23
|
-
});
|
|
24
|
-
throw new Error(errorMessage);
|
|
25
|
-
}
|
|
26
|
-
if (relationshipInfo.path) {
|
|
27
|
-
// This requires a relationship traversal
|
|
28
|
-
where[relationshipInfo.path] = locator.lk;
|
|
29
|
-
if (relationshipInfo.includes) {
|
|
30
|
-
includes.push(...relationshipInfo.includes);
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
// This is a direct field
|
|
34
|
-
const fieldName = `${locator.kt}Id`;
|
|
35
|
-
where[fieldName] = locator.lk;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const result = {
|
|
39
|
-
where
|
|
40
|
-
};
|
|
41
|
-
if (includes.length > 0) {
|
|
42
|
-
result.include = includes;
|
|
43
|
-
}
|
|
44
|
-
return result;
|
|
45
|
-
};
|
|
46
|
-
const getGetOperation = (models, definition, registry)=>{
|
|
47
|
-
const { coordinate, options: { references, aggregations } } = definition;
|
|
48
|
-
const { kta } = coordinate;
|
|
49
|
-
const get = async (key)=>{
|
|
50
|
-
if (!isValidItemKey(key)) {
|
|
51
|
-
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
52
|
-
throw new Error('Key for Get is not a valid ItemKey');
|
|
53
|
-
}
|
|
54
|
-
logger.debug(`GET operation called on ${models[0].name} with ${isPriKey(key) ? `primary key: pk=${key.pk}` : `composite key: pk=${key.pk}, loc=[${key.loc.map((l)=>`${l.kt}=${l.lk}`).join(', ')}]`}`);
|
|
55
|
-
logger.default(`Get configured for ${models[0].name} with ${isPriKey(key) ? 'primary' : 'composite'} key`);
|
|
56
|
-
const itemKey = key;
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
const model = models[0];
|
|
59
|
-
let item;
|
|
60
|
-
if (isPriKey(itemKey)) {
|
|
61
|
-
// This is the easy case because we can just find the item by its primary key
|
|
62
|
-
logger.trace(`[GET] Executing ${model.name}.findByPk() with pk: ${itemKey.pk}`);
|
|
63
|
-
item = await model.findByPk(itemKey.pk);
|
|
64
|
-
} else if (isComKey(itemKey)) {
|
|
65
|
-
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
66
|
-
const comKey = itemKey;
|
|
67
|
-
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
68
|
-
logger.default('Composite key query', {
|
|
69
|
-
queryOptions
|
|
70
|
-
});
|
|
71
|
-
logger.trace(`[GET] Executing ${model.name}.findOne() with options: ${stringifyJSON(queryOptions)}`);
|
|
72
|
-
item = await model.findOne(queryOptions);
|
|
73
|
-
}
|
|
74
|
-
if (!item) {
|
|
75
|
-
throw new NotFoundError('get', coordinate, key);
|
|
76
|
-
} else {
|
|
77
|
-
// Get the current context from context manager
|
|
78
|
-
const context = contextManager.getCurrentContext();
|
|
79
|
-
const result = validateKeys(await processRow(item, kta, references, aggregations, registry, context), kta);
|
|
80
|
-
logger.debug(`[GET] Retrieved ${model.name} with key: ${result.key ? JSON.stringify(result.key) : `id=${item.id}`}`);
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
return get;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export { getGetOperation };
|
|
88
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2V0LmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
package/dist/es/ops/one.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import LibLogger from '../logger.js';
|
|
2
|
-
import { getAllOperation } from './all.js';
|
|
3
|
-
|
|
4
|
-
const logger = LibLogger.get('sequelize', 'ops', 'one');
|
|
5
|
-
const getOneOperation = (models, definition, registry)=>{
|
|
6
|
-
const one = async (itemQuery, locations = [])=>{
|
|
7
|
-
logger.debug(`ONE operation called on ${models[0].name} with ${locations.length} location filters: ${locations.map((loc)=>`${loc.kt}=${loc.lk}`).join(', ') || 'none'}`);
|
|
8
|
-
logger.default(`One configured for ${models[0].name} delegating to all operation`);
|
|
9
|
-
const items = await getAllOperation(models, definition, registry)(itemQuery, locations);
|
|
10
|
-
if (items.length > 0) {
|
|
11
|
-
const result = items[0];
|
|
12
|
-
logger.debug(`[ONE] Found ${models[0].name} record with key: ${result.key ? JSON.stringify(result.key) : 'unknown'}`);
|
|
13
|
-
return result;
|
|
14
|
-
} else {
|
|
15
|
-
logger.debug(`[ONE] No ${models[0].name} record found`);
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
return one;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export { getOneOperation };
|
|
23
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib25lLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
package/dist/es/ops/remove.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { isValidItemKey, isPriKey, abbrevIK, isComKey } from '@fjell/core';
|
|
2
|
-
import { populateEvents } from '../EventCoordinator.js';
|
|
3
|
-
import { addKey } from '../KeyMaster.js';
|
|
4
|
-
import LibLogger from '../logger.js';
|
|
5
|
-
import { buildRelationshipPath } from '../util/relationshipUtils.js';
|
|
6
|
-
import { stringifyJSON } from '../util/general.js';
|
|
7
|
-
|
|
8
|
-
const logger = LibLogger.get('sequelize', 'ops', 'remove');
|
|
9
|
-
// Helper function to process composite key and build query options
|
|
10
|
-
const processCompositeKey = (comKey, model, kta)=>{
|
|
11
|
-
const where = {
|
|
12
|
-
id: comKey.pk
|
|
13
|
-
};
|
|
14
|
-
const includes = [];
|
|
15
|
-
for (const locator of comKey.loc){
|
|
16
|
-
const relationshipInfo = buildRelationshipPath(model, locator.kt, kta);
|
|
17
|
-
if (!relationshipInfo.found) {
|
|
18
|
-
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
19
|
-
logger.error(errorMessage, {
|
|
20
|
-
key: comKey,
|
|
21
|
-
kta
|
|
22
|
-
});
|
|
23
|
-
throw new Error(errorMessage);
|
|
24
|
-
}
|
|
25
|
-
if (relationshipInfo.path) {
|
|
26
|
-
// This requires a relationship traversal
|
|
27
|
-
where[relationshipInfo.path] = locator.lk;
|
|
28
|
-
if (relationshipInfo.includes) {
|
|
29
|
-
includes.push(...relationshipInfo.includes);
|
|
30
|
-
}
|
|
31
|
-
} else {
|
|
32
|
-
// This is a direct field
|
|
33
|
-
const fieldName = `${locator.kt}Id`;
|
|
34
|
-
where[fieldName] = locator.lk;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const result = {
|
|
38
|
-
where
|
|
39
|
-
};
|
|
40
|
-
if (includes.length > 0) {
|
|
41
|
-
result.include = includes;
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
};
|
|
45
|
-
const getRemoveOperation = (models, definition, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
46
|
-
registry)=>{
|
|
47
|
-
const { coordinate, options } = definition;
|
|
48
|
-
const { kta } = coordinate;
|
|
49
|
-
const remove = async (key)=>{
|
|
50
|
-
if (!isValidItemKey(key)) {
|
|
51
|
-
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
52
|
-
throw new Error('Key for Remove is not a valid ItemKey');
|
|
53
|
-
}
|
|
54
|
-
logger.debug(`REMOVE operation called on ${models[0].name} with ${isPriKey(key) ? `primary key: pk=${key.pk}` : `composite key: pk=${key.pk}, loc=[${key.loc.map((l)=>`${l.kt}=${l.lk}`).join(', ')}]`}`);
|
|
55
|
-
logger.default(`Remove configured for ${models[0].name} with ${isPriKey(key) ? 'primary' : 'composite'} key`);
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
const model = models[0];
|
|
58
|
-
let item;
|
|
59
|
-
let returnItem;
|
|
60
|
-
logger.debug('remove: %s', abbrevIK(key));
|
|
61
|
-
if (isPriKey(key)) {
|
|
62
|
-
logger.debug(`[REMOVE] Executing ${model.name}.findByPk() with pk: ${key.pk}`);
|
|
63
|
-
item = await model.findByPk(key.pk);
|
|
64
|
-
} else if (isComKey(key)) {
|
|
65
|
-
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
66
|
-
const comKey = key;
|
|
67
|
-
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
68
|
-
logger.default(`Remove composite key query for ${model.name} with where fields: ${queryOptions.where ? Object.keys(queryOptions.where).join(', ') : 'none'}`);
|
|
69
|
-
logger.debug(`[REMOVE] Executing ${model.name}.findOne() with options: ${stringifyJSON(queryOptions)}`);
|
|
70
|
-
item = await model.findOne(queryOptions);
|
|
71
|
-
}
|
|
72
|
-
if (!item) {
|
|
73
|
-
throw new Error(`Item not found for removal with key: ${abbrevIK(key)}`);
|
|
74
|
-
}
|
|
75
|
-
const isDeletedAttribute = model.getAttributes().isDeleted;
|
|
76
|
-
const deletedAtAttribute = model.getAttributes().deletedAt;
|
|
77
|
-
if (isDeletedAttribute || deletedAtAttribute) {
|
|
78
|
-
if (model.getAttributes().isDeleted) {
|
|
79
|
-
item.isDeleted = true;
|
|
80
|
-
}
|
|
81
|
-
if (model.getAttributes().deletedAt) {
|
|
82
|
-
item.deletedAt = new Date();
|
|
83
|
-
}
|
|
84
|
-
// Save the object
|
|
85
|
-
logger.debug(`[REMOVE] Executing ${model.name}.save() for soft delete`);
|
|
86
|
-
await (item === null || item === void 0 ? void 0 : item.save());
|
|
87
|
-
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
88
|
-
plain: true
|
|
89
|
-
});
|
|
90
|
-
returnItem = addKey(item, returnItem, kta);
|
|
91
|
-
returnItem = populateEvents(returnItem);
|
|
92
|
-
} else if (options.deleteOnRemove) {
|
|
93
|
-
logger.debug(`[REMOVE] Executing ${model.name}.destroy() for hard delete`);
|
|
94
|
-
await (item === null || item === void 0 ? void 0 : item.destroy());
|
|
95
|
-
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
96
|
-
plain: true
|
|
97
|
-
});
|
|
98
|
-
returnItem = addKey(item, returnItem, kta);
|
|
99
|
-
returnItem = populateEvents(returnItem);
|
|
100
|
-
} else {
|
|
101
|
-
throw new Error('No deletedAt or isDeleted attribute found in model, and deleteOnRemove is not set');
|
|
102
|
-
}
|
|
103
|
-
logger.debug(`[REMOVE] Removed ${model.name} with key: ${returnItem.key ? JSON.stringify(returnItem.key) : `id=${item.id}`}`);
|
|
104
|
-
return returnItem;
|
|
105
|
-
};
|
|
106
|
-
return remove;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
export { getRemoveOperation };
|
|
110
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
package/dist/es/ops/update.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { isPriKey, abbrevIK, isComKey, validateKeys } from '@fjell/core';
|
|
2
|
-
import { extractEvents, removeEvents } from '../EventCoordinator.js';
|
|
3
|
-
import { removeKey } from '../KeyMaster.js';
|
|
4
|
-
import LibLogger from '../logger.js';
|
|
5
|
-
import { processRow } from '../RowProcessor.js';
|
|
6
|
-
import { NotFoundError } from '@fjell/lib';
|
|
7
|
-
import { Op } from 'sequelize';
|
|
8
|
-
import { buildRelationshipPath } from '../util/relationshipUtils.js';
|
|
9
|
-
import { stringifyJSON } from '../util/general.js';
|
|
10
|
-
|
|
11
|
-
const logger = LibLogger.get('sequelize', 'ops', 'update');
|
|
12
|
-
// Helper function to merge includes avoiding duplicates
|
|
13
|
-
const mergeIncludes = (existingIncludes, newIncludes)=>{
|
|
14
|
-
const mergedIncludes = [
|
|
15
|
-
...existingIncludes
|
|
16
|
-
];
|
|
17
|
-
for (const newInclude of newIncludes){
|
|
18
|
-
const existingIndex = mergedIncludes.findIndex((existing)=>existing.as === newInclude.as && existing.model === newInclude.model);
|
|
19
|
-
if (existingIndex === -1) {
|
|
20
|
-
mergedIncludes.push(newInclude);
|
|
21
|
-
} else if (newInclude.include && mergedIncludes[existingIndex].include) {
|
|
22
|
-
mergedIncludes[existingIndex].include = [
|
|
23
|
-
...mergedIncludes[existingIndex].include,
|
|
24
|
-
...newInclude.include
|
|
25
|
-
];
|
|
26
|
-
} else if (newInclude.include) {
|
|
27
|
-
mergedIncludes[existingIndex].include = newInclude.include;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return mergedIncludes;
|
|
31
|
-
};
|
|
32
|
-
const getUpdateOperation = (models, definition, registry)=>{
|
|
33
|
-
const { options: { references, aggregations } } = definition;
|
|
34
|
-
const update = async (key, item)=>{
|
|
35
|
-
logger.debug(`UPDATE operation called on ${models[0].name} with ${isPriKey(key) ? `primary key: pk=${key.pk}` : `composite key: pk=${key.pk}, loc=[${key.loc.map((l)=>`${l.kt}=${l.lk}`).join(', ')}]`}`);
|
|
36
|
-
const { coordinate } = definition;
|
|
37
|
-
const { kta } = coordinate;
|
|
38
|
-
logger.debug('update: %s, %j', abbrevIK(key), item);
|
|
39
|
-
// Find the object we're updating
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
const model = models[0];
|
|
42
|
-
let response;
|
|
43
|
-
if (isPriKey(key)) {
|
|
44
|
-
// Find the model by using the PK
|
|
45
|
-
const priKey = key;
|
|
46
|
-
logger.trace(`[UPDATE] Executing ${model.name}.findByPk() with pk: ${priKey.pk}`);
|
|
47
|
-
response = await model.findByPk(priKey.pk);
|
|
48
|
-
} else if (isComKey(key)) {
|
|
49
|
-
const comKey = key;
|
|
50
|
-
// Build query options for composite key with multiple location keys
|
|
51
|
-
const where = {
|
|
52
|
-
id: comKey.pk
|
|
53
|
-
};
|
|
54
|
-
const additionalIncludes = [];
|
|
55
|
-
// Process all location keys in the composite key
|
|
56
|
-
for (const locator of comKey.loc){
|
|
57
|
-
const relationshipInfo = buildRelationshipPath(model, locator.kt, kta, true);
|
|
58
|
-
if (!relationshipInfo.found) {
|
|
59
|
-
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
60
|
-
logger.error(errorMessage, {
|
|
61
|
-
key: comKey,
|
|
62
|
-
kta
|
|
63
|
-
});
|
|
64
|
-
throw new Error(errorMessage);
|
|
65
|
-
}
|
|
66
|
-
if (relationshipInfo.isDirect) {
|
|
67
|
-
// Direct foreign key field
|
|
68
|
-
const fieldName = `${locator.kt}Id`;
|
|
69
|
-
where[fieldName] = locator.lk;
|
|
70
|
-
} else if (relationshipInfo.path) {
|
|
71
|
-
// Hierarchical relationship requiring traversal
|
|
72
|
-
where[relationshipInfo.path] = {
|
|
73
|
-
[Op.eq]: locator.lk
|
|
74
|
-
};
|
|
75
|
-
// Add necessary includes for relationship traversal
|
|
76
|
-
if (relationshipInfo.includes) {
|
|
77
|
-
additionalIncludes.push(...relationshipInfo.includes);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
// Build final query options
|
|
82
|
-
const queryOptions = {
|
|
83
|
-
where
|
|
84
|
-
};
|
|
85
|
-
if (additionalIncludes.length > 0) {
|
|
86
|
-
queryOptions.include = mergeIncludes([], additionalIncludes);
|
|
87
|
-
}
|
|
88
|
-
logger.default(`Update composite key query for ${model.name} with where fields: ${queryOptions.where ? Object.keys(queryOptions.where).join(', ') : 'none'}`);
|
|
89
|
-
logger.trace(`[UPDATE] Executing ${model.name}.findOne() with options: ${stringifyJSON(queryOptions)}`);
|
|
90
|
-
response = await model.findOne(queryOptions);
|
|
91
|
-
}
|
|
92
|
-
if (response) {
|
|
93
|
-
// Remove the key and events
|
|
94
|
-
let updateProps = removeKey(item);
|
|
95
|
-
// TODO: We need the opposite of processRow, something to step down from fjell to database.
|
|
96
|
-
updateProps = extractEvents(updateProps);
|
|
97
|
-
updateProps = removeEvents(updateProps);
|
|
98
|
-
logger.default(`Update found ${model.name} record to modify`);
|
|
99
|
-
logger.default(`Update properties configured: ${Object.keys(updateProps).join(', ')}`);
|
|
100
|
-
// Update the object
|
|
101
|
-
logger.trace(`[UPDATE] Executing ${model.name}.update() with properties: ${stringifyJSON(updateProps)}`);
|
|
102
|
-
response = await response.update(updateProps);
|
|
103
|
-
// Populate the key and events
|
|
104
|
-
const processedItem = await processRow(response, kta, references, aggregations, registry);
|
|
105
|
-
const returnItem = validateKeys(processedItem, kta);
|
|
106
|
-
logger.debug(`[UPDATE] Updated ${model.name} with key: ${returnItem.key ? JSON.stringify(returnItem.key) : `id=${response.id}`}`);
|
|
107
|
-
return returnItem;
|
|
108
|
-
} else {
|
|
109
|
-
throw new NotFoundError('update', coordinate, key);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
return update;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
export { getUpdateOperation };
|
|
116
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBkYXRlLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Primary } from '@fjell/lib';
|
|
2
|
-
import { createOperations } from '../Operations.js';
|
|
3
|
-
import { createOptions } from '../Options.js';
|
|
4
|
-
import { createCoordinate } from '../Coordinate.js';
|
|
5
|
-
import LibLogger from '../logger.js';
|
|
6
|
-
|
|
7
|
-
const logger = LibLogger.get('lib-sequelize', 'primary', 'instance');
|
|
8
|
-
function createSequelizeLibrary(keyType, models, libOptions = {}, scopes = [], registry) {
|
|
9
|
-
logger.debug('createSequelizeLibrary', {
|
|
10
|
-
keyType,
|
|
11
|
-
models,
|
|
12
|
-
libOptions,
|
|
13
|
-
scopes
|
|
14
|
-
});
|
|
15
|
-
// Create coordinate and options separately following new pattern
|
|
16
|
-
const coordinate = createCoordinate([
|
|
17
|
-
keyType
|
|
18
|
-
], scopes);
|
|
19
|
-
const options = createOptions(libOptions);
|
|
20
|
-
// Create operations with the new signature
|
|
21
|
-
const operations = createOperations(models, coordinate, registry, options);
|
|
22
|
-
// Wrap operations for primary pattern
|
|
23
|
-
const wrappedOperations = Primary.wrapOperations(operations, options, coordinate, registry);
|
|
24
|
-
return {
|
|
25
|
-
coordinate,
|
|
26
|
-
registry,
|
|
27
|
-
operations: wrappedOperations,
|
|
28
|
-
options,
|
|
29
|
-
models
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
// Legacy exports for backwards compatibility
|
|
33
|
-
const createInstance = createSequelizeLibrary;
|
|
34
|
-
|
|
35
|
-
export { createInstance, createSequelizeLibrary };
|
|
36
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU2VxdWVsaXplTGlicmFyeS5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
package/dist/es/primary/index.js
DELETED
package/dist/es/util/general.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
//Recursive implementation of jSON.stringify;
|
|
2
|
-
const stringifyJSON = function(obj, visited = new Set()) {
|
|
3
|
-
const arrOfKeyVals = [];
|
|
4
|
-
const arrVals = [];
|
|
5
|
-
let objKeys = [];
|
|
6
|
-
/*********CHECK FOR PRIMITIVE TYPES**********/ if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) return '' + obj;
|
|
7
|
-
else if (typeof obj === 'string') return '"' + obj + '"';
|
|
8
|
-
/*********DETECT CIRCULAR REFERENCES**********/ if (obj instanceof Object && visited.has(obj)) {
|
|
9
|
-
return '"(circular)"';
|
|
10
|
-
} else if (Array.isArray(obj)) {
|
|
11
|
-
//check for empty array
|
|
12
|
-
if (obj[0] === undefined) return '[]';
|
|
13
|
-
else {
|
|
14
|
-
// Add array to visited before processing its elements
|
|
15
|
-
visited.add(obj);
|
|
16
|
-
obj.forEach(function(el) {
|
|
17
|
-
arrVals.push(stringifyJSON(el, visited));
|
|
18
|
-
});
|
|
19
|
-
return '[' + arrVals + ']';
|
|
20
|
-
}
|
|
21
|
-
} else if (obj instanceof Object) {
|
|
22
|
-
// Add object to visited before processing its properties
|
|
23
|
-
visited.add(obj);
|
|
24
|
-
//get object keys
|
|
25
|
-
objKeys = Object.keys(obj);
|
|
26
|
-
//set key output;
|
|
27
|
-
objKeys.forEach(function(key) {
|
|
28
|
-
const keyOut = '"' + key + '":';
|
|
29
|
-
const keyValOut = obj[key];
|
|
30
|
-
//skip functions and undefined properties
|
|
31
|
-
if (keyValOut instanceof Function || keyValOut === undefined) return; // Skip this entry entirely instead of pushing an empty string
|
|
32
|
-
else if (typeof keyValOut === 'string') arrOfKeyVals.push(keyOut + '"' + keyValOut + '"');
|
|
33
|
-
else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null) arrOfKeyVals.push(keyOut + keyValOut);
|
|
34
|
-
else if (keyValOut instanceof Object) {
|
|
35
|
-
arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut, visited));
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
return '{' + arrOfKeyVals + '}';
|
|
39
|
-
}
|
|
40
|
-
return '';
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export { stringifyJSON };
|
|
44
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhbC5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
/* eslint-disable indent */ /**
|
|
2
|
-
* Helper function to build relationship chain includes
|
|
3
|
-
*/ const buildRelationshipChain = (targetModel, kta, currentIndex, targetIndex)=>{
|
|
4
|
-
// Build the association path and validate relationships exist
|
|
5
|
-
const associationParts = [];
|
|
6
|
-
const modelChain = [
|
|
7
|
-
targetModel
|
|
8
|
-
];
|
|
9
|
-
let currentModel = targetModel;
|
|
10
|
-
// Validate that all associations exist and build model chain
|
|
11
|
-
for(let i = currentIndex + 1; i <= targetIndex; i++){
|
|
12
|
-
const intermediateType = kta[i];
|
|
13
|
-
const associationName = intermediateType;
|
|
14
|
-
if (!currentModel.associations || !currentModel.associations[associationName]) {
|
|
15
|
-
return {
|
|
16
|
-
success: false
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
associationParts.push(associationName);
|
|
20
|
-
currentModel = currentModel.associations[associationName].target;
|
|
21
|
-
modelChain.push(currentModel);
|
|
22
|
-
}
|
|
23
|
-
// Build the full association path for the target field
|
|
24
|
-
const targetPrimaryKey = currentModel.primaryKeyAttribute || 'id';
|
|
25
|
-
const associationPath = `$${associationParts.join('.')}.${targetPrimaryKey}$`;
|
|
26
|
-
// Build nested includes structure iteratively (clearer than recursion)
|
|
27
|
-
let deepestInclude = null;
|
|
28
|
-
// Build from the deepest level back to the root
|
|
29
|
-
for(let i = targetIndex; i > currentIndex; i--){
|
|
30
|
-
const currentType = kta[i];
|
|
31
|
-
const modelIndex = i - currentIndex;
|
|
32
|
-
const includeObj = {
|
|
33
|
-
model: modelChain[modelIndex],
|
|
34
|
-
as: currentType,
|
|
35
|
-
required: true
|
|
36
|
-
};
|
|
37
|
-
if (deepestInclude) {
|
|
38
|
-
includeObj.include = [
|
|
39
|
-
deepestInclude
|
|
40
|
-
];
|
|
41
|
-
}
|
|
42
|
-
deepestInclude = includeObj;
|
|
43
|
-
}
|
|
44
|
-
const includes = deepestInclude ? [
|
|
45
|
-
deepestInclude
|
|
46
|
-
] : [];
|
|
47
|
-
return {
|
|
48
|
-
success: true,
|
|
49
|
-
path: associationPath,
|
|
50
|
-
includes
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
/**
|
|
54
|
-
* Helper function to build relationship path for a locator
|
|
55
|
-
* @param includeIsDirect Whether to include the isDirect flag in the result
|
|
56
|
-
*/ const buildRelationshipPath = (targetModel, locatorType, kta, includeIsDirect = false)=>{
|
|
57
|
-
// First check if the field exists directly
|
|
58
|
-
const directFieldName = `${locatorType}Id`;
|
|
59
|
-
const attributes = targetModel.getAttributes();
|
|
60
|
-
if (attributes && attributes[directFieldName]) {
|
|
61
|
-
const result = {
|
|
62
|
-
found: true
|
|
63
|
-
};
|
|
64
|
-
if (includeIsDirect) {
|
|
65
|
-
result.isDirect = true;
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
// If not direct, look for relationship path
|
|
70
|
-
const targetIndex = kta.indexOf(locatorType);
|
|
71
|
-
if (targetIndex === -1) {
|
|
72
|
-
const result = {
|
|
73
|
-
found: false
|
|
74
|
-
};
|
|
75
|
-
if (includeIsDirect) {
|
|
76
|
-
result.isDirect = false;
|
|
77
|
-
}
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
const currentIndex = 0; // We're always looking from the base model
|
|
81
|
-
if (targetIndex <= currentIndex) {
|
|
82
|
-
const result = {
|
|
83
|
-
found: false
|
|
84
|
-
};
|
|
85
|
-
if (includeIsDirect) {
|
|
86
|
-
result.isDirect = false;
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
const chainResult = buildRelationshipChain(targetModel, kta, currentIndex, targetIndex);
|
|
91
|
-
if (chainResult.success) {
|
|
92
|
-
const result = {
|
|
93
|
-
found: true,
|
|
94
|
-
path: chainResult.path,
|
|
95
|
-
includes: chainResult.includes
|
|
96
|
-
};
|
|
97
|
-
if (includeIsDirect) {
|
|
98
|
-
result.isDirect = false;
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
const result = {
|
|
103
|
-
found: false
|
|
104
|
-
};
|
|
105
|
-
if (includeIsDirect) {
|
|
106
|
-
result.isDirect = false;
|
|
107
|
-
}
|
|
108
|
-
return result;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export { buildRelationshipChain, buildRelationshipPath };
|
|
112
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVsYXRpb25zaGlwVXRpbHMuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|