Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor second parameter to be key-value metadata options

  • Loading branch information...
commit ace03a35cb23f27940e5b6638d6285b6932d04c8 1 parent 339896a
Kris Zyp kriszyp authored

Showing 2 changed files with 31 additions and 29 deletions. Show diff stats Hide diff stats

  1. +29 27 lib/facet.js
  2. +2 2 lib/store/memory.js
56 lib/facet.js
@@ -7,7 +7,7 @@ var DatabaseError = require("./errors").DatabaseError,
7 7 AccessError = require("./errors").AccessError,
8 8 MethodNotAllowedError = require("./errors").MethodNotAllowedError,
9 9 defineProperties = require("es5-helper").defineProperties,
10   - extendSome = require("lazy").extendSome,
  10 + LazyArray = require("lazy-array").LazyArray,
11 11 when = require("promise").when;
12 12 rpcInvoke = require("./json-rpc").invoke;
13 13
@@ -58,11 +58,12 @@ var httpHandlerPrototype = {
58 58 var NEW = {};
59 59 function FacetedStore(store, facetClass){
60 60 function constructor(){
61   - return constructor.create.apply(constructor, arguments);
  61 + return constructor.put.apply(constructor, arguments);
62 62 }
63 63 for(var i in facetClass){
64 64 constructor[i] = facetClass[i];
65 65 }
  66 + var needsOldVersion = true; // TODO: compute based on schema (if there are any readonly or blocked properties)
66 67 constructor.id = store.id;
67 68 constructor.query= function(query, options){
68 69 if(typeof facetClass.query !== "function"){
@@ -125,27 +126,32 @@ function FacetedStore(store, facetClass){
125 126 }
126 127 return this.wrap(facetClass.get(id), this.transaction);
127 128 };
128   - constructor.create = function(instance){
129   - instance = this.wrap({}, this.transaction, instance, NEW);
  129 + function create(instance){
  130 + instance = constructor.wrap({}, constructor.transaction, instance, NEW);
130 131 if(typeof instance.initialize === "function"){
131 132 instance.initialize.apply(instance, arguments);
132 133 }
133   - if(this.transaction){
134   - (this.transaction.newInstances = this.transaction.newInstances || []).push(instance);
  134 + if(constructor.transaction){
  135 + (constructor.transaction.newInstances = constructor.transaction.newInstances || []).push(instance);
135 136 }
136 137 return when(instance.save(), function(){
137 138 return instance;
138 139 });
139 140 };
140   - constructor.put = function(props, id){
  141 + constructor.put = function(props, options){
  142 + options = options || {};
141 143 if(typeof props.save !== "function"){
142 144 try{
143   - var instance = this.get(id);
  145 + if(needsOldVersion){
  146 + var instance = this.get(options.id);
  147 + }
144 148 }
145 149 catch(e){
  150 + }
  151 + if(!instance){
146 152 // doesn't exist, create a new instance
147   - return when(constructor.create(props), function(newInstance){
148   - if(id && ((newInstance.getId ? newInstance.getId() : newInstance.id) != id)){
  153 + return when(create(props, options), function(newInstance){
  154 + if(options.id && ((newInstance.getId ? newInstance.getId() : newInstance.id) != options.id)){
149 155 throw new Error("Object's id does not match the target URI");
150 156 }
151 157 return newInstance;
@@ -172,7 +178,7 @@ function FacetedStore(store, facetClass){
172 178 }
173 179
174 180 }
175   - return when(instance.save.call(props, id), function(){
  181 + return when(instance.save.call(props, options), function(){
176 182 instance.load();
177 183 return instance;
178 184 });
@@ -186,20 +192,20 @@ function FacetedStore(store, facetClass){
186 192 }
187 193
188 194 };
189   - constructor.post = function(props, id){
190   - if(!id){
  195 + constructor.post = function(props, metadata){
  196 + if(!metadata.id){
191 197 // create a new object
192   - return this.create(props);
  198 + return this.put(props, {overwrite: false});
193 199 }
194 200 else{
195 201 // check to see if it is an RPC object
196 202 // TODO: Do this: if(props instanceof RPC){ // where the media handler creates RPC objects
197 203 if("method" in props && "id" in props && "params" in props){
198 204 // looks like JSON-RPC
199   - return rpcInvoke(this.get(id), props);
  205 + return rpcInvoke(this.get(metadata.id), props);
200 206 }
201 207 // doing an incremental update
202   - return this.copyProperties(props, id);
  208 + return this.copyProperties(props, metadata.id);
203 209 }
204 210 };
205 211
@@ -252,7 +258,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
252 258 }
253 259 if(source instanceof Array){
254 260 // this handles query results, but probably should create a branch for real arrays
255   - var results = extendSome({
  261 + var results = LazyArray({
256 262 some: function(callback){
257 263 source.some(function(item){
258 264 callback((item && typeof item == "object" && wrap(item, transaction, item, true)) || item);
@@ -336,7 +342,7 @@ var SchemaControlled = function(facetSchema, sourceClass){
336 342 enumerable: false
337 343 },
338 344 save: {
339   - value: function(id){
  345 + value: function(metadata){
340 346 if(facetPrototype.save){
341 347 facetPrototype.save.call(this, source);
342 348 }
@@ -357,19 +363,15 @@ var SchemaControlled = function(facetSchema, sourceClass){
357 363 var newIndex = transaction.newInstances && transaction.newInstances.indexOf(instance);
358 364 }
359 365 try{
360   - var method = newIndex > -1 ? "create" : "put";
361   - if(typeof facetSchema[method] === "function"){
362   - var id = facetSchema[method](source, id);
363   - }
364   - else if(typeof facetSchema.put === "function"){
365   - var id = facetSchema.put(source, id);
  366 + if(typeof facetSchema.put === "function"){
  367 + var id = facetSchema.put(source, metadata);
366 368 }
367 369 else{
368 370 if(facetSchema.__noSuchMethod__){
369   - var id = facetSchema.__noSuchMethod__(method, [source, id]);
  371 + var id = facetSchema.__noSuchMethod__("put", [source, metadata]);
370 372 }
371 373 else{
372   - throw new MethodNotAllowedError(method + " is not allowed");
  374 + throw new MethodNotAllowedError("put is not allowed");
373 375 }
374 376 }
375 377 var self = this;
@@ -546,7 +548,7 @@ function Facet(appliesTo, schema){
546 548 }
547 549 if(appliesTo == sourceStore){
548 550 facetedStore = function(){
549   - return facetedStore.create.apply(facetedStore, arguments);
  551 + return facetedStore.put.apply(facetedStore, arguments);
550 552 }
551 553 facetedStore.__proto__ = baseFacetedStore;
552 554 facetedStore.wrap = createWrap(facetedStore);
4 lib/store/memory.js
@@ -7,8 +7,8 @@ exports.Memory = function(options){
7 7 var store = ReadonlyMemory(options);
8 8 var uniqueKeys = {};
9 9 // start with the read-only memory store and add write support
10   - store.put = function(object, id){
11   - object.id = id = id || object.id || Math.round(Math.random()*10000000000000);
  10 + store.put = function(object, metadata){
  11 + object.id = id = object.id || Math.round(Math.random()*10000000000000);
12 12 updateIndexes.call(this, id, object);
13 13 this.index[id] = object;
14 14 return id;

0 comments on commit ace03a3

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