@nelts/orm
nelts orm
Last updated 7 months ago by evio .
MIT · Original npm · Tarball · package.json
$ cnpm install @nelts/orm 
SYNC missed versions from official npm registry.

@nelts/orm

nelts架构的ORM库。

Install

npm i @nelts/orm

Configure

import { Plugin } from '@nelts/nelts';
export default async (app: Plugin) => {
  app.on('props', async configs => {
    await app.getComponent('@nelts/orm').props({
      // sequelize configs: http://docs.sequelizejs.com/manual/dialects.html
      sequelize: {
        database: configs.sequelize.database,
        username: configs.sequelize.username,
        password: configs.sequelize.password,
        options: configs.sequelize.options,
      },
      // local redis: true
      // object redis: { port, host }
      // string redis: `${host}:${port}`
      redis: '192.168.2.208:6379'
    });
  });
}

Sequelize Models

在项目目录下存在一个sequelize目录,里面指定的目录结构是这样的 sequelize/${database_name}/${table_name}.ts。每个ts文件内容请参考这里

import { sequelize } from '@nelts/orm';
const { Sequelize, Model, DataTypes } = sequelize;

export default class Maintainers extends Model {
  public id: number;
  public account: string;
  public pid: number;
  public ctime: Date;

  public static installer(sequelize: Sequelize) {
    Maintainers.init({
      id: {
        type: DataTypes.INTEGER.UNSIGNED,
        primaryKey: true,
        autoIncrement: true
      },
      account: DataTypes.STRING(100),
      pid: DataTypes.INTEGER({ length: 11 }),
      ctime: DataTypes.DATE
    }, {
      tableName: 'maintainer',
      sequelize,
      createdAt: 'ctime',
      updatedAt: 'utime',
      charset: 'utf8',
      collate: 'utf8_general_ci',
    });
  }
}

public static installer(sequelize: Sequelize) 函数必须存在,定义创建表的结构。

Redis

Redis是存在于 ctx.redis 上。

Redis operations

  • set(key: string, obj: any, expire?: number): Promise<void>;
  • get(key: string): Promise<any>;
  • exists(key: string): Promise<any>;
  • delete(key: string): Promise<any>;
  • clear(): Promise<void>;

具体请看 /src/redis.ts 源码。

Use Cacheable in service

import { Component, Context } from '@nelts/nelts';
import { Cacheable, CacheableInterface } from '../index';
export default class IndexService extends Component.Service {
  constructor(ctx: Context) {
    super(ctx);
  }

  @Cacheable('/test/:id(\\d+)')
  async valid() {
    return await this.ctx.dbomaintainer.findAll({
      attributes: ['id', 'pid', 'account', 'ctime']
    });
  }

  async get() {
    const obj: CacheableInterface = await this.valid();
    return await obj.get({ id: 123 });
  }
}

@Cacheable('/test/:id(\\d+)')注解指定service的方法valid是一个缓存函数。那么可以如下调用:

// eg: in controller
const service = new this.service.IndexService(ctx);
const obj: CacheableInterface = await service.valid();
const result = await obj.invoke();
const result = await obj.set({ id: 234 }, 10000);
const result = await obj.get({ id: 234 }, 10000);
await obj.delete({ id: 234 });

License

MIT

Copyright (c) 2019-present, yunjie (Evio) shen

Current Tags

  • 1.0.25                                ...           latest (7 months ago)

25 Versions

  • 1.0.25                                ...           7 months ago
  • 1.0.24                                ...           7 months ago
  • 1.0.23                                ...           7 months ago
  • 1.0.22                                ...           7 months ago
  • 1.0.21                                ...           7 months ago
  • 1.0.20                                ...           7 months ago
  • 1.0.19                                ...           7 months ago
  • 1.0.18                                ...           7 months ago
  • 1.0.17                                ...           7 months ago
  • 1.0.16                                ...           7 months ago
  • 1.0.15                                ...           8 months ago
  • 1.0.14                                ...           8 months ago
  • 1.0.13                                ...           8 months ago
  • 1.0.12                                ...           8 months ago
  • 1.0.11                                ...           8 months ago
  • 1.0.10                                ...           8 months ago
  • 1.0.9                                ...           8 months ago
  • 1.0.7                                ...           8 months ago
  • 1.0.6                                ...           8 months ago
  • 1.0.5                                ...           8 months ago
  • 1.0.4                                ...           8 months ago
  • 1.0.3                                ...           8 months ago
  • 1.0.2                                ...           8 months ago
  • 1.0.1                                ...           8 months ago
  • 1.0.0                                ...           8 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 25
Dependencies (8)
Dev Dependencies (1)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |