@decaf-ts/db-decorators
Version:
Agnostic database decorators and repository
273 lines (272 loc) • 8.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onCreateUpdate = onCreateUpdate;
exports.onUpdate = onUpdate;
exports.onCreate = onCreate;
exports.onRead = onRead;
exports.onDelete = onDelete;
exports.onAny = onAny;
exports.on = on;
exports.afterCreateUpdate = afterCreateUpdate;
exports.afterUpdate = afterUpdate;
exports.afterCreate = afterCreate;
exports.afterRead = afterRead;
exports.afterDelete = afterDelete;
exports.afterAny = afterAny;
exports.after = after;
exports.operation = operation;
const constants_1 = require("./constants.cjs");
const Operations_1 = require("./Operations.cjs");
const reflection_1 = require("@decaf-ts/reflection");
const decorator_validation_1 = require("@decaf-ts/decorator-validation");
function handle(op, handler) {
return (target, propertyKey) => {
Operations_1.Operations.register(handler, op, target, propertyKey);
};
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.CREATE_UPDATE}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
* @param {any[]} [args] Arguments that will be passed in order to the handler method
*
* @see on
*
* @function onCreateUpdate
*
* @category Decorators
*/
function onCreateUpdate(handler, data) {
return on(constants_1.DBOperations.CREATE_UPDATE, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.UPDATE}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
* @param {any[]} [args] Arguments that will be passed in order to the handler method
*
* @see on
*
* @function onUpdate
*
* @category Decorators
*/
function onUpdate(handler, data) {
return on(constants_1.DBOperations.UPDATE, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.CREATE}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see on
*
* @function onCreate
*
* @category Decorators
*/
function onCreate(handler, data) {
return on(constants_1.DBOperations.CREATE, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.READ}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see on
*
* @function onRead
*
* @category Decorators
*/
function onRead(handler, data) {
return on(constants_1.DBOperations.READ, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.DELETE}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see on
*
* @function onDelete
*
* @category Decorators
*/
function onDelete(handler, data) {
return on(constants_1.DBOperations.DELETE, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations.DELETE}
*
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see on
*
* @function onAny
*
* @category Decorators
*/
function onAny(handler, data) {
return on(constants_1.DBOperations.ALL, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations}
*
* @param {OperationKeys[] | DBOperations} op One of {@link DBOperations}
* @param {OnOperationHandler<any>} handler The method called upon the operation
* @param data
*
* ex: handler(...args, ...props.map(p => target[p]))
*
* @function on
*
* @category Decorators
*/
function on(op = constants_1.DBOperations.ALL, handler, data) {
return operation(constants_1.OperationKeys.ON, op, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.CREATE_UPDATE}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see after
*
* @function afterCreateUpdate
*
* @category Decorators
*/
function afterCreateUpdate(handler, data) {
return after(constants_1.DBOperations.CREATE_UPDATE, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.UPDATE}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see after
*
* @function afterUpdate
*
* @category Decorators
*/
function afterUpdate(handler, data) {
return after(constants_1.DBOperations.UPDATE, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.CREATE}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
*
* @see after
*
* @function afterCreate
*
* @category Decorators
*/
function afterCreate(handler, data) {
return after(constants_1.DBOperations.CREATE, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.READ}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
* @param {any[]} [args] Arguments that will be passed in order to the handler method
*
* @see after
*
* @function afterRead
*
* @category Decorators
*/
function afterRead(handler, data) {
return after(constants_1.DBOperations.READ, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.DELETE}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
* @param {any[]} [args] Arguments that will be passed in order to the handler method
*
* @see after
*
* @function afterDelete
*
* @category Decorators
*/
function afterDelete(handler, data) {
return after(constants_1.DBOperations.DELETE, handler, data);
}
/**
* @summary Defines a behaviour to set after the defined {@link DBOperations.DELETE}
*
* @param {AfterOperationHandler<any>} handler The method called upon the operation
* @param data
* @param {any[]} [args] Arguments that will be passed in order to the handler method
*
* @see after
*
* @function afterAny
*
* @category Decorators
*/
function afterAny(handler, data) {
return after(constants_1.DBOperations.ALL, handler, data);
}
/**
* @summary Defines a behaviour to set on the defined {@link DBOperations}
*
* @param {OperationKeys[] | DBOperations} op One of {@link DBOperations}
* @param {AfterOperationHandler<any>} handler The method called upon the operation
*
* ex: handler(...args, ...props.map(p => target[p]))
*
* @param data
* @param args
* @function after
*
* @category Decorators
*/
function after(op = constants_1.DBOperations.ALL, handler, data) {
return operation(constants_1.OperationKeys.AFTER, op, handler, data);
}
function operation(baseOp, operation = constants_1.DBOperations.ALL, handler, dataToAdd) {
return (target, propertyKey) => {
const name = target.constructor.name;
const decorators = operation.reduce((accum, op) => {
const compoundKey = baseOp + op;
let data = Reflect.getMetadata(Operations_1.Operations.key(compoundKey), target, propertyKey);
if (!data)
data = {
operation: op,
handlers: {},
};
const handlerKey = Operations_1.Operations.getHandlerName(handler);
if (!data.handlers[name] ||
!data.handlers[name][propertyKey] ||
!(handlerKey in data.handlers[name][propertyKey])) {
data.handlers[name] = data.handlers[name] || {};
data.handlers[name][propertyKey] =
data.handlers[name][propertyKey] || {};
data.handlers[name][propertyKey][handlerKey] = {
data: dataToAdd,
};
accum.push(handle(compoundKey, handler), (0, decorator_validation_1.propMetadata)(Operations_1.Operations.key(compoundKey), data));
}
return accum;
}, []);
return (0, reflection_1.apply)(...decorators)(target, propertyKey);
};
}