Skip to content

Commit

Permalink
Updates for removing create, and just using put
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Feb 25, 2010
1 parent 3c56be0 commit 96bcabe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
4 changes: 0 additions & 4 deletions lib/model.js
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions lib/store/memory.js
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions lib/store/sql.js
Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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 || {};
Expand Down

0 comments on commit 96bcabe

Please sign in to comment.