egg-nohm

1.0.1 • Public • Published

egg-nohm

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

为什么有这个插件

  • 让nohm支持ioredis,egg官方提供的是ioredis,更容易集成.
  • 让nohm的api promise化

Install

$ npm i egg-nohm --save

Usage

nohm 详细请看https://github.com/maritz/nohm

// 模型定义,app/nohm/model目录下的文件会自动加载
// app/nohm/model/ObjModel.js
const nohm = require('nohm').Nohm;
// 别忘记了module.exports =.   
module.exports = nohm.model('ObjectModel', {
  properties: {
    nickName: {
      type: 'string',
    },
    currentSong: {
      type: 'json',
    },
    currentListType: {
      type: 'string',
      defaultValue: 'local',
    },
    localList: {
      type: 'json',
      defaultValue: { items: [] },
    },
    playList: {
      type: 'json',
      defaultValue: { items: [] },
    },
  },
});

//也可以
// app/nohm/model/PlayingSong.js
const nohm = require('nohm').Nohm;

module.exports = app => {
  const PlayingSong = nohm.model('PlayingSong', {
    properties: {
      nickName: {
        type: 'string',
      },
      currentSong: {
        type: 'json',
      },
      currentListType: {
        type: 'string',
        defaultValue: 'local',
      },
      localList: {
        type: 'json',
        defaultValue: { items: [] },
      },
      playList: {
        type: 'json',
        defaultValue: { items: [] },
      },
    },
  });
  return PlayingSong;
};
// 所有app/nohm/model下的文件会加载到app.nohmModel
 const PlayingSong = app.nohmModel.PlayingSong;
    const ps = new PlayingSong();
    ps.id = 'abc';
    const nickName = 'who am i ';
    ps.p({
      nickName,
    });
    yield ps.save$();
    const data = yield PlayingSong.load$(ps.id);
    assert(data.nickName === nickName);
    yield PlayingSong.remove$(ps.id);
// {app_root}/config/plugin.js
exports.redis = {
  enable: true,
  package: 'egg-redis',
};

exports.nohm = {
  enable: true,
  package: 'egg-nohm',
};

promise

nohm中的方法以$结尾的都promisify了. 如原方法save, 会另外增加一个save$返回promise

Configuration

// {app_root}/config/config.default.js
exports.redis = {
  client: {
    host: '127.0.0.1',
    port: 6379,
    password: '',
    db: '0',
  },
};
exports.nohm = {
};

save id分配的问题

如果你期望下面的代码

var model=new SomeNohmModel()  
model.id='abcd'
model.save()

不管这里的id存在还是不存在,都以这个id保存到redis中. 如果存在id='abcd'的,就更新. 如果不存在新插入对象的id还是等于'abcd'.请加下面的配置.

idGenerator (cb) {
      cb(this.id)
    }

因为nohm默认的行为是

  •  如果id不填,自动生成一个不重复的
  •  如果id有值,会进一步的到数据库中验证这个值存在不,不存在他还是会自动生成一个新值代替你填入的id的

配置项目请看

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

Example

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-nohm

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • bugss