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

Commit 4a717db1 by Sascha Depold

minor bugfixing + some refactoring

1 parent ab9055fa
...@@ -48,28 +48,25 @@ var classMethods = { ...@@ -48,28 +48,25 @@ var classMethods = {
} }
} }
var instanceMethods = { Sequelize.prototype = {
asTableName: function(name) { get tableNames() {
return name + "s" var result = []
SequelizeHelper.Hash.keys(this.tables).forEach(function(tableName) {
result.push(SequelizeHelper.SQL.asTableName(tableName))
})
return result
}, },
define: function(name, attributes) { define: function(name, attributes) {
var table = new SequelizeTable(this, this.asTableName(name), attributes) var table = new SequelizeTable(this, SequelizeHelper.SQL.asTableName(name), attributes)
table.attributes = attributes table.attributes = attributes
this.tables[name] = {constructor: table, attributes: attributes} this.tables[name] = {constructor: table, attributes: attributes}
table.sequelize = this table.sequelize = this
return table return table
}, },
get tableNames() {
var result = []
var self = this
SequelizeHelper.Hash.keys(this.tables).forEach(function(tableName) {
result.push(self.asTableName(tableName))
})
return result
},
query: function(queryString, callback) { query: function(queryString, callback) {
var fields = [] var fields = []
var values = [] var values = []
...@@ -104,8 +101,4 @@ var instanceMethods = { ...@@ -104,8 +101,4 @@ var instanceMethods = {
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) { SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
Sequelize[methodName] = method Sequelize[methodName] = method
})
SequelizeHelper.Hash.forEach(instanceMethods, function(method, methodName) {
Sequelize.prototype[methodName] = method
}) })
\ No newline at end of file
...@@ -5,6 +5,10 @@ SequelizeHelper = { ...@@ -5,6 +5,10 @@ SequelizeHelper = {
}, },
SQL: { SQL: {
asTableName: function(name) {
return name + "s"
},
valuesForInsertQuery: function(object) { valuesForInsertQuery: function(object) {
var actualValues = object.values, var actualValues = object.values,
result = [] result = []
......
...@@ -63,19 +63,22 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -63,19 +63,22 @@ SequelizeTable = function(sequelize, tableName, attributes) {
} }
// instance methods // instance methods
var instanceMethods = { table.prototype = {
get values() { get values() {
var result = {} var result = {}
var self = this var self = this
SequelizeHelper.Hash.keys(attributes).forEach(function(attribute) { SequelizeHelper.Hash.keys(attributes).forEach(function(attribute) {
result[attribute] = self[attribute] result[attribute] = self[attribute]
}) })
return result return result
}, },
save: function(callback) { save: function(callback) {
var query = null var query = null
var self = this var self = this
if(this.id == null) if(this.id == null)
query = Sequelize.sqlQueryFor('insert', { query = Sequelize.sqlQueryFor('insert', {
table: this.tableName, fields: SequelizeHelper.SQL.fieldsForInsertQuery(this), values: SequelizeHelper.SQL.valuesForInsertQuery(this) table: this.tableName, fields: SequelizeHelper.SQL.fieldsForInsertQuery(this), values: SequelizeHelper.SQL.valuesForInsertQuery(this)
...@@ -86,7 +89,6 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -86,7 +89,6 @@ SequelizeTable = function(sequelize, tableName, attributes) {
sequelize.query(query, function() { sequelize.query(query, function() {
if(self.id == null) { if(self.id == null) {
table.find(self.values, function(result) { table.find(self.values, function(result) {
SequelizeHelper.log(result)
self.id = result.id self.id = result.id
if(callback) callback(self) if(callback) callback(self)
}) })
...@@ -116,10 +118,6 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -116,10 +118,6 @@ SequelizeTable = function(sequelize, tableName, attributes) {
SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) { SequelizeHelper.Hash.forEach(classMethods, function(method, methodName) {
table[methodName] = method table[methodName] = method
}) })
SequelizeHelper.Hash.forEach(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!