@hapiness/sequelize
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Hapiness

sequelize-module

Sequelize-module is a wrapper of sequelize and sequelize-typescript for the Hapiness Framework.

Table of contents

Using sequelize-module inside your Hapiness application

yarn or npm it in your package.json

// 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:

# 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
"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

@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 with the minimum of the options below:

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. However, for your Models to be integreted in your Hapiness application you need to decorate it with @TableModel:

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

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

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.

class FooProvider {

    constructor(private _sequelize: SequelizeClientService) {}

    bar(): Observable<string> {
    	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)

Back to top

Maintainers

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

Back to top

License

Copyright (c) 2017 Hapiness Licensed under the MIT license.

Back to top

Dependents (0)

Package Sidebar

Install

npm i @hapiness/sequelize

Weekly Downloads

0

Version

1.0.0

License

SEE LICENSE IN https://github.com/hapinessjs/sequelize-module/blob/master/LICENSE.md

Last publish

Collaborators

  • hapiness