Skip to content

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

License

Notifications You must be signed in to change notification settings

cclient/sequelize-sharding

Repository files navigation

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 [email protected]:cclient/sequelize-sharding.git or add dependencies sequelize-sharding":[email protected]:cclient/sequelize-sharding.git then npm install

original sequelize

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

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

typescript

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

custom function to generate table's postfix like

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

more detail see test/test.js

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published