Created
September 22, 2013 21:40
-
-
Save alejomongua/6664124 to your computer and use it in GitHub Desktop.
Test saving a model with sequelize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test", | |
"version": "0.0.1", | |
"private": true, | |
"scripts": { | |
"start": "node test.js" | |
}, | |
"dependencies": { | |
"sequielize":"https://dl.dropboxusercontent.com/u/18947902/sequelize-github-master-201309221617.tar.gz", | |
"pg": "~0.10.2", | |
"async": "*" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create database sequelize_test; | |
create user sequelize_test with password 'foobar'; | |
grant all on database sequelize_test to sequelize_test; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sequelize = require("sequelize"); | |
var async = require("async"); | |
var sequelize = new Sequelize('sequelize_test', 'sequelize_test', 'foobar', { | |
dialect: 'postgres', | |
omitNull: true | |
}); | |
var User = sequelize.define('User', { | |
name: { | |
type: Sequelize.STRING, | |
allowNull: false, | |
validate: { | |
notEmpty: true, | |
len: [6, 50], | |
} | |
}, | |
username: { | |
type: Sequelize.STRING, | |
unique: true, | |
validate: { | |
isAlphanumeric: true, | |
} | |
}, | |
gender: Sequelize.ENUM('Male', 'Female'), | |
email: { | |
type: Sequelize.STRING, | |
unique: true, | |
allowNull: false, | |
validate: { | |
isEmail: true, | |
notEmpty: true | |
} | |
} | |
}); | |
async.series([ | |
function(cb){ | |
User.sync().success(function(){ | |
cb(); | |
}).error(cb); | |
}, | |
function(cb){ | |
var user = User.build({name: 'User Test', username: 'usertest', email: '[email protected]'}); | |
user.save().success(function(u){ | |
console.log(u); | |
cb(); | |
}).error(cb); | |
} | |
],function(e){ | |
if (e){ | |
console.log(e); | |
console.log('there was an error'); | |
process.exit(code=-1); | |
} else { | |
console.log('completed successfully'); | |
process.exit(code=0); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment