Skip to content

Instantly share code, notes, and snippets.

@jyotendra
Created Feb 22, 2018
Embed
What would you like to do?
sequelize-model-importer.ts is used to connect all models with main model, in sequelize.
import * as Sequelize from 'sequelize'
export interface AppUserAttributes {
id?: string
active?: boolean,
avatar?: string,
email?: string,
firstName?: string,
lastName?: string,
notification?: boolean,
phone?: string,
pwd?: string,
languageId?: string
}
export interface AppUserInstance extends Sequelize.Instance<AppUserAttributes> {
id: string
createdAt: Date
updatedAt: Date
email: string,
pwd: string,
languageId: string
}
export default function defineUser(sequelize: Sequelize.Sequelize, DataTypes) {
const AppUser = sequelize.define('AppUser', {
email: DataTypes.STRING,
pwd: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
AppUser.belongsTo(models.Language, {
foreignKey: 'languageId',
onDelete: 'CASCADE',
})
}
}
});
return AppUser
}
import * as fs from 'fs'
import * as path from 'path'
import * as Sequelize from 'sequelize'
const config = require('../config/config.json')
// Import model specification from its own definition file.
import { LanguageInstance, LanguageAttributes } from './language'
import { AppUserInstance, AppUserAttributes } from './appuser'
interface DbConnection {
Language: Sequelize.Model<LanguageInstance, LanguageAttributes>,
AppUser: Sequelize.Model<AppUserInstance, AppUserAttributes>
}
/**
* Object that will be used to hold all imported models and sequelize instance
*/
let db = {}
const dbConfig = config[process.env.NODE_ENV]
const sequelize = new Sequelize(
dbConfig['database'],
dbConfig['username'],
dbConfig['password'],
dbConfig
)
/**
* basename of file is getted, in our case it is "sequelize-model-importer.ts"
*/
const basename = path.basename(module.filename)
/**
* readdirSync returns file (files only, not folder) from directory yielded by __dirname
* __dirname returns the directory path of the script currently invoked.
* the files being emitted by readdirSync are transferred to a filter that tests for the following conditions:
* - file.indexOf('.') !== 0) : checks if the file don't have '.' at first place. Ex: '.gitignore', '.vscode' etc
* - (file !== basename): ignores the current file thats currently executing.
* - (file.slice(-3) === '.js'): only file with js extension are included (as typescript will be compiled to js at some point)
*/
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(function(file) {
/**
* The file that successfully pass the filter is imported by sequelize.
*/
const model = sequelize['import'](path.join(__dirname, file))
// NOTE: you have to change from the original property notation to
// index notation or tsc will complain about undefined property.
db[model['name']] = model
})
Object.keys(db).forEach(function(modelName) {
/**
* If the model do have 'associate' method declared then call it.
* associate is a user defined method that's included by user in extended model
* upto sequelize v3, class extension methods were to be defined inside 'classMethods' object.
* from sequelize v4, classes are extended as Model.associate = function (models){}
* see http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html for more details
*/
if (db[modelName].associate) {
// entire db object is passed, so that required model can be getted and associated.
db[modelName].associate(db)
}
})
/**
* Sequelize is the entire 'sequelize' node module
* sequelize is instance of sequelize orm which is connected with db
*/
db['sequelize'] = sequelize
db['Sequelize'] = Sequelize
export default <DbConnection>db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment