Skip to content

Commit

Permalink
Correct usage of metadata in stores
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Feb 17, 2010
1 parent 76fa67a commit 734cd56
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/store/memory.js
@@ -1,14 +1,26 @@
/**
* An in-memory store.
*/

var PreconditionFailed = require("../errors").PreconditionFailed;
var ReadonlyMemory = require("./readonly-memory").ReadonlyMemory;
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){
object.id = id = object.id || Math.round(Math.random()*10000000000000);
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)){
throw new PreconditionFailed(id + " does not exist to overwrite");
}
}
else{
if(id in this.index){
throw new PreconditionFailed(id + " exists, and can't be overwritten");
}
}
}
updateIndexes.call(this, id, object);
this.index[id] = object;
return id;
Expand Down
9 changes: 4 additions & 5 deletions lib/store/sql.js
Expand Up @@ -31,15 +31,14 @@ exports.SQLStore = function(config){
},
put: function(object, metadata){
id = metadata.id || object[config.idColumn];
if(id !== undefined){
if(!this.get(id)){
id = undefined;
}
var overwrite = metadata.overwrite;
if(overwrite === undefined){
overwrite = this.get(id);
}
var params = [];
var valuesPlacement = "";
var columnsString = "";
if(id === undefined){
if(!overwrite){
var first = true;
for(var i in object){
if(object.hasOwnProperty(i)){
Expand Down

0 comments on commit 734cd56

Please sign in to comment.