# db-facade

> Thin abstraction layer for sequelize.

Latest version **1.1.8** (published 2022-07-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install db-facade
pnpm add db-facade
yarn add db-facade
bun add db-facade
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.8 |
| Published | 2022-07-12 |
| First published | 2022-06-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 59.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | willko747@gmail.com |
| Maintainers | wko |

## Links

- npm: https://www.npmjs.com/package/db-facade
- Repository: https://github.com/William-Olson/db-facade
- Homepage: https://github.com/William-Olson/db-facade#readme
- Issues: https://github.com/William-Olson/db-facade/issues
- npm.io page: https://npm.io/package/db-facade

## Recent versions

- 1.1.8 (latest) — 2022-07-12
- 1.1.7 — 2022-07-12
- 1.1.6 — 2022-07-12
- 1.1.5 — 2022-07-11
- 1.1.4 — 2022-06-22
- 1.1.3 — 2022-06-04
- 1.1.2 — 2022-06-01
- 1.1.1 — 2022-06-01
- 1.1.0 — 2022-06-01
- 1.0.5 — 2022-06-01
- 1.0.4 — 2022-06-01
- 1.0.3 — 2022-06-01
- 1.0.2 — 2022-06-01
- 1.0.1 — 2022-06-01

## README

## db-facade

[![npm](https://img.shields.io/npm/v/db-facade?logo=npm)](https://www.npmjs.com/package/db-facade)

Boiler-plate code and thin wrapper for [sequelize](https://www.npmjs.com/package/sequelize) and [umzug](https://www.npmjs.com/package/umzug) packages.

```
npm i --save db-facade sequelize umzug
```

---

### Connect

```TypeScript
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()`.

```TypeScript
await dbLayer.runMigrations();  
```


### Initialize Models

Pass an initialization function for loading up all your models.

```TypeScript
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.

```TypeScript
import User from '../models/User';

// ...

await User.findOne({ where: { id } });
```



---

### Examples

See [here](./examples/) for examples using other dialects.

---
_Source: https://npm.io/package/db-facade · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
