# 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 |
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 |
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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
Error
|
string
|
The error to parse |
|
reason |
string
|
<optional> |
Optional reason for the error |
The parsed error as a BaseError
BaseError