# new PouchAdapter(config, aliasopt)
Concrete adapter that bridges the generic CouchDBAdapter to a PouchDB backend. It supports CRUD (single and bulk), indexing and Mango queries, and wires flavour-specific decorations.
PouchDB implementation of the CouchDBAdapter
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
config |
PouchConfig
|
Adapter configuration (remote credentials or local storage path, db name, plugins) |
|
alias |
string
|
<optional> |
Optional alias for the database |
Example
```typescript
import { PouchAdapter } from '@decaf-ts/for-pouch';
// Create a PouchAdapter with config
const adapter = new PouchAdapter({
protocol: 'http',
host: 'localhost:5984',
user: 'admin',
password: 'secret',
dbName: 'my-database',
plugins: []
});
// Or use local storage
const localAdapter = new PouchAdapter({
protocol: 'http', // ignored for local
dbName: 'local-db',
storagePath: 'local_dbs',
plugins: []
});
// Use the adapter for database operations
const result = await adapter.read('users', 'user-123');
```
Methods
# async create(tableName, id, model) → {Promise.<Record.<string, any>>}
Inserts a new document into the PouchDB database using the put operation. This method handles error parsing and ensures the operation was successful.
Creates a new document in the database
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
id |
string
|
number
|
The document ID |
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 PouchDB database using the bulkDocs operation. This method handles error parsing and ensures all operations were successful.
Creates multiple documents in the database in a single operation
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
ids |
Array.<string>
|
Array.<number>
|
The document IDs |
models |
The document data to insert |
A promise that resolves to the created documents with metadata
# async delete(tableName, id) → {Promise.<Record.<string, any>>}
Removes a document from the PouchDB database using the remove operation. This method first retrieves the document to get its revision, then deletes it.
Deletes a document from the database by ID
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
id |
string
|
number
|
The document ID |
A promise that resolves to the deleted document with metadata
Promise.<Record.<string, any>>
# async deleteAll(tableName, ids)
Removes multiple documents from the PouchDB database in a single operation. This method first retrieves all documents to get their revisions, then marks them as deleted.
Deletes multiple documents from the database by their IDs
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
ids |
Array.<(string|number|bigint)>
|
The document IDs |
A promise that resolves to the deleted documents with metadata
# async protected flags(operation, model, flags) → {Promise.<PouchFlags>}
Creates a set of flags for a specific operation, including a UUID for identification. This method extracts the user ID from the database URL or generates a random UUID if not available.
Generates operation flags for PouchDB operations
Parameters:
Name | Type | Description |
---|---|---|
operation |
OperationKeys
|
The operation key (create, read, update, delete) |
model |
Constructor.<M>
|
The model constructor |
flags |
Partial.<PouchFlags>
|
Partial flags to be merged |
The complete set of flags for the operation
Promise.<PouchFlags>
# getClient() → {Database}
Loads required PouchDB plugins once, builds the connection URL or local storage path from config, and caches the Database instance for reuse. Throws InternalError if client creation fails.
Lazily initializes and returns the underlying PouchDB client
A PouchDB Database instance ready to perform operations
Database
# async protected index(models) → {Promise.<void>}
Generates and creates indexes in the PouchDB database based on the provided model constructors. This method uses the generateIndexes utility to create index definitions and then creates them in the database.
Creates database indexes for the given models
Parameters:
Name | Type | Description |
---|---|---|
models |
The model constructors to create indexes for |
A promise that resolves when all indexes are created
Promise.<void>
# parseError(err, reasonopt) → {BaseError}
Converts PouchDB errors to the application's error hierarchy. This instance method delegates to the static parseError method.
Parses and converts errors from PouchDB to application-specific errors
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
Error
|
string
|
The error object or message to parse |
|
reason |
string
|
<optional> |
Optional reason for the error |
The converted error object
BaseError
# async raw(rawInput, processopt) → {Promise.<V>}
Performs a direct find operation using a Mango query object. This method allows for complex queries beyond the standard CRUD operations.
Executes a raw Mango query against the database
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
rawInput |
MangoQuery
|
The Mango query to execute |
||
process |
boolean
|
<optional> |
true | Whether to process the response (true returns just docs, false returns full response) |
A promise that resolves to the query results
Promise.<V>
# async read(tableName, id) → {Promise.<Record.<string, any>>}
Fetches a document from the PouchDB database using the get operation. This method generates the document ID based on the table name and ID, then retrieves the document.
Retrieves a document from the database by ID
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
id |
string
|
number
|
The document ID |
A promise that resolves to the retrieved document with metadata
Promise.<Record.<string, any>>
# async readAll(tableName, ids)
Fetches multiple documents from the PouchDB database using the bulkGet operation. This method generates document IDs based on the table name and IDs, then retrieves the documents.
Retrieves multiple documents from the database by their IDs
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
ids |
Array.<(string|number|bigint)>
|
The document IDs |
A promise that resolves to the retrieved documents with metadata
# async update(tableName, id, model) → {Promise.<Record.<string, any>>}
Updates a document in the PouchDB database using the put operation. This method handles error parsing and ensures the operation was successful.
Updates an existing document in the database
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
id |
string
|
number
|
The document ID |
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)
Updates multiple documents in the PouchDB database using the bulkDocs operation. This method handles error parsing and ensures all operations were successful.
Updates multiple documents in the database in a single operation
Parameters:
Name | Type | Description |
---|---|---|
tableName |
string
|
The name of the table/collection |
ids |
Array.<string>
|
Array.<number>
|
The document IDs |
models |
The updated document data |
A promise that resolves to the updated documents with metadata
# static decoration()
Configures decorators for createdBy and updatedBy fields in models. This method defines how these fields should be automatically populated during create and update operations.
Sets up decorations for PouchDB-specific model properties
# static parseError(err, reasonopt) → {BaseError}
Converts PouchDB errors to the application's error hierarchy based on error codes and messages. This method analyzes the error type, status code, or message to determine the appropriate error class.
Static method to parse and convert errors from PouchDB to application-specific errors
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
Error
|
string
|
The error object or message to parse |
|
reason |
string
|
<optional> |
Optional reason for the error |
The converted error object
BaseError