不要怂,就是干,撸起袖子干!

Commit a8f6348f by Sascha Depold

added possibility to class and instance methods

1 parent 4feed050
var Sequelize = require(__dirname + "/../../src/Sequelize").Sequelize,
sequelize = new Sequelize("sequelize_test", "test", "test", {disableLogging: true})
// model definition
var Task = sequelize.define("Task", {
name: Sequelize.STRING,
deadline: Sequelize.DATE,
importance: Sequelize.INTEGER
}, {
classMethods: {
setImportance: function(newImportance, callback) {
Task.findAll(function(allTasks) {
var queries = []
allTasks.forEach(function(task) {
queries.push({updateAttributes: task, params: [{ importance: newImportance }]})
})
Sequelize.chainQueries(queries, callback)
})
}
},
instanceMethods: {
hasDeadlinePassed: function() {
return (this.deadline < new Date())
}
}
})
// instance creation
var task1 = new Task({
name: 'Choose a nice MySQL connector',
deadline: new Date(Date.parse("Jul 8, 2100")),
importance: 10
})
var task2 = new Task({
name: 'Build the rest',
deadline: new Date(Date.parse("Jul 8, 2005")),
importance: 90
})
Task.drop(function() {
Task.sync(function() {
task1.save(function() {
task2.save(function() {
Sequelize.Helper.log("should be false: " + task1.hasDeadlinePassed())
Sequelize.Helper.log("should be true: " + task2.hasDeadlinePassed())
Sequelize.Helper.log("should be 10: " + task1.importance)
Task.setImportance(30, function() {
Task.findAll(function(tasks) {
tasks.forEach(function(task) {
Sequelize.Helper.log("should be 30: " + task.importance)
})
})
})
})
})
})
})
\ No newline at end of file
...@@ -115,13 +115,13 @@ Sequelize.prototype = { ...@@ -115,13 +115,13 @@ Sequelize.prototype = {
}) })
}, },
define: function(name, attributes) { define: function(name, attributes, options) {
var SequelizeTable = require(__dirname + "/SequelizeTable").SequelizeTable var SequelizeTable = require(__dirname + "/SequelizeTable").SequelizeTable
attributes.createdAt = 'DATETIME NOT NULL' attributes.createdAt = 'DATETIME NOT NULL'
attributes.updatedAt = 'DATETIME NOT NULL' attributes.updatedAt = 'DATETIME NOT NULL'
var table = new SequelizeTable(Sequelize, this, Sequelize.Helper.SQL.asTableName(name), attributes) var table = new SequelizeTable(Sequelize, this, Sequelize.Helper.SQL.asTableName(name), attributes, options)
table.attributes = attributes table.attributes = attributes
this.tables[name] = {klass: table, attributes: attributes} this.tables[name] = {klass: table, attributes: attributes}
......
...@@ -5,7 +5,11 @@ ...@@ -5,7 +5,11 @@
A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId A.hasMany(B) + B.hasMany(A) => AB.aId + AB.bId
*/ */
exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes) { exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, options) {
options = options || {}
options.classMethods = options.classMethods || {}
options.instanceMethods = options.instanceMethods || {}
var table = function(values) { var table = function(values) {
var self = this var self = this
Sequelize.Helper.Hash.forEach(values, function(value, key) { Sequelize.Helper.Hash.forEach(values, function(value, key) {
...@@ -352,9 +356,16 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes) { ...@@ -352,9 +356,16 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes) {
} }
} }
Sequelize.Helper.Hash.forEach(classMethods, function(method, methodName) { // merge classMethods + passed classMethods
Sequelize.Helper.Hash.merge(options.classMethods, classMethods)
Sequelize.Helper.Hash.forEach(classMethods, function(method, methodName) {
table[methodName] = method table[methodName] = method
}) })
// merge passed instanceMethods
Sequelize.Helper.Hash.forEach(options.instanceMethods, function(method, methodName) {
table.prototype[methodName] = method
})
return table return table
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!