Class

NanoAdapter

NanoAdapter(scope, aliasopt)

Constructor

# new NanoAdapter(scope, aliasopt)

Provides a standardized interface for performing CRUD operations on Nano databases, extending the CouchDB adapter with Nano-specific functionality. This adapter handles document creation, reading, updating, and deletion, as well as bulk operations and index management.

Adapter for interacting with Nano databases

classDiagram class CouchDBAdapter { +flags() +Dispatch() +index() +create() +read() +update() +delete() } class NanoAdapter { +flags() +Dispatch() +index() +create() +createAll() +read() +readAll() +update() +updateAll() +delete() +deleteAll() +raw() +static connect() +static createDatabase() +static deleteDatabase() +static createUser() +static deleteUser() +static decoration() } CouchDBAdapter <|-- NanoAdapter
Parameters:
Name Type Attributes Description
scope DocumentScope.<any>

The Nano document scope to use for database operations

alias string <optional>

Optional alias for the adapter

View Source adapter.ts, line 46

Example
```typescript
// Connect to a Nano database
const server = NanoAdapter.connect('admin', 'password', 'localhost:5984');
const db = server.db.use('my_database');

// Create an adapter instance
const adapter = new NanoAdapter(db);

// Use the adapter for database operations
const document = await adapter.read('users', '123');
```

Methods

# protected Dispatch() → {NanoDispatch}

Returns a dispatcher for handling Nano-specific operations

Creates a new NanoDispatch instance

View Source adapter.ts, line 759

A new NanoDispatch instance

NanoDispatch

# async create(tableName, id, model) → {Promise.<Record.<string, any>>}

Inserts a new document into the Nano database with the provided data

Creates a new document in the database

sequenceDiagram participant A as NanoAdapter participant DB as Nano Database A->>DB: insert(model) alt Success DB-->>A: response with ok=true A->>A: assignMetadata(model, response.rev) A-->>A: return document with metadata else Error DB-->>A: error A-->>A: throw parseError(e) else Not OK DB-->>A: response with ok=false A-->>A: throw InternalError end
Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document identifier

model Record.<string, any>

The document data to insert

View Source adapter.ts, line 812

A promise that resolves to the created document with metadata

Promise.<Record.<string, any>>

# async createAll(tableName, ids, models)

Inserts multiple documents into the Nano database in a single bulk operation

Creates multiple documents in the database

sequenceDiagram participant A as NanoAdapter participant DB as Nano Database A->>DB: bulk({docs: models}) alt Success DB-->>A: response array A->>A: Check if all responses have no errors alt All OK A->>A: assignMultipleMetadata(models, revs) A-->>A: return documents with metadata else Some errors A->>A: Collect error messages A-->>A: throw InternalError with collected messages end else Error DB-->>A: error A-->>A: throw parseError(e) end
Parameters:
Name Type Description
tableName string

The name of the table/collection

ids Array.<string> | Array.<number>

Array of document identifiers

models

Array of document data to insert

View Source adapter.ts, line 842

A promise that resolves to an array of created documents with metadata

# async delete(tableName, id) → {Promise.<Record.<string, any>>}

Removes a single document from the Nano database by its ID and returns the deleted document metadata

Deletes a document from the database

Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document identifier

View Source adapter.ts, line 942

A promise that resolves to the deleted document with metadata

Promise.<Record.<string, any>>

# async deleteAll(tableName, ids) → {Promise.<Array.<Record.<string, any>>>}

Performs a bulk delete operation for the provided IDs and returns the deleted documents metadata

Deletes multiple documents from the database

Parameters:
Name Type Description
tableName string

The name of the table/collection

ids Array.<(string|number|bigint)>

Array of document identifiers to delete

View Source adapter.ts, line 952

A promise resolving to the deleted documents with metadata

Promise.<Array.<Record.<string, any>>>

# async protected flags(operation, model, flags) → {Promise.<NanoFlags>}

Creates a set of flags for a specific operation, including user information

Generates flags for database operations

Parameters:
Name Type Description
operation OperationKeys

The operation being performed (create, read, update, delete)

model Constructor.<M>

The model constructor

flags Partial.<NanoFlags>

Partial flags to be merged

View Source adapter.ts, line 751

Complete flags for the operation

Promise.<NanoFlags>

# protected getClient() → {DocumentScope.<any>}

Uses the adapter configuration to establish a connection and wrap a database scope with credentials

Lazily creates and returns the Nano DocumentScope client

View Source adapter.ts, line 738

The ready-to-use Nano DocumentScope for the configured database

DocumentScope.<any>

# async protected index(models) → {Promise.<void>}

Generates and creates indexes in the Nano database based on the provided models

Creates database indexes for models

sequenceDiagram participant A as NanoAdapter participant G as generateIndexes participant DB as Nano Database A->>G: generateIndexes(models) G-->>A: indexes loop For each index A->>DB: createIndex(index) DB-->>A: response Note over A: Check if index already exists alt Index exists A-->>A: throw ConflictError end end
Parameters:
Name Type Description
models

Model constructors to create indexes for

View Source adapter.ts, line 785

A promise that resolves when all indexes are created

Promise.<void>

# async raw(rawInput, docsOnlyopt) → {Promise.<R>}

Runs a Mango query using Nano's find API and optionally returns only the documents array

Executes a raw Mango query against the database

Parameters:
Name Type Attributes Default Description
rawInput MangoQuery

The Mango query to execute

docsOnly boolean <optional>
true

Whether to return only the docs array or the full response

View Source adapter.ts, line 963

A promise that resolves to the query result, shaped according to docsOnly

Promise.<R>

# async read(tableName, id) → {Promise.<Record.<string, any>>}

Fetches a single document from the Nano database by its ID

Retrieves a document from the database

sequenceDiagram participant A as NanoAdapter participant DB as Nano Database A->>A: generateId(tableName, id) A->>DB: get(_id) alt Success DB-->>A: record A->>A: assignMetadata(record, record._rev) A-->>A: return document with metadata else Error DB-->>A: error A-->>A: throw parseError(e) end
Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document identifier

View Source adapter.ts, line 866

A promise that resolves to the retrieved document with metadata

Promise.<Record.<string, any>>

# async readAll(tableName, ids)

Fetches multiple documents from the Nano database by their IDs in a single operation

Retrieves multiple documents from the database

sequenceDiagram participant A as NanoAdapter participant DB as Nano Database A->>A: Map ids to generateId(tableName, id) A->>DB: fetch({keys: mappedIds}, {}) DB-->>A: results A->>A: Process each result row loop For each row alt Row has error A-->>A: throw InternalError else Row has document A->>A: assignMetadata(doc, doc._rev) else No document A-->>A: throw InternalError end end A-->>A: return documents with metadata
Parameters:
Name Type Description
tableName string

The name of the table/collection

ids Array.<(string|number|bigint)>

Array of document identifiers

View Source adapter.ts, line 894

A promise that resolves to an array of retrieved documents with metadata

# async shutdown() → {Promise.<void>}

Cleans up internal resources and clears the cached Nano client instance

Shuts down the adapter instance

View Source adapter.ts, line 730

A promise that resolves when shutdown completes

Promise.<void>

# async update(tableName, id, model) → {Promise.<Record.<string, any>>}

Updates an existing document in the Nano database with the provided data

Updates a document in the database

sequenceDiagram participant A as NanoAdapter participant DB as Nano Database A->>DB: insert(model) alt Success DB-->>A: response with ok=true A->>A: assignMetadata(model, response.rev) A-->>A: return document with metadata else Error DB-->>A: error A-->>A: throw parseError(e) else Not OK DB-->>A: response with ok=false A-->>A: throw InternalError end
Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document identifier

model Record.<string, any>

The updated document data

View Source adapter.ts, line 921

A promise that resolves to the updated document with metadata

Promise.<Record.<string, any>>

# async updateAll(tableName, ids, models) → {Promise.<Promise.<Array.<Record.<string, any>>>>}

Performs a bulk update operation on the Nano database for the provided documents

Updates multiple documents in the database

Parameters:
Name Type Description
tableName string

The name of the table/collection

ids Array.<(string|number)>

Array of document identifiers

models Promise.<Array.<Record.<string, any>>>

Array of updated document data

View Source adapter.ts, line 932

A promise that resolves to the updated documents with metadata

Promise.<Promise.<Array.<Record.<string, any>>>>

# static connect(user, pass, hostopt, protocolopt) → {ServerScope}

Creates and returns a Nano ServerScope using the given credentials, host, and protocol

Establishes a connection to a Nano (CouchDB) server

Parameters:
Name Type Attributes Default Description
user string

Username used for authentication

pass string

Password used for authentication

host string <optional>
"localhost:5984"

Host and port of the CouchDB server

protocol "http" | "https" <optional>
"http"

Protocol to use for the connection

View Source adapter.ts, line 974

The Nano ServerScope connection

ServerScope

# async static createDatabase(con, name) → {Promise.<void>}

Creates a new database with the specified name on the connected Nano server

Creates a new database on the Nano server

sequenceDiagram participant A as NanoAdapter participant DB as Nano Server A->>DB: db.create(name) alt Success DB-->>A: result with ok=true else Error DB-->>A: error A-->>A: throw parseError(e) else Not OK DB-->>A: result with ok=false A-->>A: throw parseError(error, reason) end
Parameters:
Name Type Description
con ServerScope

The Nano server connection

name string

The name of the database to create

View Source adapter.ts, line 998

A promise that resolves when the database is created

Promise.<void>

# async static createUser(con, dbName, user, pass, rolesopt) → {Promise.<void>}

Creates a new user in the Nano server and configures security to grant the user access to a specific database

Creates a new user and grants access to a database

sequenceDiagram participant A as NanoAdapter participant U as _users Database participant S as Security API A->>A: Create user object A->>U: insert(user) alt Success U-->>A: response with ok=true A->>S: PUT _security with user permissions alt Security Success S-->>A: security response with ok=true else Security Failure S-->>A: security response with ok=false A-->>A: throw InternalError end else Error U-->>A: error A-->>A: throw parseError(e) else Not OK U-->>A: response with ok=false A-->>A: throw InternalError end
Parameters:
Name Type Attributes Default Description
con ServerScope

The Nano server connection

dbName string

The name of the database to grant access to

user string

The username to create

pass string

The password for the new user

roles Array.<string> <optional>
["reader", "writer"]

The roles to assign to the user

View Source adapter.ts, line 1058

A promise that resolves when the user is created and granted access

Promise.<void>

# static decoration() → {void}

Configures decorators for created_by and updated_by fields in models to be automatically populated with the user from the context when documents are created or updated

Sets up decorations for Nano-specific model properties

sequenceDiagram participant A as NanoAdapter participant D as Decoration participant R as Repository A->>R: key(PersistenceKeys.CREATED_BY) R-->>A: createdByKey A->>D: flavouredAs("nano") A->>D: for(createdByKey) A->>D: define(onCreate(createdByOnNanoCreateUpdate), propMetadata) A->>D: apply() A->>R: key(PersistenceKeys.UPDATED_BY) R-->>A: updatedByKey A->>D: flavouredAs("nano") A->>D: for(updatedByKey) A->>D: define(onCreate(createdByOnNanoCreateUpdate), propMetadata) A->>D: apply()

View Source adapter.ts, line 1108

void

# async static deleteDatabase(con, name) → {Promise.<void>}

Removes an existing database with the specified name from the connected Nano server

Deletes a database from the Nano server

sequenceDiagram participant A as NanoAdapter participant DB as Nano Server A->>DB: db.destroy(name) alt Success DB-->>A: result with ok=true else Error DB-->>A: error A-->>A: throw parseError(e) else Not OK DB-->>A: result with ok=false A-->>A: throw InternalError end
Parameters:
Name Type Description
con ServerScope

The Nano server connection

name string

The name of the database to delete

View Source adapter.ts, line 1022

A promise that resolves when the database is deleted

Promise.<void>

# async static deleteUser(con, dbName, user) → {Promise.<void>}

Removes an existing user from the Nano server

Deletes a user from the Nano server

sequenceDiagram participant A as NanoAdapter participant U as _users Database A->>A: Generate user ID A->>U: get(id) U-->>A: user document A->>U: destroy(id, user._rev) alt Success U-->>A: success response else Error U-->>A: error A-->>A: throw parseError(e) end
Parameters:
Name Type Description
con ServerScope

The Nano server connection

dbName string

The name of the database (used for logging purposes)

user string

The username to delete

View Source adapter.ts, line 1083

A promise that resolves when the user is deleted

Promise.<void>