Class

PouchAdapter

PouchAdapter(config, aliasopt)

Constructor

# 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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB participant CouchDB Client->>PouchAdapter: new PouchAdapter(config, alias?) PouchAdapter->>CouchDBAdapter: super(config, PouchFlavour, alias) Client->>PouchAdapter: create(table, id, model) PouchAdapter->>PouchDB: put(model) PouchDB->>CouchDB: HTTP PUT CouchDB-->>PouchDB: Response PouchDB-->>PouchAdapter: Response PouchAdapter-->>Client: Updated model Client->>PouchAdapter: read(table, id) PouchAdapter->>PouchDB: get(id) PouchDB->>CouchDB: HTTP GET CouchDB-->>PouchDB: Document PouchDB-->>PouchAdapter: Document PouchAdapter-->>Client: Model
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

View Source adapter.ts, line 38

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: create(tableName, id, model) PouchAdapter->>PouchDB: put(model) alt Success PouchDB-->>PouchAdapter: Response with ok=true PouchAdapter->>PouchAdapter: assignMetadata(model, response.rev) PouchAdapter-->>Client: Updated model with metadata else Error PouchDB-->>PouchAdapter: Error PouchAdapter->>PouchAdapter: parseError(e) PouchAdapter-->>Client: Throws error end
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

View Source adapter.ts, line 810

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: createAll(tableName, ids, models) PouchAdapter->>PouchDB: bulkDocs(models) alt Success PouchDB-->>PouchAdapter: Array of responses with ok=true PouchAdapter->>PouchAdapter: assignMultipleMetadata(models, revs) PouchAdapter-->>Client: Updated models with metadata else Error PouchDB-->>PouchAdapter: Array with errors PouchAdapter->>PouchAdapter: Check for errors PouchAdapter-->>Client: Throws InternalError end
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

View Source adapter.ts, line 839

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: delete(tableName, id) PouchAdapter->>PouchAdapter: generateId(tableName, id) PouchAdapter->>PouchDB: get(_id) PouchDB-->>PouchAdapter: Document with _rev PouchAdapter->>PouchDB: remove(_id, record._rev) alt Success PouchDB-->>PouchAdapter: Success response PouchAdapter->>PouchAdapter: assignMetadata(record, record._rev) PouchAdapter-->>Client: Deleted document with metadata else Error PouchDB-->>PouchAdapter: Error PouchAdapter->>PouchAdapter: parseError(e) PouchAdapter-->>Client: Throws error end
Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document ID

View Source adapter.ts, line 986

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: deleteAll(tableName, ids) PouchAdapter->>PouchAdapter: Map ids to generateId(tableName, id) PouchAdapter->>PouchDB: bulkGet({docs}) PouchDB-->>PouchAdapter: BulkGetResponse with documents PouchAdapter->>PouchAdapter: Mark documents as deleted PouchAdapter->>PouchDB: bulkDocs(marked documents) alt Success PouchDB-->>PouchAdapter: Success responses PouchAdapter->>PouchAdapter: Process results PouchAdapter->>PouchAdapter: assignMetadata for each doc PouchAdapter-->>Client: Deleted documents with metadata else Error PouchAdapter->>PouchAdapter: Check for errors PouchAdapter-->>Client: Throws InternalError end
Parameters:
Name Type Description
tableName string

The name of the table/collection

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

The document IDs

View Source adapter.ts, line 1018

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

View Source adapter.ts, line 769

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

sequenceDiagram participant Caller participant PouchAdapter participant PouchDB Caller->>PouchAdapter: getClient() alt client not initialized PouchAdapter->>PouchAdapter: register plugins PouchAdapter->>PouchDB: new PouchDB(url or path) alt creation fails PouchDB-->>PouchAdapter: Error PouchAdapter-->>Caller: throws InternalError else success PouchDB-->>PouchAdapter: Database PouchAdapter-->>Caller: cached client end else client initialized PouchAdapter-->>Caller: cached client end

View Source adapter.ts, line 755

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

View Source adapter.ts, line 781

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

View Source adapter.ts, line 1060

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: raw(rawInput, process) PouchAdapter->>PouchDB: find(rawInput) alt Success PouchDB-->>PouchAdapter: FindResponse alt process=true PouchAdapter-->>Client: response.docs as V else process=false PouchAdapter-->>Client: response as V end else Error PouchDB-->>PouchAdapter: Error PouchAdapter->>PouchAdapter: parseError(e) PouchAdapter-->>Client: Throws error end
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)

View Source adapter.ts, line 1050

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: read(tableName, id) PouchAdapter->>PouchAdapter: generateId(tableName, id) PouchAdapter->>PouchDB: get(_id) alt Success PouchDB-->>PouchAdapter: Document PouchAdapter->>PouchAdapter: assignMetadata(record, record._rev) PouchAdapter-->>Client: Document with metadata else Error PouchDB-->>PouchAdapter: Error PouchAdapter->>PouchAdapter: parseError(e) PouchAdapter-->>Client: Throws error end
Parameters:
Name Type Description
tableName string

The name of the table/collection

id string | number

The document ID

View Source adapter.ts, line 868

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: readAll(tableName, ids) PouchAdapter->>PouchAdapter: Map ids to generateId(tableName, id) PouchAdapter->>PouchDB: bulkGet({docs}) alt Success PouchDB-->>PouchAdapter: BulkGetResponse PouchAdapter->>PouchAdapter: Process results PouchAdapter->>PouchAdapter: assignMetadata for each doc PouchAdapter-->>Client: Documents with metadata else Error PouchAdapter->>PouchAdapter: parseError(error) PouchAdapter-->>Client: Throws error end
Parameters:
Name Type Description
tableName string

The name of the table/collection

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

The document IDs

View Source adapter.ts, line 897

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: update(tableName, id, model) PouchAdapter->>PouchDB: put(model) alt Success PouchDB-->>PouchAdapter: Response with ok=true PouchAdapter->>PouchAdapter: assignMetadata(model, response.rev) PouchAdapter-->>Client: Updated model with metadata else Error PouchDB-->>PouchAdapter: Error PouchAdapter->>PouchAdapter: parseError(e) PouchAdapter-->>Client: Throws error end
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

View Source adapter.ts, line 926

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

sequenceDiagram participant Client participant PouchAdapter participant PouchDB Client->>PouchAdapter: updateAll(tableName, ids, models) PouchAdapter->>PouchDB: bulkDocs(models) alt Success PouchDB-->>PouchAdapter: Array of responses with ok=true PouchAdapter->>PouchAdapter: assignMultipleMetadata(models, revs) PouchAdapter-->>Client: Updated models with metadata else Error PouchDB-->>PouchAdapter: Array with errors PouchAdapter->>PouchAdapter: Check for errors PouchAdapter-->>Client: Throws InternalError end
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

View Source adapter.ts, line 955

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

sequenceDiagram participant Caller participant PouchAdapter participant Decoration Caller->>PouchAdapter: decoration() PouchAdapter->>Repository: key(PersistenceKeys.CREATED_BY) Repository-->>PouchAdapter: createdByKey PouchAdapter->>Repository: key(PersistenceKeys.UPDATED_BY) Repository-->>PouchAdapter: updatedByKey PouchAdapter->>Decoration: flavouredAs(PouchFlavour) Decoration-->>PouchAdapter: DecoratorBuilder PouchAdapter->>Decoration: for(createdByKey) PouchAdapter->>Decoration: define(onCreate, propMetadata) PouchAdapter->>Decoration: apply() PouchAdapter->>Decoration: flavouredAs(PouchFlavour) Decoration-->>PouchAdapter: DecoratorBuilder PouchAdapter->>Decoration: for(updatedByKey) PouchAdapter->>Decoration: define(onCreate, propMetadata) PouchAdapter->>Decoration: apply()

View Source adapter.ts, line 1131

# 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

sequenceDiagram participant Caller participant PouchAdapter Caller->>PouchAdapter: parseError(err, reason) alt err is BaseError PouchAdapter-->>Caller: Return err as is else err is string alt contains "already exist" or "update conflict" PouchAdapter-->>Caller: ConflictError else contains "missing" or "deleted" PouchAdapter-->>Caller: NotFoundError end else err has status alt status is 401, 412, 409 PouchAdapter-->>Caller: ConflictError else status is 404 PouchAdapter-->>Caller: NotFoundError else status is 400 alt message contains "No index exists" PouchAdapter-->>Caller: IndexError else PouchAdapter-->>Caller: InternalError end else message contains "ECONNREFUSED" PouchAdapter-->>Caller: ConnectionError else PouchAdapter-->>Caller: InternalError end end
Parameters:
Name Type Attributes Description
err Error | string

The error object or message to parse

reason string <optional>

Optional reason for the error

View Source adapter.ts, line 1101

The converted error object

BaseError