Skip to content

Commit

Permalink
support chaining of update
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Dec 29, 2011
1 parent 745e626 commit 528ea3a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
37 changes: 34 additions & 3 deletions README.md
Expand Up @@ -81,6 +81,9 @@ You can install using Node Package Manager (npm):
* [commit](#txCommit)
* [rollback](#txRollback)

## Results Set
* [getById](#resultSetGetById)

<a name="databaseJson"/>
# database.json

Expand Down Expand Up @@ -331,7 +334,8 @@ __Example__
<a name="modelUpdate" />
### Model.update(connection, id, params, callback)

Updates the model object specified with id to the database
Updates the model object specified with id to the database. This will only update the values
specified and will not retreive the item from the database first.

__Arguments__

Expand All @@ -343,7 +347,14 @@ __Arguments__
__Example__

Person.update(connection, 5, { name: 'Tom' }, function() {
// This will update the name of the person with id = 5.
// person with id = 5 updated with name 'Tom'.
});

// or chaining
connection.chain([
Person.update(5, { name: 'Tom' })
], function(err, results) {
// person with id = 5 updated with name 'Tom'.
});

<a name="modelDelete" />
Expand Down Expand Up @@ -391,7 +402,8 @@ __Example__
<a name="queryAll" />
### query.all([connection], callback)

Gets all items from a query as a single array of items.
Gets all items from a query as a single array of items. The array returned will have additional
methods see [here for documentation](#resultSetMethods).

__Arguments__

Expand Down Expand Up @@ -632,3 +644,22 @@ __Example__
});
});
});

<a name="resultSetMethods"/>
## Result Set

<a name="resultSetGetById"/>
### rs.getById(id)

Gets an item from the result set by id.

__Arguments__

* id - The id of the item to get.

__Example__

Person.all(connection, function(err, people) {
var person2 = people.getById(2);
});

18 changes: 18 additions & 0 deletions lib/connection.js
Expand Up @@ -140,6 +140,20 @@ var Connection = persistUtils.Class.extend({
});
},

_resultsArrayGetById: function(model, array, id) {
for(var i=0; i<array.length; i++) {
var itemId = array[i].getId();
if(itemId == id) {
return array[i];
}
}
return null;
},

_augmentResultsArrayWithHelpers: function(model, array) {
array.getById = this._resultsArrayGetById.bind(this, model, array);
},

all: function(sqlTree, callback) {
var self = this;
var sqlAndValues = this.driver.getSqlFromSqlTree(sqlTree);
Expand All @@ -152,6 +166,10 @@ var Connection = persistUtils.Class.extend({
objs[i]._connection = function() { return self; }; // hide from JSON.stringify
}

if(typeof(sqlTree.model) == "function") {
self._augmentResultsArrayWithHelpers(sqlTree.model(), objs)
}

callback(null, objs);
});
},
Expand Down
20 changes: 19 additions & 1 deletion lib/model.js
Expand Up @@ -339,11 +339,29 @@ function max(fieldName) {
return query.max.apply(query, arguments);
}

function update(connection, id, data, callback) {
// connection, id, data, callback
// id, data (chaining)
function update() {
// chaining
if(arguments.length == 2) {
var id = arguments[0];
var data = arguments[1];
var self = this;
return persistUtil.bind('update', function(conn, callback) {
self.update(conn, id, data, callback);
}, this);
}

// non-chaining
var connection = arguments[0];
var id = arguments[1];
var data = arguments[2];
var callback = arguments[3];
if(!connection) throw new Error("connection is null or undefined");
if(!connection.update) throw new Error("argument 1 to save does not appear to be a connection");

connection.updatePartial(this, id, data, callback);
return null;
}

exports.define = function(name, columnDefs) {
Expand Down
15 changes: 13 additions & 2 deletions tests/integration/chain.js
Expand Up @@ -44,6 +44,7 @@ exports['Chain'] = nodeunit.testCase({
"chain": function(test) {
var self = this;
var person3 = new self.Person({ name: "fred", age: 25 });
var phone1Id = self.phone1.id;

this.connection.chain([
person3.save,
Expand All @@ -54,6 +55,8 @@ exports['Chain'] = nodeunit.testCase({
self.Person.orderBy('name').all,
self.Phone.orderBy('number').first,
self.Phone.count,
self.Phone.update(phone1Id, { number: '555-5555' }),
self.Phone.all,
self.Phone.deleteAll,
self.Phone.all
], function(err, results) {
Expand Down Expand Up @@ -85,11 +88,19 @@ exports['Chain'] = nodeunit.testCase({
// phone select count
test.equal(results[7], 2);

// phone.deleteAll
// phone.update
test.ok(results[8]);

// phone all
test.equal(results[9].length, 2);
var updatedPhone1 = results[9].getById(phone1Id);
test.equal(updatedPhone1.number, '555-5555');

// phone.deleteAll
test.ok(results[10]);

// phone.all
test.equal(results[9].length, 0);
test.equal(results[11].length, 0);

test.done();
});
Expand Down

0 comments on commit 528ea3a

Please sign in to comment.