Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of git://github.com/kriszyp/perstore

  • Loading branch information...
commit 1392275f894a810102e25eceeae8f1695aa5bbe7 2 parents 979dd8a + 734cd56
@smith authored
View
8 lib/errors.js
@@ -23,7 +23,13 @@ var DatabaseError = exports.DatabaseError = function(code,message){
DatabaseError.prototype = new Error();
var NotFoundError = exports.NotFoundError = function(message){
- DatabaseError.call(this, 3, message);
+ DatabaseError.call(this, 2, message);
this.name = "NotFoundError";
};
NotFoundError.prototype = new DatabaseError();
+
+var PreconditionFailed = exports.PreconditionFailed = function(message){
+ DatabaseError.call(this, 3, message);
+ this.name = "PreconditionFailed";
+};
+PreconditionFailed.prototype = new DatabaseError();
View
19 lib/facet.js
@@ -3,7 +3,7 @@
* views or forms of accessing the underlying data stores. Different facets can be used
* for different application access points, different security levels, and different locales.
*/
-var DatabaseError = require("./errors").DatabaseError,
+var NotFoundError = require("./errors").NotFoundError,
AccessError = require("./errors").AccessError,
MethodNotAllowedError = require("./errors").MethodNotAllowedError,
defineProperties = require("es5-helper").defineProperties,
@@ -40,17 +40,17 @@ var httpHandlerPrototype = {
throw new Error("wrap must be implemented in FacetedStore implementations");
},
patch: function(props, id){
- return this.copyProperties(props,id);
+ return this.copyProperties(props,{id:id});
},
- copyProperties: function(props, id){
- var target = this.get(id);
+ copyProperties: function(props, options){
+ var target = this.get(options.id);
for(var i in props){
if(props.hasOwnProperty(i) && (target[i] !== props[i])){
target[i] = props[i];
}
}
- target.save();
+ target.save(options);
return target;
}
@@ -134,7 +134,7 @@ function FacetedStore(store, facetClass){
if(constructor.transaction){
(constructor.transaction.newInstances = constructor.transaction.newInstances || []).push(instance);
}
- return when(instance.save(), function(){
+ return when(instance.save({overwrite:false}), function(){
return instance;
});
};
@@ -185,7 +185,7 @@ function FacetedStore(store, facetClass){
});
}
else{
- return when(props.save(), function(){
+ return when(props.save(options), function(){
props.load();
return props;
});
@@ -205,7 +205,7 @@ function FacetedStore(store, facetClass){
return rpcInvoke(this.get(metadata.id), props);
}
// doing an incremental update
- return this.copyProperties(props, metadata.id);
+ return this.copyProperties(props, metadata);
}
};
@@ -254,7 +254,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
return function wrap(source, transaction, wrapped, partial){
return when(source, function(source){
if(!source){
- throw new DatabaseError(3, "not found");
+ throw new NotFoundError("not found");
}
if(source instanceof Array){
// this handles query results, but probably should create a branch for real arrays
@@ -343,6 +343,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
},
save: {
value: function(metadata){
+ metadata = metadata || {};
if(facetPrototype.save){
facetPrototype.save.call(this, source);
}
View
29 lib/resource-query.js
@@ -91,17 +91,28 @@ exports.QueryFunctions = function(){
}
exports.QueryFunctions.prototype = {
- sort: function(sortAttribute){
- var firstChar = sortAttribute.charAt(0);
- var ascending = true;
- if(firstChar == "-" || firstChar == "+"){
- if(firstChar == "-"){
- ascending = false;
- }
- sortAttribute = sortAttribute.substring(1);
+ sort: function(){
+ var terms = [];
+ for(var i = 0; i < arguments.length; i++){
+ var sortAttribute = arguments[i];
+ var firstChar = sortAttribute.charAt(0);
+ var term = {attribute: sortAttribute, ascending: true};
+ if (firstChar == "-" || firstChar == "+") {
+ if(firstChar == "-"){
+ term.ascending = false;
+ }
+ term.attribute = term.attribute.substring(1);
+ }
+ terms.push(term);
}
this.sort(function(a, b){
- return ascending == a[sortAttribute] > b[sortAttribute] ? 1 : -1;
+ for (var i = 0; i < terms.length; i++) {
+ var term = terms[i];
+ if (a[term.attribute] != b[term.attribute]) {
+ return term.ascending == a[term.attribute] > b[term.attribute] ? 1 : -1;
+ }
+ }
+ return true; //undefined?
});
return this;
},
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)){
Please sign in to comment.
Something went wrong with that request. Please try again.