Permalink
Browse files

dont store guid if present on objects already

  • Loading branch information...
1 parent 348345e commit aa98e1aa59854a2a3e3b11e832e052fe551452d0 @mrjjwright committed Apr 15, 2010
Showing with 44 additions and 20 deletions.
  1. +18 −7 nosqlite.coffee
  2. +26 −13 nosqlite.js
View
@@ -37,8 +37,8 @@ class NoSQLite
#until we can get a truly async interface to sqlite
#process.nextTick ->
#callback(null, this)
- @db.open db_file, ->
- callback()
+ @db.open db_file, (err) ->
+ callback(err)
# Finds an object or objects in the SQLite by running a query
# derived from the supplied predicate on the supplied table.
@@ -150,9 +150,11 @@ class NoSQLite
#augment object with guid unless options say not to
if @options.no_guid is false
if not _.isArray(obj)
- obj.guid: Math.uuidFast()
+ if not obj.guid?
+ obj.guid: Math.uuidFast()
else for o in obj
- o.guid: Math.uuidFast()
+ if not o.guid
+ o.guid: Math.uuidFast()
tx_flag: false
callback: in_transaction
@@ -418,10 +420,19 @@ class NoSQLite
)
when "find"
predicate: JSON.parse(body)
- self.find(table, predicate, (err, result) ->
- if result?
+ if predicate.records?
+ # The client is sending some records to save along with asking for new records
+ # This is for convenience for clients that want to do a simple sync in one http call
+ records_to_save: predicate.records
+ predicate: predicate.predicate
+ self.save table, records_to_save, (err, result) ->
+ if err? then return self.write_res(response, err)
+ self.find table, predicate, (err, result) ->
+ self.write_res(response, err, result)
+ else
+ self.find table, predicate, (err, result) ->
self.write_res(response, err, result)
- )
+
when "find_or_save"
args: JSON.parse(body)
self.find_or_save(table, args[0], args[1], (err, result) ->
View
@@ -34,8 +34,8 @@
//until we can get a truly async interface to sqlite
//process.nextTick ->
//callback(null, this)
- this.db.open(db_file, function() {
- return callback();
+ this.db.open(db_file, function(err) {
+ return callback(err);
});
return this;
};
@@ -155,16 +155,16 @@
// You can pass in an array of objects as well. Each row will be inserted
// As always, we'll call you back when everything is ready!
NoSQLite.prototype.save = function save(table, obj, in_transaction, the_callback) {
- var _a, _b, _c, callback, db, o, objs, self, statement, tx_flag;
+ var _a, _b, _c, _d, callback, db, o, objs, self, statement, tx_flag;
//augment object with guid unless options say not to
if (this.options.no_guid === false) {
if (!_.isArray(obj)) {
- obj.guid = Math.uuidFast();
+ !(typeof (_a = obj.guid) !== "undefined" && _a !== null) ? (obj.guid = Math.uuidFast()) : null;
} else {
- _b = obj;
- for (_a = 0, _c = _b.length; _a < _c; _a++) {
- o = _b[_a];
- o.guid = Math.uuidFast();
+ _c = obj;
+ for (_b = 0, _d = _c.length; _b < _d; _b++) {
+ o = _c[_b];
+ !o.guid ? (o.guid = Math.uuidFast()) : null;
}
}
}
@@ -520,19 +520,32 @@
return body += data;
});
return request.addListener("end", function() {
- var _c, args, obj, predicate;
+ var _c, _d, args, obj, predicate, records_to_save;
if ((_c = url.query.method) === "save") {
obj = JSON.parse(body);
return self.save(table, obj, function(err, result) {
return self.write_res(response, err, result);
});
} else if (_c === "find") {
predicate = JSON.parse(body);
- return self.find(table, predicate, function(err, result) {
- if ((typeof result !== "undefined" && result !== null)) {
+ if ((typeof (_d = predicate.records) !== "undefined" && _d !== null)) {
+ // The client is sending some records to save along with asking for new records
+ // This is for convenience for clients that want to do a simple sync in one http call
+ records_to_save = predicate.records;
+ predicate = predicate.predicate;
+ return self.save(table, records_to_save, function(err, result) {
+ if ((typeof err !== "undefined" && err !== null)) {
+ return self.write_res(response, err);
+ }
+ return self.find(table, predicate, function(err, result) {
+ return self.write_res(response, err, result);
+ });
+ });
+ } else {
+ return self.find(table, predicate, function(err, result) {
return self.write_res(response, err, result);
- }
- });
+ });
+ }
} else if (_c === "find_or_save") {
args = JSON.parse(body);
return self.find_or_save(table, args[0], args[1], function(err, result) {

0 comments on commit aa98e1a

Please sign in to comment.