Create a gist now

Instantly share code, notes, and snippets.

@omouse /package.json
Last active Feb 22, 2017

What would you like to do?
example of using sequelize with Q/promises
{
"name": "sequelize-example",
"version": "0.0.1",
"private": true,
"dependencies": {
"sqlite3": "*",
"sequelize": "*",
"q": "*"
}
}
// not using Q for promises (Promises support was added in mid-2013 to
// Sequelize, https://github.com/sequelize/sequelize/issues/288
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database_name', 'username', 'password', {
dialect: 'sqlite',
storage: ':memory:'
});
var User = sequelize.define('User', {
username: Sequelize.STRING,
password: Sequelize.STRING
});
sequelize.authenticate().then(function () {
return sequelize.sync();
}).then(function () {
console.log('====');
var user = User.build({
username: 'john-doe',
password: 'i-am-so-great'
});
return user.save();
}).done();
var Q = require('q');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database_name', 'username', 'password', {
dialect: 'sqlite',
storage: ':memory:'
});
var authenticate = function () {
var deferred = Q.defer();
sequelize.authenticate().complete(function(err) {
if (!!err) {
deferred.reject(new Error(err));
} else {
deferred.resolve();
}
});
return deferred.promise;
};
var sync = function () {
var deferred = Q.defer();
sequelize.sync({ force: true }).complete(function(err) {
if (!!err) {
deferred.reject(new Error(err));
} else {
deferred.resolve();
}
});
return deferred.promise;
};
var User = sequelize.define('User', {
username: Sequelize.STRING,
password: Sequelize.STRING
});
authenticate()
.then(sync)
.then(function () {
console.log('====');
var user = User.build({
username: 'john-doe',
password: 'i-am-so-great'
});
Q.nbind(user.save, user)();
})
.done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment