1.1.8 • Published 2 years ago

db-facade v1.1.8

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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.

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago