Socket
Socket
Sign inDemoInstall

db-facade

Package Overview
Dependencies
23
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    db-facade

Thin abstraction layer for sequelize.


Version published
Maintainers
1
Install size
59.7 kB
Created

Readme

Source

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.

FAQs

Last updated on 12 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc