Permalink
Browse files

Added .limit(n) and .skip(n) support.

  • Loading branch information...
1 parent 1680b7f commit 033f67a70f913f67cd0ec80f0b3e8ab29d1135d3 Zef Hemel committed Feb 5, 2010
Showing with 52 additions and 7 deletions.
  1. +20 −7 README.md
  2. +32 −0 persistence.js
View
27 README.md
@@ -170,26 +170,32 @@ by [Google AppEngine's Query class](http://code.google.com/appengine/docs/python
A `QueryCollection` has the following methods:
* `filter(property, operator, value)`
- returns a new `QueryCollection` that adds a filter, filtering a
+ Returns a new `QueryCollection` that adds a filter, filtering a
certain property based on an operator and value. Supported operators
are '=', '!=', '<', '<=', '>' and '>='. Example: `.filter('done',
'=', true)`
* `order(property, ascending)`
- returns a new `QueryCollection` that will order its results by the
+ Returns a new `QueryCollection` that will order its results by the
property specified in either an ascending (ascending === true) or
descending (ascending === false) order.
+* `limit(n)`
+ Returns a new `QueryCollection` that limits the size of the result
+ set to `n` items. Useful for pagination.
+* `skip(n)`
+ Returns a new `QueryCollection` that skips the first `n` results.
+ Useful for pagination.
* `prefetch(rel)`
- returns a new `QueryCollection` that prefetches entities linked
+ Returns a new `QueryCollection` that prefetches entities linked
through relationship `rel`, note that this only works for one-to-one
and many-to-one relationships.
* `list(tx, callback)`
- asynchronously fetches the results matching the formulated query.
+ Asynchronously fetches the results matching the formulated query.
Once retrieved, the callback function is invoked with an array of
entity objects as argument.
* `add(obj)`
- adds object `obj` to the collection.
+ Adds object `obj` to the collection.
* `remove(obj)`
- removes object `obj` from the collection.
+ Removes object `obj` from the collection.
Query collections are returned by:
@@ -198,7 +204,7 @@ Query collections are returned by:
Example:
- var allTasks = Task.all().filter("done", '=', true).prefetch("category").order("name", false);
+ var allTasks = Task.all().filter("done", '=', true).prefetch("category").order("name", false).limit(10);
allTasks.list(null, function (results) {
results.forEach(function (r) {
@@ -224,6 +230,13 @@ Known issues:
characteristics. This can possibly be fixed by using e.g.
[WSPL](http://code.google.com/p/webstorageportabilitylayer/).
+Plans:
+
+* Implement non-persisted `QueryCollection`s, e.g. as a wrapper around
+ regular Javascript arrays to provide similar functionality to other
+ collections.
+* Synchronization with (views on) remote databases.
+
License
-------
View
32 persistence.js
@@ -720,6 +720,8 @@ var persistence = window.persistence || {};
this._additionalWhereSqls = [];
this._entityName = entityName;
this._constructor = constructor;
+ this._limit = -1;
+ this._skip = 0;
}
/**
@@ -731,6 +733,8 @@ var persistence = window.persistence || {};
c._filter = this._filter;
c._prefetchFields = this._prefetchFields.slice(0); // clone
c._orderColumns = this._orderColumns.slice(0);
+ c._limit = this._limit;
+ c._skip = this._skip;
return c;
};
@@ -761,6 +765,28 @@ var persistence = window.persistence || {};
return c;
};
+ /**
+ * Returns a new query collection will limit its size to n items
+ * @param n the number of items to limit it to
+ * @return the limited query collection
+ */
+ QueryCollection.prototype.limit = function(n) {
+ var c = this.clone();
+ c._limit = n;
+ return c;
+ };
+
+ /**
+ * Returns a new query collection which will skip the first n results
+ * @param n the number of results to skip
+ * @return the query collection that will skip n items
+ */
+ QueryCollection.prototype.skip = function(n) {
+ var c = this.clone();
+ c._skip = n;
+ return c;
+ };
+
/*
* Returns a new query collection which will prefetch a certain object relationship.
* Only works with 1:1 and N:1 relations.
@@ -875,6 +901,12 @@ var persistence = window.persistence || {};
+ (c[1] ? "ASC" : "DESC");
}).join(", ");
}
+ if(this._limit >= 0) {
+ sql += " LIMIT " + this._limit;
+ }
+ if(this._skip > 0) {
+ sql += " OFFSET " + this._skip;
+ }
persistence.flush(tx, function () {
tx.executeSql(sql, args, function (rows) {
var results = [];

0 comments on commit 033f67a

Please sign in to comment.