An open API service indexing awesome lists of open source software.

https://github.com/cclient/sequelize-sharding

通过sequelize的hooks模块,更改读写前的table_name,实现类似spring AOP的分表读写,sequelize Model hooks for sharded-table that like [tablename]_yyyy_mm...
https://github.com/cclient/sequelize-sharding

sequelize sequelize-model-hooks sharding

Last synced: 3 months ago
JSON representation

通过sequelize的hooks模块,更改读写前的table_name,实现类似spring AOP的分表读写,sequelize Model hooks for sharded-table that like [tablename]_yyyy_mm...

Awesome Lists containing this project

README

          

### desc

sequelize Model hooks for sharded-table that like `[tablename]_yyyy_mm`...( table should be created before)

use sequelize original hooks as a "AOP" to change target table name

`user => user_2018_01,user_2018_02, ... ,user_2018_11,user_2018_12`

### start

* `npm install sequelize`

* `npm install git@github.com:cclient/sequelize-sharding.git` or add dependencies `sequelize-sharding":git@github.com:cclient/sequelize-sharding.git` then `npm install`

### original sequelize

```js

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
// SQLite only
storage: 'path/to/database.sqlite',
// http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
operatorsAliases: false
});

const User = sequelize.define('user', {
id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
name: Sequelize.STRING,
birthday: Sequelize.DATE
}, { freezeTableName: true, createdAt: false, updatedAt: false, deletedAt: false });

let user = {
name: "china",
birthday: new Date("2018-10-01")
}

```

### hook

#### javascript

```js

const sequelizeSharding = require('sequelize-sharding');
sequelizeSharding.useCustomTableName(sequelize, User, "birthday", true)
User.insertOrUpdate(user)

```

#### typescript

```ts

import { useCustomTableName} from 'sequelize-sharding';
useCustomTableName(sequelize, User, "birthday", true)
User.insertOrUpdate(user)

```

### custom function to generate table's postfix like

```fun

useCustomTableName(sequelize, User, null, false, function(value) {
return getYYMM(new Date())
})

```

more detail see test/test.js