Permalink
Browse files

Updates for removing create, and just using put

  • Loading branch information...
1 parent 3c56be0 commit 96bcabeaf5b3cad4c21c3f41a914772529b27123 @kriszyp kriszyp committed Feb 25, 2010
Showing with 13 additions and 16 deletions.
  1. +0 −4 lib/model.js
  2. +9 −7 lib/store/memory.js
  3. +4 −5 lib/store/sql.js
View
4 lib/model.js
@@ -20,10 +20,6 @@ exports.Model = function(name, store, schema){
store = JSFile((require("settings").dataFolder || "data") + "/" + name);
}
store.id = name;
- if(!store.create){
- // map create to put in case it only implements the WebSimpleDB API
- store.create = store.put;
- }
schema.id = name;
schemas[name] = schema;
View
16 lib/store/memory.js
@@ -7,23 +7,25 @@ exports.Memory = function(options){
var store = ReadonlyMemory(options);
var uniqueKeys = {};
// start with the read-only memory store and add write support
- store.put = function(object, metadata){
- var id = object.id = metadata.id || object.id || Math.round(Math.random()*10000000000000);
- if("overwrite" in metadata){
- if(metadata.overwrite){
- if(!(id in this.index)){
+ store.put = function(object, directives){
+ directives = directives || {};
+ var id = object.id = directives.id || object.id || Math.round(Math.random()*10000000000000);
+ var isNew = !(id in this.index);
+ if("overwrite" in directives){
+ if(directives.overwrite){
+ if(isNew){
throw new PreconditionFailed(id + " does not exist to overwrite");
}
}
else{
- if(id in this.index){
+ if(!isNew){
throw new PreconditionFailed(id + " exists, and can't be overwritten");
}
}
}
updateIndexes.call(this, id, object);
this.index[id] = object;
- return id;
+ return isNew && id;
};
store["delete"] = function(id){
updateIndexes.call(this, id);
View
9 lib/store/sql.js
@@ -29,9 +29,9 @@ exports.SQLStore = function(config){
"delete": function(id){
store.executeSql("DELETE FROM " + config.table + " WHERE " + config.idColumn + "=?", [id]);
},
- put: function(object, metadata){
- id = metadata.id || object[config.idColumn];
- var overwrite = metadata.overwrite;
+ put: function(object, directives){
+ id = directives.id || object[config.idColumn];
+ var overwrite = directives.overwrite;
if(overwrite === undefined){
overwrite = this.get(id);
}
@@ -48,6 +48,7 @@ exports.SQLStore = function(config){
first = false;
}
}
+ params.idColumn = config.idColumn;
var results = store.executeSql("INSERT INTO " + config.table + " (" + columnsString + ") values (" + valuesPlacement + ")", params);
id = results.insertId;
object[config.idColumn] = id;
@@ -70,8 +71,6 @@ exports.SQLStore = function(config){
sql += " WHERE " + config.idColumn + "=?";
params.push(object[config.idColumn]);
store.executeSql(sql, params);
-
- return id;
},
query: function(query, options){
options = options || {};

0 comments on commit 96bcabe

Please sign in to comment.