Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save skynode/7fa26a75befab45cb02a3c610b93621f to your computer and use it in GitHub Desktop.
Save skynode/7fa26a75befab45cb02a3c610b93621f to your computer and use it in GitHub Desktop.
Sequelize
var Sequelize = require('sequelize')
var db = new Sequelize('mydb', null, null, {
dialect: 'postgres'
})
var Post = db.define('post', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: {
type: Sequelize.STRING
},
content: {
type: Sequelize.TEXT
},
visible: {
type: Sequelize.BOOLEAN,
defaultValue: false
}
})
Post.sync().then(function() {
var data = {
title: 'Hello Sequelize!',
content: 'Fill this in later'
}
Post.create(data).then(function(post) {
console.dir(post.get())
})
})
var Sequelize = require('sequelize')
var db = new Sequelize('blog', null, null, {
dialect: 'postgres'
})
var Author = db.define('author', {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
email: Sequelize.STRING
}, {
classMethods: {
buildFromArgs: function(first, last, email) {
return this.build({
firstName: first,
lastName: last,
email: email
})
}
},
instanceMethods: {
uppercaseAndSave: function(field) {
this.set(field, this.get(field).toUpperCase())
return this.save()
}
}
})
Author.sync({ force: true }).then(function() {
var mike = Author.buildFromArgs('Mike', 'Frey', 'foo@bar.com')
log(mike)
mike.uppercaseAndSave('lastName').then(log)
})
function log(val) {
val = val.get ? val.get() : val
console.log('\n\033[32m', val, '\033[0m\n')
return val
}
var Post = require('./post')
var log = function(inst) {
console.dir(inst.get())
}
Post.findAll({
attributes: ['id', 'title'],
where: { visible: true },
order: '"createdAt" DESC'
})
.then(function(posts) {
posts.forEach(log)
})
var Sequelize = require('sequelize')
var db = new Sequelize('blog', null, null, {
dialect: 'postgres'
})
var Author = db.define('author', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true,
contains: {
args: '@bar.com',
msg: 'Must use bar.com email.'
}
}
},
birthMonth: {
type: Sequelize.INTEGER,
validate: {
max: 12,
min: 1
}
},
birthDay: {
type: Sequelize.INTEGER,
validate: {
max: 31,
min: 1,
maxDay: function(value) {
if (!this.birthMonth) return
var counts = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (value > counts[this.birthMonth-1])
throw new Error('birthDay out of range.')
}
}
}
}, {
validate: {
notAnonymous: function() {
if (this.firstName == null
&& this.lastName == null
&& this.email == null) {
throw new Error('Author cannot be anonymous.')
}
}
}
})
Author.sync({ force: true }).then(function() {
Author.create({
firstName: 'Mike',
lastName: 'Frey',
email: 'foo@bar.com',
birthMonth: 4,
birthDay: 30
})
.then(function(author) {
logSuccess('Record created successfully!')
})
.catch(function(err) {
logError(err.message)
})
.finally(process.exit)
})
function logSuccess(msg) {
console.log('\n\033[32m', msg, '\033[0m\n')
}
function logError(msg) {
console.log('\n\033[31m', msg, '\033[0m\n')
}
var Sequelize = require('sequelize')
var db = new Sequelize('mydb2', null, null, {
dialect: 'postgres'
})
var BlogPost = db.define('blogPost', {
title: {
type: Sequelize.STRING,
allowNull: false
},
content: { type: Sequelize.TEXT },
publishedAt: {
type: Sequelize.DATE,
field: 'published_at'
},
visible: {
type: Sequelize.BOOLEAN,
defaultValue: false
}
}, {
createdAt: 'created_at',
updatedAt: 'updated_at',
// timestamps:false
// underscoredAll: true
})
BlogPost.sync({ force: true }).then(function() {
BlogPost.create({
title: 'some title',
content: 'some content',
publishedAt: Date.now()
}).then(function(post) {
console.dir(post.get())
})
})
var Sequelize = require('sequelize')
var db = new Sequelize('blog', null, null, {
dialect: 'postgres'
})
var Person = db.define('person', {
firstName: {
type: Sequelize.STRING,
// set: function(first) {
// this.setDataValue('firstName', first)
// var last = this.getDataValue('lastName')
// this.setDataValue('fullName', first + ' ' + last)
// }
},
lastName: {
type: Sequelize.STRING
},
// fullName: {
// type: Sequelize.VIRTUAL,
// get: function() {
// return this.getDataValue('firstName') + ' ' +
// this.getDataValue('lastName')
// },
// set: function(value) {
// this.setDataValue('fullName', value)
// var names = value.split(' ')
// this.setDataValue('firstName', names[0])
// this.setDataValue('lastName', names[1])
// },
// validate: {
// is: /^\w+\s\w+$/
// }
// }
}, {
getterMethods: {
fullName: function() {
return this.getDataValue('firstName') + ' ' +
this.getDataValue('lastName')
}
},
setterMethods: {
fullName: function(value) {
this.setDataValue('fullName', value)
var names = value.split(' ')
this.setDataValue('firstName', names[0])
this.setDataValue('lastName', names[1])
}
}
})
Person.sync({ force: true }).then(function() {
var person = Person.build({
firstName: 'Mike',
lastName: 'Frey',
fullName: 'Mike Frey'
})
log(person)
person.set('fullName', 'Shawn Martin')
log(person)
person.save().then(process.exit)
})
function log(val) {
val = val.get ? val.get() : val
console.log('\n\033[32m', val, '\033[0m\n')
return val
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment