Skip to content

vanioinformatika/node-nano-async

Repository files navigation

Coverage Status Build Status

node-nano-async

A promisified wrapper around the nano npm module, to make it easily usable with ES2017 async-await.

The wrapper introduces new functions with 'Async' suffix to make using the original nano functions possible. It can be handy when you read/write attachments in streaming mode.

Usage

Javascript

const { default: nanoAsync } = require('nano-async')
// Create a server context
const serverScope = nanoAsync({ url: "http://localhost:5984" })
// Create a document context i.e. a "database" reference
const databaseAsync = serverScope.use("mydb")

// Read a document from the database
const [doc, headers] = await databaseAsync.getAsync(id, { attachments: true })

...

TypeScript

// Import nano and nano-async types
import { default as nanoAsync, ServerScopeAsync, DocumentGetResponse, ... } from "nano-async"
// Import custom document class
import { MyDocument } from "./MyDocument"
// Create a server context
const serverAsync = nanoAsync({ url: "http://localhost:5984" }) as ServerScopeAsync
// Create a document context i.e. a "database" reference
const databaseAsync: DocumentScopeAsync<MyDocument> = serverAsync.use<MyDocument>("mydb")

// Read a document from the database
const [doc, headers] = await databaseAsync.getAsync(id, { attachments: true })
// doc is of type: DocumentGetResponse & MyDocument

...