@decaf-ts/db-decorators 0.1.0

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 (151) hide show
  1. package/LICENSE.md +19 -0
  2. package/README.md +160 -0
  3. package/dist/db-decorators.bundle.min.js +2 -0
  4. package/dist/db-decorators.bundle.min.js.LICENSE.txt +14 -0
  5. package/dist/esm/db-decorators.bundle.min.esm.js +2 -0
  6. package/dist/esm/db-decorators.bundle.min.esm.js.LICENSE.txt +14 -0
  7. package/lib/esm/identity/decorators.d.ts +1 -0
  8. package/lib/esm/identity/decorators.js +1 -0
  9. package/lib/esm/identity/index.d.ts +2 -0
  10. package/lib/esm/identity/index.js +1 -0
  11. package/lib/esm/identity/utils.d.ts +36 -0
  12. package/lib/esm/identity/utils.js +1 -0
  13. package/lib/esm/index.d.ts +33 -0
  14. package/lib/esm/index.js +1 -0
  15. package/lib/esm/interfaces/CrudOperator.d.ts +42 -0
  16. package/lib/esm/interfaces/CrudOperator.js +0 -0
  17. package/lib/esm/interfaces/IRepository.d.ts +13 -0
  18. package/lib/esm/interfaces/IRepository.js +0 -0
  19. package/lib/esm/interfaces/index.d.ts +2 -0
  20. package/lib/esm/interfaces/index.js +1 -0
  21. package/lib/esm/model/DBModel.d.ts +25 -0
  22. package/lib/esm/model/DBModel.js +1 -0
  23. package/lib/esm/model/constants.d.ts +36 -0
  24. package/lib/esm/model/constants.js +1 -0
  25. package/lib/esm/model/decorators.d.ts +25 -0
  26. package/lib/esm/model/decorators.js +1 -0
  27. package/lib/esm/model/index.d.ts +3 -0
  28. package/lib/esm/model/index.js +1 -0
  29. package/lib/esm/model/validation.d.ts +15 -0
  30. package/lib/esm/model/validation.js +1 -0
  31. package/lib/esm/operations/Operations.d.ts +19 -0
  32. package/lib/esm/operations/Operations.js +1 -0
  33. package/lib/esm/operations/OperationsRegistry.d.ts +34 -0
  34. package/lib/esm/operations/OperationsRegistry.js +1 -0
  35. package/lib/esm/operations/constants.d.ts +23 -0
  36. package/lib/esm/operations/constants.js +1 -0
  37. package/lib/esm/operations/decorators.d.ts +166 -0
  38. package/lib/esm/operations/decorators.js +1 -0
  39. package/lib/esm/operations/index.d.ts +5 -0
  40. package/lib/esm/operations/index.js +1 -0
  41. package/lib/esm/operations/types.d.ts +28 -0
  42. package/lib/esm/operations/types.js +0 -0
  43. package/lib/esm/repository/BaseRepository.d.ts +24 -0
  44. package/lib/esm/repository/BaseRepository.js +1 -0
  45. package/lib/esm/repository/DataCache.d.ts +8 -0
  46. package/lib/esm/repository/DataCache.js +1 -0
  47. package/lib/esm/repository/Repository.d.ts +12 -0
  48. package/lib/esm/repository/Repository.js +1 -0
  49. package/lib/esm/repository/constants.d.ts +6 -0
  50. package/lib/esm/repository/constants.js +1 -0
  51. package/lib/esm/repository/decorators.d.ts +3 -0
  52. package/lib/esm/repository/decorators.js +1 -0
  53. package/lib/esm/repository/errors.d.ts +69 -0
  54. package/lib/esm/repository/errors.js +1 -0
  55. package/lib/esm/repository/index.d.ts +9 -0
  56. package/lib/esm/repository/index.js +1 -0
  57. package/lib/esm/repository/types.d.ts +13 -0
  58. package/lib/esm/repository/types.js +0 -0
  59. package/lib/esm/repository/utils.d.ts +57 -0
  60. package/lib/esm/repository/utils.js +1 -0
  61. package/lib/esm/repository/wrappers.d.ts +38 -0
  62. package/lib/esm/repository/wrappers.js +1 -0
  63. package/lib/esm/validation/comparison.d.ts +1 -0
  64. package/lib/esm/validation/comparison.js +1 -0
  65. package/lib/esm/validation/constants.d.ts +49 -0
  66. package/lib/esm/validation/constants.js +1 -0
  67. package/lib/esm/validation/decorators.d.ts +70 -0
  68. package/lib/esm/validation/decorators.js +1 -0
  69. package/lib/esm/validation/index.d.ts +3 -0
  70. package/lib/esm/validation/index.js +1 -0
  71. package/lib/esm/validation/validators/ReadOnlyValidator.d.ts +23 -0
  72. package/lib/esm/validation/validators/ReadOnlyValidator.js +1 -0
  73. package/lib/esm/validation/validators/TimestampValidator.d.ts +14 -0
  74. package/lib/esm/validation/validators/TimestampValidator.js +1 -0
  75. package/lib/esm/validation/validators/UpdateValidator.d.ts +23 -0
  76. package/lib/esm/validation/validators/UpdateValidator.js +1 -0
  77. package/lib/esm/validation/validators/index.d.ts +3 -0
  78. package/lib/esm/validation/validators/index.js +1 -0
  79. package/lib/identity/decorators.cjs +1 -0
  80. package/lib/identity/decorators.d.ts +1 -0
  81. package/lib/identity/index.cjs +1 -0
  82. package/lib/identity/index.d.ts +2 -0
  83. package/lib/identity/utils.cjs +1 -0
  84. package/lib/identity/utils.d.ts +36 -0
  85. package/lib/index.cjs +1 -0
  86. package/lib/index.d.ts +33 -0
  87. package/lib/interfaces/CrudOperator.cjs +1 -0
  88. package/lib/interfaces/CrudOperator.d.ts +42 -0
  89. package/lib/interfaces/IRepository.cjs +1 -0
  90. package/lib/interfaces/IRepository.d.ts +13 -0
  91. package/lib/interfaces/index.cjs +1 -0
  92. package/lib/interfaces/index.d.ts +2 -0
  93. package/lib/model/DBModel.cjs +1 -0
  94. package/lib/model/DBModel.d.ts +25 -0
  95. package/lib/model/constants.cjs +1 -0
  96. package/lib/model/constants.d.ts +36 -0
  97. package/lib/model/decorators.cjs +1 -0
  98. package/lib/model/decorators.d.ts +25 -0
  99. package/lib/model/index.cjs +1 -0
  100. package/lib/model/index.d.ts +3 -0
  101. package/lib/model/validation.cjs +1 -0
  102. package/lib/model/validation.d.ts +15 -0
  103. package/lib/operations/Operations.cjs +1 -0
  104. package/lib/operations/Operations.d.ts +19 -0
  105. package/lib/operations/OperationsRegistry.cjs +1 -0
  106. package/lib/operations/OperationsRegistry.d.ts +34 -0
  107. package/lib/operations/constants.cjs +1 -0
  108. package/lib/operations/constants.d.ts +23 -0
  109. package/lib/operations/decorators.cjs +1 -0
  110. package/lib/operations/decorators.d.ts +166 -0
  111. package/lib/operations/index.cjs +1 -0
  112. package/lib/operations/index.d.ts +5 -0
  113. package/lib/operations/types.cjs +1 -0
  114. package/lib/operations/types.d.ts +28 -0
  115. package/lib/repository/BaseRepository.cjs +1 -0
  116. package/lib/repository/BaseRepository.d.ts +24 -0
  117. package/lib/repository/DataCache.cjs +1 -0
  118. package/lib/repository/DataCache.d.ts +8 -0
  119. package/lib/repository/Repository.cjs +1 -0
  120. package/lib/repository/Repository.d.ts +12 -0
  121. package/lib/repository/constants.cjs +1 -0
  122. package/lib/repository/constants.d.ts +6 -0
  123. package/lib/repository/decorators.cjs +1 -0
  124. package/lib/repository/decorators.d.ts +3 -0
  125. package/lib/repository/errors.cjs +1 -0
  126. package/lib/repository/errors.d.ts +69 -0
  127. package/lib/repository/index.cjs +1 -0
  128. package/lib/repository/index.d.ts +9 -0
  129. package/lib/repository/types.cjs +1 -0
  130. package/lib/repository/types.d.ts +13 -0
  131. package/lib/repository/utils.cjs +1 -0
  132. package/lib/repository/utils.d.ts +57 -0
  133. package/lib/repository/wrappers.cjs +1 -0
  134. package/lib/repository/wrappers.d.ts +38 -0
  135. package/lib/validation/comparison.cjs +1 -0
  136. package/lib/validation/comparison.d.ts +1 -0
  137. package/lib/validation/constants.cjs +1 -0
  138. package/lib/validation/constants.d.ts +49 -0
  139. package/lib/validation/decorators.cjs +1 -0
  140. package/lib/validation/decorators.d.ts +70 -0
  141. package/lib/validation/index.cjs +1 -0
  142. package/lib/validation/index.d.ts +3 -0
  143. package/lib/validation/validators/ReadOnlyValidator.cjs +1 -0
  144. package/lib/validation/validators/ReadOnlyValidator.d.ts +23 -0
  145. package/lib/validation/validators/TimestampValidator.cjs +1 -0
  146. package/lib/validation/validators/TimestampValidator.d.ts +14 -0
  147. package/lib/validation/validators/UpdateValidator.cjs +1 -0
  148. package/lib/validation/validators/UpdateValidator.d.ts +23 -0
  149. package/lib/validation/validators/index.cjs +1 -0
  150. package/lib/validation/validators/index.d.ts +3 -0
  151. package/package.json +107 -0
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @summary Set of constants to define db CRUD operations and their equivalent 'on' and 'after' phases
3
+ * @const OperationKeys
4
+ *
5
+ * @memberOf module:db-decorators.Operations
6
+ */
7
+ export declare enum OperationKeys {
8
+ REFLECT = "operations.db.",
9
+ CREATE = "create",
10
+ READ = "read",
11
+ UPDATE = "update",
12
+ DELETE = "delete",
13
+ ON = "on.",
14
+ AFTER = "after."
15
+ }
16
+ /**
17
+ * @summary Maps out groups of CRUD operations for easier mapping of decorators
18
+ *
19
+ * @constant DBOperations
20
+ *
21
+ * @memberOf module:db-decorators.Operations
22
+ */
23
+ export declare const DBOperations: Record<string, OperationKeys[]>;
@@ -0,0 +1 @@
1
+ var OperationKeys;!function(e){e.REFLECT="operations.db.",e.CREATE="create",e.READ="read",e.UPDATE="update",e.DELETE="delete",e.ON="on.",e.AFTER="after."}(OperationKeys=OperationKeys||{});const DBOperations={CREATE:[OperationKeys.CREATE],READ:[OperationKeys.READ],UPDATE:[OperationKeys.UPDATE],DELETE:[OperationKeys.DELETE],CREATE_UPDATE:[OperationKeys.CREATE,OperationKeys.UPDATE],READ_CREATE:[OperationKeys.READ,OperationKeys.CREATE],ALL:[OperationKeys.CREATE,OperationKeys.READ,OperationKeys.UPDATE,OperationKeys.DELETE]};export{OperationKeys,DBOperations};
@@ -0,0 +1,166 @@
1
+ import { IdOperationHandler, OperationHandler, StandardOperationHandler, UpdateOperationHandler } from "./types";
2
+ import { OperationKeys } from "./constants";
3
+ /**
4
+ * @summary Defines a behaviour to set on the defined {@link DBOperations.CREATE_UPDATE}
5
+ *
6
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
7
+ * @param data
8
+ * @param {any[]} [args] Arguments that will be passed in order to the handler method
9
+ *
10
+ * @see on
11
+ *
12
+ * @function onCreateUpdate
13
+ *
14
+ * @category Decorators
15
+ */
16
+ export declare function onCreateUpdate<T>(handler: StandardOperationHandler<any, any, T> | UpdateOperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
17
+ /**
18
+ * @summary Defines a behaviour to set on the defined {@link DBOperations.UPDATE}
19
+ *
20
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
21
+ * @param data
22
+ * @param {any[]} [args] Arguments that will be passed in order to the handler method
23
+ *
24
+ * @see on
25
+ *
26
+ * @function onUpdate
27
+ *
28
+ * @category Decorators
29
+ */
30
+ export declare function onUpdate<T>(handler: UpdateOperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
31
+ /**
32
+ * @summary Defines a behaviour to set on the defined {@link DBOperations.CREATE}
33
+ *
34
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
35
+ * @param data?
36
+ *
37
+ * @see on
38
+ *
39
+ * @function onCreate
40
+ *
41
+ * @category Decorators
42
+ */
43
+ export declare function onCreate<T>(handler: StandardOperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
44
+ /**
45
+ * @summary Defines a behaviour to set on the defined {@link DBOperations.READ}
46
+ *
47
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
48
+ * @param data
49
+ *
50
+ * @see on
51
+ *
52
+ * @function onRead
53
+ *
54
+ * @category Decorators
55
+ */
56
+ export declare function onRead<T>(handler: IdOperationHandler<any, any, T>, data: T): (target: object, propertyKey: string | symbol) => void;
57
+ /**
58
+ * @summary Defines a behaviour to set on the defined {@link DBOperations.DELETE}
59
+ *
60
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
61
+ * @param data
62
+ *
63
+ * @see on
64
+ *
65
+ * @function onDelete
66
+ *
67
+ * @category Decorators
68
+ */
69
+ export declare function onDelete<T>(handler: IdOperationHandler<any, any, T>, data: T): (target: object, propertyKey: string | symbol) => void;
70
+ /**
71
+ * @summary Defines a behaviour to set on the defined {@link DBOperations}
72
+ *
73
+ * @param {OperationKeys[] | DBOperations} op One of {@link DBOperations}
74
+ * @param {OnOperationHandler<any>} handler The method called upon the operation
75
+ * @param data
76
+ *
77
+ * ex: handler(...args, ...props.map(p => target[p]))
78
+ *
79
+ * @function on
80
+ *
81
+ * @category Decorators
82
+ */
83
+ export declare function on<T>(op: OperationKeys[] | undefined, handler: OperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
84
+ /**
85
+ * @summary Defines a behaviour to set after the defined {@link DBOperations.CREATE_UPDATE}
86
+ *
87
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
88
+ * @param data
89
+ *
90
+ * @see after
91
+ *
92
+ * @function afterCreateUpdate
93
+ *
94
+ * @category Decorators
95
+ */
96
+ export declare function afterCreateUpdate<T>(handler: StandardOperationHandler<any, any, T> | UpdateOperationHandler<any, any, T>, data: T): (target: object, propertyKey: string | symbol) => void;
97
+ /**
98
+ * @summary Defines a behaviour to set after the defined {@link DBOperations.UPDATE}
99
+ *
100
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
101
+ * @param data
102
+ *
103
+ * @see after
104
+ *
105
+ * @function afterUpdate
106
+ *
107
+ * @category Decorators
108
+ */
109
+ export declare function afterUpdate<T>(handler: UpdateOperationHandler<any, any, T>, data: T): (target: object, propertyKey: string | symbol) => void;
110
+ /**
111
+ * @summary Defines a behaviour to set after the defined {@link DBOperations.CREATE}
112
+ *
113
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
114
+ * @param data
115
+ *
116
+ * @see after
117
+ *
118
+ * @function afterCreate
119
+ *
120
+ * @category Decorators
121
+ */
122
+ export declare function afterCreate<T>(handler: StandardOperationHandler<any, any, T>, data: T): (target: object, propertyKey: string | symbol) => void;
123
+ /**
124
+ * @summary Defines a behaviour to set after the defined {@link DBOperations.READ}
125
+ *
126
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
127
+ * @param data
128
+ * @param {any[]} [args] Arguments that will be passed in order to the handler method
129
+ *
130
+ * @see after
131
+ *
132
+ * @function afterRead
133
+ *
134
+ * @category Decorators
135
+ */
136
+ export declare function afterRead<T>(handler: StandardOperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
137
+ /**
138
+ * @summary Defines a behaviour to set after the defined {@link DBOperations.DELETE}
139
+ *
140
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
141
+ * @param data
142
+ * @param {any[]} [args] Arguments that will be passed in order to the handler method
143
+ *
144
+ * @see after
145
+ *
146
+ * @function afterDelete
147
+ *
148
+ * @category Decorators
149
+ */
150
+ export declare function afterDelete<T>(handler: StandardOperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
151
+ /**
152
+ * @summary Defines a behaviour to set on the defined {@link DBOperations}
153
+ *
154
+ * @param {OperationKeys[] | DBOperations} op One of {@link DBOperations}
155
+ * @param {AfterOperationHandler<any>} handler The method called upon the operation
156
+ *
157
+ * ex: handler(...args, ...props.map(p => target[p]))
158
+ *
159
+ * @param data
160
+ * @param args
161
+ * @function after
162
+ *
163
+ * @category Decorators
164
+ */
165
+ export declare function after<T>(op: OperationKeys[] | undefined, handler: OperationHandler<any, any, T>, data?: T): (target: object, propertyKey: string | symbol) => void;
166
+ export declare function operation<T>(baseOp: OperationKeys.ON | OperationKeys.AFTER, operation: OperationKeys[] | undefined, handler: OperationHandler<any, any, T>, dataToAdd?: T): (target: object, propertyKey: string | symbol) => void;
@@ -0,0 +1 @@
1
+ import{DBOperations,OperationKeys}from"./constants";import{Operations}from"./Operations";import{apply,metadata}from"@decaf-ts/reflection";function handle(n,r){return(e,t)=>{Operations.register(r,n,e,t)}}function onCreateUpdate(e,t){return on(DBOperations.CREATE_UPDATE,e,t)}function onUpdate(e,t){return on(DBOperations.UPDATE,e,t)}function onCreate(e,t){return on(DBOperations.CREATE,e,t)}function onRead(e,t){return on(DBOperations.READ,e,t)}function onDelete(e,t){return on(DBOperations.DELETE,e,t)}function on(e=DBOperations.ALL,t,n){return operation(OperationKeys.ON,e,t,n)}function afterCreateUpdate(e,t){return after(DBOperations.CREATE_UPDATE,e,t)}function afterUpdate(e,t){return after(DBOperations.UPDATE,e,t)}function afterCreate(e,t){return after(DBOperations.CREATE,e,t)}function afterRead(e,t){return after(DBOperations.READ,e,t)}function afterDelete(e,t){return after(DBOperations.DELETE,e,t)}function after(e=DBOperations.ALL,t,n){return operation(OperationKeys.AFTER,e,t,n)}function operation(p,t=DBOperations.ALL,s,f){return(a,o)=>{const i=a.constructor.name;var e=t.reduce((e,t)=>{var n=p+t;let r=Reflect.getMetadata(Operations.genKey(n),a,o);r=r||{operation:t,handlers:{}};t=Operations.getHandlerName(s);return r.handlers[i]&&r.handlers[i][o]&&t in r.handlers[i][o]||(r.handlers[i]=r.handlers[i]||{},r.handlers[i][o]=r.handlers[i][o]||{},r.handlers[i][o][t]={data:f},e.push(handle(n,s),metadata(Operations.genKey(n),r))),e},[]);return apply(...e)(a,o)}}export{onCreateUpdate,onUpdate,onCreate,onRead,onDelete,on,afterCreateUpdate,afterUpdate,afterCreate,afterRead,afterDelete,after,operation};
@@ -0,0 +1,5 @@
1
+ export * from "./constants";
2
+ export * from "./decorators";
3
+ export * from "./Operations";
4
+ export * from "./OperationsRegistry";
5
+ export * from "./types";
@@ -0,0 +1 @@
1
+ export*from"./constants";export*from"./decorators";export*from"./Operations";export*from"./OperationsRegistry";export*from"./types";
@@ -0,0 +1,28 @@
1
+ import { DBModel } from "../model/DBModel";
2
+ import { OperationKeys } from "./constants";
3
+ import { IRepository } from "../interfaces/IRepository";
4
+ export type OperationMetadata<T> = {
5
+ operation: OperationKeys;
6
+ handler: string;
7
+ metadata?: T;
8
+ };
9
+ /**
10
+ * @typedef OperationHandler
11
+ * @memberOf db-decorators.operations
12
+ */
13
+ export type OperationHandler<T extends DBModel, Y extends IRepository<T>, V> = StandardOperationHandler<T, Y, V> | UpdateOperationHandler<T, Y, V> | IdOperationHandler<T, Y, V>;
14
+ /**
15
+ * @typedef OnOperationHandler
16
+ * @memberOf db-decorators.operations
17
+ */
18
+ export type StandardOperationHandler<T extends DBModel, Y extends IRepository<T>, V> = (this: Y, metadata: V, key: any, model: T) => Promise<void> | void;
19
+ /**
20
+ * @typedef IdOperationHandler
21
+ * @memberOf db-decorators.operations
22
+ */
23
+ export type IdOperationHandler<T extends DBModel, Y extends IRepository<T>, V> = (this: Y, decorator: V, key: any, id: string) => Promise<void> | void;
24
+ /**
25
+ * @typedef AfterOperationHandler
26
+ * @memberOf db-decorators.operations
27
+ */
28
+ export type UpdateOperationHandler<T extends DBModel, Y extends IRepository<T>, V> = (this: Y, decorator: V, key: any, model: T, oldModel: T) => Promise<void> | void;
File without changes
@@ -0,0 +1,24 @@
1
+ import { IRepository } from "../interfaces/IRepository";
2
+ import { DBModel } from "../model/DBModel";
3
+ import { Constructor } from "@decaf-ts/decorator-validation";
4
+ import { DataCache } from "./DataCache";
5
+ export declare abstract class BaseRepository<T extends DBModel> implements IRepository<T> {
6
+ private readonly _class;
7
+ private _cache?;
8
+ get class(): Constructor<T>;
9
+ get cache(): DataCache;
10
+ protected constructor();
11
+ create(model: T, ...args: any[]): Promise<T>;
12
+ protected createPrefix(model: T, ...args: any[]): Promise<any[]>;
13
+ protected createSuffix(model: T): Promise<T>;
14
+ read(key: string, ...args: any[]): Promise<T>;
15
+ protected readSuffix(model: T): Promise<T>;
16
+ protected readPrefix(key: string, ...args: any[]): Promise<any[]>;
17
+ update(model: T, ...args: any[]): Promise<T>;
18
+ protected updateSuffix(model: T): Promise<T>;
19
+ protected updatePrefix(model: T, ...args: any[]): Promise<any[]>;
20
+ delete(key: string, ...args: any[]): Promise<T>;
21
+ protected deleteSuffix(model: T): Promise<T>;
22
+ protected deletePrefix(key: any, ...args: any[]): Promise<any[]>;
23
+ toString(): string;
24
+ }
@@ -0,0 +1 @@
1
+ import{sf}from"@decaf-ts/decorator-validation";import{enforceDBDecorators}from"./utils";import{OperationKeys}from"../operations/constants";import{InternalError}from"./errors";import{DataCache}from"./DataCache";import{wrapMethod}from"./wrappers";import{findModelId}from"../identity";class BaseRepository{get class(){if(this._class)return this._class;throw new InternalError("No class definition found for this repository")}get cache(){return this._cache||(this._cache=new DataCache),this._cache}constructor(){const t=this;[this.create,this.read,this.update,this.delete].forEach(e=>{var r=e.name;wrapMethod(t,t[r+"Prefix"],e,t[r+"Suffix"])})}async create(e){throw new Error("Child classes must implement this.")}async createPrefix(e,...r){return e=new this.class(e),await enforceDBDecorators(this,e,OperationKeys.CREATE,OperationKeys.ON),[e,...r]}async createSuffix(e){return await enforceDBDecorators(this,e,OperationKeys.CREATE,OperationKeys.AFTER),e}async read(e){throw new Error("Child classes must implement this")}async readSuffix(e){return await enforceDBDecorators(this,e,OperationKeys.READ,OperationKeys.AFTER),e}async readPrefix(e,...r){var t=new this.class;return await enforceDBDecorators(this,t,OperationKeys.READ,OperationKeys.ON),[e,...r]}async update(e){throw new Error("Child classes must implement this")}async updateSuffix(e){return e=new this.class(e),await enforceDBDecorators(this,e,OperationKeys.UPDATE,OperationKeys.AFTER),e}async updatePrefix(e,...r){var t=findModelId(e),t=await this.read(t);return await enforceDBDecorators(this,e,OperationKeys.UPDATE,OperationKeys.ON,t),[e,...r]}async delete(e){throw new Error("Child classes must implement this")}async deleteSuffix(e){return await enforceDBDecorators(this,e,OperationKeys.DELETE,OperationKeys.AFTER),e}async deletePrefix(e,...r){var t=await this.read(e,...r);return await enforceDBDecorators(this,t,OperationKeys.DELETE,OperationKeys.ON),[e,...r]}toString(){return sf("[{0}] - Repository for {1}",this.constructor.name,this.class.name)}}export{BaseRepository};
@@ -0,0 +1,8 @@
1
+ export declare class DataCache {
2
+ private cache;
3
+ get(key: string): Promise<any>;
4
+ push(key: string, value: any): Promise<void>;
5
+ put(key: string, value: any): Promise<void>;
6
+ pop(key: string): Promise<any>;
7
+ purge(key?: string): Promise<void>;
8
+ }
@@ -0,0 +1 @@
1
+ import{ConflictError,NotFoundError}from"./errors";class DataCache{constructor(){this.cache={}}async get(t){if(t in this.cache)return this.cache[t];throw new NotFoundError(`Key ${t} not in dataStore`)}async push(t,r){if(t in this.cache)throw new ConflictError(`Key ${t} already in dataStore`);this.cache[t]=r}async put(t,r){this.cache[t]=r}async pop(t){var r=this.get(t);return delete this.cache[t],r}async purge(t){t?await this.pop(t):this.cache={}}}export{DataCache};
@@ -0,0 +1,12 @@
1
+ import { DBModel } from "../model/DBModel";
2
+ import { BaseRepository } from "./BaseRepository";
3
+ export declare abstract class Repository<T extends DBModel> extends BaseRepository<T> {
4
+ protected constructor();
5
+ create(model: T): Promise<T>;
6
+ protected createPrefix(model: T, ...args: any[]): Promise<[T, ...any[]]>;
7
+ delete(key: string | number): Promise<T>;
8
+ protected deletePrefix(key: string | number, ...args: any[]): Promise<[string | number, ...any[]]>;
9
+ read(key: string | number): Promise<T>;
10
+ update(model: T): Promise<T>;
11
+ protected updatePrefix(model: T, ...args: any[]): Promise<[T, ...args: any[]]>;
12
+ }
@@ -0,0 +1 @@
1
+ import{sf}from"@decaf-ts/decorator-validation";import{enforceDBDecorators}from"./utils";import{OperationKeys}from"../operations/constants";import{ConflictError,NotFoundError,ValidationError}from"./errors";import{BaseRepository}from"./BaseRepository";import{findModelId}from"../identity";class Repository extends BaseRepository{constructor(){super()}async create(r){throw new Error("Child classes must implement this.")}async createPrefix(t,...e){t=new this.class(t),await enforceDBDecorators(this,t,OperationKeys.CREATE,OperationKeys.ON);var r=t.hasErrors();if(r)throw new ValidationError(r.toString());let o;try{if(!(o=findModelId(t,!0)))return[t,...e];o&&await this.read(o.toString())}catch(r){if(r instanceof NotFoundError)return[t,...e];throw r}throw new ConflictError(sf("Model with id {0} already exists",o))}async delete(r){throw new Error("Child classes must implement this.")}async deletePrefix(r,...t){var e=new this.class;return await enforceDBDecorators(this,e,OperationKeys.DELETE,OperationKeys.ON),await this.read(r),[r,...t]}async read(r){throw new Error("Child classes must implement this.")}async update(r){throw new Error("Child classes must implement this.")}async updatePrefix(r,...t){r=new this.class(r);var e=findModelId(r),e=await this.read(e),e=(await enforceDBDecorators(this,r,OperationKeys.UPDATE,OperationKeys.ON,e),r.hasErrors(e));if(e)throw new ValidationError(e.toString());return[r,...t]}}export{Repository};
@@ -0,0 +1,6 @@
1
+ import { CascadeMetadata } from "./types";
2
+ export declare enum Cascade {
3
+ CASCADE = "cascade",
4
+ NONE = "none"
5
+ }
6
+ export declare const DefaultCascade: CascadeMetadata;
@@ -0,0 +1 @@
1
+ var Cascade;!function(a){a.CASCADE="cascade",a.NONE="none"}(Cascade=Cascade||{});const DefaultCascade={update:Cascade.CASCADE,delete:Cascade.NONE};export{Cascade,DefaultCascade};
@@ -0,0 +1,3 @@
1
+ import { Constructor } from "@decaf-ts/decorator-validation";
2
+ import { DBModel } from "../model/DBModel";
3
+ export declare function repository<T extends DBModel>(model: Constructor<T>, nameOverride?: string): (original: any, propertyKey?: string) => any;
@@ -0,0 +1 @@
1
+ import{inject,injectable}from"@decaf-ts/injectable-decorators";import{metadata}from"@decaf-ts/reflection";import{getDBKey}from"../model/decorators";import{DBKeys}from"../model/constants";import{InternalError}from"./errors";function repository(o,a){return(e,r)=>{if(r){var t=Reflect.getMetadata(getDBKey(DBKeys.REPOSITORY),o);if(t)return inject(t)(e,r);throw new InternalError("Could not find repository for "+o.name)}return metadata(getDBKey(DBKeys.REPOSITORY),a||e.name)(o),injectable(a,!0,e=>{Object.defineProperty(e,DBKeys.CLASS,{enumerable:!1,configurable:!1,writable:!1,value:o})})(e)}}export{repository};
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @summary Base Error
3
+ *
4
+ * @param {string} msg the error message
5
+ *
6
+ * @class BaseDLTError
7
+ * @extends Error
8
+ */
9
+ export declare abstract class BaseError extends Error {
10
+ protected constructor(name: string, msg: string | Error);
11
+ }
12
+ /**
13
+ * @summary Represents a failure in the Model details
14
+ *
15
+ * @param {string} msg the error message
16
+ *
17
+ * @class ValidationError
18
+ * @extends BaseError
19
+ */
20
+ export declare class ValidationError extends BaseError {
21
+ constructor(msg: string | Error);
22
+ }
23
+ /**
24
+ * @summary Represents an internal failure (should mean an error in code)
25
+ *
26
+ * @param {string} msg the error message
27
+ *
28
+ * @class InternalError
29
+ * @extends BaseError
30
+ */
31
+ export declare class InternalError extends BaseError {
32
+ constructor(msg: string | Error);
33
+ }
34
+ /**
35
+ * @summary Represents a failure in the Model de/serialization
36
+ *
37
+ * @param {string} msg the error message
38
+ *
39
+ * @class SerializationError
40
+ * @extends BaseError
41
+ *
42
+ */
43
+ export declare class SerializationError extends BaseError {
44
+ constructor(msg: string | Error);
45
+ }
46
+ /**
47
+ * @summary Represents a failure in finding a model
48
+ *
49
+ * @param {string} msg the error message
50
+ *
51
+ * @class NotFoundError
52
+ * @extends BaseError
53
+ *
54
+ */
55
+ export declare class NotFoundError extends BaseError {
56
+ constructor(msg: string | Error);
57
+ }
58
+ /**
59
+ * @summary Represents a conflict in the storage
60
+ *
61
+ * @param {string} msg the error message
62
+ *
63
+ * @class ConflictError
64
+ * @extends BaseError
65
+ *
66
+ */
67
+ export declare class ConflictError extends BaseError {
68
+ constructor(msg: string | Error);
69
+ }
@@ -0,0 +1 @@
1
+ class BaseError extends Error{constructor(r,o){if(o instanceof BaseError)return o;super(`[${r}] `+(o instanceof Error?o.message:o)),o instanceof Error&&(this.stack=o.stack)}}class ValidationError extends BaseError{constructor(r){super(ValidationError.name,r)}}class InternalError extends BaseError{constructor(r){super(InternalError.name,r)}}class SerializationError extends BaseError{constructor(r){super(SerializationError.name,r)}}class NotFoundError extends BaseError{constructor(r){super(NotFoundError.name,r)}}class ConflictError extends BaseError{constructor(r){super(ConflictError.name,r)}}export{BaseError,ValidationError,InternalError,SerializationError,NotFoundError,ConflictError};
@@ -0,0 +1,9 @@
1
+ export * from "./BaseRepository";
2
+ export * from "./constants";
3
+ export * from "./DataCache";
4
+ export * from "./decorators";
5
+ export * from "./errors";
6
+ export * from "./Repository";
7
+ export * from "./types";
8
+ export * from "./utils";
9
+ export * from "./wrappers";
@@ -0,0 +1 @@
1
+ export*from"./BaseRepository";export*from"./constants";export*from"./DataCache";export*from"./decorators";export*from"./errors";export*from"./Repository";export*from"./types";export*from"./utils";export*from"./wrappers";
@@ -0,0 +1,13 @@
1
+ import { Cascade } from "./constants";
2
+ import { OrderDirection } from "../validation/constants";
3
+ /**
4
+ * @summary defines the cascading behaviour
5
+ */
6
+ export type CascadeMetadata = {
7
+ update: Cascade;
8
+ delete: Cascade;
9
+ };
10
+ export type IndexMetadata = {
11
+ directions?: OrderDirection[2];
12
+ compositions?: string[];
13
+ };
File without changes
@@ -0,0 +1,57 @@
1
+ import { DBModel } from "../model/DBModel";
2
+ import { IRepository } from "../interfaces/IRepository";
3
+ import { DecoratorMetadata } from "@decaf-ts/reflection";
4
+ import { Constructor } from "@decaf-ts/decorator-validation";
5
+ /**
6
+ * @summary retrieves the arguments for the handler
7
+ * @param {any} dec the decorator
8
+ * @param {string} prop the property name
9
+ * @param {{}} m the model
10
+ * @param {{}} [accum] accumulator used for internal recursiveness
11
+ *
12
+ * @function getHandlerArgs
13
+ * @memberOf module:db-decorators.Repository
14
+ */
15
+ export declare const getHandlerArgs: (dec: any, prop: string, m: Constructor<any>, accum?: Record<string, {
16
+ args: string[];
17
+ }>) => Record<string, {
18
+ args: string[];
19
+ }> | void;
20
+ /**
21
+ *
22
+ * @param {IRepository<T>} repo
23
+ * @param {T} model
24
+ * @param operation
25
+ * @param prefix
26
+ *
27
+ * @param oldModel
28
+ * @function enforceDBPropertyDecoratorsAsync
29
+ *
30
+ * @memberOf db-decorators.utils
31
+ */
32
+ export declare function enforceDBDecorators<T extends DBModel, Y extends IRepository<T>, V>(repo: Y, model: T, operation: string, prefix: string, oldModel?: T): Promise<void>;
33
+ /**
34
+ * Specific for DB Decorators
35
+ * @param {T} model
36
+ * @param {string} operation CRUD {@link OperationKeys}
37
+ * @param {string} [extraPrefix]
38
+ *
39
+ * @function getDbPropertyDecorators
40
+ *
41
+ * @memberOf db-decorators.utils
42
+ */
43
+ export declare function getDbDecorators<T extends DBModel>(model: T, operation: string, extraPrefix?: string): Record<string, DecoratorMetadata[]> | undefined;
44
+ /**
45
+ * @summary Retrieves the decorators for an object's properties prefixed by {@param prefixes} recursively
46
+ * @param model
47
+ * @param accum
48
+ * @param prefixes
49
+ *
50
+ * @function getAllPropertyDecoratorsRecursive
51
+ * @memberOf module:db-decorators.Repository
52
+ */
53
+ export declare const getAllPropertyDecoratorsRecursive: <T extends DBModel>(model: T, accum: {
54
+ [indexer: string]: any[];
55
+ } | undefined, ...prefixes: string[]) => {
56
+ [indexer: string]: any[];
57
+ } | undefined;
@@ -0,0 +1 @@
1
+ import{Operations}from"../operations/Operations";import{OperationKeys}from"../operations/constants";import{getAllPropertyDecorators}from"@decaf-ts/reflection";import{InternalError}from"./errors";import{ModelKeys,sf}from"@decaf-ts/decorator-validation";const getHandlerArgs=function(e,r,t,o){var n=t.constructor.name;if(!n)throw new InternalError("Could not determine model class");o=o||{},e.props.handlers[n]&&e.props.handlers[n][r]&&(o={...e.props.handlers[n][r],...o});let a=Object.getPrototypeOf(t);return a===Object.prototype?o:(a.constructor.name===n&&(a=Object.getPrototypeOf(a)),getHandlerArgs(e,r,a,o))};async function enforceDBDecorators(r,t,o,e,n){var a=getDbDecorators(t,o,e);if(a)for(const f in a)for(const d of a[f]){var s=d["key"],i=Operations.get(t,f,e+s);if(!i||!i.length)throw new InternalError(`Could not find registered handler for the operation ${e+s} under property `+f);var p=getHandlerArgs(d,f,t);if(!p||Object.values(p).length!==i.length)throw new InternalError(sf("Args and handlers length do not match"));for(let e=0;e<i.length;e++){var c=i[e],l=[Object.values(p)[e].data,f,t];if(o===OperationKeys.UPDATE){if(!n)throw new InternalError("Missing old model for update operation");l.push(n)}await c.apply(r,l)}}}function getDbDecorators(e,o,r){const n=getAllPropertyDecorators(e,OperationKeys.REFLECT+(r||""));if(n)return Object.keys(n).reduce((e,r)=>{var t=n[r].filter(e=>e.key===o);return t&&t.length&&((e=e||{})[r]=t),e},void 0)}const getAllPropertyDecoratorsRecursive=function(e,r,...t){const s=r||{};function o(e){Object.entries(e).forEach(([e,r])=>{var a;s[e]=s[e]||[],[a,...e]=[e,...r],e.forEach(r=>{var e;if((e=s[a].find(e=>e.key===r.key))&&e.props.operation===r.props.operation){if(r.key!==ModelKeys.TYPE){var{handlers:t,operation:o}=r.props;if(o&&o.match(new RegExp(`^(:?${OperationKeys.ON}|${OperationKeys.AFTER})(:?${OperationKeys.CREATE}|${OperationKeys.READ}|${OperationKeys.UPDATE}|${OperationKeys.DELETE})$`))){const n=e.props.handlers;Object.entries(t).forEach(([o,e])=>{o in n?Object.entries(e).forEach(([t,e])=>{t in n[o]?Object.entries(e).forEach(([e,r])=>{e in n[o][t]?console.warn(sf("Skipping handler registration for {0} under prop {0} because handler is the same",o,t)):n[o][t][e]=r}):n[o][t]=e}):n[o]=e})}else s[a].push(r)}}else s[a].push(r)})})}var r=getAllPropertyDecorators(e,...t);return r&&o(r),Object.getPrototypeOf(e)!==Object.prototype&&(r=Object.getPrototypeOf(e))?getAllPropertyDecoratorsRecursive(r,s,...t):s};export{getHandlerArgs,enforceDBDecorators,getDbDecorators,getAllPropertyDecoratorsRecursive};
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @summary Util method to change a method of an object prefixing it with another
3
+ * @param {any} obj The Base Object
4
+ * @param {Function} after The original method
5
+ * @param {Function} prefix The Prefix method. The output will be used as arguments in the original method
6
+ * @param {string} [afterName] When the after function anme cannot be extracted, pass it here
7
+ *
8
+ * @function prefixMethod
9
+ *
10
+ * @memberOf module:db-decorators.Repository
11
+ */
12
+ export declare function prefixMethod(obj: any, after: (...args: any[]) => any, prefix: (...args: any[]) => any, afterName?: string): void;
13
+ /**
14
+ * @summary Util method to change a method of an object suffixing it with another
15
+ * @param {any} obj The Base Object
16
+ * @param {Function} before The original method
17
+ * @param {Function} suffix The Prefix method. The output will be used as arguments in the original method
18
+ * @param {string} [beforeName] When the after function anme cannot be extracted, pass it here
19
+ *
20
+ * @function suffixMethod
21
+ *
22
+ * @memberOf module:db-decorators.Repository
23
+ */
24
+ export declare function suffixMethod(obj: any, before: (...args: any[]) => any, suffix: (...args: any[]) => any, beforeName?: string): void;
25
+ /**
26
+ * @summary Util method to wrap a method of an object with additional logic
27
+ *
28
+ * @param {any} obj The Base Object
29
+ * @param {Function} before the method to be prefixed
30
+ * @param {Function} method the method to be wrapped
31
+ * @param {Function} after The method to be suffixed
32
+ * @param {string} [methodName] When the after function anme cannot be extracted, pass it here
33
+ *
34
+ * @function wrapMethod
35
+ *
36
+ * @memberOf module:db-decorators.Repository
37
+ */
38
+ export declare function wrapMethod(obj: any, before: (...args: any[]) => any, method: (...args: any[]) => any, after: (...args: any[]) => any, methodName?: string): void;
@@ -0,0 +1 @@
1
+ function prefixMethod(e,a,r,i){var n=async function(...e){return e=await Promise.resolve(r.call(this,...e)),Promise.resolve(a.apply(this,e))}.bind(e),i=i||a.name;Object.defineProperty(n,"name",{enumerable:!0,configurable:!0,writable:!1,value:i}),e[i]=n}function suffixMethod(e,a,r,i){var n=async function(...e){return e=await Promise.resolve(a.call(this,...e)),r.call(this,...e)}.bind(e),i=i||a.name;Object.defineProperty(n,"name",{enumerable:!0,configurable:!0,writable:!1,value:i}),e[i]=n}function wrapMethod(a,r,i,n,e){var t=async function(...e){return e=await Promise.resolve(r.call(a,...e)),e=await Promise.resolve(i.call(a,...e)),Promise.resolve(n.call(this,e))}.bind(a),e=e||i.name;Object.defineProperty(t,"name",{enumerable:!0,configurable:!0,writable:!1,value:e}),a[e]=t}export{prefixMethod,suffixMethod,wrapMethod};
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @summary holds the default error messages
3
+ * @const DEFAULT_ERROR_MESSAGES
4
+ *
5
+ * @memberOf module:db-decorators.Model
6
+ */
7
+ export declare const DEFAULT_ERROR_MESSAGES: {
8
+ ID: {
9
+ INVALID: string;
10
+ REQUIRED: string;
11
+ };
12
+ READONLY: {
13
+ INVALID: string;
14
+ };
15
+ TIMESTAMP: {
16
+ REQUIRED: string;
17
+ DATE: string;
18
+ INVALID: string;
19
+ };
20
+ };
21
+ /**
22
+ * @summary Update reflection keys
23
+ * @const UpdateValidationKeys
24
+ * @memberOf module:db-decorators.Operations
25
+ */
26
+ export declare const UpdateValidationKeys: {
27
+ REFLECT: string;
28
+ TIMESTAMP: string;
29
+ READONLY: string;
30
+ };
31
+ /**
32
+ * @summary defines order directions when sorting
33
+ *
34
+ * @constant OrderDirection
35
+ *
36
+ * @category Query
37
+ */
38
+ export declare enum OrderDirection {
39
+ /**
40
+ * @summary Defines the sort order as ascending
41
+ * @prop ASC
42
+ */
43
+ ASC = "asc",
44
+ /**
45
+ * @summary Defines the sort order as descending
46
+ * @property {string} DSC
47
+ */
48
+ DSC = "desc"
49
+ }
@@ -0,0 +1 @@
1
+ import{DBKeys}from"../model/constants";const DEFAULT_ERROR_MESSAGES={ID:{INVALID:"This Id is invalid",REQUIRED:"The Id is mandatory"},READONLY:{INVALID:"This cannot be updated"},TIMESTAMP:{REQUIRED:"Timestamp is Mandatory",DATE:"The Timestamp must the a valid date",INVALID:"This value must always increase"}},UpdateValidationKeys={REFLECT:"db.update.validation.",TIMESTAMP:DBKeys.TIMESTAMP,READONLY:DBKeys.READONLY};var OrderDirection;!function(e){e.ASC="asc",e.DSC="desc"}(OrderDirection=OrderDirection||{});export{DEFAULT_ERROR_MESSAGES,UpdateValidationKeys,OrderDirection};