Skip to content

Commit

Permalink
1.1.26 - add specific fields fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ganoro committed Jun 25, 2019
1 parent 167b67c commit ea01290
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 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.25",
"version": "1.1.26",
"description": "PouchDB wrapped for TypeScript lovers",
"main": "./dist/src/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export abstract class Collection<T extends Entity> {
* Get entity by its id
* @param id
*/
public get(id) : Promise<T> {
public get(id, options?) : Promise<T> {
return new Promise<T>((resolved, rejected)=> {
this._collectionBase.getById(id).then((eb) => {
this._collectionBase.getById(id, options).then((eb) => {
return resolved(new this._ctor(eb));
}).catch((m) => {
return rejected(m)
Expand Down
23 changes: 16 additions & 7 deletions src/EntityCollectionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* constructing, destructing and querying
*/
import * as PouchDB from 'pouchdb';
import { concat, compact, map, startsWith, findIndex, values, keys, uniq, uniqBy, defaults, forIn, isUndefined, reduce } from 'lodash';
import { concat, compact, map, startsWith, values, keys, uniq, uniqBy, defaults, forIn, isUndefined, reduce } from 'lodash';

import { IdGenerator } from './IdGenerator';
import { DateIdGenerator } from './DateIdGenerator';
Expand Down Expand Up @@ -93,15 +93,24 @@ export class EntityCollectionBase {
/**
* Get entity by id
*/
getById(id) : Promise<EntityBase> {
getById(id, buckets?) : Promise<EntityBase> {
return new Promise<EntityBase>((resolved, rejected) => {
// generate id
let prefix = this.prefix + id + "/";
this._db.allDocs({
include_docs: true,
startkey: prefix,
endkey: prefix + "\uffff"
}).then((docs) => {
let allDocs;
if (buckets) {
allDocs = this._db.allDocs({
include_docs: true,
keys: map(buckets, b => (prefix + b))
})
} else {
allDocs = this._db.allDocs({
include_docs: true,
startkey: prefix,
endkey: prefix + "\uffff"
})
}
allDocs.then((docs) => {
let e = this._createEntityFromDocs(docs, prefix, id);
return resolved(e);
}).catch((m) => {
Expand Down
26 changes: 26 additions & 0 deletions tests/CollectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ import { Promise } from 'ts-promise';
}).catch(_.noop);
}

@test ("get by id after inserted doc should return entity with fields")
testGetEntityByIdAndFields(done: Function) {
let users = new Users(CollectionTest.db, User);
users.insert({ name: "New One", mobile : "6465490560", street : "Orchard St."}).then((p) => {
return users.get(p.id, ['']);
}).then((p) => {
if (!p || !p.name || p.street) { // no street
throw new Error("street should not be fetched");
}
done();
}).catch(_.noop);
}

@test ("get by id after inserted doc should return entity with multiple fields")
testGetEntityByIdAndMultipleFields(done: Function) {
let users = new Users(CollectionTest.db, User);
users.insert({ name: "New One", mobile : "6465490560", street : "Orchard St."}).then((p) => {
return users.get(p.id, ['', 'address']);
}).then((p) => {
if (!p || !p.name || !p.street) {
throw new Error("street should be fetched");
}
done();
}).catch(_.noop);
}

@test ("find by key value")
testFindByKeyVal(done: Function) {
let users = new Users(CollectionTest.db, User);
Expand Down

0 comments on commit ea01290

Please sign in to comment.