@aequum/mongoose
Version:
aequum mongoose tools for repository, pagination, CRUD/CRUDL, configs and utils
57 lines (56 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongooseRepository = void 0;
const data_1 = require("@aequum/exceptions/data");
class MongooseRepository {
model;
/**
* The mongoose schema model
* @protected
*/
get schema() {
return this.model.schema;
}
async delete(filter) {
await this.model.deleteOne(filter).exec();
}
async deleteMany(filter) {
await this.model.deleteMany(filter).exec();
}
async find(filter) {
return this.model.find(filter).exec();
}
async getOne(filter) {
const doc = await this.model.findOne(filter).exec();
if (!doc)
throw new data_1.NotFoundException(`${this.model.modelName} not found`);
return doc;
}
async getOneById(id) {
const doc = await this.model.findById(id).exec();
if (!doc)
throw new data_1.NotFoundException(`${this.model.modelName} not found`);
return doc;
}
async put(data) {
const document = new this.model(data);
await document.save();
return document.toJSON();
}
async putMany(data) {
return Promise.all(data.map((item) => this.put(item)));
}
async update(filter, data) {
return this.model.updateOne(filter, { $set: data }).exec();
}
async pushOnArrayProperty(filter, property, data) {
return this.model.updateOne(filter, { $push: { [property]: data } }).exec();
}
async pullFromArrayProperty(filter, property, data) {
return this.model.updateOne(filter, { $pull: { [property]: data } });
}
async updateQuery(filter, query) {
return this.model.updateMany(filter, query).exec();
}
}
exports.MongooseRepository = MongooseRepository;