UNPKG

generic-repository

Version:

Generic repository pattern implementation for node.js. Currently supports mongo and in-memory(testing) databases.

137 lines (136 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const mongodb_1 = require("mongodb"); const not_found_error_1 = require("../errors/not_found_error"); class MongoDBRepository { constructor(type, db) { this.toInstance = (listItem) => { return new this.Type(listItem); }; this.Type = type; this.Model = db.then(ready => ready.collection(this.Type.prototype.constructor.name)) .catch(err => { console.error(this.Type.prototype.constructor.name, 'An error occured while making connection to the database.'); }); } paginate(conditions, sortOptions, page, perPage) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .find(conditions) .sort(sortOptions) .skip((perPage * page) - perPage) .limit(perPage) .map(this.toInstance) .toArray() .catch(this.reject)); } insertMany(list) { const withMappedIds = list.map((data) => { data['_id'] = this.idToObjectId(data['_id']); return data; }); return this.Model.then(model => model .insertMany(withMappedIds) .then(items => items.ops) .then(this.toInstanceArray.bind(this)) .catch(this.reject)); } count(conditions = {}) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .count(conditions)); } find(conditions) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .find(conditions) .map(this.toInstance) .toArray() .catch(this.reject)); } findOne(conditions) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .findOne(conditions) .then(doc => { if (doc) { return this.toInstance(doc); } else { throw new not_found_error_1.NotFoundError('No results for query: ' + JSON.stringify(conditions)); } }).catch(this.reject)); } findById(id) { return this.findOne({ _id: this.idToObjectId(id) }); } findLast(sortField, limit) { return this.Model.then(model => model .find() .sort({ [sortField]: -1 }) .limit(limit) .map(this.toInstance) .toArray() .catch(this.reject)); } findLastByQuery(conditions, sortField, limit) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .find(conditions) .sort({ [sortField]: -1 }) .limit(limit) .map(this.toInstance) .toArray() .catch(this.reject)); } insert(data) { data['_id'] = this.idToObjectId(data['_id']); return this.Model.then((model) => model .insertOne(data) .then(item => item.ops[0]) .catch(this.reject)); } update(conditions, newData) { this.CastQueryIdToObjectId(conditions); if (newData['_id']) { newData['_id'] = this.idToObjectId(newData['_id']); } return this.Model.then(model => model .findOneAndUpdate(conditions, newData, { upsert: true, returnOriginal: false }) .then(result => this.toInstance(result.value)) .catch(this.reject)); } deleteOne(conditions) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .deleteOne(conditions) .then(result => !!result.result.ok) .catch(this.reject)); } deleteMany(conditions) { this.CastQueryIdToObjectId(conditions); return this.Model.then(model => model .deleteMany(conditions) .then(result => !!result.result.ok) .catch(this.reject)); } CastQueryIdToObjectId(query) { if (query['_id']) { query['_id'] = this.idToObjectId(query['_id']); } } idToObjectId(id) { if (id && mongodb_1.ObjectId.isValid(id)) { return mongodb_1.ObjectId(id); } return id; } toInstanceArray(listItems) { const instantiatedListItems = listItems.map(this.toInstance); return Promise.resolve(instantiatedListItems); } reject(error) { return Promise.reject(error); } } exports.default = MongoDBRepository;