# mumba-microservice-sql

> A Mumba Microservice module that provides SQL database support (via Sequelize).

Latest version **0.5.11** (published 2018-04-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install mumba-microservice-sql
pnpm add mumba-microservice-sql
yarn add mumba-microservice-sql
bun add mumba-microservice-sql
```

Provides the command `dbmigrate`.

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.11 |
| Published | 2018-04-10 |
| First published | 2016-07-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 9 |
| Unpacked size | 94 KB |
| Known vulnerabilities | 0 (+7 in 2 direct dependencies) |
| Install scripts | no |
| Author | Mumba Pty Ltd |
| Maintainers | dgray, eddieajau, greagkeys, kennbaker, mumba.cloud, snarechops |
| Keywords | sequelize, sql, sqlite, microservice, database |

## Links

- npm: https://www.npmjs.com/package/mumba-microservice-sql
- Repository: https://gitlab.com/Mumba-Source/odyssey-microservice-router
- Homepage: https://gitlab.com/Mumba-Source/odyssey-microservice-router#readme
- Issues: https://gitlab.com/Mumba-Source/odyssey-microservice-router/issues
- npm.io page: https://npm.io/package/mumba-microservice-sql

## Dependencies (9)

- [knex](https://npm.io/package/knex.md) ^0.13.0
- [retry-if](https://npm.io/package/retry-if.md) ^1.1.0
- [sequelize](https://npm.io/package/sequelize.md) ^4.37.6
- [mumba-vault](https://npm.io/package/mumba-vault.md) ^0.2.1
- [mumba-config](https://npm.io/package/mumba-config.md) ^0.1.1
- [mumba-errors](https://npm.io/package/mumba-errors.md) ^0.1.6
- [command-line-args](https://npm.io/package/command-line-args.md) ^5.0.2
- [mumba-config-vault](https://npm.io/package/mumba-config-vault.md) ^0.2.0
- [mumba-typedef-dicontainer](https://npm.io/package/mumba-typedef-dicontainer.md) ^0.1.1

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 0.5.11 (latest) — 2018-04-10
- 0.5.10 — 2017-10-25
- 0.5.9 — 2017-10-21
- 0.5.8 — 2017-10-11
- 0.5.7 — 2017-10-11
- 0.5.6 — 2017-10-11
- 0.5.5 — 2017-09-01
- 0.5.4 — 2017-08-09
- 0.5.3 — 2017-08-08
- 0.5.2 — 2017-07-26
- 0.5.1 — 2017-06-29
- 0.5.0 — 2017-06-29
- 0.4.1 — 2017-02-20
- 0.4.0 — 2017-02-20
- 0.3.1 — 2017-02-09
- … 9 more at https://npm.io/package/mumba-microservice-sql/versions

## README

[![build status](https://gitlab.com/Mumba/node-microservice-sql/badges/master/build.svg)](https://gitlab.com/Mumba/node-microservice-sql/commits/master)
# Mumba Microservice SQL

Provide SQL support (via Sequelize) for a Microservice.

## Configuration

This module requires that a `Config` object has been registered as `config`.

Property | Type | Description
--- | :---: | ---
`db` | `object` | A dictionary of options required for [Sequelize](http://sequelize.readthedocs.io/en/latest/api/sequelize/#class-sequelize).
`db:sync:force` | `boolean` | Set to true if you want the database to perform a force sync (normally only used for testing). Default: `false`. 

## Models

Models need to be added to the `dbModels` group in the container. For example:

```
container.object('fooModel', fooModelFunction, ['dbModels']);
```

## DI Container

Name | Type | Description
--- | :---: | ---
`db` | `SqlDatabase` | A class that wraps the Sequelize database object.
`dbModels` | `object` | A dictionary of the database models.

## Installation

```sh
$ npm install --save mumba-microservice-sql
$ typings install --save dicontainer=npm:mumba-typedef-dicontainer
```

## Examples

TODO

## Database migrations

### Requirements

Database migrations for MySQL are provided via the [Knex](http://knexjs.org) package. In order to use this, you will need to install []MySQL](https://www.npmjs.com/package/mysql) into your Microservice. It's also a good idea to install Knex globally so you can use some of its command-line tools.
 
```sh
$ npm install --save mysql
$ npm install -g knex 
```

In addition, your Microservice configuration must specify the name of the Knex changelog table under the `db:knex` path. For example:
 
```typescript
let config: any = {
	db: {
		// ... other Sequelize database configuration.
		knex: '__knex'
	}
};

export default config;
```

> NOTE: If you are using a shared database, the changelog table needs to be unique for each Microservice otherwise Knex will get confused about what migration has or hasn't run yet. For example, if you have an _Articles_ Microservice, an appropriate name of the table would be `__knex_articles`.

### Creating migrations

If you've installed Knex globally, you can create a new migration script by running:

```sh
$ knex migrate:make migration_name
```

where `migration_name` is a textual label for the migration.

### Running migrations

To run the migrations during Microservice initialisation, include the following line in the `entrypoint.sh` file:

```sh
>&2 echo "Running database migrations"
/src/node_modules/.bin/dbmigrate --config=/src/dist/etc/config.js --vault-token=${VAULT_TOKEN} --vault=http://vault:8200/v1/secret/${CLIENT_DOMAIN}/common
```

Note that the expected arguments are:

* **Required** Path to the JavaScript configuration file.
* An optional Vault access token.
* An optional Vault secret path (required is the Vault token is supplied).

## Tests

To run the test suite, first install the dependencies, then run `npm test`:

```sh
$ npm run docker:up
$ npm install
$ npm test
$ npm docker:down
```

## People

The original author of _Mumba Microservice Vault_ is [Andrew Eddie](https://gitlab.com/u/aeddie.mumba).

[List of all contributors](https://gitlab.com/Mumba/node-microservice-vault/graphs/master)

## License

[Apache 2.0](LICENSE.txt)

* * *

&copy; 2017 [Mumba Pty Ltd](http://www.mumba.cloud). All rights reserved.

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