UNPKG

@aequum/typeorm

Version:

aequum TypeORM tools for repository, pagination, CRUD/CRUDL, configs and utils

107 lines (106 loc) 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseCRUDLTypeORMService = void 0; const crudl_1 = require("@aequum/crudl"); const utils_1 = require("../utils"); /** * Base CRUD/CRUDL TypeORM Service for a TypeORM Entity Model. * * **NOTE**: If you want to present different primary key * field you must pass the `PrimaryKeyField` type parameter. * and set the `primaryKeyField` property to the name of * the field. */ class BaseCRUDLTypeORMService extends crudl_1.BaseCRUDLService { /** The primary key field */ primaryKeyField = 'id'; /** Unique fields to show when a dulplicate error is found */ static uniqueFields; /** Custom duplicated entry exception message */ static duplicatedEntryMessage; /** Default TypeORM filter to apply to all queries */ defaultTypeORMFilter = {}; /** * Method to get the default TypeORM filter to apply to all * queries when an instance member is needed to create the * default filter, by default returns the * `defaultTypeORMFilter` property. * * If you don't need a local instance member to create the * default filter, you just need to change the * `defaultTypeORMFilter` property. * * @protected */ getDefaultTypeORMFilter() { return this.defaultTypeORMFilter; } /** @ignore */ static duplicateEntryExceptionMessage() { const self = this; if (self.duplicatedEntryMessage) return self.duplicatedEntryMessage; if (this.uniqueFields && this.uniqueFields?.length) return `\`${self.uniqueFields.join('` or ')}\` already exists`; return 'Duplicated entry'; } /** * Convert a custom query filter to a TypeORM filter * * @param filter Custom query filter, the fifth type argument * @returns TypeORM filter */ queryFilterToTypeORMFilter(filter) { const typeOrmFilter = Object.assign({}, filter, this.getDefaultTypeORMFilter()); return typeOrmFilter; } /** TypeORM Repository to interact with the Mongoose Model */ repository; /** @inheritdoc */ async create(data) { const self = this.constructor; try { const ormModel = this.repository.create(data); return this.repository.save(ormModel); } catch (err) { throw (0, utils_1.duplicateEntryExceptionOrError)(err, self.duplicateEntryExceptionMessage(), data, self.uniqueFields || []); } } /** @inheritdoc */ async retrieve(id) { return this.retrieveBy({ [this.primaryKeyField]: id }); } /** @inheritdoc */ async retrieveBy(filter) { return this.repository.findOneBy(this.queryFilterToTypeORMFilter(filter)); } /** @inheritdoc */ async update(id, data) { return this.updateBy({ [this.primaryKeyField]: id }, data); } async updateBy(filter, data) { const self = this.constructor; try { const typeORMfilter = this.queryFilterToTypeORMFilter(filter); await this.repository.update(typeORMfilter, data); return this.repository.findOneBy(typeORMfilter); } catch (err) { throw (0, utils_1.duplicateEntryExceptionOrError)(err, self.duplicateEntryExceptionMessage(), data, self.uniqueFields || []); } } /** @inheritdoc */ async delete(id) { await this.deleteBy({ [this.primaryKeyField]: id }); } /** @inheritdoc */ async deleteBy(filter) { await this.repository.delete(this.queryFilterToTypeORMFilter(filter)); } /** @inheritdoc */ async list(filter) { return this.repository.find(this.queryFilterToTypeORMFilter(filter || {})); } } exports.BaseCRUDLTypeORMService = BaseCRUDLTypeORMService;