Skip to content

Commit

Permalink
1.1.24 - add desc
Browse files Browse the repository at this point in the history
  • Loading branch information
ganoro committed Nov 2, 2017
1 parent 0f18504 commit 8c66f25
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pouchable",
"version": "1.1.23",
"version": "1.1.24",
"description": "PouchDB wrapped for TypeScript lovers",
"main": "./dist/src/index.js",
"scripts": {
Expand Down
23 changes: 16 additions & 7 deletions src/EntityCollectionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ export class EntityCollectionBase {
let startsWith = opts && opts.startsWith
let gte = opts && opts.gte;
let search = this.prefix + key + '/' + value + (startsWith ? "" : "/");
this._db.allDocs(defaults(opts, {
this._db.allDocs(this._switchDescending(defaults(opts, {
include_docs: false,
startkey: search,
endkey: gte ? undefined : (search + "\uffff")
})).then((docs) => {
}))).then((docs) => {
// resolve all ids from the serach keys
let ids = uniq(map(docs.rows, (r: any) => {
let start = startsWith ? r.id.indexOf('/', search.length) + 1 : search.length;
Expand Down Expand Up @@ -150,11 +150,11 @@ export class EntityCollectionBase {
let gte = opts && opts.gte;
let pk = this.prefix + key + '/';
let search = pk + value + (startsWith ? "" : "/");
this._db.allDocs(defaults(opts, {
this._db.allDocs(this._switchDescending(defaults(opts, {
include_docs: false,
startkey: search,
endkey: gte ? undefined : (search + "\uffff")
})).then((docs) => {
}))).then((docs) => {
// resolve all ids from the serach keys
let ids = uniqBy(map(docs.rows, (r : any) => {
let ss = r.id.split('/');
Expand All @@ -173,12 +173,11 @@ export class EntityCollectionBase {
findByIds(from, to, opts?) : Promise<EntityBase[]> {
return new Promise<EntityBase[]>((resolved, rejected) => {
let search = this.prefix;
let def = {
this._db.allDocs(this._switchDescending(defaults(opts, {
include_docs: false,
startkey: search + from,
endkey: (search + to + "\uffff")
};
this._db.allDocs(defaults(opts, def)).then((docs) => {
}))).then((docs) => {
// resolve all ids from the serach keys
let ids = reduce(docs.rows, (result, r: any) => {
let start = search.length;
Expand Down Expand Up @@ -310,4 +309,14 @@ export class EntityCollectionBase {
});
}

_switchDescending(options) {
if (!options.descending) {
return options;
}
let start = options['startkey'];
options['startkey'] = options['endkey'];
options['endkey'] = start;
return options;
}

}
22 changes: 21 additions & 1 deletion tests/CollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,27 @@ import { Promise } from 'ts-promise';
}).catch((m) => {
console.log(m)
});
}
}

@test ("insert values and serach descending")
testSearchWithDesc(done: Function) {
let keyupdates = new KeyUpdates(CollectionTest.db, KeyUpdate);
let ps = [];
_.times(10, (i) => {
ps.push(keyupdates.insert({ my_number: i }));
})
Promise.all(ps).then((k) => {
return keyupdates.find('my_number', '', { startsWith : true, descending: true });
}).then((k) => {
if (k[0].my_number < k[1].my_number) {
console.log(k);
throw new Error("need to be descending");
}
done();
}).catch((m) => {
console.log(m)
});
}

@test ("insert values and serach for greater than")
testSearchForGreaterThan(done: Function) {
Expand Down

0 comments on commit 8c66f25

Please sign in to comment.