egg-json-db

1.0.10 • Public • Published

egg-json-db

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-json-db --save

Usage

// {app_root}/config/plugin.js
exports.jsonDb = {
  enable: true,
  package: 'egg-json-db',
};

Configuration

// {app_root}/config/config.default.js
exports.jsonDb = {
  // 存储路径  /test/
  savePath: '',
  // 命名
  nameSpace: 'model',
  // 加载文件夹
  loadDir: 'app/model',
  saveOnPush: true,
  humanReadable: false,
  separator: '/',
};

see config/config.default.js for more detail.

Example

插件基于 node-json-db,具体方法可参考node-json-db

// {app_root}/app/model/myDataBase.js
'use strict';

module.exports = app => {
  /**
   * jsonDbSchema
   * @param {*} obj | json 初始化数据,默认{}
   * @param {*} model | 生成json文件名,默认data
   * @return | JsonDB
   */
  const myDataBase = app.jsonDbSchema({ test1: 0 }, 'test1');
  return myDataBase;
};

// {app_root}/app/controller/home.js
('use strict');

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { model } = this.app;
    model.myDataBase.push('/test3', { test: 'test', json: { test: ['test'] } });
    console.log('数据:', JSON.stringify(model.myDataBase.getData('/')));
    this.ctx.body = model.myDataBase.getData('/test3');
  }
}

module.exports = HomeController;

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-json-db

Weekly Downloads

0

Version

1.0.10

License

MIT

Unpacked Size

6.34 kB

Total Files

6

Last publish

Collaborators

  • mingpo