@fjell/lib-sequelize 4.4.0 → 4.4.2

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 (109) 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 +54 -0
  5. package/dist/cjs/Instance.cjs +40 -0
  6. package/dist/cjs/KeyMaster.cjs +129 -0
  7. package/dist/cjs/Operations.cjs +29 -0
  8. package/dist/cjs/Options.cjs +39 -0
  9. package/dist/cjs/QueryBuilder.cjs +290 -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 +154 -0
  18. package/dist/cjs/ops/find.cjs +45 -0
  19. package/dist/cjs/ops/get.cjs +85 -0
  20. package/dist/cjs/ops/one.cjs +26 -0
  21. package/dist/cjs/ops/remove.cjs +111 -0
  22. package/dist/cjs/ops/update.cjs +61 -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/cjs/util/relationshipUtils.cjs +117 -0
  27. package/dist/es/AggregationBuilder.js +27 -0
  28. package/dist/es/EventCoordinator.js +48 -0
  29. package/dist/{Instance.js → es/Instance.js} +6 -5
  30. package/dist/es/KeyMaster.js +124 -0
  31. package/dist/{Operations.js → es/Operations.js} +8 -8
  32. package/dist/{Options.js → es/Options.js} +8 -11
  33. package/dist/{QueryBuilder.js → es/QueryBuilder.js} +123 -21
  34. package/dist/es/ReferenceBuilder.js +30 -0
  35. package/dist/es/RowProcessor.js +37 -0
  36. package/dist/{contained → es/contained}/Instance.js +6 -5
  37. package/dist/{index.js → es/index.js} +1 -1
  38. package/dist/{ops → es/ops}/all.js +7 -4
  39. package/dist/es/ops/create.js +150 -0
  40. package/dist/{ops → es/ops}/find.js +16 -6
  41. package/dist/es/ops/get.js +81 -0
  42. package/dist/{ops → es/ops}/one.js +2 -2
  43. package/dist/{ops → es/ops}/remove.js +51 -11
  44. package/dist/{ops → es/ops}/update.js +15 -15
  45. package/dist/{primary → es/primary}/Instance.js +6 -5
  46. package/dist/es/util/general.js +47 -0
  47. package/dist/es/util/relationshipUtils.js +112 -0
  48. package/dist/index.cjs +1279 -0
  49. package/dist/index.cjs.map +1 -0
  50. package/dist/types/AggregationBuilder.d.ts +4 -0
  51. package/dist/{Definition.d.ts → types/Definition.d.ts} +1 -1
  52. package/dist/{EventCoordinator.d.ts → types/EventCoordinator.d.ts} +1 -0
  53. package/dist/{Instance.d.ts → types/Instance.d.ts} +1 -1
  54. package/dist/types/KeyMaster.d.ts +4 -0
  55. package/dist/{Operations.d.ts → types/Operations.d.ts} +2 -2
  56. package/dist/{Options.d.ts → types/Options.d.ts} +14 -3
  57. package/dist/{QueryBuilder.d.ts → types/QueryBuilder.d.ts} +1 -0
  58. package/dist/types/ReferenceBuilder.d.ts +3 -0
  59. package/dist/{RowProcessor.d.ts → types/RowProcessor.d.ts} +3 -1
  60. package/dist/{contained → types/contained}/Instance.d.ts +2 -1
  61. package/dist/{ops → types/ops}/all.d.ts +2 -1
  62. package/dist/{ops → types/ops}/create.d.ts +3 -2
  63. package/dist/{ops → types/ops}/find.d.ts +2 -1
  64. package/dist/{ops → types/ops}/get.d.ts +2 -1
  65. package/dist/{ops → types/ops}/one.d.ts +2 -1
  66. package/dist/{ops → types/ops}/remove.d.ts +2 -1
  67. package/dist/{ops → types/ops}/update.d.ts +2 -1
  68. package/dist/{primary → types/primary}/Instance.d.ts +2 -1
  69. package/dist/types/util/general.d.ts +4 -0
  70. package/dist/types/util/relationshipUtils.d.ts +21 -0
  71. package/package.json +37 -32
  72. package/dist/EventCoordinator.js +0 -30
  73. package/dist/KeyMaster.d.ts +0 -4
  74. package/dist/KeyMaster.js +0 -84
  75. package/dist/RowProcessor.js +0 -18
  76. package/dist/ops/create.js +0 -18
  77. package/dist/ops/get.js +0 -45
  78. package/src/Coordinate.ts +0 -16
  79. package/src/Definition.ts +0 -49
  80. package/src/EventCoordinator.ts +0 -103
  81. package/src/Instance.ts +0 -44
  82. package/src/KeyMaster.ts +0 -90
  83. package/src/Operations.ts +0 -42
  84. package/src/Options.ts +0 -41
  85. package/src/QueryBuilder.ts +0 -208
  86. package/src/RowProcessor.ts +0 -23
  87. package/src/contained/Instance.ts +0 -44
  88. package/src/contained/index.ts +0 -1
  89. package/src/index.ts +0 -7
  90. package/src/logger.ts +0 -5
  91. package/src/ops/all.ts +0 -76
  92. package/src/ops/create.ts +0 -40
  93. package/src/ops/find.ts +0 -49
  94. package/src/ops/get.ts +0 -67
  95. package/src/ops/one.ts +0 -37
  96. package/src/ops/remove.ts +0 -81
  97. package/src/ops/update.ts +0 -78
  98. package/src/primary/Instance.ts +0 -40
  99. package/src/primary/index.ts +0 -1
  100. /package/dist/{Coordinate.js → es/Coordinate.js} +0 -0
  101. /package/dist/{Definition.js → es/Definition.js} +0 -0
  102. /package/dist/{contained → es/contained}/index.js +0 -0
  103. /package/dist/{logger.js → es/logger.js} +0 -0
  104. /package/dist/{primary → es/primary}/index.js +0 -0
  105. /package/dist/{Coordinate.d.ts → types/Coordinate.d.ts} +0 -0
  106. /package/dist/{contained → types/contained}/index.d.ts +0 -0
  107. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  108. /package/dist/{logger.d.ts → types/logger.d.ts} +0 -0
  109. /package/dist/{primary → types/primary}/index.d.ts +0 -0
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
- }
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