@fjell/lib-sequelize 4.3.4 → 4.4.1
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 +35 -0
- package/dist/cjs/Instance.cjs +40 -0
- package/dist/cjs/KeyMaster.cjs +91 -0
- package/dist/cjs/Operations.cjs +29 -0
- package/dist/cjs/Options.cjs +39 -0
- package/dist/cjs/QueryBuilder.cjs +188 -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 +23 -0
- package/dist/cjs/ops/find.cjs +45 -0
- package/dist/cjs/ops/get.cjs +49 -0
- package/dist/cjs/ops/one.cjs +26 -0
- package/dist/cjs/ops/remove.cjs +72 -0
- package/dist/cjs/ops/update.cjs +59 -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/es/AggregationBuilder.js +27 -0
- package/dist/es/Coordinate.js +13 -0
- package/dist/es/Definition.js +23 -0
- package/dist/es/EventCoordinator.js +30 -0
- package/dist/es/Instance.js +17 -0
- package/dist/es/KeyMaster.js +86 -0
- package/dist/es/Operations.js +25 -0
- package/dist/es/Options.js +16 -0
- package/dist/{src → es}/QueryBuilder.js +67 -46
- package/dist/es/ReferenceBuilder.js +30 -0
- package/dist/es/RowProcessor.js +37 -0
- package/dist/es/contained/Instance.js +17 -0
- package/dist/es/contained/index.js +2 -0
- package/dist/es/index.js +10 -0
- package/dist/es/logger.js +6 -0
- package/dist/{src → es}/ops/all.js +25 -14
- package/dist/es/ops/create.js +19 -0
- package/dist/es/ops/find.js +41 -0
- package/dist/es/ops/get.js +45 -0
- package/dist/es/ops/one.js +22 -0
- package/dist/es/ops/remove.js +68 -0
- package/dist/es/ops/update.js +55 -0
- package/dist/es/primary/Instance.js +27 -0
- package/dist/es/primary/index.js +2 -0
- package/dist/es/util/general.js +47 -0
- package/dist/index.cjs +810 -0
- package/dist/index.cjs.map +1 -0
- package/dist/types/AggregationBuilder.d.ts +4 -0
- package/dist/{src → types}/Definition.d.ts +2 -2
- package/dist/{src → types}/Instance.d.ts +2 -2
- package/dist/{src → types}/KeyMaster.d.ts +1 -2
- package/dist/{src → types}/Operations.d.ts +4 -4
- package/dist/{src → types}/Options.d.ts +15 -4
- package/dist/types/ReferenceBuilder.d.ts +3 -0
- package/dist/types/RowProcessor.d.ts +5 -0
- package/dist/{src → types}/contained/Instance.d.ts +2 -1
- package/dist/types/logger.d.ts +2 -0
- package/dist/types/ops/all.d.ts +5 -0
- package/dist/{src → types}/ops/create.d.ts +5 -4
- package/dist/types/ops/find.d.ts +5 -0
- package/dist/{src → types}/ops/get.d.ts +2 -1
- package/dist/types/ops/one.d.ts +5 -0
- package/dist/types/ops/remove.d.ts +5 -0
- package/dist/types/ops/update.d.ts +5 -0
- package/dist/{src → types}/primary/Instance.d.ts +2 -1
- package/dist/types/util/general.d.ts +4 -0
- package/package.json +45 -35
- package/dist/src/Coordinate.js +0 -7
- package/dist/src/Coordinate.js.map +0 -1
- package/dist/src/Definition.js +0 -16
- package/dist/src/Definition.js.map +0 -1
- package/dist/src/EventCoordinator.js +0 -56
- package/dist/src/EventCoordinator.js.map +0 -1
- package/dist/src/Instance.js +0 -14
- package/dist/src/Instance.js.map +0 -1
- package/dist/src/KeyMaster.js +0 -58
- package/dist/src/KeyMaster.js.map +0 -1
- package/dist/src/Operations.js +0 -22
- package/dist/src/Operations.js.map +0 -1
- package/dist/src/Options.js +0 -16
- package/dist/src/Options.js.map +0 -1
- package/dist/src/QueryBuilder.js.map +0 -1
- package/dist/src/RowProcessor.d.ts +0 -3
- package/dist/src/RowProcessor.js +0 -11
- package/dist/src/RowProcessor.js.map +0 -1
- package/dist/src/contained/Instance.js +0 -14
- package/dist/src/contained/Instance.js.map +0 -1
- package/dist/src/contained/index.js +0 -2
- package/dist/src/contained/index.js.map +0 -1
- package/dist/src/index.js +0 -8
- package/dist/src/index.js.map +0 -1
- package/dist/src/logger.d.ts +0 -2
- package/dist/src/logger.js +0 -4
- package/dist/src/logger.js.map +0 -1
- package/dist/src/ops/all.d.ts +0 -4
- package/dist/src/ops/all.js.map +0 -1
- package/dist/src/ops/create.js +0 -14
- package/dist/src/ops/create.js.map +0 -1
- package/dist/src/ops/find.d.ts +0 -4
- package/dist/src/ops/find.js +0 -26
- package/dist/src/ops/find.js.map +0 -1
- package/dist/src/ops/get.js +0 -36
- package/dist/src/ops/get.js.map +0 -1
- package/dist/src/ops/one.d.ts +0 -4
- package/dist/src/ops/one.js +0 -17
- package/dist/src/ops/one.js.map +0 -1
- package/dist/src/ops/remove.d.ts +0 -5
- package/dist/src/ops/remove.js +0 -56
- package/dist/src/ops/remove.js.map +0 -1
- package/dist/src/ops/update.d.ts +0 -4
- package/dist/src/ops/update.js +0 -49
- package/dist/src/ops/update.js.map +0 -1
- package/dist/src/primary/Instance.js +0 -17
- package/dist/src/primary/Instance.js.map +0 -1
- package/dist/src/primary/index.js +0 -2
- package/dist/src/primary/index.js.map +0 -1
- package/eslint.config.mjs +0 -70
- package/src/Coordinate.ts +0 -16
- package/src/Definition.ts +0 -49
- package/src/EventCoordinator.ts +0 -103
- package/src/Instance.ts +0 -45
- 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 -45
- 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 -41
- package/src/primary/index.ts +0 -1
- /package/dist/{src → types}/Coordinate.d.ts +0 -0
- /package/dist/{src → types}/EventCoordinator.d.ts +0 -0
- /package/dist/{src → types}/QueryBuilder.d.ts +0 -0
- /package/dist/{src → types}/contained/index.d.ts +0 -0
- /package/dist/{src → types}/index.d.ts +0 -0
- /package/dist/{src → types}/primary/index.d.ts +0 -0
package/src/KeyMaster.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AllItemTypeArrays,
|
|
3
|
-
Item,
|
|
4
|
-
ItemProperties
|
|
5
|
-
} from '@fjell/core';
|
|
6
|
-
|
|
7
|
-
import LibLogger from '@/logger';
|
|
8
|
-
|
|
9
|
-
const logger = LibLogger.get('sequelize', 'KeyMaster');
|
|
10
|
-
|
|
11
|
-
export const removeKey = <
|
|
12
|
-
S extends string,
|
|
13
|
-
L1 extends string = never,
|
|
14
|
-
L2 extends string = never,
|
|
15
|
-
L3 extends string = never,
|
|
16
|
-
L4 extends string = never,
|
|
17
|
-
L5 extends string = never
|
|
18
|
-
>(
|
|
19
|
-
item: ItemProperties<S, L1, L2, L3, L4, L5>
|
|
20
|
-
): ItemProperties<S, L1, L2, L3, L4, L5> => {
|
|
21
|
-
logger.default('Removing Key', { item });
|
|
22
|
-
delete item.key;
|
|
23
|
-
return item;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const populateKey = <
|
|
27
|
-
S extends string,
|
|
28
|
-
L1 extends string = never,
|
|
29
|
-
L2 extends string = never,
|
|
30
|
-
L3 extends string = never,
|
|
31
|
-
L4 extends string = never,
|
|
32
|
-
L5 extends string = never
|
|
33
|
-
>(
|
|
34
|
-
item: ItemProperties<S, L1, L2, L3, L4, L5>,
|
|
35
|
-
keyTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>
|
|
36
|
-
): ItemProperties<S, L1, L2, L3, L4, L5> => {
|
|
37
|
-
if( keyTypes.length === 1 ) {
|
|
38
|
-
item.key = { kt: keyTypes[0], pk: item.id };
|
|
39
|
-
delete item.id;
|
|
40
|
-
} else if( keyTypes.length === 2 ) {
|
|
41
|
-
item.key = {
|
|
42
|
-
kt: keyTypes[0], pk: item.id,
|
|
43
|
-
// TODO: Shouldn't this be inspecting the model to get the primary key type?
|
|
44
|
-
loc: [{ kt: keyTypes[1], lk: item[keyTypes[1] + 'Id']}],
|
|
45
|
-
};
|
|
46
|
-
delete item.id;
|
|
47
|
-
delete item[keyTypes[1] + 'Id'];
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error('Not implemented');
|
|
50
|
-
}
|
|
51
|
-
return item;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const addKey = <
|
|
55
|
-
S extends string,
|
|
56
|
-
L1 extends string = never,
|
|
57
|
-
L2 extends string = never,
|
|
58
|
-
L3 extends string = never,
|
|
59
|
-
L4 extends string = never,
|
|
60
|
-
L5 extends string = never
|
|
61
|
-
>(
|
|
62
|
-
item: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
63
|
-
keyTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>
|
|
64
|
-
): void => {
|
|
65
|
-
logger.default('Adding Key', { item });
|
|
66
|
-
const key = {};
|
|
67
|
-
if (Array.isArray(keyTypes) && keyTypes.length > 1) {
|
|
68
|
-
const type = [...keyTypes];
|
|
69
|
-
const pkType = type.shift();
|
|
70
|
-
Object.assign(key, { kt: pkType, pk: item.id });
|
|
71
|
-
// TODO: This is really just for primary items
|
|
72
|
-
if (type.length === 1) {
|
|
73
|
-
// TODO: This should be looking at the model to get the primary key of the reference item or association
|
|
74
|
-
const locKeyTypeId = type[0] + 'Id';
|
|
75
|
-
Object.assign(key, { loc: [{ kt: type[0], lk: item[locKeyTypeId] }] });
|
|
76
|
-
} else if (type.length === 2) {
|
|
77
|
-
throw new Error('Not implemented');
|
|
78
|
-
} else if (type.length === 3) {
|
|
79
|
-
throw new Error('Not implemented');
|
|
80
|
-
} else if (type.length === 4) {
|
|
81
|
-
throw new Error('Not implemented');
|
|
82
|
-
} else if (type.length === 5) {
|
|
83
|
-
throw new Error('Not implemented');
|
|
84
|
-
}
|
|
85
|
-
} else {
|
|
86
|
-
Object.assign(key, { kt: keyTypes[0], pk: item.id });
|
|
87
|
-
}
|
|
88
|
-
Object.assign(item, { key });
|
|
89
|
-
};
|
|
90
|
-
|
package/src/Operations.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Item } from "@fjell/core";
|
|
2
|
-
|
|
3
|
-
import * as Library from "@fjell/lib";
|
|
4
|
-
import { getAllOperation } from "./ops/all";
|
|
5
|
-
import { getCreateOperation } from "./ops/create";
|
|
6
|
-
import { getFindOperation } from "./ops/find";
|
|
7
|
-
import { getGetOperation } from "./ops/get";
|
|
8
|
-
import { getOneOperation } from "./ops/one";
|
|
9
|
-
import { getRemoveOperation } from "./ops/remove";
|
|
10
|
-
import { getUpdateOperation } from "./ops/update";
|
|
11
|
-
import { ModelStatic } from "sequelize";
|
|
12
|
-
import { Definition } from "@fjell/lib";
|
|
13
|
-
|
|
14
|
-
export const createOperations = <
|
|
15
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
16
|
-
S extends string,
|
|
17
|
-
L1 extends string = never,
|
|
18
|
-
L2 extends string = never,
|
|
19
|
-
L3 extends string = never,
|
|
20
|
-
L4 extends string = never,
|
|
21
|
-
L5 extends string = never,
|
|
22
|
-
>(
|
|
23
|
-
models: Array<ModelStatic<any>>,
|
|
24
|
-
definition: Definition<V, S, L1, L2, L3, L4, L5>,
|
|
25
|
-
// eslint-disable-next-line max-params
|
|
26
|
-
): Library.Operations<V, S, L1, L2, L3, L4, L5> => {
|
|
27
|
-
|
|
28
|
-
const operations = {} as Library.Operations<V, S, L1, L2, L3, L4, L5>;
|
|
29
|
-
|
|
30
|
-
operations.all = getAllOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
31
|
-
operations.one = getOneOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
32
|
-
operations.create = getCreateOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
33
|
-
operations.update = getUpdateOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
34
|
-
operations.get = getGetOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
35
|
-
operations.remove = getRemoveOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
36
|
-
operations.find = getFindOperation<V, S, L1, L2, L3, L4, L5>(models, definition);
|
|
37
|
-
operations.upsert = () => {
|
|
38
|
-
throw new Error('Not implemented');
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return operations;
|
|
42
|
-
}
|
package/src/Options.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import * as Library from '@fjell/lib';
|
|
2
|
-
import { Item } from '@fjell/core';
|
|
3
|
-
import deepmerge from 'deepmerge';
|
|
4
|
-
|
|
5
|
-
export interface Options<
|
|
6
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
7
|
-
S extends string,
|
|
8
|
-
L1 extends string = never,
|
|
9
|
-
L2 extends string = never,
|
|
10
|
-
L3 extends string = never,
|
|
11
|
-
L4 extends string = never,
|
|
12
|
-
L5 extends string = never
|
|
13
|
-
> extends Library.Options<V, S, L1, L2, L3, L4, L5> {
|
|
14
|
-
deleteOnRemove?: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const defaultOptions: Options<any, any, any, any, any, any, any> = {
|
|
18
|
-
deleteOnRemove: false,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const createOptions = <
|
|
22
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
23
|
-
S extends string,
|
|
24
|
-
L1 extends string = never,
|
|
25
|
-
L2 extends string = never,
|
|
26
|
-
L3 extends string = never,
|
|
27
|
-
L4 extends string = never,
|
|
28
|
-
L5 extends string = never
|
|
29
|
-
>(libOptions?: Options<V, S, L1, L2, L3, L4, L5>): Options<V, S, L1, L2, L3, L4, L5> => {
|
|
30
|
-
const defaultOptions = {
|
|
31
|
-
deleteOnRemove: false,
|
|
32
|
-
} as Options<V, S, L1, L2, L3, L4, L5>;
|
|
33
|
-
|
|
34
|
-
const options = Library.createOptions(libOptions);
|
|
35
|
-
|
|
36
|
-
const mergedOptions = deepmerge(defaultOptions, options);
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
...mergedOptions,
|
|
40
|
-
} as Options<V, S, L1, L2, L3, L4, L5>;
|
|
41
|
-
}
|
package/src/QueryBuilder.ts
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CompoundCondition,
|
|
3
|
-
Condition,
|
|
4
|
-
EventQuery,
|
|
5
|
-
isComKey,
|
|
6
|
-
isCondition,
|
|
7
|
-
isPriKey,
|
|
8
|
-
ItemQuery,
|
|
9
|
-
OrderBy,
|
|
10
|
-
PriKey,
|
|
11
|
-
References
|
|
12
|
-
} from '@fjell/core';
|
|
13
|
-
|
|
14
|
-
import { ModelStatic, Op } from 'sequelize';
|
|
15
|
-
import LibLogger from '@/logger';
|
|
16
|
-
|
|
17
|
-
const logger = LibLogger.get('sequelize', 'QueryBuilder');
|
|
18
|
-
|
|
19
|
-
export type QueryOptions = {
|
|
20
|
-
where: Record<string, any>;
|
|
21
|
-
limit?: number;
|
|
22
|
-
offset?: number;
|
|
23
|
-
order?: Array<[string, string]>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const addDeleteQuery = (options: QueryOptions, model: ModelStatic<any>): QueryOptions => {
|
|
27
|
-
logger.default('Adding Delete Query', { options });
|
|
28
|
-
if(model.getAttributes().deletedAt) {
|
|
29
|
-
options.where['deletedAt'] = {
|
|
30
|
-
[Op.eq]: null
|
|
31
|
-
}
|
|
32
|
-
} else if(model.getAttributes().isDeleted) {
|
|
33
|
-
options.where['isDeleted'] = {
|
|
34
|
-
[Op.eq]: false
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return options;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const addEventQueries = (
|
|
42
|
-
options: QueryOptions, events: Record<string, EventQuery>, model: ModelStatic<any>): QueryOptions => {
|
|
43
|
-
logger.default('Adding Event Queries', { options, events });
|
|
44
|
-
Object.keys(events).forEach((key: string) => {
|
|
45
|
-
|
|
46
|
-
if(!model.getAttributes()[`${key}At`]) {
|
|
47
|
-
throw new Error(`Event ${key} is not supported on this model, column ${key}At not found`);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let whereClauses = {};
|
|
51
|
-
|
|
52
|
-
const event = events[key];
|
|
53
|
-
if (event.start) {
|
|
54
|
-
whereClauses = { ...whereClauses, [Op.gte]: new Date(event.start) };
|
|
55
|
-
}
|
|
56
|
-
if (event.end) {
|
|
57
|
-
whereClauses = { ...whereClauses, [Op.lt]: new Date(event.end) };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if(event.by) {
|
|
61
|
-
if(!model.getAttributes()[`${key}By`]) {
|
|
62
|
-
throw new Error(`Event ${key} is not supported on this model, column ${key}By not found`);
|
|
63
|
-
}
|
|
64
|
-
whereClauses = { ...whereClauses, [Op.eq]: event.by };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
options.where[`${key}At`] = whereClauses;
|
|
68
|
-
|
|
69
|
-
});
|
|
70
|
-
return options;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Add the references to the query
|
|
74
|
-
const addReferenceQueries = (options: any, references: References, model: ModelStatic<any>): any => {
|
|
75
|
-
logger.default('Adding Reference Queries', { options, references });
|
|
76
|
-
|
|
77
|
-
Object.keys(references).forEach((key: string) => {
|
|
78
|
-
logger.default('Adding Reference Query', { key, references });
|
|
79
|
-
|
|
80
|
-
if(!model.getAttributes()[`${key}Id`]) {
|
|
81
|
-
throw new Error(`Reference ${key} is not supported on this model, column ${key}Id not found`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (isPriKey(references[key])) {
|
|
85
|
-
const priKey: PriKey<string> = references[key] as PriKey<string>;
|
|
86
|
-
|
|
87
|
-
options.where[`${key}Id`] = {
|
|
88
|
-
[Op.eq]: priKey.pk
|
|
89
|
-
}
|
|
90
|
-
} else if (isComKey(references[key])) {
|
|
91
|
-
throw new Error('ComKeys are not supported in Sequelize');
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
return options;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export const addCompoundCondition = (options: any, compoundCondition: CompoundCondition, model: ModelStatic<any>) => {
|
|
98
|
-
const where: Record<symbol, any> = {};
|
|
99
|
-
|
|
100
|
-
let compoundOp: symbol;
|
|
101
|
-
const compoundType = compoundCondition.compoundType;
|
|
102
|
-
if( compoundType === "AND" ) {
|
|
103
|
-
compoundOp = Op.and;
|
|
104
|
-
} else {
|
|
105
|
-
compoundOp = Op.or;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
let conditions: Record<symbol, any> = {};
|
|
109
|
-
compoundCondition.conditions.forEach((condition: Condition | CompoundCondition) => {
|
|
110
|
-
if(isCondition(condition)) {
|
|
111
|
-
conditions = addCondition(conditions,condition, model);
|
|
112
|
-
} else {
|
|
113
|
-
throw new Error('Nest Compound conditions not supported');
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
where[compoundOp] = conditions;
|
|
118
|
-
options.where = where;
|
|
119
|
-
|
|
120
|
-
return options;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export const addCondition = (conditions: Record<string, any>, condition: Condition, model: ModelStatic<any>) => {
|
|
124
|
-
let conditionOp: symbol;
|
|
125
|
-
const conditionColumn: string = condition.column;
|
|
126
|
-
|
|
127
|
-
if(!model.getAttributes()[conditionColumn]) {
|
|
128
|
-
throw new Error(`Condition column ${conditionColumn} not found on model ${model.name}`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if(condition.operator === '==') {
|
|
132
|
-
conditionOp = Op.eq;
|
|
133
|
-
} else if(condition.operator === '<') {
|
|
134
|
-
conditionOp = Op.lt;
|
|
135
|
-
} else if(condition.operator === '>') {
|
|
136
|
-
conditionOp = Op.gt;
|
|
137
|
-
} else if(condition.operator === '<=') {
|
|
138
|
-
conditionOp = Op.lte;
|
|
139
|
-
} else if(condition.operator === '>=') {
|
|
140
|
-
conditionOp = Op.gte;
|
|
141
|
-
} else if(condition.operator === 'in') {
|
|
142
|
-
conditionOp = Op.in;
|
|
143
|
-
} else {
|
|
144
|
-
throw new Error(`Operator ${condition.operator} not supported`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
conditions[conditionColumn] = {
|
|
148
|
-
[conditionOp]: condition.value
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return conditions;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export const buildQuery = (
|
|
155
|
-
itemQuery: ItemQuery,
|
|
156
|
-
model: ModelStatic<any>
|
|
157
|
-
): any => {
|
|
158
|
-
logger.default('build', { itemQuery });
|
|
159
|
-
|
|
160
|
-
let options: any = {
|
|
161
|
-
where: {},
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
if (itemQuery.compoundCondition) {
|
|
165
|
-
logger.default('Adding Conditions', { compoundCondition: itemQuery.compoundCondition });
|
|
166
|
-
options = addCompoundCondition(options, itemQuery.compoundCondition, model);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// If the model has a deletedAt column, we need to add a delete query
|
|
170
|
-
if(model.getAttributes().deletedAt || model.getAttributes().isDeleted) {
|
|
171
|
-
options = addDeleteQuery(options, model);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (itemQuery.refs) {
|
|
175
|
-
options = addReferenceQueries(options, itemQuery.refs, model);
|
|
176
|
-
}
|
|
177
|
-
if (itemQuery.events) {
|
|
178
|
-
options = addEventQueries(options, itemQuery.events, model);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// TODO: Once we start to support Aggs on the server-side, we'll need to parse agg queries
|
|
182
|
-
|
|
183
|
-
// Apply a limit to the result set
|
|
184
|
-
if (itemQuery.limit) {
|
|
185
|
-
logger.default('Limiting to', { limit: itemQuery.limit });
|
|
186
|
-
options.limit = itemQuery.limit;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Apply an offset to the result set
|
|
190
|
-
if (itemQuery.offset) {
|
|
191
|
-
options.offset = itemQuery.offset;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Add orderBy to the query
|
|
195
|
-
if (itemQuery.orderBy) {
|
|
196
|
-
itemQuery.orderBy.forEach((orderBy: OrderBy) => {
|
|
197
|
-
if(!model.getAttributes()[orderBy.field]) {
|
|
198
|
-
throw new Error(`Order by field ${orderBy.field} not found on model ${model.name}`);
|
|
199
|
-
}
|
|
200
|
-
options.order = [
|
|
201
|
-
[orderBy.field, orderBy.direction]
|
|
202
|
-
];
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return options;
|
|
207
|
-
}
|
|
208
|
-
|
package/src/RowProcessor.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { AllItemTypeArrays, Item } from "@fjell/core";
|
|
2
|
-
import { Model } from "sequelize";
|
|
3
|
-
|
|
4
|
-
import LibLogger from "@/logger";
|
|
5
|
-
import { addKey } from "./KeyMaster";
|
|
6
|
-
|
|
7
|
-
const logger = LibLogger.get('sequelize', 'RowProcessor');
|
|
8
|
-
|
|
9
|
-
export const processRow = <S extends string,
|
|
10
|
-
L1 extends string = never,
|
|
11
|
-
L2 extends string = never,
|
|
12
|
-
L3 extends string = never,
|
|
13
|
-
L4 extends string = never,
|
|
14
|
-
L5 extends string = never>(
|
|
15
|
-
row: Model<any, any>,
|
|
16
|
-
keyTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>
|
|
17
|
-
): Item<S, L1, L2, L3, L4, L5> => {
|
|
18
|
-
logger.default('Processing Row', { row });
|
|
19
|
-
const item = row.get({ plain: true }) as any;
|
|
20
|
-
addKey(item, keyTypes);
|
|
21
|
-
logger.default('Processed Row: ' + JSON.stringify(item));
|
|
22
|
-
return item as Item<S, L1, L2, L3, L4, L5>;
|
|
23
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Instance as AbstractSequelizeInstance } from '@/Instance';
|
|
2
|
-
import { Item, ItemTypeArray } from '@fjell/core';
|
|
3
|
-
import { Contained } from '@fjell/lib';
|
|
4
|
-
import { createDefinition } from '@/Definition';
|
|
5
|
-
import { createOperations } from '@/Operations';
|
|
6
|
-
import { ModelStatic } from 'sequelize';
|
|
7
|
-
|
|
8
|
-
export interface Instance<
|
|
9
|
-
V extends Item<S>,
|
|
10
|
-
S extends string,
|
|
11
|
-
L1 extends string = never,
|
|
12
|
-
L2 extends string = never,
|
|
13
|
-
L3 extends string = never,
|
|
14
|
-
L4 extends string = never,
|
|
15
|
-
L5 extends string = never
|
|
16
|
-
> extends AbstractSequelizeInstance<V, S, L1, L2, L3, L4, L5> {
|
|
17
|
-
models: ModelStatic<any>[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// eslint-disable-next-line max-params
|
|
21
|
-
export function createInstance<
|
|
22
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
23
|
-
S extends string,
|
|
24
|
-
L1 extends string = never,
|
|
25
|
-
L2 extends string = never,
|
|
26
|
-
L3 extends string = never,
|
|
27
|
-
L4 extends string = never,
|
|
28
|
-
L5 extends string = never
|
|
29
|
-
>(
|
|
30
|
-
keyTypes: ItemTypeArray<S, L1, L2, L3, L4, L5>,
|
|
31
|
-
models: ModelStatic<any>[],
|
|
32
|
-
libOptions: Contained.Options<V, S, L1, L2, L3, L4, L5> = {},
|
|
33
|
-
scopes: string[] = [],
|
|
34
|
-
): Instance<V, S, L1, L2, L3, L4, L5> {
|
|
35
|
-
|
|
36
|
-
const definition = createDefinition(keyTypes, scopes, libOptions);
|
|
37
|
-
const operations = createOperations(models, definition);
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
definition,
|
|
41
|
-
operations: Contained.wrapOperations(operations, definition),
|
|
42
|
-
models
|
|
43
|
-
} as Instance<V, S, L1, L2, L3, L4, L5>;
|
|
44
|
-
|
|
45
|
-
}
|
package/src/contained/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Instance';
|
package/src/index.ts
DELETED
package/src/logger.ts
DELETED
package/src/ops/all.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { validateKeys } from "@fjell/core";
|
|
2
|
-
|
|
3
|
-
import { buildQuery } from "@/QueryBuilder";
|
|
4
|
-
|
|
5
|
-
import { Definition } from "@/Definition";
|
|
6
|
-
import LibLogger from '@/logger';
|
|
7
|
-
import { processRow } from "@/RowProcessor";
|
|
8
|
-
import { Item, ItemQuery, LocKeyArray } from "@fjell/core";
|
|
9
|
-
import { Association, ModelStatic, Op } from "sequelize";
|
|
10
|
-
|
|
11
|
-
const logger = LibLogger.get('sequelize', 'ops', 'all');
|
|
12
|
-
|
|
13
|
-
export const getAllOperation = <
|
|
14
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
15
|
-
S extends string,
|
|
16
|
-
L1 extends string = never,
|
|
17
|
-
L2 extends string = never,
|
|
18
|
-
L3 extends string = never,
|
|
19
|
-
L4 extends string = never,
|
|
20
|
-
L5 extends string = never
|
|
21
|
-
>(models: ModelStatic<any>[], definition: Definition<V, S, L1, L2, L3, L4, L5>) => {
|
|
22
|
-
|
|
23
|
-
const { coordinate } = definition;
|
|
24
|
-
|
|
25
|
-
//#region Query
|
|
26
|
-
const all = async (
|
|
27
|
-
itemQuery: ItemQuery,
|
|
28
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | [] | undefined,
|
|
29
|
-
// eslint-disable-next-line max-params
|
|
30
|
-
): Promise<V[]> => {
|
|
31
|
-
logger.default('All', { itemQuery, locations });
|
|
32
|
-
const loc: LocKeyArray<L1, L2, L3, L4, L5> | [] = locations || [];
|
|
33
|
-
|
|
34
|
-
// SQ Libs don't support locations
|
|
35
|
-
if( loc.length > 1 ) {
|
|
36
|
-
throw new Error('Not implemented for more than one location key');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// We have the model here?
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
const model = models[0];
|
|
42
|
-
|
|
43
|
-
// We have the model here?
|
|
44
|
-
const options = buildQuery(itemQuery, model);
|
|
45
|
-
|
|
46
|
-
// If this has a location array, we need to add a where clause
|
|
47
|
-
if( loc.length === 1 ) {
|
|
48
|
-
const locKeyType = loc[0].kt;
|
|
49
|
-
if( model.associations[locKeyType] ) {
|
|
50
|
-
const association: Association<any, any> = model.associations[locKeyType];
|
|
51
|
-
options.where = {
|
|
52
|
-
...options.where,
|
|
53
|
-
[association.foreignKey]: {
|
|
54
|
-
[Op.eq]: loc[0].lk
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
} else {
|
|
58
|
-
logger.error('Location key type not found in sequelize model association for: %s', locKeyType);
|
|
59
|
-
throw new Error('Location key type not found in model');
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
logger.default('Configured this Item Query', { itemQuery, options });
|
|
64
|
-
|
|
65
|
-
const matchingItems = await model.findAll(options);
|
|
66
|
-
|
|
67
|
-
// this.logger.default('Matching Items', { matchingItems });
|
|
68
|
-
|
|
69
|
-
// TODO: Move this Up!
|
|
70
|
-
return matchingItems.map((row: any) => validateKeys(processRow(row, coordinate.kta), coordinate.kta)) as V[];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return all;
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
package/src/ops/create.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { ComKey, Item, LocKeyArray, PriKey, TypesProperties } from "@fjell/core";
|
|
2
|
-
|
|
3
|
-
import LibLogger from '@/logger';
|
|
4
|
-
import { ModelStatic } from "sequelize";
|
|
5
|
-
import { Definition } from "@/Definition";
|
|
6
|
-
|
|
7
|
-
const logger = LibLogger.get('sequelize', 'ops', 'create');
|
|
8
|
-
|
|
9
|
-
export const getCreateOperation = <
|
|
10
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
11
|
-
S extends string,
|
|
12
|
-
L1 extends string = never,
|
|
13
|
-
L2 extends string = never,
|
|
14
|
-
L3 extends string = never,
|
|
15
|
-
L4 extends string = never,
|
|
16
|
-
L5 extends string = never
|
|
17
|
-
>(
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
-
models: ModelStatic<any>[],
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
-
definition: Definition<V, S, L1, L2, L3, L4, L5>,
|
|
22
|
-
) => {
|
|
23
|
-
|
|
24
|
-
const create = async (
|
|
25
|
-
item: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
26
|
-
options? : {
|
|
27
|
-
key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>,
|
|
28
|
-
locations?: never;
|
|
29
|
-
} | {
|
|
30
|
-
key?: never;
|
|
31
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5>,
|
|
32
|
-
},
|
|
33
|
-
): Promise<V> => {
|
|
34
|
-
logger.default('Create', { item, options });
|
|
35
|
-
|
|
36
|
-
throw new Error('Not implemented');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return create;
|
|
40
|
-
}
|
package/src/ops/find.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Item, LocKeyArray } from "@fjell/core";
|
|
2
|
-
|
|
3
|
-
import { Definition } from "@/Definition";
|
|
4
|
-
import LibLogger from '@/logger';
|
|
5
|
-
import { ModelStatic } from "sequelize";
|
|
6
|
-
|
|
7
|
-
const logger = LibLogger.get('sequelize', 'ops', 'find');
|
|
8
|
-
|
|
9
|
-
export const getFindOperation = <
|
|
10
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
11
|
-
S extends string,
|
|
12
|
-
L1 extends string = never,
|
|
13
|
-
L2 extends string = never,
|
|
14
|
-
L3 extends string = never,
|
|
15
|
-
L4 extends string = never,
|
|
16
|
-
L5 extends string = never
|
|
17
|
-
>(
|
|
18
|
-
models: ModelStatic<any>[],
|
|
19
|
-
definition: Definition<V, S, L1, L2, L3, L4, L5>,
|
|
20
|
-
) => {
|
|
21
|
-
|
|
22
|
-
const { options } = definition;
|
|
23
|
-
|
|
24
|
-
const find = async (
|
|
25
|
-
finder: string,
|
|
26
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
27
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | [],
|
|
28
|
-
// eslint-disable-next-line max-params
|
|
29
|
-
): Promise<V[]> => {
|
|
30
|
-
logger.default('Find', { finder, finderParams, locations });
|
|
31
|
-
|
|
32
|
-
// Note that we execute the createFinders function here because we want to make sure we're always getting the
|
|
33
|
-
// most up to date methods.
|
|
34
|
-
if(options.finders && options.finders[finder]) {
|
|
35
|
-
const finderMethod = options.finders[finder];
|
|
36
|
-
if (finderMethod) {
|
|
37
|
-
return finderMethod(finderParams, locations) as Promise<V[]>;
|
|
38
|
-
} else {
|
|
39
|
-
logger.error(`Finder %s not found`, finder);
|
|
40
|
-
throw new Error(`Finder ${finder} not found`);
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
logger.error(`No finders have been defined for this lib`);
|
|
44
|
-
throw new Error(`No finders found`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return find;
|
|
49
|
-
}
|