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 59

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

# async Sequence(options) → {Promise.<Sequence>}

Factory method that creates a new CouchDBSequence instance for managing sequences

Creates a new CouchDB sequence

Parameters:
Name Type Description
options SequenceOptions

The options for the sequence

View Source adapter.ts, line 382

A promise that resolves to a new Sequence instance

Promise.<Sequence>

# 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 373

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 422

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 432

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 455

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 467

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 443

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 string

The name of the table

id string | number

The ID of the record

args Array.<any>

Additional arguments

View Source adapter.ts, line 524

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 534

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 401

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 390

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 552

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 543

The parsed error as a BaseError

BaseError

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

Abstract method that must be implemented to execute raw Mango queries

Executes a raw Mango query against the database

Parameters:
Name Type Description
rawInput MangoQuery

The raw Mango query to execute

docsOnly boolean

Whether to return only the documents or the full response

View Source adapter.ts, line 412

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 478

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 501

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 513

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)

Adds necessary CouchDB fields to a record before update

Prepares a record for update

Parameters:
Name Type Description
tableName string

The name of the table

id string | number

The ID of the record

model

The model to prepare

View Source adapter.ts, line 489

If no revision number is found in the model

InternalError

A tuple containing the tableName, id, and prepared record

# 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 611

The parsed error as a BaseError

BaseError