@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/ops/remove.ts DELETED
@@ -1,81 +0,0 @@
1
- import { ComKey, isValidItemKey, PriKey } from "@fjell/core";
2
-
3
- import { abbrevIK, isComKey, isPriKey, Item, ItemProperties } from "@fjell/core";
4
-
5
- import { Definition } from "@/Definition";
6
- import { populateEvents } from "@/EventCoordinator";
7
- import { populateKey } from "@/KeyMaster";
8
- import LibLogger from '@/logger';
9
- import { ModelStatic } from "sequelize";
10
-
11
- const logger = LibLogger.get('sequelize', 'ops', 'remove');
12
-
13
- export const getRemoveOperation = <
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
- >(
22
- models: ModelStatic<any>[],
23
- definition: Definition<V, S, L1, L2, L3, L4, L5>,
24
- ) => {
25
- const { coordinate, options } = definition;
26
- const { kta } = coordinate;
27
-
28
- const remove = async (
29
- key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>,
30
- ): Promise<V> => {
31
- logger.default('Remove', { key });
32
-
33
- if (!isValidItemKey(key)) {
34
- logger.error('Key for Remove is not a valid ItemKey: %j', key);
35
- throw new Error('Key for Remove is not a valid ItemKey');
36
- }
37
-
38
- // @ts-ignore
39
- const model = models[0];
40
-
41
- let item;
42
- let returnItem;
43
-
44
- logger.debug('remove: %s', abbrevIK(key));
45
- if( isPriKey(key) ) {
46
- item = await model.findByPk((key as PriKey<S>).pk);
47
- } else if( isComKey(key) ) {
48
- const comKey = key as ComKey<S, L1, L2, L3, L4, L5>;
49
- item = await model.findOne({ where: { id: comKey.pk, [comKey?.loc[0]?.kt + 'Id']: comKey?.loc[0]?.lk } });
50
- }
51
-
52
- const isDeletedAttribute = model.getAttributes().isDeleted;
53
- const deletedAtAttribute = model.getAttributes().deletedAt;
54
-
55
- if( isDeletedAttribute || deletedAtAttribute ) {
56
- if( model.getAttributes().isDeleted ) {
57
- item.isDeleted = true;
58
- }
59
-
60
- if( model.getAttributes().deletedAt ) {
61
- item.deletedAt = new Date();
62
- }
63
-
64
- // Save the object
65
- await item?.save();
66
- returnItem = item?.get({ plain: true }) as ItemProperties<S, L1, L2, L3, L4, L5>;
67
- returnItem = populateKey(returnItem, kta);
68
- returnItem = populateEvents(returnItem);
69
- } else if( options.deleteOnRemove ) {
70
- await item?.destroy();
71
- returnItem = item?.get({ plain: true }) as ItemProperties<S, L1, L2, L3, L4, L5>;
72
- returnItem = populateKey(returnItem, kta);
73
- returnItem = populateEvents(returnItem);
74
- } else {
75
- throw new Error('No deletedAt or isDeleted attribute found in model, and deleteOnRemove is not set');
76
- }
77
- return returnItem as V;
78
- }
79
-
80
- return remove;
81
- }
package/src/ops/update.ts DELETED
@@ -1,78 +0,0 @@
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
- }
@@ -1,40 +0,0 @@
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
- export function createInstance<
21
- V extends Item<S>,
22
- S extends string
23
- >(
24
- keyType: S,
25
- models: ModelStatic<any>[],
26
- libOptions: Options<V, S> = {},
27
- scopes: string[] = [],
28
- ): Instance<V, S> {
29
- logger.debug('createInstance', { keyType, models, libOptions, scopes });
30
- const definition = createDefinition([keyType], scopes, libOptions);
31
-
32
- const operations = createOperations(models, definition);
33
-
34
- return {
35
- definition,
36
- operations: Primary.wrapOperations(operations, definition),
37
- models
38
- } as Instance<V, S>;
39
-
40
- }
@@ -1 +0,0 @@
1
- export * from './Instance';
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes