@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
89 lines (85 loc) • 5.42 kB
JavaScript
;
var exception_utility = require('../../../utility/error/exception.utility.js');
class ApiFunctionServiceContextFactory {
/**
* Creates a service-bound ApiFunction context for custom functions and internal steps.
* @template E - Entity type associated with the decorated function or step.
* @param {object} options - Context creation options.
* @param {new (...arguments_: Array<unknown>) => E} options.entity - Entity constructor associated with the service context.
* @param {EntityManager} [options.eventManager] - Active transaction manager, when available.
* @param {{ repository?: Repository<E> }} options.target - Service instance that owns the decorated method.
* @param {Repository<E>} [options.target.repository] - Service repository used to resolve non-transactional repositories.
* @returns {IApiFunctionContext<E>} Service-bound function context.
*/
static create(options) {
const repository = options.eventManager?.getRepository(options.entity) ?? ApiFunctionServiceContextFactory.resolveManagerRepository(options.target, options.entity);
if (!repository) {
throw exception_utility.ErrorException("Repository is not available in this context");
}
return {
entity: options.entity,
eventManager: options.eventManager,
getRepository: (repositoryEntity) => options.eventManager?.getRepository(repositoryEntity) ?? ApiFunctionServiceContextFactory.getManagerRepository(options.target, repositoryEntity),
operations: {
create: async (properties) => await options.target.create(properties),
delete: async (criteria) => {
await options.target.delete(criteria);
},
get: async (properties) => await options.target.get(properties),
getList: async (properties) => await options.target.getList(properties),
getMany: async (properties) => await options.target.getMany(properties),
getRepository: (repositoryEntity) => options.eventManager?.getRepository(repositoryEntity) ?? ApiFunctionServiceContextFactory.getManagerRepository(options.target, repositoryEntity),
update: async (criteria, properties) => await options.target.update(criteria, properties),
},
repository,
};
}
/**
* Creates a narrow service-bound ApiFunctionStep context.
* @template E - Entity type associated with the decorated step.
* @param {object} options - Context creation options.
* @param {new (...arguments_: Array<unknown>) => E} options.entity - Entity constructor associated with the step context.
* @param {EntityManager} [options.eventManager] - Active transaction manager, when available.
* @param {{ repository?: Repository<E> }} options.target - Service instance that owns the decorated step.
* @param {Repository<E>} [options.target.repository] - Service repository used to resolve non-transactional repositories.
* @returns {IApiFunctionStepContext<E>} Service-bound step context.
*/
static createStep(options) {
const repository = options.eventManager?.getRepository(options.entity) ?? ApiFunctionServiceContextFactory.resolveManagerRepository(options.target, options.entity);
if (!repository) {
throw exception_utility.ErrorException("Repository is not available in this context");
}
return {
eventManager: options.eventManager,
getRepository: (repositoryEntity) => options.eventManager?.getRepository(repositoryEntity) ?? ApiFunctionServiceContextFactory.getManagerRepository(options.target, repositoryEntity),
repository,
};
}
/**
* Resolves repositories from the service repository manager when no transaction manager is active.
* @template T - Repository entity type.
* @param {{ repository?: Repository<IApiBaseEntity> }} target - Service instance with an optional repository.
* @param {Repository<IApiBaseEntity>} [target.repository] - Service repository used to resolve repositories.
* @param {new () => T} entity - Entity constructor to resolve.
* @returns {Repository<T>} Repository from the service manager.
*/
static getManagerRepository(target, entity) {
if (!target.repository) {
throw exception_utility.ErrorException("Repository is not available in this context");
}
return target.repository.manager.getRepository(entity);
}
/**
* Resolves a repository from the service repository manager if a service repository exists.
* @template T - Repository entity type.
* @param {{ repository?: Repository<IApiBaseEntity> }} target - Service instance with an optional repository.
* @param {Repository<IApiBaseEntity>} [target.repository] - Service repository used to resolve repositories.
* @param {new () => T} entity - Entity constructor to resolve.
* @returns {Repository<T> | undefined} Repository from the service manager, when available.
*/
static resolveManagerRepository(target, entity) {
return target.repository?.manager.getRepository(entity);
}
}
exports.ApiFunctionServiceContextFactory = ApiFunctionServiceContextFactory;
//# sourceMappingURL=service-context.factory.class.js.map