1.1.8 • Published 2 years ago
db-facade v1.1.8
db-facade
Boiler-plate code and thin wrapper for sequelize and umzug packages.
npm i --save db-facade sequelize umzug
Connect
import path from 'path';
import { DbLayer, DbLayerFactory, DialectTypes } from 'db-facade';
(async function() {
const dbLayer: DbLayer = DbLayerFactory.newDbLayer({
dialectType: DialectTypes.POSTGRES,
databaseCredentials: {
username: 'tmp_user',
password: 'tmp_pass',
database: 'tmp_db'
},
migrationOptions: {
migrationsPath: path.join(__dirname, 'app-migrations'),
migrationTableName: 'app_migrations'
}
});
console.info('authenticating db . . .');
await dbLayer.authenticate();
console.info('Success!');
}());
Run Migrations
Run the umzug migrations by simply calling runMigrations()
.
await dbLayer.runMigrations();
Initialize Models
Pass an initialization function for loading up all your models.
import { Sequelize } from 'sequelize';
import User from '../models/User';
import Post from '../models/Post';
// ...
await dbLayer.initialize(async (sequelize: Sequelize) => {
User.init({ /* ... */ });
Post.init({ /* ... */ });
User.hasMany(Post, {
sourceKey: 'id',
foreignKey: 'creatorId'
});
Post.belongsTo(User, { targetKey: 'id' });
});
Models are used like normal after initialization.
import User from '../models/User';
// ...
await User.findOne({ where: { id } });
Examples
See here for examples using other dialects.