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

'use strict';

const DataBase = require('nedb-promise');
const path = require('path');
const config = require('../config');

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
    });
  }
}

module.exports = Model;