From 9a32cea0adaa190eadd242e2aaa8cb545a995748 Mon Sep 17 00:00:00 2001 From: Jeff Barnes Date: Tue, 27 Dec 2016 15:42:23 -0900 Subject: [PATCH] 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. --- pouchdb-core/index.d.ts | 19 +++-- pouchdb-core/pouchdb-core-tests.ts | 19 +++++ pouchdb-mapreduce/index.d.ts | 85 ++++++++++++++++++++ pouchdb-mapreduce/pouchdb-mapreduce-tests.ts | 26 ++++++ 4 files changed, 143 insertions(+), 6 deletions(-) diff --git a/pouchdb-core/index.d.ts b/pouchdb-core/index.d.ts index 0fd7eddb37..9797443f95 100644 --- a/pouchdb-core/index.d.ts +++ b/pouchdb-core/index.d.ts @@ -4,6 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// 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(name?: string, options?: Configuration.DatabaseConfiguration): Database; diff --git a/pouchdb-core/pouchdb-core-tests.ts b/pouchdb-core/pouchdb-core-tests.ts index 1901f38245..2f047d40d2 100644 --- a/pouchdb-core/pouchdb-core-tests.ts +++ b/pouchdb-core/pouchdb-core-tests.ts @@ -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 + }); + } } diff --git a/pouchdb-mapreduce/index.d.ts b/pouchdb-mapreduce/index.d.ts index 76acbab156..9d60fa40ef 100644 --- a/pouchdb-mapreduce/index.d.ts +++ b/pouchdb-mapreduce/index.d.ts @@ -6,6 +6,67 @@ /// 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 they’re out-of-date. + * 'update_after': Returns results immediately, but kicks off a build afterwards. + */ + stale?: 'ok' | 'update_after'; + } + + interface Response { + total_rows: number; + offset: number; + rows: { + id: any; + key: any; + value: any; + doc?: Core.ExistingDocument; + }[] + } + } + export interface Database { /** * Cleans up any stale map/reduce indexes. @@ -16,7 +77,31 @@ declare namespace PouchDB { * index files. */ viewCleanup(callback: PouchDB.Core.Callback): 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; + + /** + * 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) => 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) => 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>; } } diff --git a/pouchdb-mapreduce/pouchdb-mapreduce-tests.ts b/pouchdb-mapreduce/pouchdb-mapreduce-tests.ts index 50d7979fd6..a2cdd053c9 100644 --- a/pouchdb-mapreduce/pouchdb-mapreduce-tests.ts +++ b/pouchdb-mapreduce/pouchdb-mapreduce-tests.ts @@ -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 + }); + + } }