@aequum/mongoose
Version:
aequum mongoose tools for repository, pagination, CRUD/CRUDL, configs and utils
127 lines (126 loc) • 4.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCRUDLMongooseService = void 0;
const crudl_1 = require("@aequum/crudl");
const exception_util_1 = require("../utils/exception.util");
/**
* Base CRUDL Mongoose Service for a Mongoose Schema Model, by default
* assumes that the primary key field as `id` provided by Virtual ID
*
* @typeParam SchemaModel Mongoose Schema Model
* @typeParam SchemaModelDto DTO of the Mongoose Schema Model
* @typeParam SchemaModelCreateDto DTO to create a new Mongoose Schema Model
* @typeParam SchemaModelUpdateDto DTO to update a Mongoose Schema Model
* @typeParam CustomFilterType Custom filter type to filter the query
* @typeParam PrimaryKeyField Primary key field of the Mongoose Schema
* Model by default is `id` (Virtual ID)
*/
class BaseCRUDLMongooseService extends crudl_1.BaseCRUDLService {
/** The primary key field, default is `_id` even if Virtual ID */
primaryKeyField = '_id';
/** Unique fields to check for duplicated entries */
static uniqueFields;
/** Custom duplicated entry exception message */
static duplicatedEntryMessage;
/** Default MongoDB filter to apply to all queries */
defaultMongoDBFilter = {};
/**
* Method to get the default MongoDB filter to apply to all
* queries when an instance member is needed to create the
* default filter, by default returns the
* `defaultMongoDBFilter` property.
*
* If you don't need a local instance member to create the
* default filter, you just need to change the
* `defaultMongoDBFilter` property.
*
* @protected
*/
getDefaultMongoDBFilter() {
return this.defaultMongoDBFilter;
}
/** @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 MongoDB filter, the
* default convert transform the `id` (Virtual ID) field to the
* MongoDB `_id` field. using `virtualIDFilterTransform` method.
*
* @param filter Custom query filter, the fifth type argument
* @returns MongoDB filter
*/
queryFilterToMongoDBFilter(filter) {
const mongoFilter = Object.assign({}, filter, this.getDefaultMongoDBFilter());
this.virtualIDFilterTransform(mongoFilter);
return mongoFilter;
}
/**
* Checks if model have virtual ID field and if it has
* transforms the `id` field to `_id` field.
*
* @param filter
*/
virtualIDFilterTransform(filter) {
if (!this.repository.schema.__hasVirtualID__)
return;
if (filter?.id) {
filter._id = filter.id;
delete filter.id;
}
}
/** Mongoose Repository to interact with the Mongoose Model */
repository;
/** @inheritdoc */
async create(data) {
const self = this.constructor;
try {
return this.repository.put(data);
}
catch (err) {
throw (0, exception_util_1.duplicateEntryExceptionOrError)(err, self.duplicateEntryExceptionMessage(), data, self.uniqueFields || []);
}
}
/** @inheritdoc */
async retrieve(id) {
return this.repository.getOneById(id);
}
/** @inheritdoc */
async retrieveBy(filter) {
return this.repository.getOne(this.queryFilterToMongoDBFilter(filter));
}
/** @inheritdoc */
async update(id, data) {
return this.updateBy({ [this.primaryKeyField]: id }, data);
}
/** @inheritdoc */
async updateBy(filter, data) {
const self = this.constructor;
try {
await this.repository.update(this.queryFilterToMongoDBFilter(filter), data);
return this.retrieveBy(filter);
}
catch (err) {
throw (0, exception_util_1.duplicateEntryExceptionOrError)(err, self.duplicateEntryExceptionMessage(), data, self.uniqueFields || []);
}
}
/** @inheritdoc */
async delete(id) {
return this.deleteBy({ [this.primaryKeyField]: id });
}
/** @inheritdoc */
async deleteBy(filter) {
return this.repository.delete(this.queryFilterToMongoDBFilter(filter));
}
/** @inheritdoc */
async list(filter) {
return this.repository.find(this.queryFilterToMongoDBFilter(filter));
}
}
exports.BaseCRUDLMongooseService = BaseCRUDLMongooseService;