Giter Site home page Giter Site logo

juanelorganelo / sequelize-classes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from homeservicesofamerica/sequelize-classes

0.0 2.0 0.0 73 KB

Create Sequelizejs models with ES6 Class syntax.

License: GNU General Public License v3.0

JavaScript 100.00%

sequelize-classes's Introduction

Sequelize Classes

travis-ci Stories in Ready

Overview

This package serves to assist in the creation of sequelize models via ES7 class syntax.

Requirements

  • Babel.js with --stage 0 flag OR using the stage 0 plugin for babel 6.0+
  • Sequelize.js

Extending from the Model Class

Lets start with an example of what a model could look like with Sequelize classes.

import {Model, hasMany} from 'sequelize-classes';
import {STRING} from 'sequelize';

@hasMany('Post', {as: 'posts', foreignKey: 'authorId'})
export default class User extends Model {
    username = {type: STRING, allowNull: false, unique: true};
    email = {type: STRING, allowNull: false, unique: true};
    firstName = {type: STRING};
    lastName = {type: STRING};

    get name() {
        return `${this.firstName} ${this.lastName}`;
    }
}

Note that this class is just a class, not a sequelize model at this point. The purpose of the Model class is to set up your classes in such a way so that the Builder tool can create your sequelize model definitions properly.

Using Builder to create your sequelize connection and define models

 import {Builder} from 'sequelize-classes';
 import {User} from './models/user';

 // Normal sequelize options.
 const options = {
   database: process.env.DB_NAME,
   username: process.env.DB_USER,
   pass: process.env.DB_PASS,
   config: {
     host: process.env.DB_HOST,
     port: process.env.DB_PORT,
     logging: false,
     dialect: 'postgres'
   }
 };

 // Pass sequelize connection options and an array of Classes extended from Model.
 const database = new Builder(options, [User]);

 // You can now access your sequelize instance via database.base and access all your models by name - database.User

sequelize-classes's People

Contributors

brad-decker avatar yazshel avatar dwightbe avatar

Watchers

James Cloos avatar Juan Manuel Garcia Junco moreno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.