Giter Club home page Giter Club logo

ectypes-sequelize.js's Introduction

build status

ectypes-sequelize

A ectypes strategy for Sequelize.js - http://www.sequelizejs.com/.

Ectypes can be found at http://github.com/nicholasf/ectypes.js

Installation

npm install ectypes-sequelize

Explanation

Calls to build() and create() on a Sequelize model are supported.

First, create the Sequelize connection and configure it on the ectypes-sequelize strategy. Then load the strategy into ectypes.

var ectypes = require('ectypes');
var sequelize = new Sequelize('ectypes_test', 'nicholas', null);
var ectypesSequelize = require('ectypes-sequelize').setup(sequelize);

ectypes.load(ectypesSequelize);

Once you have defined an ectype for your model:

//the Sequelize model definition
var Project = sequelize.define('Project', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
});

//the ectype for the Project model
var projectType = {
	Project: {
		title: function(){ return Faker.Name.findName() }
	}
};

ectypes.add(projectType);

You may then call build() or create() upon it through ectypes to produce the object.

ectypes.Project.create().success(function(project){
	console.log(project.id, project.title);  //this logs something like 6 'Autumn Medhurst' (using Faker)
});

Use hooks to model associations.

	ectypes.plan(
		{
			Project: {
				title: function(){ return Faker.Name.findName() },
				description: function(){ return Faker.Lorem.findSentences() },
				_hooks: [ 'add a task to a project', function(project, functionName){
					if (functionName === 'create'){				
						Task.create().success(function(task){
							project.task = task;
							project.save();
						});
					}
				}]
			}
		});

	project = ectypes.Project.build();

Running Tests

mocha --ignore-leaks tests/ectypes-sequelize-test.js 

Credits

This library was made while working at Moneytribe - http://moneytribe.com.au.

ectypes-sequelize.js's People

Contributors

nicholasf avatar

Stargazers

dave avatar Nate avatar

Watchers

James Cloos avatar

ectypes-sequelize.js's Issues

hook with sequelize causing error

        , Broker: {
            name: function(){ return Faker.Name.findName() }
            , _hooks: [{'create a user': function(broker, funcName){
                if (funcName === "create"){
                    ectypes.User.create().success(function(user){
                        console.log(" - - - - - ", broker);
                        user.broker_id = broker.id;
                        user.save();
                    })
                }
            }
        }]

the stacktrace:




/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/mysql/lib/client.js:187
  val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
            ^
TypeError: Object function () {
    switch(Helpers.randomNumber(2))
    {
    case 0:
        return Helpers.randomize(definitions.first_name());
        break;
    case 1:
        return Helpers.randomize(definitions.first_name()) + Helpers.randomize([".", "_"]) + Helpers.randomize(definitions.last_name()) ;
        break;
    }
} has no method 'replace'
    at Client.escape (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/mysql/lib/client.js:187:13)
    at Object.module.exports.escape (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/utils.js:38:19)
    at module.exports.QueryGenerator.insertQuery.replacements.values (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dialects/mysql/query-generator.js:138:24)
    at Array.map (native)
    at Array.<anonymous> (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sugar/release/1.3/sugar-1.3-full.development.js:104:25)
    at Object.module.exports.QueryGenerator.insertQuery (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dialects/mysql/query-generator.js:137:47)
    at module.exports.QueryInterface.insert (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/query-interface.js:181:35)
    at module.exports.DAO.save (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dao.js:107:34)
    at module.exports.DAOFactory.create (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/sequelize/lib/dao-factory.js:193:31)
    at Object.exports.create (/Users/nicholas/code/src/clients/moneytribe/quoting/node_modules/ectypes-sequelize/lib/ectypes-sequelize.js:17:26)

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.