From 7f2f2c32b8d660c0da1018240f1d24fbab555965 Mon Sep 17 00:00:00 2001 From: Tim Jacobi Date: Sun, 26 Feb 2017 01:34:49 +0000 Subject: [PATCH] add more tests and update as needed --- couchdb-nano/couchdb-nano-tests.ts | 118 +++++++++++++++++++++++++++-- couchdb-nano/index.d.ts | 65 ++++++++-------- 2 files changed, 147 insertions(+), 36 deletions(-) diff --git a/couchdb-nano/couchdb-nano-tests.ts b/couchdb-nano/couchdb-nano-tests.ts index f61240b8e1..4780635d12 100644 --- a/couchdb-nano/couchdb-nano-tests.ts +++ b/couchdb-nano/couchdb-nano-tests.ts @@ -1,12 +1,120 @@ import * as nano from "couchdb-nano"; +import * as fs from 'fs'; +import * as path from 'path'; -const instance: nano.Client = nano('http://localhost:5984/emails'); - -const config: nano.Config = { +/* + * Instantiate with configuration object + */ +const config: nano.Configuration = { url: "http://localhost:5984/foo", requestDefaults: { "proxy": "http://someproxy" } }; -const cfgInstance: nano.Client = nano(config); +const cfgInstance = nano(config); -const db: nano.Database = instance.use("mydb"); \ No newline at end of file +/* + * Server Scope + */ +const instance: nano.ServerScope = nano('http://localhost:5984/emails'); + +instance.dinosaur('', function (err, response) { }); +const url = instance.config.url; + +instance.relax({ + method: 'POST', + path: '_users', + body: { + _id: 'org.couchdb.user:', + type: 'user', + name: 'ss', + roles: ['admin'], + password: 'rf' + } +}, function (err: any) { }); + +instance.session(function (error, session) { }); + +instance.request({ + db: 'shared_headers', + doc: 'new', + headers: {'If-None-Match': JSON.stringify({})} + }, function(error, helloWorld, rh) {}); + +instance.relax({ + db: 'document_list', + doc: '_all_docs', + method: 'GET', + qs: {limit: 1} + }, function(error, docs) {}); + +instance.auth("", "", function(err: any, _: any, headers: any) {}); + +instance.uuids(3, function (error, data) { }); + +/* + * Database Scope + */ +const db: nano.DatabaseScope = instance.db; + +db.create('az09_$()+-/', function (err) { }); +db.get('database_get', function(error, response) {}); +db.destroy('_users', function() {}); +db.list(function(error, list) {}); +db.replicate(instance.use("a"), instance.use("b"), function(error: any) {}); +db.replicate("a", "b", function(error: any) {}); + + +/* + * Document Scope + */ +const mydb: nano.DocumentScope = instance.use("mydb"); + +mydb.insert({ 'foo': 'baz' }, null, function (error, response) { }); +mydb.insert({ 'foo': 'baz' }, 'foobar', function (error, foo) { }); +mydb.get('foobaz', { 'revs_info': true }, function (error, foobaz) { }); +mydb.head('foobaz', function (error, body, headers) { }); +mydb.copy('foo_src', 'foo_dest', { overwrite: true }, function (error, response, headers) { }); +mydb.destroy(undefined, undefined, function (error, response) { }); +mydb.bulk( + { + 'docs': [ + { 'key': 'baz', 'name': 'bazzel' }, + { 'key': 'bar', 'name': 'barry' } + ] + }, + function (error: any, response: any) { } +); +mydb.list(function (error: any, docs: any) { }); +mydb.fetch({ keys: ['foobar'] }, function (error: any, docs: any) { }); +mydb.fetchRevs({ keys: ['foobar'] }, function (error: any, docs: any) { }); +mydb.changes({ since: 0 }, function (error, response) { }); +mydb.compact(function (error) { }); +const feed = mydb.follow({ since: '0' }); +feed.on('change', function (change: any) { }); +mydb.atomic('update', 'addbaz', 'baz', function (error: any, response: any) { }); +mydb.viewWithList('people', 'by_name_and_city', 'my_list', { + key: [ + 'Derek', + 'San Francisco' + ] +}, function (error, list) { } +); +mydb.view('alice', 'by_id', { + keys: ['foobar', 'barfoo'], + 'include_docs': true +}, function (err, view) { } +); +mydb.show('people', 'singleDoc', 'p_clemens', function(error: any, doc: any, rh: any) {}); +mydb.replicate('database_replica', function(error: any) {}); + +mydb.attachment.insert('new', 'att', 'Hello World!', 'text/plain', function (error: any, att: any) { }); +mydb.attachment.destroy('new', 'att', { rev: '123' }, function (error, response) { }); +mydb.attachment.get('new_string', 'att', function (error: any, helloWorld: any) { }); + +mydb.multipart.insert({'foo': 'baz'}, [{}], 'foobaz', function(error, foo) {}); +mydb.multipart.get('foobaz', function(error: any, foobaz: any, headers: any) {}); + +const rs = fs.createReadStream(""); +const is = mydb.attachment.insert('nodejs', 'logo.png', null, 'image/png'); +is.on('end', function () { }); +rs.pipe(is); \ No newline at end of file diff --git a/couchdb-nano/index.d.ts b/couchdb-nano/index.d.ts index fc895777f0..7030db9a54 100644 --- a/couchdb-nano/index.d.ts +++ b/couchdb-nano/index.d.ts @@ -16,6 +16,10 @@ declare namespace nano { parseUrl?: boolean } + interface Callback { + (error: any, result: any, headers?: any): void + } + interface ServerScope { readonly config: ServerConfig db: DatabaseScope @@ -28,7 +32,7 @@ declare namespace nano { session(callback?: Callback): Request updates(params?: UpdatesParams, callback?: Callback): Request followUpdates(params?: object, callback?: Callback): EventEmitter - uuids(num: number, callback: Function): Request + uuids(num: number, callback: Callback): Request } interface DatabaseScope { @@ -38,7 +42,7 @@ declare namespace nano { list(callback?: Callback): Request use(db: string): DocumentScope compact(name: string, designname?: string, callback?: Callback): Request - replicate(source: string, target: string, options?: object, callback?: Callback): Request + replicate(source: string | DocumentScope, target: string | DocumentScope, options?: object, callback?: Callback): Request changes(name: string, params?: object, callback?: Callback): Request follow(source: string, params?: FollowUpdatesParams, callback?: Callback): any followUpdates(params?: object, callback?: Callback): EventEmitter @@ -51,13 +55,13 @@ declare namespace nano { replicate(target: string, options?: object, callback?: Callback): Request compact(callback?: Callback): Request changes(params?: object, callback?: Callback): Request - follow(params?: FollowUpdatesParams, callback?: Callback): any + follow(params?: ScopedFollowUpdatesParams, callback?: Callback): any auth(username: string, userpass: string, callback?: Function): Request session(callback?: Callback): Request insert(document: object, params?: string | object, callback?: Callback): Request get(docname: string, params?: object, callback?: Callback): Request head(docname: string, callback: Callback): Request - copy(src_document: object, dst_document: object, options: object, callback?: Callback): Request + copy(src_document: string, dst_document: string, options: object, callback?: Callback): Request destroy(docname: string, rev: string, callback?: Callback): Request bulk(docs: BulkModifyDocsWrapper, params?: object, callback?: Callback): Request list(params?: object, callback?: Callback): Request @@ -92,21 +96,21 @@ declare namespace nano { } interface RequestFunction { - (options: RequestOptions, callback?: Callback): void + (options?: RequestOptions | string, callback?: Callback): void } interface RequestOptions { - db: string - method: string - path: string - doc: string - att: string - qs: any - content_type: string - headers: any - body: any - encoding: string - multipart: any[] + db?: string + method?: string + path?: string + doc?: string + att?: string + qs?: any + content_type?: string + headers?: any + body?: any + encoding?: string + multipart?: any[] } interface UpdatesParams { @@ -115,22 +119,9 @@ declare namespace nano { heartbeat: boolean } - interface Callback { - (error: any, result: any): void - } - - interface BulkModifyDocsWrapper { - docs: object[] - } - - interface BulkFetchDocsWrapper { - keys: string[] - } - - interface FollowUpdatesParams { - db: string + interface ScopedFollowUpdatesParams { inlucde_docs?: boolean - since?: number | "now" + since?: string heartbeat?: number feed?: "continuous" filter?: string | FollowUpdatesParamsFilterFunction @@ -142,9 +133,21 @@ declare namespace nano { response_grace_time?: number } + interface FollowUpdatesParams extends ScopedFollowUpdatesParams { + db: string + } + interface FollowUpdatesParamsFilterFunction { (doc: any, req: any): boolean } + + interface BulkModifyDocsWrapper { + docs: object[] + } + + interface BulkFetchDocsWrapper { + keys: string[] + } } export = nano