Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor second parameter to be key-value metadata options

  • Loading branch information...
commit ace03a35cb23f27940e5b6638d6285b6932d04c8 1 parent 339896a
Kris Zyp kriszyp authored
Showing with 31 additions and 29 deletions.
  1. +29 −27 lib/facet.js
  2. +2 −2 lib/store/memory.js
56 lib/facet.js
View
@@ -7,7 +7,7 @@ var DatabaseError = require("./errors").DatabaseError,
AccessError = require("./errors").AccessError,
MethodNotAllowedError = require("./errors").MethodNotAllowedError,
defineProperties = require("es5-helper").defineProperties,
- extendSome = require("lazy").extendSome,
+ LazyArray = require("lazy-array").LazyArray,
when = require("promise").when;
rpcInvoke = require("./json-rpc").invoke;
@@ -58,11 +58,12 @@ var httpHandlerPrototype = {
var NEW = {};
function FacetedStore(store, facetClass){
function constructor(){
- return constructor.create.apply(constructor, arguments);
+ return constructor.put.apply(constructor, arguments);
}
for(var i in facetClass){
constructor[i] = facetClass[i];
}
+ var needsOldVersion = true; // TODO: compute based on schema (if there are any readonly or blocked properties)
constructor.id = store.id;
constructor.query= function(query, options){
if(typeof facetClass.query !== "function"){
@@ -125,27 +126,32 @@ function FacetedStore(store, facetClass){
}
return this.wrap(facetClass.get(id), this.transaction);
};
- constructor.create = function(instance){
- instance = this.wrap({}, this.transaction, instance, NEW);
+ function create(instance){
+ instance = constructor.wrap({}, constructor.transaction, instance, NEW);
if(typeof instance.initialize === "function"){
instance.initialize.apply(instance, arguments);
}
- if(this.transaction){
- (this.transaction.newInstances = this.transaction.newInstances || []).push(instance);
+ if(constructor.transaction){
+ (constructor.transaction.newInstances = constructor.transaction.newInstances || []).push(instance);
}
return when(instance.save(), function(){
return instance;
});
};
- constructor.put = function(props, id){
+ constructor.put = function(props, options){
+ options = options || {};
if(typeof props.save !== "function"){
try{
- var instance = this.get(id);
+ if(needsOldVersion){
+ var instance = this.get(options.id);
+ }
}
catch(e){
+ }
+ if(!instance){
// doesn't exist, create a new instance
- return when(constructor.create(props), function(newInstance){
- if(id && ((newInstance.getId ? newInstance.getId() : newInstance.id) != id)){
+ return when(create(props, options), function(newInstance){
+ if(options.id && ((newInstance.getId ? newInstance.getId() : newInstance.id) != options.id)){
throw new Error("Object's id does not match the target URI");
}
return newInstance;
@@ -172,7 +178,7 @@ function FacetedStore(store, facetClass){
}
}
- return when(instance.save.call(props, id), function(){
+ return when(instance.save.call(props, options), function(){
instance.load();
return instance;
});
@@ -186,20 +192,20 @@ function FacetedStore(store, facetClass){
}
};
- constructor.post = function(props, id){
- if(!id){
+ constructor.post = function(props, metadata){
+ if(!metadata.id){
// create a new object
- return this.create(props);
+ return this.put(props, {overwrite: false});
}
else{
// check to see if it is an RPC object
// TODO: Do this: if(props instanceof RPC){ // where the media handler creates RPC objects
if("method" in props && "id" in props && "params" in props){
// looks like JSON-RPC
- return rpcInvoke(this.get(id), props);
+ return rpcInvoke(this.get(metadata.id), props);
}
// doing an incremental update
- return this.copyProperties(props, id);
+ return this.copyProperties(props, metadata.id);
}
};
@@ -252,7 +258,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
}
if(source instanceof Array){
// this handles query results, but probably should create a branch for real arrays
- var results = extendSome({
+ var results = LazyArray({
some: function(callback){
source.some(function(item){
callback((item && typeof item == "object" && wrap(item, transaction, item, true)) || item);
@@ -336,7 +342,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
enumerable: false
},
save: {
- value: function(id){
+ value: function(metadata){
if(facetPrototype.save){
facetPrototype.save.call(this, source);
}
@@ -357,19 +363,15 @@ var SchemaControlled = function(facetSchema, sourceClass){
var newIndex = transaction.newInstances && transaction.newInstances.indexOf(instance);
}
try{
- var method = newIndex > -1 ? "create" : "put";
- if(typeof facetSchema[method] === "function"){
- var id = facetSchema[method](source, id);
- }
- else if(typeof facetSchema.put === "function"){
- var id = facetSchema.put(source, id);
+ if(typeof facetSchema.put === "function"){
+ var id = facetSchema.put(source, metadata);
}
else{
if(facetSchema.__noSuchMethod__){
- var id = facetSchema.__noSuchMethod__(method, [source, id]);
+ var id = facetSchema.__noSuchMethod__("put", [source, metadata]);
}
else{
- throw new MethodNotAllowedError(method + " is not allowed");
+ throw new MethodNotAllowedError("put is not allowed");
}
}
var self = this;
@@ -546,7 +548,7 @@ function Facet(appliesTo, schema){
}
if(appliesTo == sourceStore){
facetedStore = function(){
- return facetedStore.create.apply(facetedStore, arguments);
+ return facetedStore.put.apply(facetedStore, arguments);
}
facetedStore.__proto__ = baseFacetedStore;
facetedStore.wrap = createWrap(facetedStore);
4 lib/store/memory.js
View
@@ -7,8 +7,8 @@ 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, id){
- object.id = id = id || object.id || Math.round(Math.random()*10000000000000);
+ store.put = function(object, metadata){
+ object.id = id = object.id || Math.round(Math.random()*10000000000000);
updateIndexes.call(this, id, object);
this.index[id] = object;
return id;
Please sign in to comment.
Something went wrong with that request. Please try again.