@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.
- package/LICENSE.md +19 -0
- package/README.md +160 -0
- package/dist/db-decorators.bundle.min.js +2 -0
- package/dist/db-decorators.bundle.min.js.LICENSE.txt +14 -0
- package/dist/esm/db-decorators.bundle.min.esm.js +2 -0
- package/dist/esm/db-decorators.bundle.min.esm.js.LICENSE.txt +14 -0
- package/lib/esm/identity/decorators.d.ts +1 -0
- package/lib/esm/identity/decorators.js +1 -0
- package/lib/esm/identity/index.d.ts +2 -0
- package/lib/esm/identity/index.js +1 -0
- package/lib/esm/identity/utils.d.ts +36 -0
- package/lib/esm/identity/utils.js +1 -0
- package/lib/esm/index.d.ts +33 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/interfaces/CrudOperator.d.ts +42 -0
- package/lib/esm/interfaces/CrudOperator.js +0 -0
- package/lib/esm/interfaces/IRepository.d.ts +13 -0
- package/lib/esm/interfaces/IRepository.js +0 -0
- package/lib/esm/interfaces/index.d.ts +2 -0
- package/lib/esm/interfaces/index.js +1 -0
- package/lib/esm/model/DBModel.d.ts +25 -0
- package/lib/esm/model/DBModel.js +1 -0
- package/lib/esm/model/constants.d.ts +36 -0
- package/lib/esm/model/constants.js +1 -0
- package/lib/esm/model/decorators.d.ts +25 -0
- package/lib/esm/model/decorators.js +1 -0
- package/lib/esm/model/index.d.ts +3 -0
- package/lib/esm/model/index.js +1 -0
- package/lib/esm/model/validation.d.ts +15 -0
- package/lib/esm/model/validation.js +1 -0
- package/lib/esm/operations/Operations.d.ts +19 -0
- package/lib/esm/operations/Operations.js +1 -0
- package/lib/esm/operations/OperationsRegistry.d.ts +34 -0
- package/lib/esm/operations/OperationsRegistry.js +1 -0
- package/lib/esm/operations/constants.d.ts +23 -0
- package/lib/esm/operations/constants.js +1 -0
- package/lib/esm/operations/decorators.d.ts +166 -0
- package/lib/esm/operations/decorators.js +1 -0
- package/lib/esm/operations/index.d.ts +5 -0
- package/lib/esm/operations/index.js +1 -0
- package/lib/esm/operations/types.d.ts +28 -0
- package/lib/esm/operations/types.js +0 -0
- package/lib/esm/repository/BaseRepository.d.ts +24 -0
- package/lib/esm/repository/BaseRepository.js +1 -0
- package/lib/esm/repository/DataCache.d.ts +8 -0
- package/lib/esm/repository/DataCache.js +1 -0
- package/lib/esm/repository/Repository.d.ts +12 -0
- package/lib/esm/repository/Repository.js +1 -0
- package/lib/esm/repository/constants.d.ts +6 -0
- package/lib/esm/repository/constants.js +1 -0
- package/lib/esm/repository/decorators.d.ts +3 -0
- package/lib/esm/repository/decorators.js +1 -0
- package/lib/esm/repository/errors.d.ts +69 -0
- package/lib/esm/repository/errors.js +1 -0
- package/lib/esm/repository/index.d.ts +9 -0
- package/lib/esm/repository/index.js +1 -0
- package/lib/esm/repository/types.d.ts +13 -0
- package/lib/esm/repository/types.js +0 -0
- package/lib/esm/repository/utils.d.ts +57 -0
- package/lib/esm/repository/utils.js +1 -0
- package/lib/esm/repository/wrappers.d.ts +38 -0
- package/lib/esm/repository/wrappers.js +1 -0
- package/lib/esm/validation/comparison.d.ts +1 -0
- package/lib/esm/validation/comparison.js +1 -0
- package/lib/esm/validation/constants.d.ts +49 -0
- package/lib/esm/validation/constants.js +1 -0
- package/lib/esm/validation/decorators.d.ts +70 -0
- package/lib/esm/validation/decorators.js +1 -0
- package/lib/esm/validation/index.d.ts +3 -0
- package/lib/esm/validation/index.js +1 -0
- package/lib/esm/validation/validators/ReadOnlyValidator.d.ts +23 -0
- package/lib/esm/validation/validators/ReadOnlyValidator.js +1 -0
- package/lib/esm/validation/validators/TimestampValidator.d.ts +14 -0
- package/lib/esm/validation/validators/TimestampValidator.js +1 -0
- package/lib/esm/validation/validators/UpdateValidator.d.ts +23 -0
- package/lib/esm/validation/validators/UpdateValidator.js +1 -0
- package/lib/esm/validation/validators/index.d.ts +3 -0
- package/lib/esm/validation/validators/index.js +1 -0
- package/lib/identity/decorators.cjs +1 -0
- package/lib/identity/decorators.d.ts +1 -0
- package/lib/identity/index.cjs +1 -0
- package/lib/identity/index.d.ts +2 -0
- package/lib/identity/utils.cjs +1 -0
- package/lib/identity/utils.d.ts +36 -0
- package/lib/index.cjs +1 -0
- package/lib/index.d.ts +33 -0
- package/lib/interfaces/CrudOperator.cjs +1 -0
- package/lib/interfaces/CrudOperator.d.ts +42 -0
- package/lib/interfaces/IRepository.cjs +1 -0
- package/lib/interfaces/IRepository.d.ts +13 -0
- package/lib/interfaces/index.cjs +1 -0
- package/lib/interfaces/index.d.ts +2 -0
- package/lib/model/DBModel.cjs +1 -0
- package/lib/model/DBModel.d.ts +25 -0
- package/lib/model/constants.cjs +1 -0
- package/lib/model/constants.d.ts +36 -0
- package/lib/model/decorators.cjs +1 -0
- package/lib/model/decorators.d.ts +25 -0
- package/lib/model/index.cjs +1 -0
- package/lib/model/index.d.ts +3 -0
- package/lib/model/validation.cjs +1 -0
- package/lib/model/validation.d.ts +15 -0
- package/lib/operations/Operations.cjs +1 -0
- package/lib/operations/Operations.d.ts +19 -0
- package/lib/operations/OperationsRegistry.cjs +1 -0
- package/lib/operations/OperationsRegistry.d.ts +34 -0
- package/lib/operations/constants.cjs +1 -0
- package/lib/operations/constants.d.ts +23 -0
- package/lib/operations/decorators.cjs +1 -0
- package/lib/operations/decorators.d.ts +166 -0
- package/lib/operations/index.cjs +1 -0
- package/lib/operations/index.d.ts +5 -0
- package/lib/operations/types.cjs +1 -0
- package/lib/operations/types.d.ts +28 -0
- package/lib/repository/BaseRepository.cjs +1 -0
- package/lib/repository/BaseRepository.d.ts +24 -0
- package/lib/repository/DataCache.cjs +1 -0
- package/lib/repository/DataCache.d.ts +8 -0
- package/lib/repository/Repository.cjs +1 -0
- package/lib/repository/Repository.d.ts +12 -0
- package/lib/repository/constants.cjs +1 -0
- package/lib/repository/constants.d.ts +6 -0
- package/lib/repository/decorators.cjs +1 -0
- package/lib/repository/decorators.d.ts +3 -0
- package/lib/repository/errors.cjs +1 -0
- package/lib/repository/errors.d.ts +69 -0
- package/lib/repository/index.cjs +1 -0
- package/lib/repository/index.d.ts +9 -0
- package/lib/repository/types.cjs +1 -0
- package/lib/repository/types.d.ts +13 -0
- package/lib/repository/utils.cjs +1 -0
- package/lib/repository/utils.d.ts +57 -0
- package/lib/repository/wrappers.cjs +1 -0
- package/lib/repository/wrappers.d.ts +38 -0
- package/lib/validation/comparison.cjs +1 -0
- package/lib/validation/comparison.d.ts +1 -0
- package/lib/validation/constants.cjs +1 -0
- package/lib/validation/constants.d.ts +49 -0
- package/lib/validation/decorators.cjs +1 -0
- package/lib/validation/decorators.d.ts +70 -0
- package/lib/validation/index.cjs +1 -0
- package/lib/validation/index.d.ts +3 -0
- package/lib/validation/validators/ReadOnlyValidator.cjs +1 -0
- package/lib/validation/validators/ReadOnlyValidator.d.ts +23 -0
- package/lib/validation/validators/TimestampValidator.cjs +1 -0
- package/lib/validation/validators/TimestampValidator.d.ts +14 -0
- package/lib/validation/validators/UpdateValidator.cjs +1 -0
- package/lib/validation/validators/UpdateValidator.d.ts +23 -0
- package/lib/validation/validators/index.cjs +1 -0
- package/lib/validation/validators/index.d.ts +3 -0
- package/package.json +107 -0
|
@@ -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
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&("get"in i?r.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){e[o=void 0===o?t:o]=r[t]}),__exportStar=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||__createBinding(r,e,t)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./constants.cjs"),exports),__exportStar(require("./decorators.cjs"),exports),__exportStar(require("./Operations.cjs"),exports),__exportStar(require("./OperationsRegistry.cjs"),exports),__exportStar(require("./types.cjs"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|
|
@@ -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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BaseRepository=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),utils_1=require("./utils.cjs"),constants_1=require("../operations/constants.cjs"),errors_1=require("./errors.cjs"),DataCache_1=require("./DataCache.cjs"),wrappers_1=require("./wrappers.cjs"),identity_1=require("../identity/index.cjs");class BaseRepository{get class(){if(this._class)return this._class;throw new errors_1.InternalError("No class definition found for this repository")}get cache(){return this._cache||(this._cache=new DataCache_1.DataCache),this._cache}constructor(){const s=this;[this.create,this.read,this.update,this.delete].forEach(t=>{var e=t.name;(0,wrappers_1.wrapMethod)(s,s[e+"Prefix"],t,s[e+"Suffix"])})}async create(t){throw new Error("Child classes must implement this.")}async createPrefix(t,...e){return t=new this.class(t),await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.CREATE,constants_1.OperationKeys.ON),[t,...e]}async createSuffix(t){return await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.CREATE,constants_1.OperationKeys.AFTER),t}async read(t){throw new Error("Child classes must implement this")}async readSuffix(t){return await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.READ,constants_1.OperationKeys.AFTER),t}async readPrefix(t,...e){var s=new this.class;return await(0,utils_1.enforceDBDecorators)(this,s,constants_1.OperationKeys.READ,constants_1.OperationKeys.ON),[t,...e]}async update(t){throw new Error("Child classes must implement this")}async updateSuffix(t){return t=new this.class(t),await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.UPDATE,constants_1.OperationKeys.AFTER),t}async updatePrefix(t,...e){var s=(0,identity_1.findModelId)(t),s=await this.read(s);return await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.UPDATE,constants_1.OperationKeys.ON,s),[t,...e]}async delete(t){throw new Error("Child classes must implement this")}async deleteSuffix(t){return await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.DELETE,constants_1.OperationKeys.AFTER),t}async deletePrefix(t,...e){var s=await this.read(t,...e);return await(0,utils_1.enforceDBDecorators)(this,s,constants_1.OperationKeys.DELETE,constants_1.OperationKeys.ON),[t,...e]}toString(){return(0,decorator_validation_1.sf)("[{0}] - Repository for {1}",this.constructor.name,this.class.name)}}exports.BaseRepository=BaseRepository;
|
|
@@ -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
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DataCache=void 0;const errors_1=require("./errors.cjs");class DataCache{constructor(){this.cache={}}async get(e){if(e in this.cache)return this.cache[e];throw new errors_1.NotFoundError(`Key ${e} not in dataStore`)}async push(e,t){if(e in this.cache)throw new errors_1.ConflictError(`Key ${e} already in dataStore`);this.cache[e]=t}async put(e,t){this.cache[e]=t}async pop(e){var t=this.get(e);return delete this.cache[e],t}async purge(e){e?await this.pop(e):this.cache={}}}exports.DataCache=DataCache;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Repository=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),utils_1=require("./utils.cjs"),constants_1=require("../operations/constants.cjs"),errors_1=require("./errors.cjs"),BaseRepository_1=require("./BaseRepository.cjs"),identity_1=require("../identity/index.cjs");class Repository extends BaseRepository_1.BaseRepository{constructor(){super()}async create(r){throw new Error("Child classes must implement this.")}async createPrefix(e,...t){e=new this.class(e),await(0,utils_1.enforceDBDecorators)(this,e,constants_1.OperationKeys.CREATE,constants_1.OperationKeys.ON);var r=e.hasErrors();if(r)throw new errors_1.ValidationError(r.toString());let s;try{if(!(s=(0,identity_1.findModelId)(e,!0)))return[e,...t];s&&await this.read(s.toString())}catch(r){if(r instanceof errors_1.NotFoundError)return[e,...t];throw r}throw new errors_1.ConflictError((0,decorator_validation_1.sf)("Model with id {0} already exists",s))}async delete(r){throw new Error("Child classes must implement this.")}async deletePrefix(r,...e){var t=new this.class;return await(0,utils_1.enforceDBDecorators)(this,t,constants_1.OperationKeys.DELETE,constants_1.OperationKeys.ON),await this.read(r),[r,...e]}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,...e){r=new this.class(r);var t=(0,identity_1.findModelId)(r),t=await this.read(t),t=(await(0,utils_1.enforceDBDecorators)(this,r,constants_1.OperationKeys.UPDATE,constants_1.OperationKeys.ON,t),r.hasErrors(t));if(t)throw new errors_1.ValidationError(t.toString());return[r,...e]}}exports.Repository=Repository;
|
|
@@ -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
|
+
"use strict";var Cascade;Object.defineProperty(exports,"__esModule",{value:!0}),exports.DefaultCascade=exports.Cascade=void 0,function(e){e.CASCADE="cascade",e.NONE="none"}(Cascade||(exports.Cascade=Cascade={})),exports.DefaultCascade={update:Cascade.CASCADE,delete:Cascade.NONE};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.repository=void 0;const injectable_decorators_1=require("@decaf-ts/injectable-decorators"),reflection_1=require("@decaf-ts/reflection"),decorators_1=require("../model/decorators.cjs"),constants_1=require("../model/constants.cjs"),errors_1=require("./errors.cjs");function repository(o,s){return(e,r)=>{if(r){var t=Reflect.getMetadata((0,decorators_1.getDBKey)(constants_1.DBKeys.REPOSITORY),o);if(t)return(0,injectable_decorators_1.inject)(t)(e,r);throw new errors_1.InternalError("Could not find repository for "+o.name)}return(0,reflection_1.metadata)((0,decorators_1.getDBKey)(constants_1.DBKeys.REPOSITORY),s||e.name)(o),(0,injectable_decorators_1.injectable)(s,!0,e=>{Object.defineProperty(e,constants_1.DBKeys.CLASS,{enumerable:!1,configurable:!1,writable:!1,value:o})})(e)}}exports.repository=repository;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ConflictError=exports.NotFoundError=exports.SerializationError=exports.InternalError=exports.ValidationError=exports.BaseError=void 0;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(exports.BaseError=BaseError){constructor(r){super(ValidationError.name,r)}}exports.ValidationError=ValidationError;class InternalError extends BaseError{constructor(r){super(InternalError.name,r)}}exports.InternalError=InternalError;class SerializationError extends BaseError{constructor(r){super(SerializationError.name,r)}}exports.SerializationError=SerializationError;class NotFoundError extends BaseError{constructor(r){super(NotFoundError.name,r)}}exports.NotFoundError=NotFoundError;class ConflictError extends BaseError{constructor(r){super(ConflictError.name,r)}}exports.ConflictError=ConflictError;
|
|
@@ -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
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&("get"in i?r.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){e[o=void 0===o?t:o]=r[t]}),__exportStar=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||__createBinding(r,e,t)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./BaseRepository.cjs"),exports),__exportStar(require("./constants.cjs"),exports),__exportStar(require("./DataCache.cjs"),exports),__exportStar(require("./decorators.cjs"),exports),__exportStar(require("./errors.cjs"),exports),__exportStar(require("./Repository.cjs"),exports),__exportStar(require("./types.cjs"),exports),__exportStar(require("./utils.cjs"),exports),__exportStar(require("./wrappers.cjs"),exports);
|
|
@@ -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
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getAllPropertyDecoratorsRecursive=exports.getDbDecorators=exports.enforceDBDecorators=exports.getHandlerArgs=void 0;const Operations_1=require("../operations/Operations.cjs"),constants_1=require("../operations/constants.cjs"),reflection_1=require("@decaf-ts/reflection"),errors_1=require("./errors.cjs"),decorator_validation_1=require("@decaf-ts/decorator-validation"),getHandlerArgs=function(e,r,t,o){var n=t.constructor.name;if(!n)throw new errors_1.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 s=Object.getPrototypeOf(t);return s===Object.prototype?o:(s.constructor.name===n&&(s=Object.getPrototypeOf(s)),(0,exports.getHandlerArgs)(e,r,s,o))};async function enforceDBDecorators(r,t,o,e,n){var s=getDbDecorators(t,o,e);if(s)for(const d in s)for(const f of s[d]){var a=f["key"],c=Operations_1.Operations.get(t,d,e+a);if(!c||!c.length)throw new errors_1.InternalError(`Could not find registered handler for the operation ${e+a} under property `+d);var i=(0,exports.getHandlerArgs)(f,d,t);if(!i||Object.values(i).length!==c.length)throw new errors_1.InternalError((0,decorator_validation_1.sf)("Args and handlers length do not match"));for(let e=0;e<c.length;e++){var p=c[e],l=[Object.values(i)[e].data,d,t];if(o===constants_1.OperationKeys.UPDATE){if(!n)throw new errors_1.InternalError("Missing old model for update operation");l.push(n)}await p.apply(r,l)}}}function getDbDecorators(e,o,r){const n=(0,reflection_1.getAllPropertyDecorators)(e,constants_1.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)}exports.getHandlerArgs=getHandlerArgs,exports.enforceDBDecorators=enforceDBDecorators,exports.getDbDecorators=getDbDecorators;const getAllPropertyDecoratorsRecursive=function(e,r,...t){const a=r||{};function o(e){Object.entries(e).forEach(([e,r])=>{var s;a[e]=a[e]||[],[s,...e]=[e,...r],e.forEach(r=>{var e;if((e=a[s].find(e=>e.key===r.key))&&e.props.operation===r.props.operation){if(r.key!==decorator_validation_1.ModelKeys.TYPE){var{handlers:t,operation:o}=r.props;if(o&&o.match(new RegExp(`^(:?${constants_1.OperationKeys.ON}|${constants_1.OperationKeys.AFTER})(:?${constants_1.OperationKeys.CREATE}|${constants_1.OperationKeys.READ}|${constants_1.OperationKeys.UPDATE}|${constants_1.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((0,decorator_validation_1.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 a[s].push(r)}}else a[s].push(r)})})}var r=(0,reflection_1.getAllPropertyDecorators)(e,...t);return r&&o(r),Object.getPrototypeOf(e)!==Object.prototype&&(r=Object.getPrototypeOf(e))?(0,exports.getAllPropertyDecoratorsRecursive)(r,a,...t):a};exports.getAllPropertyDecoratorsRecursive=getAllPropertyDecoratorsRecursive;
|
|
@@ -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
|
+
"use strict";function prefixMethod(e,r,t,o){var a=async function(...e){return e=await Promise.resolve(t.call(this,...e)),Promise.resolve(r.apply(this,e))}.bind(e),o=o||r.name;Object.defineProperty(a,"name",{enumerable:!0,configurable:!0,writable:!1,value:o}),e[o]=a}function suffixMethod(e,r,t,o){var a=async function(...e){return e=await Promise.resolve(r.call(this,...e)),t.call(this,...e)}.bind(e),o=o||r.name;Object.defineProperty(a,"name",{enumerable:!0,configurable:!0,writable:!1,value:o}),e[o]=a}function wrapMethod(r,t,o,a,e){var i=async function(...e){return e=await Promise.resolve(t.call(r,...e)),e=await Promise.resolve(o.call(r,...e)),Promise.resolve(a.call(this,e))}.bind(r),e=e||o.name;Object.defineProperty(i,"name",{enumerable:!0,configurable:!0,writable:!1,value:e}),r[e]=i}Object.defineProperty(exports,"__esModule",{value:!0}),exports.wrapMethod=exports.suffixMethod=exports.prefixMethod=void 0,exports.prefixMethod=prefixMethod,exports.suffixMethod=suffixMethod,exports.wrapMethod=wrapMethod;
|
|
@@ -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
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OrderDirection=exports.UpdateValidationKeys=exports.DEFAULT_ERROR_MESSAGES=void 0;const constants_1=require("../model/constants.cjs");var OrderDirection;exports.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"}},exports.UpdateValidationKeys={REFLECT:"db.update.validation.",TIMESTAMP:constants_1.DBKeys.TIMESTAMP,READONLY:constants_1.DBKeys.READONLY},function(e){e.ASC="asc",e.DSC="desc"}(OrderDirection||(exports.OrderDirection=OrderDirection={}));
|
|
@@ -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
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.serialize=exports.serializeAfterAll=exports.serializeOnCreateUpdate=exports.unique=exports.uniqueOnCreateUpdate=exports.timestamp=exports.timestampHandler=exports.readonly=exports.getDBUpdateKey=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),constants_1=require("../model/constants.cjs"),constants_2=require("./constants.cjs"),constants_3=require("../operations/constants.cjs"),decorators_1=require("../operations/decorators.cjs"),errors_1=require("../repository/errors.cjs"),reflection_1=require("@decaf-ts/reflection"),decorators_2=require("../model/decorators.cjs");function getDBUpdateKey(e){return constants_2.UpdateValidationKeys.REFLECT+e}function readonly(e=constants_2.DEFAULT_ERROR_MESSAGES.READONLY.INVALID){return(0,reflection_1.metadata)(getDBUpdateKey(constants_1.DBKeys.READONLY),{message:e})}function timestampHandler(e,t,r){r[t]=new Date}function timestamp(e=constants_3.DBOperations.CREATE_UPDATE,t=constants_1.DEFAULT_TIMESTAMP_FORMAT){t=[(0,decorator_validation_1.date)(t,constants_2.DEFAULT_ERROR_MESSAGES.TIMESTAMP.DATE),(0,decorator_validation_1.required)(constants_2.DEFAULT_ERROR_MESSAGES.TIMESTAMP.REQUIRED),(0,decorators_1.on)(e,timestampHandler)];return-1!==e.indexOf(constants_3.OperationKeys.UPDATE)&&t.push((0,reflection_1.metadata)(getDBUpdateKey(constants_1.DBKeys.TIMESTAMP),{message:constants_2.DEFAULT_ERROR_MESSAGES.TIMESTAMP.INVALID})),(0,reflection_1.apply)(...t)}async function uniqueOnCreateUpdate(e,t,r){if(r[t]){try{await this.read(r[t])}catch(e){if(e instanceof errors_1.NotFoundError)return}throw new errors_1.ConflictError(`model already exists with ${t} equal to `+JSON.stringify(r[t],void 0,2))}}function unique(){return(0,reflection_1.apply)((0,decorators_1.onCreateUpdate)(uniqueOnCreateUpdate),(0,reflection_1.metadata)((0,decorators_2.getDBKey)(constants_1.DBKeys.UNIQUE),{}))}async function serializeOnCreateUpdate(e,t,r,a){if(r[t])try{r[t]=JSON.stringify(r[t])}catch(e){throw new errors_1.SerializationError((0,decorator_validation_1.sf)("Failed to serialize {0} property on {0} model",t,r.constructor.name))}}async function serializeAfterAll(e,t,r){if(r[t]&&"string"==typeof r[t])try{r[t]=JSON.parse(r[t])}catch(e){throw new errors_1.SerializationError((0,decorator_validation_1.sf)("Failed to deserialize {0} property on {0} model",t,r.constructor.name))}}function serialize(){return(0,reflection_1.apply)((0,decorators_1.onCreateUpdate)(serializeOnCreateUpdate),(0,decorators_1.after)(constants_3.DBOperations.ALL,serializeAfterAll),(0,decorator_validation_1.type)([String.name,Object.name]),(0,reflection_1.metadata)((0,decorators_2.getDBKey)(constants_1.DBKeys.SERIALIZE),{}))}exports.getDBUpdateKey=getDBUpdateKey,exports.readonly=readonly,exports.timestampHandler=timestampHandler,exports.timestamp=timestamp,exports.uniqueOnCreateUpdate=uniqueOnCreateUpdate,exports.unique=unique,exports.serializeOnCreateUpdate=serializeOnCreateUpdate,exports.serializeAfterAll=serializeAfterAll,exports.serialize=serialize;
|