Skip to content
Browse files

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

  • Loading branch information...
2 parents 70a37d4 + 560bc79 commit 979dd8ae6433474f83880730ab67e2c8a087c6b5 @smith committed
View
1 engines/node/lib/store/sql-engine.js
@@ -5,7 +5,6 @@
*/
-var extendSome = require("lazy").extendSome;
exports.SQLDatabase = function(parameters){
var connectionProvider;
if(parameters.type == "postgres"){
View
6 engines/rhino/lib/store/full-text.js
@@ -1,4 +1,4 @@
-var extendSome = require("lazy").extendSome;
+var LazyArray = require("lazy-array").LazyArray;
var FullText = exports.FullText = function(store, name){
searcher = new org.persvr.store.LuceneSearch("lucene/" + name);
var defaultPut = store.put;
@@ -9,12 +9,12 @@ var FullText = exports.FullText = function(store, name){
return id;
};
store.fulltext = function(query, field, options){
- var idResults = extendSome(searcher.query(query, field, options.start || 0, options.end || 100000000, null));
+ var idResults = LazyArray(searcher.query(query, field, options.start || 0, options.end || 100000000, null));
return {
query: "?id.in(" + idResults.join(",") + ")",
totalCount: idResults.totalCount
};
- /*return extendSome({
+ /*return LazyArray({
some: function(callback){
idResults.some(function(id){
try{
View
4 engines/rhino/lib/store/sql-engine.js
@@ -4,7 +4,7 @@
*/
-var extendSome = require("lazy").extendSome;
+var LazyArray = require("lazy-array").LazyArray;
var drivers = {
mysql: "com.mysql.jdbc.Driver",
sqlite: "org.sqlite.JDBC",
@@ -23,7 +23,7 @@ exports.SQLDatabase = function(parameters){
executeSql: function(query, parameters){
// should roughly follow executeSql in http://www.w3.org/TR/webdatabase/
var rawResults = adapter.executeSql(query, parameters);
- var results = {rows:extendSome(rawResults)};
+ var results = {rows:LazyArray(rawResults)};
if(rawResults.insertId){
results.insertId = rawResults.insertId;
}
View
56 lib/facet.js
@@ -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);
View
10 lib/json-rpc.js
@@ -14,7 +14,7 @@
* });
*
*/
-var addListener = require("listen").addListener,
+var observe = require("observe").observe,
when = require("promise").when;
var invoke = exports.invoke = function(target, rpc){
@@ -59,9 +59,9 @@ var invoke = exports.invoke = function(target, rpc){
exports.server = function(rpcObject){
if (global.onmessage) // dedicated worker
- addListener(global, "onmessage", handleMessage);
+ observe(global, "onmessage", handleMessage);
else // shared worker
- addListener(global, "onconnect", function (e) { e.port.onmessage = handleMessage; });
+ observe(global, "onconnect", function (e) { e.port.onmessage = handleMessage; });
function handleMessage(event){
var data = event.data;
@@ -77,7 +77,7 @@ exports.client = function(worker){
worker = worker.port;
}
var requestsWaiting = {};
- addListener(worker, "onmessage", function(event){
+ observe(worker, "onmessage", function(event){
var data = event.data;
if(requestsWaiting[data.id]){
if(data.error === null){
@@ -105,5 +105,5 @@ exports.client = function(worker){
};
};
-var Response = require("./jsgi/response").Response;
+var Response = require("jsgi/response").Response;
View
2 lib/resource-query.js
@@ -181,7 +181,7 @@ exports.executeQuery = function(query, options, target){
var results = eval(js + "target;");
if(options.start || options.end){
var totalCount = results.length;
- results = results.slice(options.start || 0, (options.end || Infinity));
+ results = results.slice(options.start || 0, (options.end || Infinity) + 1);
results.totalCount = totalCount;
}
return results;
View
5 lib/settings.js
@@ -1,5 +0,0 @@
-
-var localSettings = require("packages").root;
-for(var i in localSettings){
- exports[i] = localSettings[i];
-}
View
4 lib/store/memory.js
@@ -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;
View
7 lib/store/readonly-memory.js
@@ -2,7 +2,7 @@
* An readonly in-memory store.
*/
var executeQuery = require("../resource-query").executeQuery;
-var extendSome = require("lazy").extendSome;
+var LazyArray = require("lazy-array").LazyArray;
function MemoryObject(){}
MemoryObject.prototype = {
getId: function(object){
@@ -33,7 +33,7 @@ exports.ReadonlyMemory = function(options){
}
var result = executeQuery(query, options, all);
// make a copy
- return extendSome({
+ return LazyArray({
some: function(callback){
result.some(function(item){
var object = {};
@@ -45,7 +45,8 @@ exports.ReadonlyMemory = function(options){
return callback(object);
});
},
- length: result.length
+ length: result.length,
+ totalCount: result.totalCount
});
}
};
View
6 lib/store/sql.js
@@ -5,7 +5,7 @@
* based on http://www.w3.org/TR/webdatabase/
*/
var SQLDatabase = require("./sql-engine").SQLDatabase;
-var first = require("lazy").first;
+var first = require("lazy-array").first;
var AutoTransaction = require("../stores").AutoTransaction;
var parseQuery = require("../resource-query").parseQuery;
var defineProperty = require("es5-helper").defineProperty;
@@ -29,8 +29,8 @@ exports.SQLStore = function(config){
"delete": function(id){
store.executeSql("DELETE FROM " + config.table + " WHERE " + config.idColumn + "=?", [id]);
},
- put: function(object, id){
- id = id || object[config.idColumn];
+ put: function(object, metadata){
+ id = metadata.id || object[config.idColumn];
if(id !== undefined){
if(!this.get(id)){
id = undefined;

0 comments on commit 979dd8a

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