# 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
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 |
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
A new NanoDispatch instance
# 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
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 |
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
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 |
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 |
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 |
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 |
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
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
Parameters:
Name | Type | Description |
---|---|---|
models |
Model constructors to create indexes for |
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 |
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
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
id |
string
|
number
|
The document identifier |
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
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
ids |
Array.<(string|number|bigint)>
|
Array of document identifiers |
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
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
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 |
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 |
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 |
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
Parameters:
Name | Type | Description |
---|---|---|
con |
ServerScope
|
The Nano server connection |
name |
string
|
The name of the database to create |
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
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 |
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
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
Parameters:
Name | Type | Description |
---|---|---|
con |
ServerScope
|
The Nano server connection |
name |
string
|
The name of the database to delete |
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
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 |
A promise that resolves when the user is deleted
Promise.<void>