Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hapinessjs/sequelize-module

Sequelize integration for Hapiness framework
https://github.com/hapinessjs/sequelize-module

Last synced: 22 days ago
JSON representation

Sequelize integration for Hapiness framework

Awesome Lists containing this project

README

        

Hapiness




build


coveralls


dependencies


devDependencies




Typescript logo


ReactiveX logo


Hapijs logo


# sequelize-module

Sequelize-module is a wrapper of [sequelize](http://docs.sequelizejs.com/) and [sequelize-typescript](https://github.com/RobinBuschmann/sequelize-typescript) for the Hapiness Framework.

## Table of contents
* [Using sequelize-module inside your Hapiness application](#using-sequelize-module-inside-your-hapiness-application)
* [`yarn` or `npm` it in your `package.json`](#yarn-or-npm-it-in-your-packagejson)
* [Installing peer dependencies](#installing-peer-dependencies)
* [Importing `SequelizeModule` from the library](#importing-sequelizemodule-from-the-library)
* [Models](#models)
* [Defining a model](#defining-a-model)
* [Injecting a model](#injecting-a-model)
* [`SequelizeClientService` functions](#sequelizeclientservice-functions)
* [Mainteners](#maintainers)

## Using sequelize-module inside your Hapiness application

### `yarn` or `npm` it in your `package.json`
```bash
// Using NPM
$ npm install --save @hapiness/core @hapiness/sequelize rxjs

// Using Yarn
$ yarn add @hapiness/core @hapiness/sequelize rxjs
```

### Installing peer dependencies
According to `sequelize` documentation you will need to install at least one of these packages for sequelize-module to work:

```bash
# Using NPM
$ npm install --save pg@6 pg-hstore #pg@7 is currently not supported
$ npm install --save mysql2
$ npm install --save sqlite3
$ npm install --save tedious # MSSQL

# Using Yarn
$ yarn add pg pg-hstore
$ yarn add mysql2
$ yarn add sqlite3
$ yarn add tedious # MSSQL
```
```javascript
"dependencies": {
"@hapiness/core": "^1.3.0",
"@hapiness/sequelize": "^1.0.0",
"rxjs": "^5.5.3",
// + one of these
"mysql2": "^1.5.1",
"pg": "^6.4.2",
"pg-hstore": "^2.3.2",
"sqlite3": "^3.1.13",
"tedious": "^2.1.5"
//...
}
```

### Importing `SequelizeModule` from the library

This module provide an Hapiness extension for Sequelize.
To use it, simply register it during the ```bootstrap``` step of your project and provide the ```SequelizeExt``` with its config

```typescript
@HapinessModule({
version: '1.0.0',
providers: [],
declarations: [],
imports: [SequelizeModule]
})
class MyApp implements OnStart {
constructor() {}
onStart() {}
}

Hapiness
.bootstrap(
MyApp,
[
/* ... */
SequelizeExt.setConfig(
{
dialect: 'sqlite', // 'mysql'|'sqlite'|'postgres'|'mssql'
username: 'username',
password: 'password',
database: 'database',
storage: ':memory:' // Only for sqlite (path of the file or :memory:)
// ...
}
)
]
)
.catch(err => {
/* ... */
});
```

`SequelizeExt` needs an `Option` object so you can provide all the properties defined by [Sequelize](http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor) with the minimum of the options below:
```typescript
interface Options {
username: string;
password: string;
database: string;
dialect: 'mysql'|'sqlite'|'postgres'|'mssql';
}
```

## Models

### Defining a model

Sequelize-module uses `sequelize-typescript` so you can define your models with classes in the form of [sequelize-typescript models](https://github.com/RobinBuschmann/sequelize-typescript#model-definition). **However**, for your Models to be integreted in your Hapiness application you need to decorate it with `@TableModel`:

```Typescript
import { Model, Table } from 'sequelize-typescript';
import { TableModel } from '@hapiness/sequelize';

@TableModel()
@Table
export class User extends Model { }
```

> **NB**: You need to either name your model's file the name of your + `.ts` (here `user.ts`) or you need to export your model as default: `export default class User extends...`

### Injecting a model

To Inject a Model in your application you just need to pass it to the array of `declarations` of your @`HapinessModule`.

> See examples directory for a basic working implementation. Run it with the command `npm run dev:watch`.

## ```SequelizeClientService``` functions

To use sequelize, you need to inject inside your providers the ```SequelizeClientService```.

```typescript
class FooProvider {

constructor(private _sequelize: SequelizeClientService) {}

bar(): Observable {
return this._sequelize.instance.model('MyModel');
}
}
```

`SequelizeClientService.instance` this will return you the sequelize client and you will be able to call on it every sequelize commands (see the reference [here](http://docs.sequelizejs.com/identifiers.html))

[Back to top](#table-of-contents)

## Maintainers


tadaweb









Julien Fauville
Antoine Gomez
Sébastien Ritz
Nicolas Jessel
Florent Bennani

[Back to top](#table-of-contents)

## License

Copyright (c) 2017 **Hapiness** Licensed under the [MIT license](https://github.com/hapinessjs/sequelize-module/blob/master/LICENSE.md).

[Back to top](#table-of-contents)