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

You need to sign in or sign up before continuing.
Commit 3f0f52d9 by Sascha Depold

add createdAt, updatedAt, deletedAt

1 parent 7720bc15
var Utils = require("./utils")
, Model = require("./model")
, QueryGenerator = require("./query-generator")
, DataTypes = require("./data-types")
var ModelDefinition = module.exports = function(name, attributes, options) {
var self = this
this.options = options || {}
this.options.timestamps = this.options.hasOwnProperty('timestamps') ? this.options.timestamps : true
this.name = name
this.tableName = name
this.attributes = Utils.simplifyAttributes(attributes)
this.modelManager = null // defined by model-manager during addModel
// additional attributes
this.attributes.id = 'INT NOT NULL auto_increment PRIMARY KEY'
this.addDefaultAttributes()
}
Utils.addEventEmitter(ModelDefinition)
ModelDefinition.prototype.addDefaultAttributes = function() {
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
, self = this
if(this.options.timestamps) {
defaultAttributes[this.options.camelcase ? 'created_at' : 'createdAt'] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[this.options.camelcase ? 'updated_at' : 'updatedAt'] = {type: DataTypes.DATE, allowNull: false}
if(this.options.safeDelete)
defaultAttributes[this.options.camelcase ? 'deleted_at' : 'deletedAt'] = {type: DataTypes.DATE}
}
defaultAttributes = Utils.simplifyAttributes(defaultAttributes)
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value })
}
ModelDefinition.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize
......@@ -64,7 +82,7 @@ ModelDefinition.prototype.find = function(options) {
}
ModelDefinition.prototype.build = function(values) {
var instance = new Model(values)
var instance = new Model(values, this.options)
, self = this
instance.definition = this
......
......@@ -2,17 +2,20 @@ var Utils = require("./utils")
, Mixin = require("./association-mixin")
, QueryGenerator = require("./query-generator")
var Model = module.exports = function(values) {
var Model = module.exports = function(values, options) {
var self = this
this.definition = null // will be set on Model.build or Model.create
this.attributes = []
this.options = options || {}
// add all passed values to the model and store the attribute names in this.attributes
Utils._.map(values, function(value, key) { self.addAttribute(key, value) })
// set id to null if not passed as value
// a newly created model has no id
if(!this.hasOwnProperty('id'))
this.addAttribute('id', null) // a newly created model has no id
this.addAttribute('id', null)
}
Utils.addEventEmitter(Model)
Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct })
......
......@@ -65,6 +65,7 @@ var Utils = module.exports = {
, replacements = { type: dataType.type }
if(dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) template += " NOT NULL"
if(dataType.autoIncrement) template +=" auto_increment"
if(dataType.defaultValue) {
template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = Utils.escape(dataType.defaultValue)
......
......@@ -12,25 +12,36 @@ module.exports = {
'it should handle extended attributes correctly - unique': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, unique: true}
})
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) UNIQUE",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - default': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, defaultValue: 'foo'}
})
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) DEFAULT 'foo'",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - null': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, allowNull: false}
})
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) NOT NULL",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - primary key': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, primaryKey: true}
})
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) PRIMARY KEY",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should add updatedAt and createdAt if timestamps is undefined or true': function() {
var User1 = sequelize.define('User' + parseInt(Math.random() * 999999999), {})
var User2 = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { timestamps: true })
assert.eql(User1.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
assert.eql(User2.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
},
'it should add deletedAt if safeDelete is true': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {}, { safeDelete: true })
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
}
}
\ 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!