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

Commit 17fd3447 by Sascha Depold

find will return a single record not an array

1 parent fa190e8a
......@@ -20,7 +20,7 @@ ModelDefinition.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize
args.push(this)
if(arguments.length == 1) args.push(this)
return s.query.apply(s, args)
}
......@@ -53,8 +53,14 @@ ModelDefinition.prototype.findAll = function(options) {
}
ModelDefinition.prototype.find = function(options) {
// options is not a hash but an id
if(typeof options == 'number')
options = {where: options}
options.limit = 1
return this.findAll(options)
var query = QueryGenerator.selectQuery(this.tableName, options)
return this.query(query, this, {plain: true})
}
ModelDefinition.prototype.build = function(values) {
......
......@@ -2,7 +2,7 @@ var Utils = require("./utils")
var Query = module.exports = function(databaseConfig, callee, options) {
this.config = databaseConfig
this.callee = callee
this.options = options
this.options = options || {}
}
Utils.addEventEmitter(Query)
......@@ -20,22 +20,34 @@ Query.prototype.run = function(query) {
client.connect()
client.query(query, function(err, results, fields) {
if(err) {
self.emit('failure', err, self.callee)
} else {
var result = self.callee
err ? self.onFailure(err) : self.onSuccess(query, results, fields)
})
client.end()
return this
}
Query.prototype.onSuccess = function(query, results, fields) {
var result = this.callee
, self = this
// add the inserted row id to the instance
if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee.id = results.insertId
if (self.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
self.callee.id = results.insertId
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if (query.indexOf('SELECT') == 0) {
// will transform result into models
result = results.map(function(result) { return self.callee.build(result) })
}
self.emit('success', result)
console.log(this.options)
if(this.options.plain)
result = (result.length == 0) ? null : result[0]
}
})
client.end()
return this
this.emit('success', result)
}
Query.prototype.onFailure = function(err) {
this.emit('failure', err, this.callee)
}
\ No newline at end of file
......@@ -26,8 +26,8 @@ var instanceMethods = {
return model
},
query: function(sql, callee) {
return new Query(this.config, callee).run(sql)
query: function(sql, callee, options) {
return new Query(this.config, callee, options).run(sql)
}
}
......
......@@ -11,8 +11,11 @@ User.sync({force: true}).on('success', function() {
User
.create({name: '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.'})
.on('success', function(user) {
User.all.on('success', function(users) {
console.log(users.map(function(u) { return u.values }))
// User.findAll().on('success', function(users) {
// console.log('Found the following users', users.map(function(u) { return u.name }))
// })
User.find(user.id).on('success', function(_user) {
console.log('Found the following user', _user)
})
})
})
\ 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!