Sequelize supports all of these database drivers
mysql
postgres
sqlite
mariadb
mssql
db2
snowflake
oracle
Make sure to install one of the drivers first.
# Install One of the following:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server
$ npm install --save oracledb # Oracle DatabaseMake sure to add those values before using middlewares.
const {TenantSchema, DomainSchema} = require('node-tenancy');
const UserSchema = require('user.js');
config.setConfig({
"central_domains": [
"test"
],
"central_schemas": [
TenantSchema,
DomainSchema
],
"tenant_schemas": [
UserSchema
]
});This is how you can make your own models.
user.js
'use strict';
const {
DataTypes,
Model,
} = require('sequelize');
module.exports = (sequelize) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* This method is called after defining all app models
*/
static associate(models) {
// define association here
}
}
User.init({
id: {
type: DataTypes.INTEGER,
autoIncrementIdentity: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
},
username: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
sequelize,
modelName: 'User',
tableName: 'users',
});
return User;
};We made useful config obj to make it easier to access some values. Ex:
const {config} = require('node-tenancy');
//values exists
const tenancy_config = {
"connection": "tenant", // central in case of central connection
"queue_connection": "", // null by default
"tenant_id": "example",
"tenant_connection": "sequelize db connection object",
"central_connection": "sequelize db connection object",
}
//add more if needed
config.setConfig({
"test": "new"
});We tried to make it easy to access a specific connection model
const {db} = require('node-tenancy');
db.getModel('Tenant').findAll().then(tenants => {
console.log(tenants);
});First thing you have to separate tenants migration files from central migrations
so, it's recommended to make all tenants migrations under /migrations/tenants.
we provided some commands to make it fast to migrate all changes to all tenants or for a specific tenant.
First, you can use sequelize CLI to create tenant migration files with
providing --migration-path option and provide your tenant migration
path, and you can use /migrations/tenants
as it's the default path the package is using.
tenancy-db <command>
Commands:
tenancy-db migrate Run pending migrations
tenancy-db migrate:rollback Rollback latest migrations
tenancy-db migrate:fresh Rollback all migrations
Options:
--help Show help
--t or --tenants Apply to multiple tenants (comma seperated)
--path Provide custom migrations path (default is /migrations/tenants)Examples:
tenancy-db migrate --path 'migrations/tenant'tenancy-db migrate --tenants id1,id2We use sequelize CLI for central related migrations and models