@fjell/lib-sequelize 4.4.0 → 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.
Files changed (101) hide show
  1. package/dist/cjs/AggregationBuilder.cjs +31 -0
  2. package/dist/cjs/Coordinate.cjs +37 -0
  3. package/dist/cjs/Definition.cjs +46 -0
  4. package/dist/cjs/EventCoordinator.cjs +35 -0
  5. package/dist/cjs/Instance.cjs +40 -0
  6. package/dist/cjs/KeyMaster.cjs +91 -0
  7. package/dist/cjs/Operations.cjs +29 -0
  8. package/dist/cjs/Options.cjs +39 -0
  9. package/dist/cjs/QueryBuilder.cjs +188 -0
  10. package/dist/cjs/ReferenceBuilder.cjs +34 -0
  11. package/dist/cjs/RowProcessor.cjs +41 -0
  12. package/dist/cjs/contained/Instance.cjs +21 -0
  13. package/dist/cjs/contained/index.cjs +10 -0
  14. package/dist/cjs/index.cjs +23 -0
  15. package/dist/cjs/logger.cjs +10 -0
  16. package/dist/cjs/ops/all.cjs +62 -0
  17. package/dist/cjs/ops/create.cjs +23 -0
  18. package/dist/cjs/ops/find.cjs +45 -0
  19. package/dist/cjs/ops/get.cjs +49 -0
  20. package/dist/cjs/ops/one.cjs +26 -0
  21. package/dist/cjs/ops/remove.cjs +72 -0
  22. package/dist/cjs/ops/update.cjs +59 -0
  23. package/dist/cjs/primary/Instance.cjs +31 -0
  24. package/dist/cjs/primary/index.cjs +10 -0
  25. package/dist/cjs/util/general.cjs +52 -0
  26. package/dist/es/AggregationBuilder.js +27 -0
  27. package/dist/{EventCoordinator.js → es/EventCoordinator.js} +2 -2
  28. package/dist/{Instance.js → es/Instance.js} +6 -5
  29. package/dist/{KeyMaster.js → es/KeyMaster.js} +30 -28
  30. package/dist/{Operations.js → es/Operations.js} +6 -6
  31. package/dist/{Options.js → es/Options.js} +8 -11
  32. package/dist/es/ReferenceBuilder.js +30 -0
  33. package/dist/es/RowProcessor.js +37 -0
  34. package/dist/{contained → es/contained}/Instance.js +6 -5
  35. package/dist/{index.js → es/index.js} +1 -1
  36. package/dist/{ops → es/ops}/all.js +7 -4
  37. package/dist/{ops → es/ops}/create.js +3 -2
  38. package/dist/{ops → es/ops}/find.js +16 -6
  39. package/dist/{ops → es/ops}/get.js +3 -3
  40. package/dist/{ops → es/ops}/one.js +2 -2
  41. package/dist/{ops → es/ops}/remove.js +6 -5
  42. package/dist/{ops → es/ops}/update.js +13 -15
  43. package/dist/{primary → es/primary}/Instance.js +6 -5
  44. package/dist/es/util/general.js +47 -0
  45. package/dist/index.cjs +810 -0
  46. package/dist/index.cjs.map +1 -0
  47. package/dist/types/AggregationBuilder.d.ts +4 -0
  48. package/dist/{Definition.d.ts → types/Definition.d.ts} +1 -1
  49. package/dist/{Instance.d.ts → types/Instance.d.ts} +1 -1
  50. package/dist/{KeyMaster.d.ts → types/KeyMaster.d.ts} +1 -2
  51. package/dist/{Operations.d.ts → types/Operations.d.ts} +2 -2
  52. package/dist/{Options.d.ts → types/Options.d.ts} +14 -3
  53. package/dist/types/ReferenceBuilder.d.ts +3 -0
  54. package/dist/{RowProcessor.d.ts → types/RowProcessor.d.ts} +3 -1
  55. package/dist/{contained → types/contained}/Instance.d.ts +2 -1
  56. package/dist/{ops → types/ops}/all.d.ts +2 -1
  57. package/dist/{ops → types/ops}/create.d.ts +2 -1
  58. package/dist/{ops → types/ops}/find.d.ts +2 -1
  59. package/dist/{ops → types/ops}/get.d.ts +2 -1
  60. package/dist/{ops → types/ops}/one.d.ts +2 -1
  61. package/dist/{ops → types/ops}/remove.d.ts +2 -1
  62. package/dist/{ops → types/ops}/update.d.ts +2 -1
  63. package/dist/{primary → types/primary}/Instance.d.ts +2 -1
  64. package/dist/types/util/general.d.ts +4 -0
  65. package/package.json +36 -31
  66. package/dist/RowProcessor.js +0 -18
  67. package/src/Coordinate.ts +0 -16
  68. package/src/Definition.ts +0 -49
  69. package/src/EventCoordinator.ts +0 -103
  70. package/src/Instance.ts +0 -44
  71. package/src/KeyMaster.ts +0 -90
  72. package/src/Operations.ts +0 -42
  73. package/src/Options.ts +0 -41
  74. package/src/QueryBuilder.ts +0 -208
  75. package/src/RowProcessor.ts +0 -23
  76. package/src/contained/Instance.ts +0 -44
  77. package/src/contained/index.ts +0 -1
  78. package/src/index.ts +0 -7
  79. package/src/logger.ts +0 -5
  80. package/src/ops/all.ts +0 -76
  81. package/src/ops/create.ts +0 -40
  82. package/src/ops/find.ts +0 -49
  83. package/src/ops/get.ts +0 -67
  84. package/src/ops/one.ts +0 -37
  85. package/src/ops/remove.ts +0 -81
  86. package/src/ops/update.ts +0 -78
  87. package/src/primary/Instance.ts +0 -40
  88. package/src/primary/index.ts +0 -1
  89. /package/dist/{Coordinate.js → es/Coordinate.js} +0 -0
  90. /package/dist/{Definition.js → es/Definition.js} +0 -0
  91. /package/dist/{QueryBuilder.js → es/QueryBuilder.js} +0 -0
  92. /package/dist/{contained → es/contained}/index.js +0 -0
  93. /package/dist/{logger.js → es/logger.js} +0 -0
  94. /package/dist/{primary → es/primary}/index.js +0 -0
  95. /package/dist/{Coordinate.d.ts → types/Coordinate.d.ts} +0 -0
  96. /package/dist/{EventCoordinator.d.ts → types/EventCoordinator.d.ts} +0 -0
  97. /package/dist/{QueryBuilder.d.ts → types/QueryBuilder.d.ts} +0 -0
  98. /package/dist/{contained → types/contained}/index.d.ts +0 -0
  99. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  100. /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
  101. /package/dist/{primary → types/primary}/index.d.ts +0 -0
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
-
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
- }
@@ -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
-
@@ -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,44 +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
- export function createInstance<
21
- V extends Item<S, L1, L2, L3, L4, L5>,
22
- S extends string,
23
- L1 extends string = never,
24
- L2 extends string = never,
25
- L3 extends string = never,
26
- L4 extends string = never,
27
- L5 extends string = never
28
- >(
29
- keyTypes: ItemTypeArray<S, L1, L2, L3, L4, L5>,
30
- models: ModelStatic<any>[],
31
- libOptions: Contained.Options<V, S, L1, L2, L3, L4, L5> = {},
32
- scopes: string[] = [],
33
- ): Instance<V, S, L1, L2, L3, L4, L5> {
34
-
35
- const definition = createDefinition(keyTypes, scopes, libOptions);
36
- const operations = createOperations(models, definition);
37
-
38
- return {
39
- definition,
40
- operations: Contained.wrapOperations(operations, definition),
41
- models
42
- } as Instance<V, S, L1, L2, L3, L4, L5>;
43
-
44
- }
@@ -1 +0,0 @@
1
- export * from './Instance';
package/src/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export * from './Coordinate';
2
- export * from './Definition';
3
- export * from './Instance';
4
- export * from './Options';
5
- export * from './Operations';
6
- export * as Contained from './contained';
7
- export * as Primary from './primary';
package/src/logger.ts DELETED
@@ -1,5 +0,0 @@
1
- import Logging from '@fjell/logging';
2
-
3
- const LibLogger = Logging.getLogger('@fjell/lib-sequelize');
4
-
5
- export default LibLogger;
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
-
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
-
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
- }
package/src/ops/get.ts DELETED
@@ -1,67 +0,0 @@
1
- import {
2
- ComKey,
3
- isComKey,
4
- isPriKey,
5
- isValidItemKey,
6
- Item,
7
- PriKey,
8
- validateKeys
9
- } from '@fjell/core';
10
-
11
- import LibLogger from '@/logger';
12
- import { ModelStatic } from 'sequelize';
13
- import { processRow } from '@/RowProcessor';
14
- import { Definition } from '@/Definition';
15
- import { NotFoundError } from '@fjell/lib';
16
-
17
- const logger = LibLogger.get('sequelize', 'ops', 'get');
18
-
19
- export const getGetOperation = <
20
- V extends Item<S, L1, L2, L3, L4, L5>,
21
- S extends string,
22
- L1 extends string = never,
23
- L2 extends string = never,
24
- L3 extends string = never,
25
- L4 extends string = never,
26
- L5 extends string = never
27
- >(
28
- models: Array<ModelStatic<any>>,
29
- definition: Definition<V, S, L1, L2, L3, L4, L5>,
30
- ) => {
31
-
32
- const { coordinate } = definition;
33
- const { kta } = coordinate;
34
-
35
- const get = async (
36
- key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>,
37
- ): Promise<V> => {
38
- logger.default('Get', { key });
39
- if (!isValidItemKey(key)) {
40
- logger.error('Key for Get is not a valid ItemKey: %j', key);
41
- throw new Error('Key for Get is not a valid ItemKey');
42
- }
43
-
44
- const itemKey = key;
45
-
46
- // @ts-ignore
47
- const model = models[0];
48
-
49
- let item;
50
-
51
- if( isPriKey(itemKey) ) {
52
- item = await model.findByPk((itemKey as PriKey<S>).pk);
53
- } else if( isComKey(itemKey) ) {
54
- const comKey = itemKey as ComKey<S, L1, L2, L3, L4, L5>;
55
- // TODO: This should probably interrogate the model?
56
- item = await model.findOne({ where: { id: comKey.pk, [comKey?.loc[0]?.kt + 'Id']: comKey?.loc[0]?.lk } });
57
- }
58
-
59
- if (!item) {
60
- throw new NotFoundError<S, L1, L2, L3, L4, L5>('get', coordinate, key);
61
- } else {
62
- return validateKeys(processRow(item, kta), kta) as V;
63
- }
64
- }
65
-
66
- return get;
67
- }
package/src/ops/one.ts DELETED
@@ -1,37 +0,0 @@
1
- import { Item, ItemQuery, LocKeyArray } from "@fjell/core";
2
-
3
- import { Definition } from "@/Definition";
4
- import LibLogger from '@/logger';
5
- import { ModelStatic } from "sequelize";
6
- import { getAllOperation } from "./all";
7
-
8
- const logger = LibLogger.get('sequelize', 'ops', 'one');
9
-
10
- export const getOneOperation = <
11
- V extends Item<S, L1, L2, L3, L4, L5>,
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
- models: ModelStatic<any>[],
20
- definition: Definition<V, S, L1, L2, L3, L4, L5>,
21
- ) => {
22
- const one = async (
23
- itemQuery: ItemQuery,
24
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = [],
25
- ): Promise<V | null> => {
26
- logger.default('One', { itemQuery, locations });
27
-
28
- const items = await getAllOperation(models, definition)(itemQuery, locations);
29
- if (items.length > 0) {
30
- return items[0] as V;
31
- } else {
32
- return null;
33
- }
34
- }
35
-
36
- return one;
37
- }