Giter VIP home page Giter VIP logo

ah-sequelize-plugin's Introduction

ah-sequelize-plugin

This pluggin will use the sequelize orm to create api.models which contain your sequelize models

Setup

  • install this plugin: npm install ah-sequelize-plugin --save
  • be sure to enable the pluggin within actionhero (config/plugins.js)
  • you will need to add the sequelize package (npm install sequelize --save) to your package.json
  • you will need to add the sequelize-fixtures package (npm install sequelize-fixtures --save) to your package.json
  • you will need to add the mysql (or other supported database) package (npm install mysql --save) to your package.json

A ./config/sequelize.json file will be created which will store your database configuration

Use the exports form of sequelize models in ./models with the file name matching that of the model, IE:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define("Project", {
    name: DataTypes.STRING,
    description: DataTypes.TEXT
  })
}

Models are loaded into api.models, so the example above would be api.models.Project.

This pluggin does not condone the use of Sequelize.sync() in favor of migrations. Keep you migrations in ./migrationss and run api.sequelize.migrate().

An example migration to create a users table would look like:

// from ./migrations/20140101000001-create-users.js

module.exports = {
  up: function(migration, DataTypes, done) {
    migration.createTable('users', {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: DataTypes.STRING,
      email: DataTypes.STRING,
      phone: DataTypes.STRING,
      passwordHash: DataTypes.TEXT,
      passwordSalt: DataTypes.TEXT,
      createdAt: DataTypes.DATE
      updatedAt: DataTypes.DATE
    }).complete(function(){

    migration.addIndex('users', ['email'], {
      indexName: 'email_index',
      indicesType: 'UNIQUE'
    }).complete(function(){

    migration.addIndex('users', ['name'], {
      indexName: 'name_index',
      indicesType: 'UNIQUE'
    }).complete(function(){

    migration.addIndex('users', ['phone'], {
      indexName: 'phone_index',
      indicesType: 'UNIQUE'
    }).complete(function(){

      done();

    });
    });
    });
    });
  },
 
  down: function(migration, DataTypes, done) {
    migration.dropTable('users').complete(done);
  }
}

You can use the sequelize-cli for more utilities or you can add a migration grunt helper(s) to your actionhero project by adding the below to your gruntfile.js:

grunt.registerTask('migrate','run any pending database migrations',function(file){
  var done = this.async();
  init(function(api){
    api.sequelize.migrate(function(){
      done();
    })
  })
})

To migrate down also add the following:

grunt.registerTask('migrate:undo','revert and run the “down” action on the last run migration',function(file){
  var done = this.async();
  init(function(api){
    api.sequelize.migrateUndo(function(){
      done();
    })
  })
})

If you want to sync, you can api.sequelize.sync() or api.models.yourModel.sync();

If you want to declare associations, best practice has you create an associations initializer within your project which might look like this:

exports.associations = function(api, next){

  api.associations = {};

  api.associations._start = function(api, next){
    api.models.user.hasMany(api.models.posts);
    api.models.posts.hasMany(api.models.comments);

    api.models.comments.belongsTo(api.models.posts);
    api.models.posts.belongsTo(api.models.user);

    next();
  };

  next();
}

We use the sequelize-fixtures package to load in JSON-defined fixtures in the test NODE_ENV. Store your fixtures in ./test/fixtures/*.json or ./test/fixtures/*.yml

ah-sequelize-plugin's People

Contributors

evantahler avatar mlix8hoblc avatar submitteddenied avatar

Watchers

 avatar  avatar  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.