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

Commit 10111c99 by Sascha Depold

changes on data handling

1 parent 0152354c
module.exports = require("./lib/sequelize/Sequelize") module.exports = require("./lib/sequelize/sequelize")
\ No newline at end of file \ No newline at end of file
var Utils = require("./utils") var Utils = require("./utils")
, Model = require("./model") , ModelDefinition = require("./model-definition")
var Sequelize = module.exports = function(database, username, password, options) { var Sequelize = module.exports = function(database, username, password, options) {
options = options || {} options = options || {}
...@@ -20,7 +20,7 @@ var instanceMethods = { ...@@ -20,7 +20,7 @@ var instanceMethods = {
define: function(modelName, attributes, options) { define: function(modelName, attributes, options) {
options = options || {} options = options || {}
var model = this.modelManager.addModel(new Model(modelName, attributes, options)) var model = this.modelManager.addModel(new ModelDefinition(modelName, attributes, options))
return model return model
}, },
......
var Utils = require("./utils")
, Model = require("./model")
, QueryGenerator = require("./query-generator")
var ModelDefinition = module.exports = function(name, attributes, options) {
var self = this
this.options = options || {}
this.name = name
this.tableName = name
this.attributes = attributes
this.modelManager = null // defined by model-manager during addModel
}
ModelDefinition.prototype.sync = function() {
console.log(QueryGenerator.createTableQuery(this.tableName, this.attributes))
}
ModelDefinition.prototype.drop = function() {
console.log(QueryGenerator.dropTableQuery(this.tableName))
}
ModelDefinition.prototype.create = function(values) {
var instance = new Model(values)
return instance
}
\ No newline at end of file
var Utils = require("./utils") var Utils = require("./utils")
, Mixin = require("./association-mixin") , Mixin = require("./association-mixin")
, QueryGenerator = require("./query-generator")
/*
Defines the connection between data and the sql table. var Model = module.exports = function(values) {
Parameters: var self = this
- name: the name of the model
- attributes: A name-datatype-hash -> {name: 'VARCHAR(255)', id: "INT"}
Options:
-
*/
var Model = module.exports = function(name, attributes, options) {
this.name = name
this.attributes = attributes
this.options = options || {}
this.modelManager = null // defined by model-manager during addModel this.id = null // a newly created model has no id
this.associations = []
Utils._.map(values, function(value, key) {
self[key] = value
})
} }
require("sys").inherits(Model, require('events').EventEmitter);
Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct })
var classMethods = { Model.Events = {
sync: function() { insert: 'InsertQuery',
update: 'UpdateQuery',
}, destroy: 'DestroyQuery'
drop: function() {
}
} }
var instanceMethods = { var instanceMethods = {
save: function() { save: function() {
this.isNewRecord ? this.emit(Model.Events.insert, this) : this.emit(Model.Events.update, this)
}, },
destroy: function() { destroy: function() {
this.emit(Model.Events.destroy, this)
} }
} }
/* Add the class and instance methods to Model */ Model.prototype.__defineGetter__('isNewRecord', function() {
Utils._.map(classMethods, function(fct, name) { Model[name] = fct }) return this.id == null
})
/* Add the instance methods to Model */
Utils._.map(instanceMethods, function(fct, name) { Model.prototype[name] = fct}) Utils._.map(instanceMethods, function(fct, name) { Model.prototype[name] = fct})
Utils._.map(Mixin.classMethods, function(fct, name) { Model[name] = fct })
Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct}) Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct})
\ No newline at end of file
var config = require("./config")
, Sequelize = require("./../index")
, sequelize = new Sequelize(config.database, config.username, config.password)
var User = sequelize.define('User', {
title: Sequelize.STRING,
bio: Sequelize.TEXT
})
console.log(User)
User.sync()
User.drop()
var user = User.create({title: 'barfooz', bio: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'})
console.log(user)
console.log(user.save())
\ 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!