db-facade
TypeScript icon, indicating that this package has built-in type declarations

1.1.8 • Public • Published

db-facade

npm

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.

Readme

Keywords

none

Package Sidebar

Install

npm i db-facade

Weekly Downloads

0

Version

1.1.8

License

ISC

Unpacked Size

61.1 kB

Total Files

22

Last publish

Collaborators

  • wko