1.0.25 • Published 5 years ago

@nelts/orm v1.0.25

Weekly downloads
25
License
MIT
Repository
-
Last release
5 years ago

@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

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago