Skip to content

Field you can use to add automatical JSON stringifying/parsing to you sequelize models.

Notifications You must be signed in to change notification settings

alex-friedl/sequelize-json

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sequelize-json

Want to store JSON data in your database (that isn't PostGres)? This is for you.

How

  • Create a database and a Schema:
    var Sequelize = require('sequelize'),
        JsonField = require('sequelize-json'),
        db,
        User;

    db = new Sequelize('database', 'username', 'password', {
      dialect: 'sqlite',
      logging: false
    });

    User = db.define('User', {
      username: Sequelize.STRING,
      jsonField: JsonField(db, 'User', 'jsonField')
    });

Note the parameters of JsonField, you pass your Sequelize instance, the name of the model, and the name of the field. A little awkard, but this is needed in order to add the proper hooks to the model instance.

Now, you can always treat that field as a json object:

User.create({
      username: 'Scott',
      jsonField: {
        likes: ['running', 'node']
      }
    })
    .then(function(user) {
      user.jsonField.likes.push('tests');
      return user.save();
    })
    .then(function(user) {
      expect(user.jsonField).to.be.a('object');
      expect(user.jsonField.likes).to.have.length(3);
    });

It will work with normal save commands, as well as updateAttribute commands.

About

Field you can use to add automatical JSON stringifying/parsing to you sequelize models.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%