@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
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { validateKeys } from '@fjell/core';
|
|
1
2
|
import LibLogger from '../logger.js';
|
|
3
|
+
import { processRow } from '../RowProcessor.js';
|
|
2
4
|
|
|
3
5
|
const logger = LibLogger.get('sequelize', 'ops', 'find');
|
|
4
|
-
const getFindOperation = (models, definition)=>{
|
|
5
|
-
const { options } = definition;
|
|
6
|
+
const getFindOperation = (models, definition, registry)=>{
|
|
7
|
+
const { options: { finders, references, aggregations } } = definition;
|
|
6
8
|
const find = async (finder, finderParams, locations)=>{
|
|
7
9
|
logger.default('Find', {
|
|
8
10
|
finder,
|
|
@@ -11,10 +13,18 @@ const getFindOperation = (models, definition)=>{
|
|
|
11
13
|
});
|
|
12
14
|
// Note that we execute the createFinders function here because we want to make sure we're always getting the
|
|
13
15
|
// most up to date methods.
|
|
14
|
-
if (
|
|
15
|
-
const finderMethod =
|
|
16
|
+
if (finders && finders[finder]) {
|
|
17
|
+
const finderMethod = finders[finder];
|
|
16
18
|
if (finderMethod) {
|
|
17
|
-
|
|
19
|
+
const results = await finderMethod(finderParams, locations);
|
|
20
|
+
if (results && results.length > 0) {
|
|
21
|
+
return await Promise.all(results.map(async (row)=>{
|
|
22
|
+
const processedRow = await processRow(row, definition.coordinate.kta, references, aggregations, registry);
|
|
23
|
+
return validateKeys(processedRow, definition.coordinate.kta);
|
|
24
|
+
}));
|
|
25
|
+
} else {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
18
28
|
} else {
|
|
19
29
|
logger.error(`Finder %s not found`, finder);
|
|
20
30
|
throw new Error(`Finder ${finder} not found`);
|
|
@@ -28,4 +38,4 @@ const getFindOperation = (models, definition)=>{
|
|
|
28
38
|
};
|
|
29
39
|
|
|
30
40
|
export { getFindOperation };
|
|
31
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
41
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmluZC5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
|
|
7
|
+
const logger = LibLogger.get('sequelize', 'ops', 'get');
|
|
8
|
+
// Helper function to process composite key and build query options
|
|
9
|
+
const processCompositeKey = (comKey, model, kta)=>{
|
|
10
|
+
const where = {
|
|
11
|
+
id: comKey.pk
|
|
12
|
+
};
|
|
13
|
+
const includes = [];
|
|
14
|
+
for (const locator of comKey.loc){
|
|
15
|
+
const relationshipInfo = buildRelationshipPath(model, locator.kt, kta);
|
|
16
|
+
if (!relationshipInfo.found) {
|
|
17
|
+
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
18
|
+
logger.error(errorMessage, {
|
|
19
|
+
key: comKey,
|
|
20
|
+
kta
|
|
21
|
+
});
|
|
22
|
+
throw new Error(errorMessage);
|
|
23
|
+
}
|
|
24
|
+
if (relationshipInfo.path) {
|
|
25
|
+
// This requires a relationship traversal
|
|
26
|
+
where[relationshipInfo.path] = locator.lk;
|
|
27
|
+
if (relationshipInfo.includes) {
|
|
28
|
+
includes.push(...relationshipInfo.includes);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
// This is a direct field
|
|
32
|
+
const fieldName = `${locator.kt}Id`;
|
|
33
|
+
where[fieldName] = locator.lk;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const result = {
|
|
37
|
+
where
|
|
38
|
+
};
|
|
39
|
+
if (includes.length > 0) {
|
|
40
|
+
result.include = includes;
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
const getGetOperation = (models, definition, registry)=>{
|
|
45
|
+
const { coordinate, options: { references, aggregations } } = definition;
|
|
46
|
+
const { kta } = coordinate;
|
|
47
|
+
const get = async (key)=>{
|
|
48
|
+
logger.default('Get', {
|
|
49
|
+
key
|
|
50
|
+
});
|
|
51
|
+
if (!isValidItemKey(key)) {
|
|
52
|
+
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
53
|
+
throw new Error('Key for Get is not a valid ItemKey');
|
|
54
|
+
}
|
|
55
|
+
const itemKey = key;
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
const model = models[0];
|
|
58
|
+
let item;
|
|
59
|
+
if (isPriKey(itemKey)) {
|
|
60
|
+
// This is the easy case because we can just find the item by its primary key
|
|
61
|
+
item = await model.findByPk(itemKey.pk);
|
|
62
|
+
} else if (isComKey(itemKey)) {
|
|
63
|
+
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
64
|
+
const comKey = itemKey;
|
|
65
|
+
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
66
|
+
logger.default('Composite key query', {
|
|
67
|
+
queryOptions
|
|
68
|
+
});
|
|
69
|
+
item = await model.findOne(queryOptions);
|
|
70
|
+
}
|
|
71
|
+
if (!item) {
|
|
72
|
+
throw new NotFoundError('get', coordinate, key);
|
|
73
|
+
} else {
|
|
74
|
+
return validateKeys(await processRow(item, kta, references, aggregations, registry), kta);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return get;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export { getGetOperation };
|
|
81
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2V0LmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -2,13 +2,13 @@ import LibLogger from '../logger.js';
|
|
|
2
2
|
import { getAllOperation } from './all.js';
|
|
3
3
|
|
|
4
4
|
const logger = LibLogger.get('sequelize', 'ops', 'one');
|
|
5
|
-
const getOneOperation = (models, definition)=>{
|
|
5
|
+
const getOneOperation = (models, definition, registry)=>{
|
|
6
6
|
const one = async (itemQuery, locations = [])=>{
|
|
7
7
|
logger.default('One', {
|
|
8
8
|
itemQuery,
|
|
9
9
|
locations
|
|
10
10
|
});
|
|
11
|
-
const items = await getAllOperation(models, definition)(itemQuery, locations);
|
|
11
|
+
const items = await getAllOperation(models, definition, registry)(itemQuery, locations);
|
|
12
12
|
if (items.length > 0) {
|
|
13
13
|
return items[0];
|
|
14
14
|
} else {
|
|
@@ -1,10 +1,48 @@
|
|
|
1
1
|
import { isValidItemKey, abbrevIK, isPriKey, isComKey } from '@fjell/core';
|
|
2
2
|
import { populateEvents } from '../EventCoordinator.js';
|
|
3
|
-
import {
|
|
3
|
+
import { addKey } from '../KeyMaster.js';
|
|
4
4
|
import LibLogger from '../logger.js';
|
|
5
|
+
import { buildRelationshipPath } from '../util/relationshipUtils.js';
|
|
5
6
|
|
|
6
7
|
const logger = LibLogger.get('sequelize', 'ops', 'remove');
|
|
7
|
-
|
|
8
|
+
// Helper function to process composite key and build query options
|
|
9
|
+
const processCompositeKey = (comKey, model, kta)=>{
|
|
10
|
+
const where = {
|
|
11
|
+
id: comKey.pk
|
|
12
|
+
};
|
|
13
|
+
const includes = [];
|
|
14
|
+
for (const locator of comKey.loc){
|
|
15
|
+
const relationshipInfo = buildRelationshipPath(model, locator.kt, kta);
|
|
16
|
+
if (!relationshipInfo.found) {
|
|
17
|
+
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
18
|
+
logger.error(errorMessage, {
|
|
19
|
+
key: comKey,
|
|
20
|
+
kta
|
|
21
|
+
});
|
|
22
|
+
throw new Error(errorMessage);
|
|
23
|
+
}
|
|
24
|
+
if (relationshipInfo.path) {
|
|
25
|
+
// This requires a relationship traversal
|
|
26
|
+
where[relationshipInfo.path] = locator.lk;
|
|
27
|
+
if (relationshipInfo.includes) {
|
|
28
|
+
includes.push(...relationshipInfo.includes);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
// This is a direct field
|
|
32
|
+
const fieldName = `${locator.kt}Id`;
|
|
33
|
+
where[fieldName] = locator.lk;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const result = {
|
|
37
|
+
where
|
|
38
|
+
};
|
|
39
|
+
if (includes.length > 0) {
|
|
40
|
+
result.include = includes;
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
const getRemoveOperation = (models, definition, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
45
|
+
registry)=>{
|
|
8
46
|
const { coordinate, options } = definition;
|
|
9
47
|
const { kta } = coordinate;
|
|
10
48
|
const remove = async (key)=>{
|
|
@@ -23,14 +61,16 @@ const getRemoveOperation = (models, definition)=>{
|
|
|
23
61
|
if (isPriKey(key)) {
|
|
24
62
|
item = await model.findByPk(key.pk);
|
|
25
63
|
} else if (isComKey(key)) {
|
|
26
|
-
|
|
64
|
+
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
27
65
|
const comKey = key;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
[(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
|
|
32
|
-
}
|
|
66
|
+
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
67
|
+
logger.default('Composite key query', {
|
|
68
|
+
queryOptions
|
|
33
69
|
});
|
|
70
|
+
item = await model.findOne(queryOptions);
|
|
71
|
+
}
|
|
72
|
+
if (!item) {
|
|
73
|
+
throw new Error(`Item not found for removal with key: ${abbrevIK(key)}`);
|
|
34
74
|
}
|
|
35
75
|
const isDeletedAttribute = model.getAttributes().isDeleted;
|
|
36
76
|
const deletedAtAttribute = model.getAttributes().deletedAt;
|
|
@@ -46,14 +86,14 @@ const getRemoveOperation = (models, definition)=>{
|
|
|
46
86
|
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
47
87
|
plain: true
|
|
48
88
|
});
|
|
49
|
-
returnItem =
|
|
89
|
+
returnItem = addKey(item, returnItem, kta);
|
|
50
90
|
returnItem = populateEvents(returnItem);
|
|
51
91
|
} else if (options.deleteOnRemove) {
|
|
52
92
|
await (item === null || item === void 0 ? void 0 : item.destroy());
|
|
53
93
|
returnItem = item === null || item === void 0 ? void 0 : item.get({
|
|
54
94
|
plain: true
|
|
55
95
|
});
|
|
56
|
-
returnItem =
|
|
96
|
+
returnItem = addKey(item, returnItem, kta);
|
|
57
97
|
returnItem = populateEvents(returnItem);
|
|
58
98
|
} else {
|
|
59
99
|
throw new Error('No deletedAt or isDeleted attribute found in model, and deleteOnRemove is not set');
|
|
@@ -64,4 +104,4 @@ const getRemoveOperation = (models, definition)=>{
|
|
|
64
104
|
};
|
|
65
105
|
|
|
66
106
|
export { getRemoveOperation };
|
|
67
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
107
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { abbrevIK, isPriKey, isComKey } from '@fjell/core';
|
|
2
|
-
import {
|
|
3
|
-
import { removeKey
|
|
1
|
+
import { abbrevIK, isPriKey, isComKey, validateKeys } from '@fjell/core';
|
|
2
|
+
import { extractEvents, removeEvents } from '../EventCoordinator.js';
|
|
3
|
+
import { removeKey } from '../KeyMaster.js';
|
|
4
4
|
import LibLogger from '../logger.js';
|
|
5
|
+
import { processRow } from '../RowProcessor.js';
|
|
6
|
+
import { stringifyJSON } from '../util/general.js';
|
|
5
7
|
import { NotFoundError } from '@fjell/lib';
|
|
6
8
|
|
|
7
9
|
const logger = LibLogger.get('sequelize', 'ops', 'update');
|
|
8
|
-
const getUpdateOperation = (models, definition)=>{
|
|
10
|
+
const getUpdateOperation = (models, definition, registry)=>{
|
|
11
|
+
const { options: { references, aggregations } } = definition;
|
|
9
12
|
const update = async (key, item)=>{
|
|
10
13
|
const { coordinate } = definition;
|
|
11
14
|
const { kta } = coordinate;
|
|
@@ -32,19 +35,16 @@ const getUpdateOperation = (models, definition)=>{
|
|
|
32
35
|
if (response) {
|
|
33
36
|
// Remove the key and events
|
|
34
37
|
let updateProps = removeKey(item);
|
|
35
|
-
|
|
38
|
+
// TODO: We need the opposite of processRow, something to step down from fjell to database.
|
|
39
|
+
updateProps = extractEvents(updateProps);
|
|
40
|
+
updateProps = removeEvents(updateProps);
|
|
41
|
+
logger.default('Response: %s', stringifyJSON(response));
|
|
42
|
+
logger.default('Update Properties: %s', stringifyJSON(updateProps));
|
|
36
43
|
// Update the object
|
|
37
|
-
response =
|
|
38
|
-
...response,
|
|
39
|
-
...updateProps
|
|
40
|
-
};
|
|
41
|
-
await (response === null || response === void 0 ? void 0 : response.save());
|
|
44
|
+
response = await response.update(updateProps);
|
|
42
45
|
// Populate the key and events
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
returnItem = populateKey(returnItem, kta);
|
|
47
|
-
returnItem = populateEvents(returnItem);
|
|
46
|
+
const processedItem = await processRow(response, kta, references, aggregations, registry);
|
|
47
|
+
const returnItem = validateKeys(processedItem, kta);
|
|
48
48
|
return returnItem;
|
|
49
49
|
} else {
|
|
50
50
|
throw new NotFoundError('update', coordinate, key);
|
|
@@ -4,7 +4,7 @@ import { createOperations } from '../Operations.js';
|
|
|
4
4
|
import LibLogger from '../logger.js';
|
|
5
5
|
|
|
6
6
|
const logger = LibLogger.get('lib-sequelize', 'primary', 'instance');
|
|
7
|
-
function createInstance(keyType, models, libOptions = {}, scopes = []) {
|
|
7
|
+
function createInstance(keyType, models, libOptions = {}, scopes = [], registry) {
|
|
8
8
|
logger.debug('createInstance', {
|
|
9
9
|
keyType,
|
|
10
10
|
models,
|
|
@@ -14,13 +14,14 @@ function createInstance(keyType, models, libOptions = {}, scopes = []) {
|
|
|
14
14
|
const definition = createDefinition([
|
|
15
15
|
keyType
|
|
16
16
|
], scopes, libOptions);
|
|
17
|
-
const operations = createOperations(models, definition);
|
|
17
|
+
const operations = createOperations(models, definition, registry);
|
|
18
18
|
return {
|
|
19
19
|
definition,
|
|
20
|
-
operations: Primary.wrapOperations(operations, definition),
|
|
21
|
-
models
|
|
20
|
+
operations: Primary.wrapOperations(operations, definition, registry),
|
|
21
|
+
models,
|
|
22
|
+
registry
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
export { createInstance };
|
|
26
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,
|
|
27
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiSW5zdGFuY2UuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable no-undefined */ const clean = (obj)=>{
|
|
2
|
+
return Object.fromEntries(Object.entries(obj).filter(([_, v])=>v !== undefined));
|
|
3
|
+
};
|
|
4
|
+
//Recursive implementation of jSON.stringify;
|
|
5
|
+
const stringifyJSON = function(obj, visited = new Set()) {
|
|
6
|
+
const arrOfKeyVals = [];
|
|
7
|
+
const arrVals = [];
|
|
8
|
+
let objKeys = [];
|
|
9
|
+
/*********CHECK FOR PRIMITIVE TYPES**********/ if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) return '' + obj;
|
|
10
|
+
else if (typeof obj === 'string') return '"' + obj + '"';
|
|
11
|
+
/*********DETECT CIRCULAR REFERENCES**********/ if (obj instanceof Object && visited.has(obj)) {
|
|
12
|
+
return '"(circular)"';
|
|
13
|
+
} else if (Array.isArray(obj)) {
|
|
14
|
+
//check for empty array
|
|
15
|
+
if (obj[0] === undefined) return '[]';
|
|
16
|
+
else {
|
|
17
|
+
// Add array to visited before processing its elements
|
|
18
|
+
visited.add(obj);
|
|
19
|
+
obj.forEach(function(el) {
|
|
20
|
+
arrVals.push(stringifyJSON(el, visited));
|
|
21
|
+
});
|
|
22
|
+
return '[' + arrVals + ']';
|
|
23
|
+
}
|
|
24
|
+
} else if (obj instanceof Object) {
|
|
25
|
+
// Add object to visited before processing its properties
|
|
26
|
+
visited.add(obj);
|
|
27
|
+
//get object keys
|
|
28
|
+
objKeys = Object.keys(obj);
|
|
29
|
+
//set key output;
|
|
30
|
+
objKeys.forEach(function(key) {
|
|
31
|
+
const keyOut = '"' + key + '":';
|
|
32
|
+
const keyValOut = obj[key];
|
|
33
|
+
//skip functions and undefined properties
|
|
34
|
+
if (keyValOut instanceof Function || keyValOut === undefined) return; // Skip this entry entirely instead of pushing an empty string
|
|
35
|
+
else if (typeof keyValOut === 'string') arrOfKeyVals.push(keyOut + '"' + keyValOut + '"');
|
|
36
|
+
else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null) arrOfKeyVals.push(keyOut + keyValOut);
|
|
37
|
+
else if (keyValOut instanceof Object) {
|
|
38
|
+
arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut, visited));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return '{' + arrOfKeyVals + '}';
|
|
42
|
+
}
|
|
43
|
+
return '';
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { clean, stringifyJSON };
|
|
47
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhbC5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,112 @@
|
|
|
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 targetType = kta[targetIndex];
|
|
25
|
+
const associationPath = `$${associationParts.join('.')}.${targetType}Id$`;
|
|
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
|