@fjell/lib-sequelize 4.3.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 (91) hide show
  1. package/LICENSE +202 -0
  2. package/dist/src/Coordinate.d.ts +4 -0
  3. package/dist/src/Coordinate.js +7 -0
  4. package/dist/src/Coordinate.js.map +1 -0
  5. package/dist/src/Definition.d.ts +7 -0
  6. package/dist/src/Definition.js +16 -0
  7. package/dist/src/Definition.js.map +1 -0
  8. package/dist/src/EventCoordinator.d.ts +5 -0
  9. package/dist/src/EventCoordinator.js +56 -0
  10. package/dist/src/EventCoordinator.js.map +1 -0
  11. package/dist/src/Instance.d.ts +8 -0
  12. package/dist/src/Instance.js +14 -0
  13. package/dist/src/Instance.js.map +1 -0
  14. package/dist/src/KeyMaster.d.ts +4 -0
  15. package/dist/src/KeyMaster.js +58 -0
  16. package/dist/src/KeyMaster.js.map +1 -0
  17. package/dist/src/Operations.d.ts +5 -0
  18. package/dist/src/Operations.js +22 -0
  19. package/dist/src/Operations.js.map +1 -0
  20. package/dist/src/Options.d.ts +7 -0
  21. package/dist/src/Options.js +16 -0
  22. package/dist/src/Options.js.map +1 -0
  23. package/dist/src/QueryBuilder.d.ts +11 -0
  24. package/dist/src/QueryBuilder.js +161 -0
  25. package/dist/src/QueryBuilder.js.map +1 -0
  26. package/dist/src/RowProcessor.d.ts +3 -0
  27. package/dist/src/RowProcessor.js +11 -0
  28. package/dist/src/RowProcessor.js.map +1 -0
  29. package/dist/src/contained/Instance.d.ts +8 -0
  30. package/dist/src/contained/Instance.js +14 -0
  31. package/dist/src/contained/Instance.js.map +1 -0
  32. package/dist/src/contained/index.d.ts +1 -0
  33. package/dist/src/contained/index.js +2 -0
  34. package/dist/src/contained/index.js.map +1 -0
  35. package/dist/src/index.d.ts +7 -0
  36. package/dist/src/index.js +8 -0
  37. package/dist/src/index.js.map +1 -0
  38. package/dist/src/logger.d.ts +2 -0
  39. package/dist/src/logger.js +4 -0
  40. package/dist/src/logger.js.map +1 -0
  41. package/dist/src/ops/all.d.ts +4 -0
  42. package/dist/src/ops/all.js +47 -0
  43. package/dist/src/ops/all.js.map +1 -0
  44. package/dist/src/ops/create.d.ts +10 -0
  45. package/dist/src/ops/create.js +14 -0
  46. package/dist/src/ops/create.js.map +1 -0
  47. package/dist/src/ops/find.d.ts +4 -0
  48. package/dist/src/ops/find.js +26 -0
  49. package/dist/src/ops/find.js.map +1 -0
  50. package/dist/src/ops/get.d.ts +4 -0
  51. package/dist/src/ops/get.js +36 -0
  52. package/dist/src/ops/get.js.map +1 -0
  53. package/dist/src/ops/one.d.ts +4 -0
  54. package/dist/src/ops/one.js +17 -0
  55. package/dist/src/ops/one.js.map +1 -0
  56. package/dist/src/ops/remove.d.ts +5 -0
  57. package/dist/src/ops/remove.js +56 -0
  58. package/dist/src/ops/remove.js.map +1 -0
  59. package/dist/src/ops/update.d.ts +4 -0
  60. package/dist/src/ops/update.js +49 -0
  61. package/dist/src/ops/update.js.map +1 -0
  62. package/dist/src/primary/Instance.d.ts +8 -0
  63. package/dist/src/primary/Instance.js +17 -0
  64. package/dist/src/primary/Instance.js.map +1 -0
  65. package/dist/src/primary/index.d.ts +1 -0
  66. package/dist/src/primary/index.js +2 -0
  67. package/dist/src/primary/index.js.map +1 -0
  68. package/eslint.config.mjs +70 -0
  69. package/package.json +61 -0
  70. package/src/Coordinate.ts +16 -0
  71. package/src/Definition.ts +49 -0
  72. package/src/EventCoordinator.ts +103 -0
  73. package/src/Instance.ts +45 -0
  74. package/src/KeyMaster.ts +90 -0
  75. package/src/Operations.ts +42 -0
  76. package/src/Options.ts +41 -0
  77. package/src/QueryBuilder.ts +208 -0
  78. package/src/RowProcessor.ts +23 -0
  79. package/src/contained/Instance.ts +45 -0
  80. package/src/contained/index.ts +1 -0
  81. package/src/index.ts +7 -0
  82. package/src/logger.ts +5 -0
  83. package/src/ops/all.ts +76 -0
  84. package/src/ops/create.ts +40 -0
  85. package/src/ops/find.ts +49 -0
  86. package/src/ops/get.ts +67 -0
  87. package/src/ops/one.ts +37 -0
  88. package/src/ops/remove.ts +81 -0
  89. package/src/ops/update.ts +78 -0
  90. package/src/primary/Instance.ts +41 -0
  91. package/src/primary/index.ts +1 -0
@@ -0,0 +1,78 @@
1
+ import { abbrevIK, isComKey, ItemProperties } from "@fjell/core";
2
+
3
+ import { isPriKey } from "@fjell/core";
4
+
5
+ import { ComKey, Item, PriKey, TypesProperties } from "@fjell/core";
6
+
7
+ import { Definition } from "@/Definition";
8
+ import { populateEvents, removeEvents } from "@/EventCoordinator";
9
+ import { populateKey, removeKey } from "@/KeyMaster";
10
+ import LibLogger from '@/logger';
11
+ import { ModelStatic } from "sequelize";
12
+ import { NotFoundError } from "@fjell/lib";
13
+
14
+ const logger = LibLogger.get('sequelize', 'ops', 'update');
15
+
16
+ export const getUpdateOperation = <
17
+ V extends Item<S, L1, L2, L3, L4, L5>,
18
+ S extends string,
19
+ L1 extends string = never,
20
+ L2 extends string = never,
21
+ L3 extends string = never,
22
+ L4 extends string = never,
23
+ L5 extends string = never
24
+ >(
25
+ models: ModelStatic<any>[],
26
+ definition: Definition<V, S, L1, L2, L3, L4, L5>,
27
+ ) => {
28
+
29
+ const update = async (
30
+ key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>,
31
+ item: TypesProperties<V, S, L1, L2, L3, L4, L5>,
32
+ ): Promise<V> => {
33
+ const { coordinate } = definition;
34
+ const { kta } = coordinate;
35
+
36
+ logger.debug('update: %s, %j', abbrevIK(key), item);
37
+ // Find the object we're updating
38
+ // @ts-ignore
39
+ const model = models[0];
40
+
41
+ let response;
42
+
43
+ if( isPriKey(key) ) {
44
+ // Find the model by using the PK
45
+ const priKey = key as PriKey<S>;
46
+ response = await model.findByPk(priKey.pk);
47
+ } else if( isComKey(key) ) {
48
+ const comKey = key as ComKey<S, L1, L2, L3, L4, L5>;
49
+ // Find the model by using both of the identifiers.
50
+ response = await model.findOne({ where: {
51
+ [comKey?.loc[0]?.kt + 'Id']: comKey?.loc[0]?.lk,
52
+ id: comKey?.pk
53
+ } });
54
+ }
55
+
56
+ if( response ) {
57
+
58
+ // Remove the key and events
59
+ let updateProps = removeKey(item)
60
+ updateProps = removeEvents(item);
61
+
62
+ // Update the object
63
+ response = { ...response, ...updateProps };
64
+ await response?.save();
65
+
66
+ // Populate the key and events
67
+ let returnItem = response?.get({ plain: true }) as ItemProperties<S, L1, L2, L3, L4, L5>;
68
+ returnItem = populateKey(returnItem, kta);
69
+ returnItem = populateEvents(returnItem);
70
+ return returnItem as V;
71
+ } else {
72
+ throw new NotFoundError<S, L1, L2, L3, L4, L5>('update', coordinate, key);
73
+ }
74
+
75
+ }
76
+
77
+ return update;
78
+ }
@@ -0,0 +1,41 @@
1
+ import { Instance as AbstractSequelizeInstance } from '@/Instance';
2
+ import { Item } from '@fjell/core';
3
+ import { Primary } from '@fjell/lib';
4
+ import { createDefinition } from '@/Definition';
5
+ import { createOperations } from '@/Operations';
6
+ import { ModelStatic } from 'sequelize';
7
+ import { Options } from '@/Options';
8
+
9
+ import LibLogger from '@/logger';
10
+
11
+ const logger = LibLogger.get('lib-sequelize', 'primary', 'instance');
12
+
13
+ export interface Instance<
14
+ V extends Item<S>,
15
+ S extends string
16
+ > extends AbstractSequelizeInstance<V, S> {
17
+ models: ModelStatic<any>[];
18
+ }
19
+
20
+ // eslint-disable-next-line max-params
21
+ export function createInstance<
22
+ V extends Item<S>,
23
+ S extends string
24
+ >(
25
+ keyType: S,
26
+ models: ModelStatic<any>[],
27
+ libOptions: Options<V, S> = {},
28
+ scopes: string[] = [],
29
+ ): Instance<V, S> {
30
+ logger.debug('createInstance', { keyType, models, libOptions, scopes });
31
+ const definition = createDefinition([keyType], scopes, libOptions);
32
+
33
+ const operations = createOperations(models, definition);
34
+
35
+ return {
36
+ definition,
37
+ operations: Primary.wrapOperations(operations, definition),
38
+ models
39
+ } as Instance<V, S>;
40
+
41
+ }
@@ -0,0 +1 @@
1
+ export * from './Instance';