Pouchdb core fixes and mapreduce additions (#13472)

* pouchdb-core fixes.

* pouchdb-core fixes.

* pouchdb-mapreduce additions.

* add semicolons.

* Mapreduce fixes from code review.

* Add third parameter to Pouchdb.adapter()

* Make Pouchdb.adapter() third parameter optional.

* Duplicate docs for overloaded functions.

* Remove PouchDB.adapter(...) definition.
This commit is contained in:
Jeff Barnes
2016-12-27 15:42:23 -09:00
committed by Andy
parent aded248021
commit 9a32cea0ad
4 changed files with 143 additions and 6 deletions

View File

@@ -4,6 +4,7 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
/// <reference types="debug" />
declare namespace PouchDB {
namespace Core {
@@ -399,22 +400,26 @@ declare namespace PouchDB {
headers?: {
[name: string]: string;
}
username?: string;
password?: string;
/**
* Enables transferring cookies and HTTP Authorization information.
*
* Defaults to true.
*/
withCredentials?: boolean;
/**
* Disables automatic creation of databases.
*/
skip_setup?: boolean;
}
interface RemoteDatabaseConfiguration extends CommonDatabaseConfiguration {
ajax?: RemoteRequesterConfiguration;
auth?: {
username?: string;
password?: string;
}
/**
* Disables automatic creation of databases.
*/
skip_setup?: boolean;
}
type DatabaseConfiguration = LocalDatabaseConfiguration |
@@ -434,6 +439,8 @@ declare namespace PouchDB {
on(event: 'created', listener: (dbName: string) => any): this;
on(event: 'destroyed', listener: (dbName: string) => any): this;
debug: debug.IDebug;
new<Content extends Core.Encodable>(name?: string,
options?: Configuration.DatabaseConfiguration): Database<Content>;

View File

@@ -99,6 +99,8 @@ namespace PouchDBCoreTests {
});
db.info({ ajax: { cache: true }}, (error, result) => {
});
PouchDB.debug.enable('*');
}
function testRemove() {
@@ -178,4 +180,21 @@ namespace PouchDBCoreTests {
db.changes({ limit: 50 }).then(() => {});
}
function testRemoteOptions() {
let db = new PouchDB('http://example.com/dbname', {
ajax: {
cache: false,
timeout: 10000,
headers: {
'X-Some-Special-Header': 'foo'
},
},
auth: {
username: 'mysecretusername',
password: 'mysecretpassword'
},
skip_setup: true
});
}
}

View File

@@ -6,6 +6,67 @@
/// <reference types="pouchdb-core" />
declare namespace PouchDB {
interface Filter {
map: (doc: any) => void;
reduce?: (key: string, value: any) => any;
}
namespace Query {
interface Options {
/** Reduce function, or the string name of a built-in function: '_sum', '_count', or '_stats'. */
reduce?: ((...args: any[]) => void) | '_sum' | '_count' | '_stats' | boolean;
/** Include the document in each row in the doc field. */
include_docs?: boolean;
/** Include conflicts in the _conflicts field of a doc. */
conflicts?: boolean;
/** Include attachment data. */
attachments?: boolean;
/** Return attachment data as Blobs/Buffers, instead of as base64-encoded strings. */
binary?: boolean;
/** Get rows with keys in a certain range (inclusive/inclusive). */
startkey?: any;
/** Get rows with keys in a certain range (inclusive/inclusive). */
endkey?: any;
/** Include rows having a key equal to the given options.endkey. */
inclusive_end?: boolean;
/** Maximum number of rows to return. */
limit?: number;
/** Number of rows to skip before returning (warning: poor performance on IndexedDB/LevelDB!). */
skip?: number;
/** Reverse the order of the output rows. */
descending?: boolean;
/** Only return rows matching this key. */
key?: any;
/** Array of keys to fetch in a single shot. */
keys?: any[];
/** True if you want the reduce function to group results by keys, rather than returning a single result. */
group?: boolean;
/**
* Number of elements in a key to group by, assuming the keys are arrays.
* Defaults to the full length of the array.
*/
group_level?: number;
/**
* unspecified (default): Returns the latest results, waiting for the view to build if necessary.
* 'ok': Returns results immediately, even if theyre out-of-date.
* 'update_after': Returns results immediately, but kicks off a build afterwards.
*/
stale?: 'ok' | 'update_after';
}
interface Response<Content extends Core.Encodable> {
total_rows: number;
offset: number;
rows: {
id: any;
key: any;
value: any;
doc?: Core.ExistingDocument<Content & Core.AllDocsMeta>;
}[]
}
}
export interface Database<Content extends Core.Encodable> {
/**
* Cleans up any stale map/reduce indexes.
@@ -16,7 +77,31 @@ declare namespace PouchDB {
* index files.
*/
viewCleanup(callback: PouchDB.Core.Callback<any,void>): void;
/**
* Cleans up any stale map/reduce indexes.
*
* As design docs are deleted or modified, their associated index
* files(in CouchDB) or companion databases (in local PouchDBs) continue
* to take up space on disk. viewCleanup() removes these unnecessary
* index files.
*/
viewCleanup(): Promise<void>;
/**
* Invoke a map/reduce function, which allows you to perform more complex queries
* on PouchDB than what you get with allDocs().
*/
query(fun: string | Filter | Function, opts: Query.Options, callback: (err: Core.Error, res: Query.Response<Content>) => void): void;
/**
* Invoke a map/reduce function, which allows you to perform more complex queries
* on PouchDB than what you get with allDocs().
*/
query(fun: string | Filter | Function, callback: (err: Core.Error, res: Query.Response<Content>) => void): void;
/**
* Invoke a map/reduce function, which allows you to perform more complex queries
* on PouchDB than what you get with allDocs().
*/
query(fun: string | Filter | Function, opts?: Query.Options): Promise<Query.Response<Content>>;
}
}

View File

@@ -7,4 +7,30 @@ namespace PouchDBBrowserTests {
db.viewCleanup().catch((error) => {
});
}
function testQuery() {
let pouch = new PouchDB<{}>('mydb');
// find pokemon with name === 'Pika pi!'
pouch.query('my_index/by_name', {
key : 'Pika pi!',
include_docs : true
}).then(function (result) {
// handle result
}).catch(function (err) {
// handle errors
});
// find the first 5 pokemon whose name starts with 'P'
pouch.query('my_index/by_name', {
startkey : 'P',
endkey : 'P\uffff',
limit : 5,
include_docs : true
}).then(function (result) {
// handle result
}).catch(function (err) {
// handle errors
});
}
}