maestro-server/server-app

View on GitHub
app/core/repositories/DBRepository.js

Summary

Maintainability
D
2 days
Test Coverage
A
100%
'use strict';

const _ = require('lodash');
const findFilledFormat = require('./transforms/findFilledFormat');
const activeTransform = require('./transforms/activeFormat');
const validAccessUpdater = require('./validator/validAccessUpdater');
const factoryValid = require('core/libs/factoryValid');

const DBRepository = (Entity, options = {}) => {

    const DB = Entity.dao;

    return {
        find(query, resFilled = Entity.resFilled) {
            return new Promise((resolve, reject) => {
                const limit = _.parseInt(query.limit);
                const page = _.parseInt(query.page);
                const skip = limit * (page - 1);

                const ascending = _.parseInt(_.get(query, 'ascending'));
                const direction = ascending ? 1 : -1;
                const orderBy = _.get(query, 'orderBy', 'updated_at');
                const filter = findFilledFormat(query, Entity.singleFilled);

                return DB
                    .limit(limit)
                    .skip(skip)
                    .sort(orderBy, direction)
                    .include(resFilled)
                    .find(filter)
                    .then((e) => _.map(e, (value) => value.get()))
                    .then(resolve)
                    .catch(reject);
            });
        },

        findOne(filters, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                const entityVS = _.get(Entity, 'visibility.single');
                const filter = Object.assign({}, filters, activeTransform.active(entityVS));

                return DB
                    .findOne(filter)
                    .then((e) => {
                        if (e)
                            e = e.get();

                        return _.pick(e, resFilled);
                    })
                    .then(resolve)
                    .catch(reject);

            });
        },

        count(filters = {}, fill = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                const filter = findFilledFormat(filters, fill);

                return DB.count(filter)
                    .then(resolve)
                    .catch(reject);
            });
        },

        update(filter, post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                let data = findFilledFormat(post, fill);

                if(!_.has(options, 'ignoreValid'))
                  data = factoryValid(data, Entity.validators.update);

                return new DB(data)
                    .updateFull(filter)
                    .then(validAccessUpdater)
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);
            });
        },

        patch(filter, post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                let data = findFilledFormat(post, fill);

                if(!_.has(options, 'ignoreValid'))
                  data = factoryValid(data, Entity.validators.update);

                return new DB(data, filter)
                    .updateAndModify(filter, options)
                    .then(validAccessUpdater)
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);

            });
        },

        updateByPushUnique(filter, post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                const data = _.pick(post, fill);

                return new DB(data)
                    .updateByPushUnique(filter, options)
                    .then(validAccessUpdater)
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);

            });

        },

        updateByPull(filter, post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                const data = _.pick(post, fill);

                return new DB(data)
                    .updateByPull(filter, options)
                    .then(validAccessUpdater)
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);
            });
        },

        increment(filter, post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                const data = _.pick(post, fill);

                return new DB(data)
                    .incrementBY(filter, options)
                    .then(validAccessUpdater)
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);

            });

        },

        create(post, fill = Entity.filled, resFilled = Entity.singleFilled) {
            return new Promise((resolve, reject) => {
                let data = findFilledFormat(post, fill);

                data = factoryValid(data, Entity.validators.create);

                return new DB(data)
                    .save()
                    .then((e) => _.pick(e.get(), resFilled))
                    .then(resolve)
                    .catch(reject);

            });

        },

        remove(filter, data = {}) {
            return new Promise((resolve, reject) => {
                const desactive = activeTransform.desactive();
                const ndata = _.assign(data, desactive);

                return new DB(ndata)
                    .updateAndModify(filter, options)
                    .then(validAccessUpdater)
                    .then(resolve)
                    .catch(reject);

            });
        }
    };
};

module.exports = DBRepository;