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

Commit 54e1d51d by Israel De La Hoz

add dinamic timestamp fields names to sequelize tables

1 parent a4c973f4
...@@ -88,6 +88,13 @@ Sequelize.prototype = { ...@@ -88,6 +88,13 @@ Sequelize.prototype = {
define: function(name, attributes, options) { define: function(name, attributes, options) {
var SequelizeTable = require(__dirname + "/SequelizeTable").SequelizeTable var SequelizeTable = require(__dirname + "/SequelizeTable").SequelizeTable
var _attributes = {} var _attributes = {}
var createdAt = "createdAt";
var updatedAt = "updatedAt";
if(options){
if(options.createdAt)createdAt = options.createdAt;
if(options.updatedAt)updatedAt = options.updatedAt;
}
Sequelize.Helper.Hash.forEach(attributes, function(value, key) { Sequelize.Helper.Hash.forEach(attributes, function(value, key) {
if(typeof value == 'string') if(typeof value == 'string')
...@@ -98,8 +105,8 @@ Sequelize.prototype = { ...@@ -98,8 +105,8 @@ Sequelize.prototype = {
throw new Error("Please specify a datatype either by using Sequelize.* or pass a hash!") throw new Error("Please specify a datatype either by using Sequelize.* or pass a hash!")
}) })
_attributes.createdAt = { type: Sequelize.DATE, allowNull: false} _attributes[createdAt] = { type: Sequelize.DATE, allowNull: false}
_attributes.updatedAt = { type: Sequelize.DATE, allowNull: false} _attributes[updatedAt] = { type: Sequelize.DATE, allowNull: false}
var table = new SequelizeTable(Sequelize, this, Sequelize.Helper.SQL.asTableName(name), _attributes, options) var table = new SequelizeTable(Sequelize, this, Sequelize.Helper.SQL.asTableName(name), _attributes, options)
......
...@@ -10,6 +10,13 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -10,6 +10,13 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
options.classMethods = options.classMethods || {} options.classMethods = options.classMethods || {}
options.instanceMethods = options.instanceMethods || {} options.instanceMethods = options.instanceMethods || {}
//for dinamic timestamp field names
var createdAt = "createdAt";
var updatedAt = "updatedAt";
if(options.createdAt)createdAt = options.createdAt;
if(options.updatedAt)updatedAt = options.updatedAt;
var table = function(values) { var table = function(values) {
var self = this, var self = this,
defaults = {} defaults = {}
...@@ -292,7 +299,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -292,7 +299,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
self = this self = this
Sequelize.Helper.Hash.forEach(table.attributes, function(options, attribute) { Sequelize.Helper.Hash.forEach(table.attributes, function(options, attribute) {
if(['createdAt', 'updatedAt'].indexOf(attribute) > -1) return if([createdAt, updatedAt].indexOf(attribute) > -1) return
var allowsNull = ((typeof options.allowNull == 'undefined') || (options.allowNull !== false)) var allowsNull = ((typeof options.allowNull == 'undefined') || (options.allowNull !== false))
var hasDefault = (typeof options.default != 'undefined') var hasDefault = (typeof options.default != 'undefined')
...@@ -356,9 +363,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -356,9 +363,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
throw new Error(errorText) throw new Error(errorText)
} }
this.updatedAt = new Date() this[updatedAt] = new Date()
if(this.isNewRecord) { if(this.isNewRecord) {
this.createdAt = new Date() this[createdAt] = new Date()
query = Sequelize.sqlQueryFor('insert', { query = Sequelize.sqlQueryFor('insert', {
table: table.tableName, table: table.tableName,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!