Class

CouchDBAdapter

CouchDBAdapter(scope, flavour, aliasopt)

Constructor

# new CouchDBAdapter(scope, flavour, aliasopt)

Provides a base implementation for CouchDB database operations, including CRUD operations, sequence management, and error handling

Abstract adapter for CouchDB database operations

Parameters:
Name Type Attributes Description
scope Y

The scope for the adapter

flavour string

The flavour of the adapter

alias string <optional>

Optional alias for the adapter

View Source adapter.ts, line 60

Example
// Example of extending CouchDBAdapter
class MyCouchDBAdapter extends CouchDBAdapter<MyScope, MyFlags, MyContext> {
  constructor(scope: MyScope) {
    super(scope, 'my-couchdb', 'my-alias');
  }

  // Implement abstract methods
  async index<M extends Model>(...models: Constructor<M>[]): Promise<void> {
    // Implementation
  }

  async raw<R>(rawInput: MangoQuery, docsOnly: boolean): Promise<R> {
    // Implementation
  }

  async create(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>> {
    // Implementation
  }

  async read(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>> {
    // Implementation
  }

  async update(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>> {
    // Implementation
  }

  async delete(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>> {
    // Implementation
  }
}

Classes

CouchDBAdapter

Provides a base implementation for CouchDB database operations, including CRUD operations, sequence management, and error handling

Methods

# Statement() → {CouchDBStatement.<M, any>}

Factory method that creates a new CouchDBStatement instance for building queries

Creates a new CouchDB statement for querying

View Source adapter.ts, line 436

A new CouchDBStatement instance

CouchDBStatement.<M, any>

# protected assignMetadata(model, rev) → {Record.<string, any>}

Adds revision metadata to a model as a non-enumerable property

Assigns metadata to a model

Parameters:
Name Type Description
model Record.<string, any>

The model to assign metadata to

rev string

The revision string to assign

View Source adapter.ts, line 490

The model with metadata assigned

Record.<string, any>

# protected assignMultipleMetadata(models, revs)

Adds revision metadata to multiple models as non-enumerable properties

Assigns metadata to multiple models

Parameters:
Name Type Description
models

The models to assign metadata to

revs Array.<string>

The revision strings to assign

View Source adapter.ts, line 500

The models with metadata assigned

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

Abstract method that must be implemented to create a new record

Creates a new record in the database

Parameters:
Name Type Attributes Description
tableName string

The name of the table

id string | number

The ID of the record

model Record.<string, any>

The model to create

args Array.<any> <repeatable>

Additional arguments

View Source adapter.ts, line 523

A promise that resolves to the created record

Promise.<Record.<string, any>>

# protected createAllPrefix(tableName, ids, models)

Adds necessary CouchDB fields to multiple records before creation

Prepares multiple records for creation

Parameters:
Name Type Description
tableName string

The name of the table

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

The IDs of the records

models

The models to prepare

View Source adapter.ts, line 535

If ids and models arrays have different lengths

InternalError

A tuple containing the tableName, ids, and prepared records

# protected createPrefix(tableName, id, model)

Adds necessary CouchDB fields to a record before creation

Prepares a record for creation

Parameters:
Name Type Description
tableName string

The name of the table

id string | number

The ID of the record

model Record.<string, any>

The model to prepare

View Source adapter.ts, line 511

A tuple containing the tableName, id, and prepared record

# abstract delete(tableName, id, args)

Abstract method that must be implemented to delete a record

Deletes a record from the database

Parameters:
Name Type Description
tableName Constructor.<M>

The name of the table

id PrimaryKeyType

The ID of the record

args Array.<any>

Additional arguments

View Source adapter.ts, line 593

A promise that resolves to the deleted record

# protected generateId(tableName, id) → {string}

Combines the table name and ID to create a CouchDB document ID

Generates a CouchDB document ID

Parameters:
Name Type Description
tableName string

The name of the table

id string | number

The ID of the record

View Source adapter.ts, line 603

The generated CouchDB document ID

string

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

Abstract method that must be implemented to create database indexes for the specified models

Creates indexes for the given models

Parameters:
Name Type Attributes Description
models Constructor.<M> <repeatable>

The model constructors to create indexes for

View Source adapter.ts, line 455

A promise that resolves when all indexes are created

Promise.<void>

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

Sets up the necessary database indexes for all models managed by this adapter

Initializes the adapter by creating indexes for all managed models

View Source adapter.ts, line 444

A promise that resolves when initialization is complete

Promise.<void>

# protected isReserved(attr) → {boolean}

Determines if an attribute name is reserved in CouchDB

Checks if an attribute is reserved

Parameters:
Name Type Description
attr string

The attribute name to check

View Source adapter.ts, line 621

True if the attribute is reserved, false otherwise

boolean

# parseError(err, reasonopt) → {BaseError}

Converts various error types to appropriate BaseError subtypes

Parses an error and converts it to a BaseError

Parameters:
Name Type Attributes Description
err Error | string

The error to parse

reason string <optional>

Optional reason for the error

View Source adapter.ts, line 612

The parsed error as a BaseError

BaseError

# abstract raw(rawInput, …args) → {Promise.<R>}

Abstract method that must be implemented to execute raw Mango queries. Implementations may treat the first additional argument as a boolean docsOnly flag before the contextual arguments provided by repositories.

Executes a raw Mango query against the database

Parameters:
Name Type Attributes Description
rawInput MangoQuery

The raw Mango query to execute

args MaybeContextualArg.<C> <repeatable>

Optional docsOnly flag followed by contextual arguments

View Source adapter.ts, line 467

A promise that resolves to the query result

Promise.<R>

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

Abstract method that must be implemented to read a record

Reads a record from the database

Parameters:
Name Type Attributes Description
tableName string

The name of the table

id string | number

The ID of the record

args Array.<any> <repeatable>

Additional arguments

View Source adapter.ts, line 546

A promise that resolves to the read record

Promise.<Record.<string, any>>

# abstract update(tableName, id, model, args)

Abstract method that must be implemented to update a record

Updates a record in the database

Parameters:
Name Type Description
tableName string

The name of the table

id string | number

The ID of the record

model Record.<string, any>

The model to update

args Array.<any>

Additional arguments

View Source adapter.ts, line 570

A promise that resolves to the updated record

# protected updateAllPrefix(tableName, ids, models)

Adds necessary CouchDB fields to multiple records before update

Prepares multiple records for update

Parameters:
Name Type Description
tableName string

The name of the table

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

The IDs of the records

models

The models to prepare

View Source adapter.ts, line 582

If ids and models arrays have different lengths or if no revision number is found in a model

InternalError

A tuple containing the tableName, ids, and prepared records

# updatePrefix(tableName, id, model, argsopt)

Adds necessary CouchDB fields to a record before update

Prepares a record for update

Parameters:
Name Type Attributes Description
tableName string

The name of the table

id string | number

The ID of the record

model

The model to prepare

args <optional>

optional args for subclassing

View Source adapter.ts, line 558

If no revision number is found in the model

InternalError

A tuple containing the tableName, id, and prepared record

# abstract view(ddoc, view, options, …args) → {Promise.<ViewResponse.<R>>}

Invokes a design document view and returns its response

Executes a CouchDB view query

Parameters:
Name Type Attributes Description
ddoc string

Design document name

view string

View name

options Record.<string, any>

Mango query options

args ContextualArgs.<C> <repeatable>

Optional contextual arguments

View Source adapter.ts, line 480

The view response

Promise.<ViewResponse.<R>>

# static getMetadata(model) → {any}

Retrieves previously attached metadata from a model instance.

Gets metadata from a model instance.

Parameters:
Name Type Description
model M

The model instance.

View Source adapter.ts, line 698

The metadata or undefined if not found.

any

# protected static parseError(err, reasonopt) → {BaseError}

Converts various error types to appropriate BaseError subtypes based on error codes and messages

Static method to parse an error and convert it to a BaseError

sequenceDiagram participant Caller participant parseError participant ErrorTypes Caller->>parseError: err, reason Note over parseError: Check if err is already a BaseError alt err is BaseError parseError-->>Caller: return err else err is string Note over parseError: Extract code from string alt code matches "already exist|update conflict" parseError->>ErrorTypes: new ConflictError(code) ErrorTypes-->>Caller: ConflictError else code matches "missing|deleted" parseError->>ErrorTypes: new NotFoundError(code) ErrorTypes-->>Caller: NotFoundError end else err has code property Note over parseError: Extract code and reason else err has statusCode property Note over parseError: Extract code and reason else Note over parseError: Use err.message as code end Note over parseError: Switch on code alt code is 401, 412, or 409 parseError->>ErrorTypes: new ConflictError(reason) ErrorTypes-->>Caller: ConflictError else code is 404 parseError->>ErrorTypes: new NotFoundError(reason) ErrorTypes-->>Caller: NotFoundError else code is 400 alt code matches "No index exists" parseError->>ErrorTypes: new IndexError(err) ErrorTypes-->>Caller: IndexError else parseError->>ErrorTypes: new InternalError(err) ErrorTypes-->>Caller: InternalError end else code matches "ECONNREFUSED" parseError->>ErrorTypes: new ConnectionError(err) ErrorTypes-->>Caller: ConnectionError else parseError->>ErrorTypes: new InternalError(err) ErrorTypes-->>Caller: InternalError end
Parameters:
Name Type Attributes Description
err Error | string

The error to parse

reason string <optional>

Optional reason for the error

View Source adapter.ts, line 680

The parsed error as a BaseError

BaseError

# static removeMetadata(model)

Deletes the metadata property from a model instance.

Removes metadata from a model instance.

Parameters:
Name Type Description
model M

The model instance.

View Source adapter.ts, line 706

# static setMetadata(model, metadata)

Attaches metadata to a model instance using a non-enumerable property.

Sets metadata on a model instance.

Parameters:
Name Type Description
model M

The model instance.

metadata any

The metadata to attach to the model.

View Source adapter.ts, line 689