Skip to content
Browse files

Correct usage of metadata in stores

  • Loading branch information...
1 parent 76fa67a commit 734cd56f9d0b6ff045ed52b1d5373ffb3e39bec2 @kriszyp kriszyp committed Feb 17, 2010
Showing with 18 additions and 7 deletions.
  1. +14 −2 lib/store/memory.js
  2. +4 −5 lib/store/sql.js
View
16 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;
View
9 lib/store/sql.js
@@ -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)){

0 comments on commit 734cd56

Please sign in to comment.
Something went wrong with that request. Please try again.