@fjell/lib-sequelize 4.4.14 → 4.4.16
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/cjs/ops/all.cjs
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const core = require('@fjell/core');
|
|
6
|
-
const QueryBuilder = require('../QueryBuilder.cjs');
|
|
7
|
-
const logger$1 = require('../logger.cjs');
|
|
8
|
-
const RowProcessor = require('../RowProcessor.cjs');
|
|
9
|
-
const sequelize = require('sequelize');
|
|
10
|
-
const relationshipUtils = require('../util/relationshipUtils.cjs');
|
|
11
|
-
const OperationContext = require('../OperationContext.cjs');
|
|
12
|
-
const general = require('../util/general.cjs');
|
|
13
|
-
|
|
14
|
-
const logger = logger$1.default.get('sequelize', 'ops', 'all');
|
|
15
|
-
// Helper function to merge includes avoiding duplicates
|
|
16
|
-
const mergeIncludes = (existingIncludes, newIncludes)=>{
|
|
17
|
-
const mergedIncludes = [
|
|
18
|
-
...existingIncludes
|
|
19
|
-
];
|
|
20
|
-
for (const newInclude of newIncludes){
|
|
21
|
-
const existingIndex = mergedIncludes.findIndex((existing)=>existing.as === newInclude.as && existing.model === newInclude.model);
|
|
22
|
-
if (existingIndex === -1) {
|
|
23
|
-
mergedIncludes.push(newInclude);
|
|
24
|
-
} else if (newInclude.include && mergedIncludes[existingIndex].include) {
|
|
25
|
-
mergedIncludes[existingIndex].include = [
|
|
26
|
-
...mergedIncludes[existingIndex].include,
|
|
27
|
-
...newInclude.include
|
|
28
|
-
];
|
|
29
|
-
} else if (newInclude.include) {
|
|
30
|
-
mergedIncludes[existingIndex].include = newInclude.include;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return mergedIncludes;
|
|
34
|
-
};
|
|
35
|
-
const getAllOperation = (models, definition, registry)=>{
|
|
36
|
-
const { coordinate, options: { references, aggregations } } = definition;
|
|
37
|
-
//#region Query
|
|
38
|
-
const all = async (itemQuery, locations)=>{
|
|
39
|
-
var _options_include;
|
|
40
|
-
logger.debug(`ALL operation called on ${models[0].name} with ${(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'}`);
|
|
41
|
-
const loc = locations || [];
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
const model = models[0];
|
|
44
|
-
// Build base query from itemQuery
|
|
45
|
-
const options = QueryBuilder.buildQuery(itemQuery, model);
|
|
46
|
-
// Handle location keys if present
|
|
47
|
-
if (loc.length > 0) {
|
|
48
|
-
const { kta } = coordinate;
|
|
49
|
-
const directLocations = [];
|
|
50
|
-
const hierarchicalLocations = [];
|
|
51
|
-
const additionalIncludes = [];
|
|
52
|
-
// Categorize location keys as direct or hierarchical
|
|
53
|
-
for (const locKey of loc){
|
|
54
|
-
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locKey.kt, kta, true);
|
|
55
|
-
if (!relationshipInfo.found) {
|
|
56
|
-
const errorMessage = `Location key '${locKey.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
57
|
-
logger.error(errorMessage, {
|
|
58
|
-
locations: loc,
|
|
59
|
-
kta
|
|
60
|
-
});
|
|
61
|
-
throw new Error(errorMessage);
|
|
62
|
-
}
|
|
63
|
-
if (relationshipInfo.isDirect) {
|
|
64
|
-
directLocations.push(locKey);
|
|
65
|
-
} else {
|
|
66
|
-
hierarchicalLocations.push(locKey);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// Handle direct location keys (simple foreign key constraints)
|
|
70
|
-
for (const locKey of directLocations){
|
|
71
|
-
if (locKey.lk === undefined || locKey.lk == null || locKey.lk === '' || typeof locKey.lk === 'object' && Object.keys(locKey.lk).length === 0) {
|
|
72
|
-
logger.error(`Location key '${locKey.kt}' has invalid lk value: ${general.stringifyJSON(locKey.lk)}`, {
|
|
73
|
-
locKey,
|
|
74
|
-
locations: loc
|
|
75
|
-
});
|
|
76
|
-
throw new Error(`Location key '${locKey.kt}' has invalid lk value: ${general.stringifyJSON(locKey.lk)}`);
|
|
77
|
-
}
|
|
78
|
-
const foreignKeyField = locKey.kt + 'Id';
|
|
79
|
-
// Check if this field already has a condition from the itemQuery
|
|
80
|
-
if (options.where[foreignKeyField]) {
|
|
81
|
-
logger.debug(`[ALL] Field ${foreignKeyField} already constrained by itemQuery, skipping location constraint to avoid conflicts`);
|
|
82
|
-
continue; // Skip this location constraint to avoid conflicts
|
|
83
|
-
}
|
|
84
|
-
logger.trace(`[ALL] Setting direct location where clause: ${foreignKeyField} = ${general.stringifyJSON(locKey.lk)} (type: ${typeof locKey.lk})`);
|
|
85
|
-
options.where[foreignKeyField] = {
|
|
86
|
-
[sequelize.Op.eq]: locKey.lk
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
// Handle hierarchical location keys (requires relationship traversal)
|
|
90
|
-
for (const locKey of hierarchicalLocations){
|
|
91
|
-
if (locKey.lk === undefined || locKey.lk == null || locKey.lk === '' || typeof locKey.lk === 'object' && Object.keys(locKey.lk).length === 0) {
|
|
92
|
-
logger.error(`Hierarchical location key '${locKey.kt}' has invalid lk value: ${general.stringifyJSON(locKey.lk)}`, {
|
|
93
|
-
locKey,
|
|
94
|
-
locations: loc
|
|
95
|
-
});
|
|
96
|
-
throw new Error(`Hierarchical location key '${locKey.kt}' has invalid lk value: ${general.stringifyJSON(locKey.lk)}`);
|
|
97
|
-
}
|
|
98
|
-
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locKey.kt, kta);
|
|
99
|
-
if (relationshipInfo.found && relationshipInfo.path) {
|
|
100
|
-
// Check if this field already has a condition from the itemQuery
|
|
101
|
-
if (options.where[relationshipInfo.path]) {
|
|
102
|
-
logger.debug(`[ALL] Field ${relationshipInfo.path} already constrained by itemQuery, skipping hierarchical location constraint to avoid conflicts`);
|
|
103
|
-
continue; // Skip this location constraint to avoid conflicts
|
|
104
|
-
}
|
|
105
|
-
// Add the relationship constraint using the path
|
|
106
|
-
logger.trace(`[ALL] Setting hierarchical location where clause: ${relationshipInfo.path} = ${general.stringifyJSON(locKey.lk)} (type: ${typeof locKey.lk})`);
|
|
107
|
-
options.where[relationshipInfo.path] = {
|
|
108
|
-
[sequelize.Op.eq]: locKey.lk
|
|
109
|
-
};
|
|
110
|
-
// Add necessary includes for the relationship traversal
|
|
111
|
-
if (relationshipInfo.includes) {
|
|
112
|
-
additionalIncludes.push(...relationshipInfo.includes);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
// Merge additional includes with existing includes
|
|
117
|
-
if (additionalIncludes.length > 0) {
|
|
118
|
-
const existingIncludes = options.include || [];
|
|
119
|
-
options.include = mergeIncludes(existingIncludes, additionalIncludes);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
logger.default(`All query configured for ${model.name} with where fields: ${options.where ? Object.keys(options.where).join(', ') : 'none'}, includes: ${((_options_include = options.include) === null || _options_include === void 0 ? void 0 : _options_include.length) || 0}`);
|
|
123
|
-
try {
|
|
124
|
-
logger.trace(`[ALL] Executing ${model.name}.findAll() with options: ${JSON.stringify(options, null, 2)}`);
|
|
125
|
-
} catch {
|
|
126
|
-
// Fallback for cases where JSON.stringify fails on Sequelize operators
|
|
127
|
-
logger.trace(`[ALL] Executing ${model.name}.findAll() with options containing non-serializable operators (${Object.keys(options.where || {}).length} where conditions)`);
|
|
128
|
-
}
|
|
129
|
-
const matchingItems = await model.findAll(options);
|
|
130
|
-
// this.logger.default('Matching Items', { matchingItems });
|
|
131
|
-
// Get the current context from context manager
|
|
132
|
-
const context = OperationContext.contextManager.getCurrentContext();
|
|
133
|
-
// TODO: Move this Up!
|
|
134
|
-
const results = await Promise.all(matchingItems.map(async (row)=>{
|
|
135
|
-
const processedRow = await RowProcessor.processRow(row, coordinate.kta, references, aggregations, registry, context);
|
|
136
|
-
return core.validateKeys(processedRow, coordinate.kta);
|
|
137
|
-
}));
|
|
138
|
-
logger.debug(`[ALL] Returning ${results.length} ${model.name} records`);
|
|
139
|
-
return results;
|
|
140
|
-
};
|
|
141
|
-
return all;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
exports.getAllOperation = getAllOperation;
|
|
145
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWxsLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
package/dist/cjs/ops/create.cjs
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
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 EventCoordinator = require('../EventCoordinator.cjs');
|
|
9
|
-
const relationshipUtils = require('../util/relationshipUtils.cjs');
|
|
10
|
-
const general = require('../util/general.cjs');
|
|
11
|
-
|
|
12
|
-
const logger = logger$1.default.get('sequelize', 'ops', 'create');
|
|
13
|
-
// Helper function to translate PostgreSQL errors to meaningful messages
|
|
14
|
-
function translateDatabaseError(error, itemData, modelName) {
|
|
15
|
-
var _error_original, _error_original1, _error_original2;
|
|
16
|
-
const originalMessage = error.message || '';
|
|
17
|
-
const errorCode = (_error_original = error.original) === null || _error_original === void 0 ? void 0 : _error_original.code;
|
|
18
|
-
const constraint = (_error_original1 = error.original) === null || _error_original1 === void 0 ? void 0 : _error_original1.constraint;
|
|
19
|
-
const detail = (_error_original2 = error.original) === null || _error_original2 === void 0 ? void 0 : _error_original2.detail;
|
|
20
|
-
logger.error('Database error during create operation', {
|
|
21
|
-
errorCode,
|
|
22
|
-
constraint,
|
|
23
|
-
detail,
|
|
24
|
-
originalMessage,
|
|
25
|
-
modelName,
|
|
26
|
-
itemData: JSON.stringify(itemData, null, 2)
|
|
27
|
-
});
|
|
28
|
-
// Handle specific PostgreSQL error codes
|
|
29
|
-
switch(errorCode){
|
|
30
|
-
case '23505':
|
|
31
|
-
if (constraint) {
|
|
32
|
-
return new Error(`Duplicate value violates unique constraint '${constraint}'. ${detail || ''}`);
|
|
33
|
-
}
|
|
34
|
-
return new Error(`Duplicate value detected. This record already exists. ${detail || ''}`);
|
|
35
|
-
case '23503':
|
|
36
|
-
if (constraint) {
|
|
37
|
-
return new Error(`Foreign key constraint '${constraint}' violated. Referenced record does not exist. ${detail || ''}`);
|
|
38
|
-
}
|
|
39
|
-
return new Error(`Referenced record does not exist. Check that all related records are valid. ${detail || ''}`);
|
|
40
|
-
case '23502':
|
|
41
|
-
var _error_original3;
|
|
42
|
-
const column = (_error_original3 = error.original) === null || _error_original3 === void 0 ? void 0 : _error_original3.column;
|
|
43
|
-
if (column) {
|
|
44
|
-
return new Error(`Required field '${column}' cannot be null`);
|
|
45
|
-
}
|
|
46
|
-
return new Error(`Required field is missing or null`);
|
|
47
|
-
case '23514':
|
|
48
|
-
if (constraint) {
|
|
49
|
-
return new Error(`Check constraint '${constraint}' violated. ${detail || ''}`);
|
|
50
|
-
}
|
|
51
|
-
return new Error(`Data validation failed. Check constraint violated. ${detail || ''}`);
|
|
52
|
-
case '22001':
|
|
53
|
-
return new Error(`Data too long for field. Check string lengths. ${detail || ''}`);
|
|
54
|
-
case '22003':
|
|
55
|
-
return new Error(`Numeric value out of range. Check number values. ${detail || ''}`);
|
|
56
|
-
case '42703':
|
|
57
|
-
var _error_original4;
|
|
58
|
-
const undefinedColumn = (_error_original4 = error.original) === null || _error_original4 === void 0 ? void 0 : _error_original4.column;
|
|
59
|
-
if (undefinedColumn) {
|
|
60
|
-
return new Error(`Column '${undefinedColumn}' does not exist in table '${modelName}'`);
|
|
61
|
-
}
|
|
62
|
-
return new Error(`Referenced column does not exist`);
|
|
63
|
-
case '42P01':
|
|
64
|
-
return new Error(`Table '${modelName}' does not exist`);
|
|
65
|
-
default:
|
|
66
|
-
// For unknown errors, provide the original message with context
|
|
67
|
-
return new Error(`Database error in ${modelName}.create(): ${originalMessage}. Item data: ${JSON.stringify(itemData, null, 2)}`);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// Helper function to validate hierarchical chain exists
|
|
71
|
-
async function validateHierarchicalChain(models, locKey, kta) {
|
|
72
|
-
try {
|
|
73
|
-
// Find the direct parent model that contains this locator
|
|
74
|
-
const locatorIndex = kta.indexOf(locKey.kt);
|
|
75
|
-
if (locatorIndex === -1) {
|
|
76
|
-
throw new Error(`Locator type '${locKey.kt}' not found in kta array`);
|
|
77
|
-
}
|
|
78
|
-
// Get the model for this locator
|
|
79
|
-
const locatorModel = models[locatorIndex] || models[0]; // Fallback to primary model
|
|
80
|
-
// Build a query to validate the chain exists
|
|
81
|
-
const chainResult = relationshipUtils.buildRelationshipChain(locatorModel, kta, locatorIndex, kta.length - 1);
|
|
82
|
-
if (!chainResult.success) {
|
|
83
|
-
// If we can't build a chain, just validate the record exists
|
|
84
|
-
const record = await locatorModel.findByPk(locKey.lk);
|
|
85
|
-
if (!record) {
|
|
86
|
-
throw new Error(`Referenced ${locKey.kt} with id ${locKey.lk} does not exist`);
|
|
87
|
-
}
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
// Validate that the chain exists
|
|
91
|
-
const queryOptions = {
|
|
92
|
-
where: {
|
|
93
|
-
id: locKey.lk
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
if (chainResult.includes && chainResult.includes.length > 0) {
|
|
97
|
-
queryOptions.include = chainResult.includes;
|
|
98
|
-
}
|
|
99
|
-
const record = await locatorModel.findOne(queryOptions);
|
|
100
|
-
if (!record) {
|
|
101
|
-
throw new Error(`Referenced ${locKey.kt} with id ${locKey.lk} does not exist or chain is invalid`);
|
|
102
|
-
}
|
|
103
|
-
} catch (error) {
|
|
104
|
-
// Add context to validation errors
|
|
105
|
-
if (error.original) {
|
|
106
|
-
throw translateDatabaseError(error, {
|
|
107
|
-
locKey,
|
|
108
|
-
kta
|
|
109
|
-
}, locKey.kt);
|
|
110
|
-
}
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const getCreateOperation = (models, definition, registry)=>{
|
|
115
|
-
const create = async (item, options)=>{
|
|
116
|
-
logger.debug(`CREATE operation called on ${models[0].name} with ${(options === null || options === void 0 ? void 0 : options.key) ? `key: pk=${options.key.pk}, loc=[${core.isComKey(options.key) ? options.key.loc.map((l)=>`${l.kt}=${l.lk}`).join(', ') : ''}]` : (options === null || options === void 0 ? void 0 : options.locations) ? `locations: ${options.locations.map((loc)=>`${loc.kt}=${loc.lk}`).join(', ')}` : 'no constraints'}`);
|
|
117
|
-
logger.default(`Create configured for ${models[0].name} with ${Object.keys(item).length} item fields`);
|
|
118
|
-
const { coordinate, options: { references, aggregations } } = definition;
|
|
119
|
-
const { kta } = coordinate;
|
|
120
|
-
// Get the primary model (first model in array)
|
|
121
|
-
const model = models[0];
|
|
122
|
-
const modelAttributes = model.getAttributes();
|
|
123
|
-
// Validate that all item attributes exist on the model
|
|
124
|
-
let itemData = {
|
|
125
|
-
...item
|
|
126
|
-
};
|
|
127
|
-
// TODO: We need the opposite of processRow, something to step down from fjell to database.
|
|
128
|
-
itemData = EventCoordinator.extractEvents(itemData);
|
|
129
|
-
itemData = EventCoordinator.removeEvents(itemData);
|
|
130
|
-
// Validate that all item attributes exist on the model
|
|
131
|
-
const invalidAttributes = [];
|
|
132
|
-
for (const key of Object.keys(itemData)){
|
|
133
|
-
if (!modelAttributes[key]) {
|
|
134
|
-
invalidAttributes.push(key);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (invalidAttributes.length > 0) {
|
|
138
|
-
const availableAttributes = Object.keys(modelAttributes).join(', ');
|
|
139
|
-
throw new Error(`Invalid attributes for model '${model.name}': [${invalidAttributes.join(', ')}]. ` + `Available attributes: [${availableAttributes}]. ` + `Item data: ${JSON.stringify(itemData, null, 2)}`);
|
|
140
|
-
}
|
|
141
|
-
// Handle key options
|
|
142
|
-
// If a key is supplied, assume its contents are to be assigned to the appropriate ids.
|
|
143
|
-
// For most cases this will be null as key generation is often through autoIncrement.
|
|
144
|
-
// If this is a CItem then the locations will be present.
|
|
145
|
-
if (options === null || options === void 0 ? void 0 : options.key) {
|
|
146
|
-
const key = options.key;
|
|
147
|
-
if (core.isPriKey(key)) {
|
|
148
|
-
// Set the primary key
|
|
149
|
-
itemData.id = key.pk;
|
|
150
|
-
} else if (core.isComKey(key)) {
|
|
151
|
-
// Set primary key
|
|
152
|
-
itemData.id = key.pk;
|
|
153
|
-
// Process location keys - only set direct foreign keys, validate hierarchical chains
|
|
154
|
-
const comKey = key;
|
|
155
|
-
const directLocations = [];
|
|
156
|
-
const hierarchicalLocations = [];
|
|
157
|
-
// Categorize location keys as direct or hierarchical
|
|
158
|
-
for (const locKey of comKey.loc){
|
|
159
|
-
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locKey.kt, kta, true);
|
|
160
|
-
if (!relationshipInfo.found) {
|
|
161
|
-
const associations = model.associations ? Object.keys(model.associations) : [];
|
|
162
|
-
const errorMessage = `Composite key locator '${locKey.kt}' cannot be resolved on model '${model.name}' or through its relationships. ` + `Available associations: [${associations.join(', ')}]. ` + `KTA: [${kta.join(', ')}]. ` + `Composite key: ${JSON.stringify(comKey, null, 2)}`;
|
|
163
|
-
logger.error(errorMessage, {
|
|
164
|
-
key: comKey,
|
|
165
|
-
kta,
|
|
166
|
-
associations
|
|
167
|
-
});
|
|
168
|
-
throw new Error(errorMessage);
|
|
169
|
-
}
|
|
170
|
-
if (relationshipInfo.isDirect) {
|
|
171
|
-
directLocations.push(locKey);
|
|
172
|
-
} else {
|
|
173
|
-
hierarchicalLocations.push(locKey);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
// Set direct foreign keys
|
|
177
|
-
for (const locKey of directLocations){
|
|
178
|
-
if (locKey.lk == null || locKey.lk === '') {
|
|
179
|
-
logger.error(`Composite key location '${locKey.kt}' has undefined/null lk value`, {
|
|
180
|
-
locKey,
|
|
181
|
-
key: comKey
|
|
182
|
-
});
|
|
183
|
-
throw new Error(`Composite key location '${locKey.kt}' has undefined/null lk value`);
|
|
184
|
-
}
|
|
185
|
-
const foreignKeyField = locKey.kt + 'Id';
|
|
186
|
-
itemData[foreignKeyField] = locKey.lk;
|
|
187
|
-
}
|
|
188
|
-
// Validate hierarchical chains exist
|
|
189
|
-
for (const locKey of hierarchicalLocations){
|
|
190
|
-
await validateHierarchicalChain(models, locKey, kta);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
// Handle locations options
|
|
195
|
-
// This is the most frequent way relationship ids will be set
|
|
196
|
-
if (options === null || options === void 0 ? void 0 : options.locations) {
|
|
197
|
-
const directLocations = [];
|
|
198
|
-
const hierarchicalLocations = [];
|
|
199
|
-
// Categorize location keys as direct or hierarchical
|
|
200
|
-
for (const locKey of options.locations){
|
|
201
|
-
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locKey.kt, kta, true);
|
|
202
|
-
if (!relationshipInfo.found) {
|
|
203
|
-
const associations = model.associations ? Object.keys(model.associations) : [];
|
|
204
|
-
const errorMessage = `Location key '${locKey.kt}' cannot be resolved on model '${model.name}' or through its relationships. ` + `Available associations: [${associations.join(', ')}]. ` + `KTA: [${kta.join(', ')}]. ` + `Locations: ${JSON.stringify(options.locations, null, 2)}`;
|
|
205
|
-
logger.error(errorMessage, {
|
|
206
|
-
locations: options.locations,
|
|
207
|
-
kta,
|
|
208
|
-
associations
|
|
209
|
-
});
|
|
210
|
-
throw new Error(errorMessage);
|
|
211
|
-
}
|
|
212
|
-
if (relationshipInfo.isDirect) {
|
|
213
|
-
directLocations.push(locKey);
|
|
214
|
-
} else {
|
|
215
|
-
hierarchicalLocations.push(locKey);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
// Set direct foreign keys
|
|
219
|
-
for (const locKey of directLocations){
|
|
220
|
-
if (locKey.lk == null || locKey.lk === '') {
|
|
221
|
-
logger.error(`Location option '${locKey.kt}' has undefined/null lk value`, {
|
|
222
|
-
locKey,
|
|
223
|
-
locations: options.locations
|
|
224
|
-
});
|
|
225
|
-
throw new Error(`Location option '${locKey.kt}' has undefined/null lk value`);
|
|
226
|
-
}
|
|
227
|
-
const foreignKeyField = locKey.kt + 'Id';
|
|
228
|
-
itemData[foreignKeyField] = locKey.lk;
|
|
229
|
-
}
|
|
230
|
-
// Validate hierarchical chains exist
|
|
231
|
-
for (const locKey of hierarchicalLocations){
|
|
232
|
-
await validateHierarchicalChain(models, locKey, kta);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
// Create the record
|
|
236
|
-
try {
|
|
237
|
-
logger.trace(`[CREATE] Executing ${model.name}.create() with data: ${general.stringifyJSON(itemData)}`);
|
|
238
|
-
const createdRecord = await model.create(itemData);
|
|
239
|
-
// Add key and events
|
|
240
|
-
const processedRecord = await RowProcessor.processRow(createdRecord, kta, references, aggregations, registry);
|
|
241
|
-
const result = core.validateKeys(processedRecord, kta);
|
|
242
|
-
logger.debug(`[CREATE] Created ${model.name} with key: ${result.key ? JSON.stringify(result.key) : `id=${createdRecord.id}`}`);
|
|
243
|
-
return result;
|
|
244
|
-
} catch (error) {
|
|
245
|
-
throw translateDatabaseError(error, itemData, model.name);
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
return create;
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
exports.getCreateOperation = getCreateOperation;
|
|
252
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3JlYXRlLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
package/dist/cjs/ops/find.cjs
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
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 general = require('../util/general.cjs');
|
|
9
|
-
|
|
10
|
-
const logger = logger$1.default.get('sequelize', 'ops', 'find');
|
|
11
|
-
const getFindOperation = (models, definition, registry)=>{
|
|
12
|
-
const { options: { finders, references, aggregations } } = definition;
|
|
13
|
-
const find = async (finder, finderParams, locations)=>{
|
|
14
|
-
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'}`);
|
|
15
|
-
logger.default(`Find configured for ${models[0].name} using finder '${finder}' with ${Object.keys(finderParams).length} params`);
|
|
16
|
-
// Note that we execute the createFinders function here because we want to make sure we're always getting the
|
|
17
|
-
// most up to date methods.
|
|
18
|
-
if (finders && finders[finder]) {
|
|
19
|
-
const finderMethod = finders[finder];
|
|
20
|
-
if (finderMethod) {
|
|
21
|
-
logger.trace(`[FIND] Executing finder '${finder}' on ${models[0].name} with params: ${general.stringifyJSON(finderParams)}, locations: ${general.stringifyJSON(locations)}`);
|
|
22
|
-
const results = await finderMethod(finderParams, locations);
|
|
23
|
-
if (results && results.length > 0) {
|
|
24
|
-
const processedResults = await Promise.all(results.map(async (row)=>{
|
|
25
|
-
const processedRow = await RowProcessor.processRow(row, definition.coordinate.kta, references, aggregations, registry);
|
|
26
|
-
return core.validateKeys(processedRow, definition.coordinate.kta);
|
|
27
|
-
}));
|
|
28
|
-
logger.debug(`[FIND] Found ${processedResults.length} ${models[0].name} records using finder '${finder}'`);
|
|
29
|
-
return processedResults;
|
|
30
|
-
} else {
|
|
31
|
-
logger.debug(`[FIND] Found 0 ${models[0].name} records using finder '${finder}'`);
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
logger.error(`Finder %s not found`, finder);
|
|
36
|
-
throw new Error(`Finder ${finder} not found`);
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
logger.error(`No finders have been defined for this lib`);
|
|
40
|
-
throw new Error(`No finders found`);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
return find;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
exports.getFindOperation = getFindOperation;
|
|
47
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmluZC5janMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
package/dist/cjs/ops/get.cjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
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
|
-
const OperationContext = require('../OperationContext.cjs');
|
|
11
|
-
const general = require('../util/general.cjs');
|
|
12
|
-
|
|
13
|
-
const logger = logger$1.default.get('sequelize', 'ops', 'get');
|
|
14
|
-
// Helper function to process composite key and build query options
|
|
15
|
-
const processCompositeKey = (comKey, model, kta)=>{
|
|
16
|
-
const where = {
|
|
17
|
-
id: comKey.pk
|
|
18
|
-
};
|
|
19
|
-
const includes = [];
|
|
20
|
-
for (const locator of comKey.loc){
|
|
21
|
-
const relationshipInfo = relationshipUtils.buildRelationshipPath(model, locator.kt, kta);
|
|
22
|
-
if (!relationshipInfo.found) {
|
|
23
|
-
const errorMessage = `Composite key locator '${locator.kt}' cannot be resolved on model '${model.name}' or through its relationships.`;
|
|
24
|
-
logger.error(errorMessage, {
|
|
25
|
-
key: comKey,
|
|
26
|
-
kta
|
|
27
|
-
});
|
|
28
|
-
throw new Error(errorMessage);
|
|
29
|
-
}
|
|
30
|
-
if (relationshipInfo.path) {
|
|
31
|
-
// This requires a relationship traversal
|
|
32
|
-
where[relationshipInfo.path] = locator.lk;
|
|
33
|
-
if (relationshipInfo.includes) {
|
|
34
|
-
includes.push(...relationshipInfo.includes);
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
// This is a direct field
|
|
38
|
-
const fieldName = `${locator.kt}Id`;
|
|
39
|
-
where[fieldName] = locator.lk;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const result = {
|
|
43
|
-
where
|
|
44
|
-
};
|
|
45
|
-
if (includes.length > 0) {
|
|
46
|
-
result.include = includes;
|
|
47
|
-
}
|
|
48
|
-
return result;
|
|
49
|
-
};
|
|
50
|
-
const getGetOperation = (models, definition, registry)=>{
|
|
51
|
-
const { coordinate, options: { references, aggregations } } = definition;
|
|
52
|
-
const { kta } = coordinate;
|
|
53
|
-
const get = async (key)=>{
|
|
54
|
-
if (!core.isValidItemKey(key)) {
|
|
55
|
-
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
56
|
-
throw new Error('Key for Get is not a valid ItemKey');
|
|
57
|
-
}
|
|
58
|
-
logger.debug(`GET operation called on ${models[0].name} with ${core.isPriKey(key) ? `primary key: pk=${key.pk}` : `composite key: pk=${key.pk}, loc=[${key.loc.map((l)=>`${l.kt}=${l.lk}`).join(', ')}]`}`);
|
|
59
|
-
logger.default(`Get configured for ${models[0].name} with ${core.isPriKey(key) ? 'primary' : 'composite'} key`);
|
|
60
|
-
const itemKey = key;
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
const model = models[0];
|
|
63
|
-
let item;
|
|
64
|
-
if (core.isPriKey(itemKey)) {
|
|
65
|
-
// This is the easy case because we can just find the item by its primary key
|
|
66
|
-
logger.trace(`[GET] Executing ${model.name}.findByPk() with pk: ${itemKey.pk}`);
|
|
67
|
-
item = await model.findByPk(itemKey.pk);
|
|
68
|
-
} else if (core.isComKey(itemKey)) {
|
|
69
|
-
// This is a composite key, so we need to build a where clause based on the composite key's locators
|
|
70
|
-
const comKey = itemKey;
|
|
71
|
-
const queryOptions = processCompositeKey(comKey, model, kta);
|
|
72
|
-
logger.default('Composite key query', {
|
|
73
|
-
queryOptions
|
|
74
|
-
});
|
|
75
|
-
logger.trace(`[GET] Executing ${model.name}.findOne() with options: ${general.stringifyJSON(queryOptions)}`);
|
|
76
|
-
item = await model.findOne(queryOptions);
|
|
77
|
-
}
|
|
78
|
-
if (!item) {
|
|
79
|
-
throw new Library.NotFoundError('get', coordinate, key);
|
|
80
|
-
} else {
|
|
81
|
-
// Get the current context from context manager
|
|
82
|
-
const context = OperationContext.contextManager.getCurrentContext();
|
|
83
|
-
const result = core.validateKeys(await RowProcessor.processRow(item, kta, references, aggregations, registry, context), kta);
|
|
84
|
-
logger.debug(`[GET] Retrieved ${model.name} with key: ${result.key ? JSON.stringify(result.key) : `id=${item.id}`}`);
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
return get;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
exports.getGetOperation = getGetOperation;
|
|
92
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2V0LmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
package/dist/cjs/ops/one.cjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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.debug(`ONE operation called on ${models[0].name} with ${locations.length} location filters: ${locations.map((loc)=>`${loc.kt}=${loc.lk}`).join(', ') || 'none'}`);
|
|
12
|
-
logger.default(`One configured for ${models[0].name} delegating to all operation`);
|
|
13
|
-
const items = await all.getAllOperation(models, definition, registry)(itemQuery, locations);
|
|
14
|
-
if (items.length > 0) {
|
|
15
|
-
const result = items[0];
|
|
16
|
-
logger.debug(`[ONE] Found ${models[0].name} record with key: ${result.key ? JSON.stringify(result.key) : 'unknown'}`);
|
|
17
|
-
return result;
|
|
18
|
-
} else {
|
|
19
|
-
logger.debug(`[ONE] No ${models[0].name} record found`);
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
return one;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
exports.getOneOperation = getOneOperation;
|
|
27
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib25lLmNqcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|