model.js 1.29 KB
/**
 * Base Model Class
 */

'use strict';

import DataBase from 'nedb-promise';
import path from 'path';
import config from '../../config/config';

const env = process.env.NODE_ENV || 'development';

class Model {
    constructor(name) {
        this.name = name;

        this.db = new DataBase({
            filename: path.join(config.dbDir, `${name}.json`),
            autoload: true
        });
    }

    _seeds() {
        return [];
    }

    get _name() {
        return this.name;
    }

    insert(...docs) {
        return this.db.insert(docs);
    }

    find(doc) {
        return this.db.find(doc)
    }

    cfind(doc) {
        return this.db.cfind(doc)
    }

    findOne(doc) {
        return this.db.findOne(doc);
    }

    findById(id) {
        return this.db.findOne({
            _id: id
        });
    }

    findAll() {
        return this.db.find({});
    }

    count(doc) {
        return this.db.count(doc);
    }

    update(query, update, options = {
        multi: true,
        upsert: false
    }) {
        return this.db.update(query, update, options);
    }

    remove(query, options = {
        multi: true
    }) {
        return this.db.remove(query, options);
    }

    removeById(id) {
        return this.remove({
            _id: id
        });
    }
}

export default Model;