@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,70 @@
|
|
|
1
|
+
import { OperationKeys } from "../operations/constants";
|
|
2
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
3
|
+
import { DBModel } from "../model/DBModel";
|
|
4
|
+
import { CustomDecorator } from "@decaf-ts/reflection";
|
|
5
|
+
export declare function getDBUpdateKey(str: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Marks the property as readonly.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} [message] the error message. Defaults to {@link DEFAULT_ERROR_MESSAGES.READONLY.INVALID}
|
|
10
|
+
*
|
|
11
|
+
* @decorator readonly
|
|
12
|
+
*
|
|
13
|
+
* @category Decorators
|
|
14
|
+
*/
|
|
15
|
+
export declare function readonly(message?: string): CustomDecorator<V>;
|
|
16
|
+
export declare function timestampHandler<T extends DBModel, V extends IRepository<T>, Y = any>(this: V, data: Y, key: string, model: T): void;
|
|
17
|
+
/**
|
|
18
|
+
* Marks the property as timestamp.
|
|
19
|
+
* Makes it {@link required}
|
|
20
|
+
* Makes it a {@link date}
|
|
21
|
+
*
|
|
22
|
+
* Date Format:
|
|
23
|
+
*
|
|
24
|
+
* <pre>
|
|
25
|
+
* Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java),
|
|
26
|
+
* I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
|
|
27
|
+
* You can display date, time, AM/PM, etc.
|
|
28
|
+
*
|
|
29
|
+
* Date and Time Patterns
|
|
30
|
+
* yy = 2-digit year; yyyy = full year
|
|
31
|
+
* M = digit month; MM = 2-digit month; MMM = short month name; MMMM = full month name
|
|
32
|
+
* EEEE = full weekday name; EEE = short weekday name
|
|
33
|
+
* d = digit day; dd = 2-digit day
|
|
34
|
+
* h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
|
|
35
|
+
* m = minutes; mm = 2-digit minutes; aaa = AM/PM
|
|
36
|
+
* s = seconds; ss = 2-digit seconds
|
|
37
|
+
* S = miliseconds
|
|
38
|
+
* </pre>
|
|
39
|
+
*
|
|
40
|
+
* @param {string[]} operation The {@link DBOperations} to act on. Defaults to {@link DBOperations.CREATE_UPDATE}
|
|
41
|
+
* @param {string} [format] The TimeStamp format. defaults to {@link DEFAULT_TIMESTAMP_FORMAT}
|
|
42
|
+
* @param {{new: UpdateValidator}} [validator] defaults to {@link TimestampValidator}
|
|
43
|
+
*
|
|
44
|
+
* @decorator timestamp
|
|
45
|
+
*
|
|
46
|
+
* @category Decorators
|
|
47
|
+
*/
|
|
48
|
+
export declare function timestamp(operation?: OperationKeys[], format?: string): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
49
|
+
export declare function uniqueOnCreateUpdate<T extends DBModel, V extends IRepository<T>, Y = any>(this: V, data: Y, key: string, model: T): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* @summary Unique Decorator
|
|
52
|
+
* @description Tags a property as unique.
|
|
53
|
+
* No other elements in that table can have the same property value
|
|
54
|
+
*
|
|
55
|
+
* @function unique
|
|
56
|
+
*
|
|
57
|
+
* @memberOf module:wallet-db.Decorators
|
|
58
|
+
*/
|
|
59
|
+
export declare function unique(): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
60
|
+
export declare function serializeOnCreateUpdate<T extends DBModel, V extends IRepository<T>, Y = any>(this: V, data: Y, key: string, model: T, oldModel: T): Promise<void>;
|
|
61
|
+
export declare function serializeAfterAll<T extends DBModel, V extends IRepository<T>, Y = any>(this: V, data: Y, key: string, model: T): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* @summary Serialize Decorator
|
|
64
|
+
* @description properties decorated will the serialized before stored in the db
|
|
65
|
+
*
|
|
66
|
+
* @function serialize
|
|
67
|
+
*
|
|
68
|
+
* @memberOf module:wallet-db.Decorators
|
|
69
|
+
*/
|
|
70
|
+
export declare function serialize(): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{date,required,sf,type}from"@decaf-ts/decorator-validation";import{DBKeys,DEFAULT_TIMESTAMP_FORMAT}from"../model/constants";import{DEFAULT_ERROR_MESSAGES,UpdateValidationKeys}from"./constants";import{DBOperations,OperationKeys}from"../operations/constants";import{after,on,onCreateUpdate}from"../operations/decorators";import{ConflictError,NotFoundError,SerializationError}from"../repository/errors";import{apply,metadata}from"@decaf-ts/reflection";import{getDBKey}from"../model/decorators";function getDBUpdateKey(e){return UpdateValidationKeys.REFLECT+e}function readonly(e=DEFAULT_ERROR_MESSAGES.READONLY.INVALID){return metadata(getDBUpdateKey(DBKeys.READONLY),{message:e})}function timestampHandler(e,t,r){r[t]=new Date}function timestamp(e=DBOperations.CREATE_UPDATE,t=DEFAULT_TIMESTAMP_FORMAT){t=[date(t,DEFAULT_ERROR_MESSAGES.TIMESTAMP.DATE),required(DEFAULT_ERROR_MESSAGES.TIMESTAMP.REQUIRED),on(e,timestampHandler)];return-1!==e.indexOf(OperationKeys.UPDATE)&&t.push(metadata(getDBUpdateKey(DBKeys.TIMESTAMP),{message:DEFAULT_ERROR_MESSAGES.TIMESTAMP.INVALID})),apply(...t)}async function uniqueOnCreateUpdate(e,t,r){if(r[t]){try{await this.read(r[t])}catch(e){if(e instanceof NotFoundError)return}throw new ConflictError(`model already exists with ${t} equal to `+JSON.stringify(r[t],void 0,2))}}function unique(){return apply(onCreateUpdate(uniqueOnCreateUpdate),metadata(getDBKey(DBKeys.UNIQUE),{}))}async function serializeOnCreateUpdate(e,t,r,a){if(r[t])try{r[t]=JSON.stringify(r[t])}catch(e){throw new SerializationError(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 SerializationError(sf("Failed to deserialize {0} property on {0} model",t,r.constructor.name))}}function serialize(){return apply(onCreateUpdate(serializeOnCreateUpdate),after(DBOperations.ALL,serializeAfterAll),type([String.name,Object.name]),metadata(getDBKey(DBKeys.SERIALIZE),{}))}export{getDBUpdateKey,readonly,timestampHandler,timestamp,uniqueOnCreateUpdate,unique,serializeOnCreateUpdate,serializeAfterAll,serialize};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./validators";export*from"./constants";export*from"./decorators";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Validator } from "@decaf-ts/decorator-validation";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Validator for the {@link readonly} decorator
|
|
4
|
+
*
|
|
5
|
+
* @class ReadOnlyValidator
|
|
6
|
+
* @extends Validator
|
|
7
|
+
*
|
|
8
|
+
* @category Validators
|
|
9
|
+
*/
|
|
10
|
+
export declare class ReadOnlyValidator extends Validator {
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* @inheritDoc
|
|
14
|
+
*/
|
|
15
|
+
hasErrors(value: any, ...args: any[]): string | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* @summary Validates a value has not changed
|
|
18
|
+
* @param {any} value
|
|
19
|
+
* @param {any} oldValue
|
|
20
|
+
* @param {string} [message] the error message override
|
|
21
|
+
*/
|
|
22
|
+
updateHasErrors(value: any, oldValue: any, message?: string): string | undefined;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var __decorate=this&&this.__decorate||function(t,e,a,r){var o,i=arguments.length,d=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)d=Reflect.decorate(t,e,a,r);else for(var l=t.length-1;0<=l;l--)(o=t[l])&&(d=(i<3?o(d):3<i?o(e,a,d):o(e,a))||d);return 3<i&&d&&Object.defineProperty(e,a,d),d},__metadata=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)};import{validator,Validator}from"@decaf-ts/decorator-validation";import{DEFAULT_ERROR_MESSAGES,UpdateValidationKeys}from"../constants";import{isEqual}from"@decaf-ts/reflection";let ReadOnlyValidator=class extends Validator{constructor(){super(DEFAULT_ERROR_MESSAGES.READONLY.INVALID)}hasErrors(t){}updateHasErrors(t,e,a){return void 0===t||isEqual(t,e)?void 0:this.getMessage(a||this.message)}};ReadOnlyValidator=__decorate([validator(UpdateValidationKeys.READONLY),__metadata("design:paramtypes",[])],ReadOnlyValidator);export{ReadOnlyValidator};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Validator } from "@decaf-ts/decorator-validation";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Validates the update of a timestamp
|
|
4
|
+
*
|
|
5
|
+
* @class TimestampValidator
|
|
6
|
+
* @extends Validator
|
|
7
|
+
*
|
|
8
|
+
* @category Validators
|
|
9
|
+
*/
|
|
10
|
+
export declare class TimestampValidator extends Validator {
|
|
11
|
+
constructor();
|
|
12
|
+
hasErrors(value: any, ...args: any[]): string | undefined;
|
|
13
|
+
updateHasErrors(value: Date | string | number, oldValue: Date | string | number, message?: string): string | undefined;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var __decorate=this&&this.__decorate||function(t,e,a,r){var o,i=arguments.length,s=i<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,a):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,a,r);else for(var d=t.length-1;0<=d;d--)(o=t[d])&&(s=(i<3?o(s):3<i?o(e,a,s):o(e,a))||s);return 3<i&&s&&Object.defineProperty(e,a,s),s},__metadata=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)};import{validator,Validator}from"@decaf-ts/decorator-validation";import{DEFAULT_ERROR_MESSAGES,UpdateValidationKeys}from"../constants";let TimestampValidator=class extends Validator{constructor(){super(DEFAULT_ERROR_MESSAGES.TIMESTAMP.INVALID)}hasErrors(t){}updateHasErrors(t,e,a){if(void 0!==t){a=a||this.getMessage(a||this.message);try{t=new Date(t),e=new Date(e)}catch(t){return a}return t<=e?a:void 0}}};TimestampValidator=__decorate([validator(UpdateValidationKeys.TIMESTAMP),__metadata("design:paramtypes",[])],TimestampValidator);export{TimestampValidator};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Validator } from "@decaf-ts/decorator-validation";
|
|
2
|
+
/**
|
|
3
|
+
* @summary Base class for an Update validator
|
|
4
|
+
*
|
|
5
|
+
* @param {string} [message] error message. defaults to {@link DecoratorMessages#DEFAULT}
|
|
6
|
+
* @param {string[]} [acceptedTypes] the accepted value types by the decorator
|
|
7
|
+
*
|
|
8
|
+
* @class UpdateValidator
|
|
9
|
+
* @abstract
|
|
10
|
+
* @extends Validator
|
|
11
|
+
*
|
|
12
|
+
* @category Validators
|
|
13
|
+
*/
|
|
14
|
+
export declare abstract class UpdateValidator extends Validator {
|
|
15
|
+
protected constructor(message?: string, ...acceptedTypes: string[]);
|
|
16
|
+
/**
|
|
17
|
+
* @summary validates a value by comparing to its old version
|
|
18
|
+
* @param {any} value
|
|
19
|
+
* @param {any} oldValue
|
|
20
|
+
* @param {any[]} args
|
|
21
|
+
*/
|
|
22
|
+
abstract updateHasErrors(value: any, oldValue: any, ...args: any[]): string | undefined;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{DEFAULT_ERROR_MESSAGES as DecoratorMessages,Validator}from"@decaf-ts/decorator-validation";class UpdateValidator extends Validator{constructor(a=DecoratorMessages.DEFAULT,...o){super(a,...o)}}export{UpdateValidator};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"./ReadOnlyValidator";export*from"./TimestampValidator";export*from"./UpdateValidator";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.id=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),reflection_1=require("@decaf-ts/reflection"),decorators_1=require("../model/decorators.cjs"),decorators_2=require("../validation/decorators.cjs"),constants_1=require("../model/constants.cjs");function id(){return(0,reflection_1.apply)((0,decorators_1.index)(),(0,decorator_validation_1.required)(),(0,decorators_2.readonly)(),(0,reflection_1.metadata)((0,decorators_1.getDBKey)(constants_1.DBKeys.ID),{}))}exports.id=id;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function id(): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&("get"in o?t.__esModule:!o.writable&&!o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){e[i=void 0===i?r:i]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./decorators.cjs"),exports),__exportStar(require("./utils.cjs"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createOrUpdate=exports.findModelId=exports.findPrimaryKey=void 0;const DBModel_1=require("../model/DBModel.cjs"),decorators_1=require("../model/decorators.cjs"),constants_1=require("../model/constants.cjs"),utils_1=require("../repository/utils.cjs"),decorator_validation_1=require("@decaf-ts/decorator-validation"),errors_1=require("../repository/errors.cjs");function findPrimaryKey(e){e=(0,utils_1.getAllPropertyDecoratorsRecursive)(e,void 0,(0,decorators_1.getDBKey)(constants_1.DBKeys.ID)),e=Object.entries(e).reduce((e,[r,o])=>{o=o.filter(e=>e.key!==decorator_validation_1.ModelKeys.TYPE);return o&&o.length&&(e[r]=e[r]||[],e[r].push(...o)),e},{});if(e&&Object.keys(e).length){if(1<Object.keys(e).length)throw new errors_1.InternalError((0,decorator_validation_1.sf)(Object.keys(e).join(", ")));var r=Object.keys(e)[0];if(r)return{id:r,props:e[r][0].props.options}}throw new errors_1.InternalError("Could not find ID decorated Property")}function findModelId(e,r=!1){var o=findPrimaryKey(e).id,e=e[o];if(e||r)return e;throw new errors_1.NotFoundError((0,decorator_validation_1.sf)("No value for the Id is defined under the property {0}",o))}async function createOrUpdate(e,r=void 0,o){return r=r||DBModel_1.DBModel.findRepository(e.constructor),void 0===e[o=o||findPrimaryKey(e).id]?r.create(e):r.update(e)}exports.findPrimaryKey=findPrimaryKey,exports.findModelId=findModelId,exports.createOrUpdate=createOrUpdate;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DBModel } from "../model/DBModel";
|
|
2
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
3
|
+
/**
|
|
4
|
+
* @summary Returns the primary key attribute for a {@link DBModel}
|
|
5
|
+
* @description searches in all the properties in the object for an {@link id} decorated property
|
|
6
|
+
*
|
|
7
|
+
* @param {DBModel} model
|
|
8
|
+
*
|
|
9
|
+
* @throws {InternalError} if no property or more than one properties are {@link id} decorated
|
|
10
|
+
* or no value is set in that property
|
|
11
|
+
*
|
|
12
|
+
* @function findPrimaryKey
|
|
13
|
+
*
|
|
14
|
+
* @category managers
|
|
15
|
+
*/
|
|
16
|
+
export declare function findPrimaryKey<T extends DBModel>(model: T): {
|
|
17
|
+
id: string;
|
|
18
|
+
props: any;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @summary Returns the primary key value for a {@link DBModel}
|
|
22
|
+
* @description searches in all the properties in the object for an {@link pk} decorated property
|
|
23
|
+
*
|
|
24
|
+
* @param {DBModel} model
|
|
25
|
+
* @param {boolean} [returnEmpty]
|
|
26
|
+
* @return {string} primary key
|
|
27
|
+
*
|
|
28
|
+
* @throws {InternalError} if no property or more than one properties are {@link pk} decorated
|
|
29
|
+
* @throws {NotFoundError} returnEmpty is false and no value is set on the {@link pk} decorated property
|
|
30
|
+
*
|
|
31
|
+
* @function findModelID
|
|
32
|
+
*
|
|
33
|
+
* @category managers
|
|
34
|
+
*/
|
|
35
|
+
export declare function findModelId(model: DBModel, returnEmpty?: boolean): any;
|
|
36
|
+
export declare function createOrUpdate<T extends DBModel>(model: T, manager?: IRepository<T> | undefined, pk?: string): Promise<T>;
|
package/lib/index.cjs
ADDED
|
@@ -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("./identity/index.cjs"),exports),__exportStar(require("./interfaces/index.cjs"),exports),__exportStar(require("./model/index.cjs"),exports),__exportStar(require("./operations/index.cjs"),exports),__exportStar(require("./repository/index.cjs"),exports),__exportStar(require("./validation/index.cjs"),exports);
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module db-decorators
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @namespace Model
|
|
6
|
+
* @memberOf module:db-decorators
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* @namespace Operations
|
|
10
|
+
* @memberOf module:db-decorators
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @namespace Repository
|
|
14
|
+
* @memberOf module:db-decorators
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @namespace Validation
|
|
18
|
+
* @memberOf module:db-decorators
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* @namespace Validators
|
|
22
|
+
* @memberOf module:db-decorators.Validation
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* @namespace Utils
|
|
26
|
+
* @memberOf module:db-decorators
|
|
27
|
+
*/
|
|
28
|
+
export * from "./identity";
|
|
29
|
+
export * from "./interfaces";
|
|
30
|
+
export * from "./model";
|
|
31
|
+
export * from "./operations";
|
|
32
|
+
export * from "./repository";
|
|
33
|
+
export * from "./validation";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @summary Crud API
|
|
3
|
+
* @description Exposes a CRUD API
|
|
4
|
+
*
|
|
5
|
+
* @interface CrudOperator
|
|
6
|
+
*
|
|
7
|
+
* @category Managers
|
|
8
|
+
*/
|
|
9
|
+
export interface CrudOperator<T> {
|
|
10
|
+
/**
|
|
11
|
+
* @summary Create a new model
|
|
12
|
+
* @param {T} model
|
|
13
|
+
* @param {any[]} [args]
|
|
14
|
+
*
|
|
15
|
+
* @method
|
|
16
|
+
*/
|
|
17
|
+
create(model: T, ...args: any[]): Promise<T>;
|
|
18
|
+
/**
|
|
19
|
+
* @summary Read a model
|
|
20
|
+
* @param {string} key
|
|
21
|
+
* @param {any[]} [args]
|
|
22
|
+
*
|
|
23
|
+
* @method
|
|
24
|
+
*/
|
|
25
|
+
read(key: string, ...args: any[]): Promise<T>;
|
|
26
|
+
/**
|
|
27
|
+
* @summary update a model
|
|
28
|
+
* @param {T} model
|
|
29
|
+
* @param {any[]} [args]
|
|
30
|
+
*
|
|
31
|
+
* @method
|
|
32
|
+
*/
|
|
33
|
+
update(model: T, ...args: any[]): Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* @summary delete a model
|
|
36
|
+
* @param {string} key
|
|
37
|
+
* @param {any[]} [args]
|
|
38
|
+
*
|
|
39
|
+
* @method
|
|
40
|
+
*/
|
|
41
|
+
delete(key: string, ...args: any[]): Promise<T>;
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DBModel } from "../model/DBModel";
|
|
2
|
+
import { DataCache } from "../repository/DataCache";
|
|
3
|
+
import { Constructor } from "@decaf-ts/decorator-validation";
|
|
4
|
+
import { CrudOperator } from "./CrudOperator";
|
|
5
|
+
/**
|
|
6
|
+
* @summary Interface holding basic CRUD APIs
|
|
7
|
+
* @typedef T extends {@link DBModel}
|
|
8
|
+
* @interface IRepository
|
|
9
|
+
*/
|
|
10
|
+
export interface IRepository<T extends DBModel> extends CrudOperator<T> {
|
|
11
|
+
readonly cache: DataCache;
|
|
12
|
+
readonly class: Constructor<T>;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&("get"in o?t.__esModule:!o.writable&&!o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){e[i=void 0===i?r:i]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./CrudOperator.cjs"),exports),__exportStar(require("./IRepository.cjs"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DBModel=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),decorators_1=require("./decorators.cjs"),constants_1=require("./constants.cjs"),errors_1=require("../repository/errors.cjs"),injectable_decorators_1=require("@decaf-ts/injectable-decorators"),validation_1=require("./validation.cjs");class DBModel extends decorator_validation_1.Model{constructor(r){super(r)}hasErrors(r,...e){!r||r instanceof DBModel||(e.unshift(r),r=void 0);var o=super.hasErrors(...e);return o||!r?o:(0,validation_1.validateCompare)(r,this,...e)}static findRepository(r){var e=Reflect.getMetadata((0,decorators_1.getDBKey)(constants_1.DBKeys.REPOSITORY),r);if(!e)throw new errors_1.InternalError("Could not find any registered repositories for "+r.name);e=injectable_decorators_1.Injectables.get(e);if(e)return e;throw new errors_1.NotFoundError("Could not find repository for "+r.name)}}exports.DBModel=DBModel;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Constructor, Model, ModelArg, ModelErrorDefinition } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
3
|
+
/**
|
|
4
|
+
* @summary Abstract class representing a Validatable DBModel object
|
|
5
|
+
*
|
|
6
|
+
* @param {ModelArg} [arg]
|
|
7
|
+
*
|
|
8
|
+
* @see Model
|
|
9
|
+
*
|
|
10
|
+
* @class DBModel
|
|
11
|
+
* @abstract
|
|
12
|
+
* @extends Model
|
|
13
|
+
*
|
|
14
|
+
* @category Model
|
|
15
|
+
*/
|
|
16
|
+
export declare abstract class DBModel extends Model {
|
|
17
|
+
protected constructor(arg?: ModelArg<DBModel>);
|
|
18
|
+
/**
|
|
19
|
+
* @param {DBModel | any} [previousVersion] validates an update via the {@link DBModel} decorators
|
|
20
|
+
* @param {any[]} [exclusions] {@see Model#hasErrors}
|
|
21
|
+
* @return {ModelErrorDefinition | undefined}
|
|
22
|
+
*/
|
|
23
|
+
hasErrors(previousVersion?: DBModel | any, ...exclusions: any[]): ModelErrorDefinition | undefined;
|
|
24
|
+
static findRepository<V extends DBModel>(model: Constructor<V>): IRepository<V>;
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DEFAULT_TIMESTAMP_FORMAT=exports.DefaultSeparator=exports.DBKeys=void 0,exports.DBKeys={REFLECT:"model.db.",REPOSITORY:"repository",CLASS:"_class",ID:"id",INDEX:"index",UNIQUE:"unique",SERIALIZE:"serialize",READONLY:"readonly",TIMESTAMP:"timestamp",HASH:"hash",COMPOSED:"composed",ORIGINAL:"__originalObj"},exports.DefaultSeparator="_",exports.DEFAULT_TIMESTAMP_FORMAT="dd/MM/yyyy HH:mm:ss:S";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @summary Holds the DBModel reflection keys
|
|
3
|
+
* @const DBKeys
|
|
4
|
+
*
|
|
5
|
+
* @memberOf module:db-decorators.Model
|
|
6
|
+
*/
|
|
7
|
+
export declare const DBKeys: {
|
|
8
|
+
REFLECT: string;
|
|
9
|
+
REPOSITORY: string;
|
|
10
|
+
CLASS: string;
|
|
11
|
+
ID: string;
|
|
12
|
+
INDEX: string;
|
|
13
|
+
UNIQUE: string;
|
|
14
|
+
SERIALIZE: string;
|
|
15
|
+
READONLY: string;
|
|
16
|
+
TIMESTAMP: string;
|
|
17
|
+
HASH: string;
|
|
18
|
+
COMPOSED: string;
|
|
19
|
+
ORIGINAL: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @summary The default separator when concatenating indexes
|
|
23
|
+
*
|
|
24
|
+
* @const DefaultIndexSeparator
|
|
25
|
+
*
|
|
26
|
+
* @category Managers
|
|
27
|
+
* @subcategory Constants
|
|
28
|
+
*/
|
|
29
|
+
export declare const DefaultSeparator = "_";
|
|
30
|
+
/**
|
|
31
|
+
* @summary Holds the default timestamp date format
|
|
32
|
+
* @constant DEFAULT_TIMESTAMP_FORMAT
|
|
33
|
+
*
|
|
34
|
+
* @memberOf module:db-decorators.Model
|
|
35
|
+
*/
|
|
36
|
+
export declare const DEFAULT_TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss:S";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.composed=exports.composedFromKeys=exports.composedFromCreateUpdate=exports.hash=exports.hashOnCreateUpdate=exports.index=exports.getDBKey=void 0;const constants_1=require("./constants.cjs"),reflection_1=require("@decaf-ts/reflection"),decorator_validation_1=require("@decaf-ts/decorator-validation"),decorators_1=require("../operations/decorators.cjs"),errors_1=require("../repository/errors.cjs");function getDBKey(e){return constants_1.DBKeys.REFLECT+e}function index(e,o){return(0,reflection_1.metadata)(getDBKey(""+constants_1.DBKeys.INDEX+(e&&e.length?"."+e.join("."):"")),{directions:o,compositions:e})}function hashOnCreateUpdate(e,o,t,r){var a;t[o]&&(a=decorator_validation_1.Hashing.hash(t[o]),r&&t[o]===a||(t[o]=a))}function hash(){return(0,reflection_1.apply)((0,decorators_1.onCreateUpdate)(hashOnCreateUpdate),(0,reflection_1.metadata)(getDBKey(constants_1.DBKeys.HASH),{}))}function composedFromCreateUpdate(r,a=constants_1.DefaultSeparator,s="values",n="",p=""){return function(e,o){try{var t=r.map(e=>{if(!(e in o))throw new errors_1.InternalError((0,decorator_validation_1.sf)("Property {0} not found to compose from",e));if("keys"===s)return e;if(void 0===o[e])throw new errors_1.InternalError((0,decorator_validation_1.sf)("Property {0} does not contain a value to compose from",e));return o[e].toString()});n&&t.unshift(n),p&&t.push(p),o[e]=t.join(a)}catch(e){throw new errors_1.InternalError("Failed to compose value: "+e)}}}function composedFrom(e,o=!1,t=constants_1.DefaultSeparator,r="values",a="",s=""){var n={args:e,hashResult:o,separator:t,type:r,prefix:a,suffix:s},e=[(0,decorators_1.onCreateUpdate)(composedFromCreateUpdate(e,t,r,a,s)),(0,reflection_1.metadata)(getDBKey(constants_1.DBKeys.COMPOSED),n)];return o&&e.push(hash()),(0,reflection_1.apply)(...e)}function composedFromKeys(e,o=!1,t="",r=""){return composedFrom(e,o,constants_1.DefaultSeparator,"keys",t,r)}function composed(e,o=!1,t="",r=""){return composedFrom(e,o,constants_1.DefaultSeparator,"values",t,r)}exports.getDBKey=getDBKey,exports.index=index,exports.hashOnCreateUpdate=hashOnCreateUpdate,exports.hash=hash,exports.composedFromCreateUpdate=composedFromCreateUpdate,exports.composedFromKeys=composedFromKeys,exports.composed=composed;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { OrderDirection } from "../validation/constants";
|
|
2
|
+
import { DBModel } from "./DBModel";
|
|
3
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {str} str
|
|
7
|
+
* @memberOf db-decorators.model
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDBKey(str: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* @summary Index Decorator
|
|
12
|
+
* @description properties decorated will the index in the
|
|
13
|
+
* DB for performance in queries
|
|
14
|
+
*
|
|
15
|
+
* @param {OrderDirection[]} [directions]
|
|
16
|
+
* @param {string[]} [compositions]
|
|
17
|
+
*
|
|
18
|
+
* @function index
|
|
19
|
+
*/
|
|
20
|
+
export declare function index(compositions?: string[], directions?: OrderDirection[]): import("@decaf-ts/reflection").CustomDecorator<V>;
|
|
21
|
+
export declare function hashOnCreateUpdate<T extends DBModel, V extends IRepository<T>, Y = any>(this: V, data: Y, key: string, model: T, oldModel?: T): void;
|
|
22
|
+
export declare function hash(): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
23
|
+
export declare function composedFromCreateUpdate(args: string[], separator?: string, type?: "keys" | "values", prefix?: string, suffix?: string): <T extends DBModel, V extends IRepository<T>>(this: V, key: string, model: T) => void;
|
|
24
|
+
export declare function composedFromKeys(args: string[], hash?: boolean, prefix?: string, suffix?: string): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
25
|
+
export declare function composed(args: string[], hash?: boolean, prefix?: string, suffix?: string): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol | undefined, descriptor?: TypedPropertyDescriptor<Y> | undefined) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&("get"in i?t.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,o,i)}:function(e,t,r,o){e[o=void 0===o?r:o]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./constants.cjs"),exports),__exportStar(require("./DBModel.cjs"),exports),__exportStar(require("./decorators.cjs"),exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.validateCompare=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),reflection_1=require("@decaf-ts/reflection"),validation_1=require("../validation/index.cjs"),identity_1=require("../identity/index.cjs");function validateCompare(r,a,...e){var o=[];for(const p in a)a.hasOwnProperty(p)&&-1===e.indexOf(p)&&o.push((0,reflection_1.getPropertyDecorators)(validation_1.UpdateValidationKeys.REFLECT,a,p));let t=void 0;for(const f of o){var{prop:i,decorators:d}=f;if(d.shift(),d&&d.length){let e=void 0;for(const v of d){var n=decorator_validation_1.Validation.get(v.key);n?(n=n.updateHasErrors(a[i.toString()],r[i.toString()],...Object.values(v.props)))&&((e=e||{})[v.key]=n):console.error(`Could not find Matching validator for ${v.key} for property `+String(f.prop))}e&&((t=t||{})[f.prop.toString()]=e)}}for(const y of Object.keys(a).filter(e=>!t||!t[e])){let o;var s=(0,reflection_1.getPropertyDecorators)(decorator_validation_1.ValidationKeys.REFLECT,a,y).decorators,l=(0,reflection_1.getPropertyDecorators)(decorator_validation_1.ValidationKeys.REFLECT,a,y).decorators.filter(e=>-1!==[decorator_validation_1.ModelKeys.TYPE,decorator_validation_1.ValidationKeys.TYPE].indexOf(e.key));if(l&&l.length){var l=l.pop(),l=l.props.name?[l.props.name]:Array.isArray(l.props.customTypes)?l.props.customTypes:[l.props.customTypes],c=Object.values(decorator_validation_1.ReservedModels).map(e=>e.toLowerCase());for(const _ of l){if(-1===c.indexOf(_.toLowerCase()))switch(_){case Array.name:case Set.name:if(s.length)if(s.find(e=>e.key===decorator_validation_1.ValidationKeys.LIST)){let e,t;switch(_){case Array.name:e=a[y],t=r[y];break;case Set.name:e=a[y].values(),t=r[y].values();break;default:throw new Error("Invalid attribute type "+_)}(o=e.map(e=>{const o=(0,identity_1.findModelId)(e,!0);var r;return o?(r=t.find(e=>o===(0,identity_1.findModelId)(e,!0)))?e.hasErrors(r):void 0:"Failed to find model id"}).filter(e=>!!e))?.length||(o=void 0)}break;default:try{a[y]&&r[y]&&(o=a[y].hasErrors(r[y]))}catch(e){console.warn((0,decorator_validation_1.sf)("Model should be validatable but its not"))}}o&&((t=t||{})[y]=o)}}}return t?new decorator_validation_1.ModelErrorDefinition(t):void 0}exports.validateCompare=validateCompare;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ModelErrorDefinition } from "@decaf-ts/decorator-validation";
|
|
2
|
+
import { DBModel } from "./DBModel";
|
|
3
|
+
/**
|
|
4
|
+
* @summary Validates the update of a model
|
|
5
|
+
*
|
|
6
|
+
* @param {T} oldModel
|
|
7
|
+
* @param {T} newModel
|
|
8
|
+
* @param {string[]} [exceptions]
|
|
9
|
+
*
|
|
10
|
+
* @function validateCompare
|
|
11
|
+
* @return {ModelErrorDefinition | undefined}
|
|
12
|
+
*
|
|
13
|
+
* @memberOf module:db-decorators.Model
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateCompare<T extends DBModel>(oldModel: T, newModel: T, ...exceptions: string[]): ModelErrorDefinition | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Operations=void 0;const decorator_validation_1=require("@decaf-ts/decorator-validation"),OperationsRegistry_1=require("./OperationsRegistry.cjs"),constants_1=require("./constants.cjs");class Operations{constructor(){}static getHandlerName(e){return e.name||(console.warn("Handler name not defined. A name will be generated, but this is not desirable. please avoid using anonymous functions"),decorator_validation_1.Hashing.hash(e.toString()))}static genKey(e){return constants_1.OperationKeys.REFLECT+e}static get(e,t,r){return Operations.registry.get(e,t,r)}static getOpRegistry(){return Operations.registry||(Operations.registry=new OperationsRegistry_1.OperationsRegistry),Operations.registry}static register(e,t,r,s){Operations.getOpRegistry().register(e,t,r,s)}}exports.Operations=Operations;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { OperationHandler } from "./types";
|
|
2
|
+
import { DBModel } from "../model/DBModel";
|
|
3
|
+
import { OperationKeys } from "./constants";
|
|
4
|
+
/**
|
|
5
|
+
* @summary Static class holding common Operation Functionality
|
|
6
|
+
*
|
|
7
|
+
* @class Operations
|
|
8
|
+
*
|
|
9
|
+
* @category Operations
|
|
10
|
+
*/
|
|
11
|
+
export declare class Operations {
|
|
12
|
+
private static registry;
|
|
13
|
+
private constructor();
|
|
14
|
+
static getHandlerName(handler: OperationHandler<any, any, any>): any;
|
|
15
|
+
static genKey(str: string): string;
|
|
16
|
+
static get(targetName: string | Record<string, any>, propKey: string, operation: string): OperationHandler<DBModel, import("..").IRepository<DBModel>, unknown>[] | undefined;
|
|
17
|
+
private static getOpRegistry;
|
|
18
|
+
static register<V extends DBModel>(handler: OperationHandler<V, any, any>, operation: OperationKeys, target: V, propKey: string | symbol): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.OperationsRegistry=void 0;const Operations_1=require("./Operations.cjs");class OperationsRegistry{constructor(){this.cache={}}get(e,t,r,s){s=s||[];let c;try{c="string"==typeof e?e:e.constructor.name,s.unshift(...Object.values(this.cache[c][t][r]||[]))}catch(t){if("string"==typeof e||e===Object.prototype||Object.getPrototypeOf(e)===Object.prototype)return s}let o=Object.getPrototypeOf(e);return o.constructor.name===c&&(o=Object.getPrototypeOf(o)),this.get(o,t,r,s)}register(t,e,r,s){var r=r.constructor.name,c=Operations_1.Operations.getHandlerName(t);this.cache[r]||(this.cache[r]={}),this.cache[r][s]||(this.cache[r][s]={}),this.cache[r][s][e]||(this.cache[r][s][e]={}),this.cache[r][s][e][c]||(this.cache[r][s][e][c]=t)}}exports.OperationsRegistry=OperationsRegistry;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { OperationHandler } from "./types";
|
|
2
|
+
import { DBModel } from "../model/DBModel";
|
|
3
|
+
import { OperationKeys } from "./constants";
|
|
4
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
5
|
+
/**
|
|
6
|
+
* @summary Holds the registered operation handlers
|
|
7
|
+
*
|
|
8
|
+
* @class OperationsRegistry
|
|
9
|
+
* @implements IRegistry<OperationHandler<any>>
|
|
10
|
+
*
|
|
11
|
+
* @see OperationHandler
|
|
12
|
+
*
|
|
13
|
+
* @category Operations
|
|
14
|
+
*/
|
|
15
|
+
export declare class OperationsRegistry {
|
|
16
|
+
private readonly cache;
|
|
17
|
+
/**
|
|
18
|
+
* @summary retrieves an {@link OperationHandler} if it exists
|
|
19
|
+
* @param {string} target
|
|
20
|
+
* @param {string} propKey
|
|
21
|
+
* @param {string} operation
|
|
22
|
+
* @param accum
|
|
23
|
+
* @return {OperationHandler | undefined}
|
|
24
|
+
*/
|
|
25
|
+
get<T extends DBModel, V extends IRepository<T>, Y>(target: string | Record<string, any>, propKey: string, operation: string, accum?: OperationHandler<T, V, Y>[]): OperationHandler<T, V, Y>[] | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* @summary Registers an {@link OperationHandler}
|
|
28
|
+
* @param {OperationHandler} handler
|
|
29
|
+
* @param {string} operation
|
|
30
|
+
* @param {{}} target
|
|
31
|
+
* @param {string | symbol} propKey
|
|
32
|
+
*/
|
|
33
|
+
register<T extends DBModel, V extends IRepository<T>, Y>(handler: OperationHandler<T, V, Y>, operation: OperationKeys, target: T, propKey: string | symbol): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var OperationKeys;Object.defineProperty(exports,"__esModule",{value:!0}),exports.DBOperations=exports.OperationKeys=void 0,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||(exports.OperationKeys=OperationKeys={})),exports.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]};
|
|
@@ -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
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.operation=exports.after=exports.afterDelete=exports.afterRead=exports.afterCreate=exports.afterUpdate=exports.afterCreateUpdate=exports.on=exports.onDelete=exports.onRead=exports.onCreate=exports.onUpdate=exports.onCreateUpdate=void 0;const constants_1=require("./constants.cjs"),Operations_1=require("./Operations.cjs"),reflection_1=require("@decaf-ts/reflection");function handle(r,n){return(e,t)=>{Operations_1.Operations.register(n,r,e,t)}}function onCreateUpdate(e,t){return on(constants_1.DBOperations.CREATE_UPDATE,e,t)}function onUpdate(e,t){return on(constants_1.DBOperations.UPDATE,e,t)}function onCreate(e,t){return on(constants_1.DBOperations.CREATE,e,t)}function onRead(e,t){return on(constants_1.DBOperations.READ,e,t)}function onDelete(e,t){return on(constants_1.DBOperations.DELETE,e,t)}function on(e=constants_1.DBOperations.ALL,t,r){return operation(constants_1.OperationKeys.ON,e,t,r)}function afterCreateUpdate(e,t){return after(constants_1.DBOperations.CREATE_UPDATE,e,t)}function afterUpdate(e,t){return after(constants_1.DBOperations.UPDATE,e,t)}function afterCreate(e,t){return after(constants_1.DBOperations.CREATE,e,t)}function afterRead(e,t){return after(constants_1.DBOperations.READ,e,t)}function afterDelete(e,t){return after(constants_1.DBOperations.DELETE,e,t)}function after(e=constants_1.DBOperations.ALL,t,r){return operation(constants_1.OperationKeys.AFTER,e,t,r)}function operation(p,t=constants_1.DBOperations.ALL,i,f){return(a,o)=>{const s=a.constructor.name;var e=t.reduce((e,t)=>{var r=p+t;let n=Reflect.getMetadata(Operations_1.Operations.genKey(r),a,o);n=n||{operation:t,handlers:{}};t=Operations_1.Operations.getHandlerName(i);return n.handlers[s]&&n.handlers[s][o]&&t in n.handlers[s][o]||(n.handlers[s]=n.handlers[s]||{},n.handlers[s][o]=n.handlers[s][o]||{},n.handlers[s][o][t]={data:f},e.push(handle(r,i),(0,reflection_1.metadata)(Operations_1.Operations.genKey(r),n))),e},[]);return(0,reflection_1.apply)(...e)(a,o)}}exports.onCreateUpdate=onCreateUpdate,exports.onUpdate=onUpdate,exports.onCreate=onCreate,exports.onRead=onRead,exports.onDelete=onDelete,exports.on=on,exports.afterCreateUpdate=afterCreateUpdate,exports.afterUpdate=afterUpdate,exports.afterCreate=afterCreate,exports.afterRead=afterRead,exports.afterDelete=afterDelete,exports.after=after,exports.operation=operation;
|