@decaf-ts/for-couchdb 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +646 -144
- package/README.md +371 -1
- package/dist/for-couchdb.cjs +907 -326
- package/dist/for-couchdb.esm.cjs +910 -329
- package/lib/adapter.cjs +249 -41
- package/lib/adapter.d.ts +243 -17
- package/lib/constants.cjs +30 -2
- package/lib/constants.d.ts +28 -0
- package/lib/errors.cjs +19 -2
- package/lib/errors.d.ts +17 -0
- package/lib/esm/adapter.d.ts +243 -17
- package/lib/esm/adapter.js +249 -41
- package/lib/esm/constants.d.ts +28 -0
- package/lib/esm/constants.js +30 -2
- package/lib/esm/errors.d.ts +17 -0
- package/lib/esm/errors.js +19 -2
- package/lib/esm/index.d.ts +6 -13
- package/lib/esm/index.js +7 -14
- package/lib/esm/indexes/generator.d.ts +47 -0
- package/lib/esm/indexes/generator.js +58 -1
- package/lib/esm/interfaces/CouchDBRepository.d.ts +10 -0
- package/lib/esm/interfaces/CouchDBRepository.js +1 -1
- package/lib/esm/model/CouchDBSequence.d.ts +15 -9
- package/lib/esm/model/CouchDBSequence.js +12 -1
- package/lib/esm/query/Paginator.d.ts +117 -4
- package/lib/esm/query/Paginator.js +128 -23
- package/lib/esm/query/Statement.d.ts +141 -8
- package/lib/esm/query/Statement.js +256 -28
- package/lib/esm/query/constants.d.ts +43 -0
- package/lib/esm/query/constants.js +44 -1
- package/lib/esm/query/index.d.ts +4 -1
- package/lib/esm/query/index.js +5 -2
- package/lib/esm/query/translate.d.ts +31 -0
- package/lib/esm/query/translate.js +32 -1
- package/lib/esm/sequences/Sequence.d.ts +0 -2
- package/lib/esm/sequences/Sequence.js +5 -9
- package/lib/esm/types.d.ts +55 -12
- package/lib/esm/types.js +1 -1
- package/lib/esm/utils.d.ts +105 -0
- package/lib/esm/utils.js +106 -1
- package/lib/index.cjs +7 -14
- package/lib/index.d.ts +6 -13
- package/lib/indexes/generator.cjs +58 -1
- package/lib/indexes/generator.d.ts +47 -0
- package/lib/interfaces/CouchDBRepository.cjs +1 -1
- package/lib/interfaces/CouchDBRepository.d.ts +10 -0
- package/lib/model/CouchDBSequence.cjs +12 -1
- package/lib/model/CouchDBSequence.d.ts +15 -9
- package/lib/query/Paginator.cjs +126 -21
- package/lib/query/Paginator.d.ts +117 -4
- package/lib/query/Statement.cjs +255 -27
- package/lib/query/Statement.d.ts +141 -8
- package/lib/query/constants.cjs +45 -2
- package/lib/query/constants.d.ts +43 -0
- package/lib/query/index.cjs +5 -2
- package/lib/query/index.d.ts +4 -1
- package/lib/query/translate.cjs +32 -1
- package/lib/query/translate.d.ts +31 -0
- package/lib/sequences/Sequence.cjs +5 -9
- package/lib/sequences/Sequence.d.ts +0 -2
- package/lib/types.cjs +1 -1
- package/lib/types.d.ts +55 -12
- package/lib/utils.cjs +106 -1
- package/lib/utils.d.ts +105 -0
- package/package.json +2 -2
- package/lib/esm/query/FromClause.d.ts +0 -7
- package/lib/esm/query/FromClause.js +0 -20
- package/lib/esm/query/InsertClause.d.ts +0 -7
- package/lib/esm/query/InsertClause.js +0 -13
- package/lib/esm/query/SelectClause.d.ts +0 -7
- package/lib/esm/query/SelectClause.js +0 -16
- package/lib/esm/query/ValuesClause.d.ts +0 -7
- package/lib/esm/query/ValuesClause.js +0 -12
- package/lib/esm/query/WhereClause.d.ts +0 -7
- package/lib/esm/query/WhereClause.js +0 -57
- package/lib/esm/query/factory.d.ts +0 -25
- package/lib/esm/query/factory.js +0 -117
- package/lib/esm/sequences/utils.d.ts +0 -1
- package/lib/esm/sequences/utils.js +0 -16
- package/lib/query/FromClause.cjs +0 -24
- package/lib/query/FromClause.d.ts +0 -7
- package/lib/query/InsertClause.cjs +0 -17
- package/lib/query/InsertClause.d.ts +0 -7
- package/lib/query/SelectClause.cjs +0 -20
- package/lib/query/SelectClause.d.ts +0 -7
- package/lib/query/ValuesClause.cjs +0 -16
- package/lib/query/ValuesClause.d.ts +0 -7
- package/lib/query/WhereClause.cjs +0 -61
- package/lib/query/WhereClause.d.ts +0 -7
- package/lib/query/factory.cjs +0 -121
- package/lib/query/factory.d.ts +0 -25
- package/lib/sequences/utils.cjs +0 -19
- package/lib/sequences/utils.d.ts +0 -1
package/lib/esm/adapter.d.ts
CHANGED
|
@@ -1,36 +1,262 @@
|
|
|
1
|
-
import { Adapter, Sequence, SequenceOptions
|
|
1
|
+
import { Adapter, Sequence, type SequenceOptions } from "@decaf-ts/core";
|
|
2
2
|
import { BaseError, Context, RepositoryFlags } from "@decaf-ts/db-decorators";
|
|
3
3
|
import "reflect-metadata";
|
|
4
|
-
import { Factory } from "./query";
|
|
5
4
|
import { Constructor, Model } from "@decaf-ts/decorator-validation";
|
|
6
5
|
import { MangoQuery } from "./types";
|
|
6
|
+
import { CouchDBStatement } from "./query";
|
|
7
|
+
/**
|
|
8
|
+
* @description Abstract adapter for CouchDB database operations
|
|
9
|
+
* @summary Provides a base implementation for CouchDB database operations, including CRUD operations, sequence management, and error handling
|
|
10
|
+
* @template Y - The scope type
|
|
11
|
+
* @template F - The repository flags type
|
|
12
|
+
* @template C - The context type
|
|
13
|
+
* @param {Y} scope - The scope for the adapter
|
|
14
|
+
* @param {string} flavour - The flavour of the adapter
|
|
15
|
+
* @param {string} [alias] - Optional alias for the adapter
|
|
16
|
+
* @class
|
|
17
|
+
* @example
|
|
18
|
+
* // Example of extending CouchDBAdapter
|
|
19
|
+
* class MyCouchDBAdapter extends CouchDBAdapter<MyScope, MyFlags, MyContext> {
|
|
20
|
+
* constructor(scope: MyScope) {
|
|
21
|
+
* super(scope, 'my-couchdb', 'my-alias');
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* // Implement abstract methods
|
|
25
|
+
* async index<M extends Model>(...models: Constructor<M>[]): Promise<void> {
|
|
26
|
+
* // Implementation
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* async raw<R>(rawInput: MangoQuery, docsOnly: boolean): Promise<R> {
|
|
30
|
+
* // Implementation
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* async create(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>> {
|
|
34
|
+
* // Implementation
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* async read(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>> {
|
|
38
|
+
* // Implementation
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* async update(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>> {
|
|
42
|
+
* // Implementation
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* async delete(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>> {
|
|
46
|
+
* // Implementation
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
7
50
|
export declare abstract class CouchDBAdapter<Y, F extends RepositoryFlags, C extends Context<F>> extends Adapter<Y, MangoQuery, F, C> {
|
|
8
|
-
protected
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
51
|
+
protected constructor(scope: Y, flavour: string, alias?: string);
|
|
52
|
+
/**
|
|
53
|
+
* @description Creates a new CouchDB statement for querying
|
|
54
|
+
* @summary Factory method that creates a new CouchDBStatement instance for building queries
|
|
55
|
+
* @template M - The model type
|
|
56
|
+
* @return {CouchDBStatement<M, any>} A new CouchDBStatement instance
|
|
57
|
+
*/
|
|
58
|
+
Statement<M extends Model>(): CouchDBStatement<M, any>;
|
|
59
|
+
/**
|
|
60
|
+
* @description Creates a new CouchDB sequence
|
|
61
|
+
* @summary Factory method that creates a new CouchDBSequence instance for managing sequences
|
|
62
|
+
* @param {SequenceOptions} options - The options for the sequence
|
|
63
|
+
* @return {Promise<Sequence>} A promise that resolves to a new Sequence instance
|
|
64
|
+
*/
|
|
14
65
|
Sequence(options: SequenceOptions): Promise<Sequence>;
|
|
66
|
+
/**
|
|
67
|
+
* @description Initializes the adapter by creating indexes for all managed models
|
|
68
|
+
* @summary Sets up the necessary database indexes for all models managed by this adapter
|
|
69
|
+
* @return {Promise<void>} A promise that resolves when initialization is complete
|
|
70
|
+
*/
|
|
15
71
|
initialize(): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* @description Creates indexes for the given models
|
|
74
|
+
* @summary Abstract method that must be implemented to create database indexes for the specified models
|
|
75
|
+
* @template M - The model type
|
|
76
|
+
* @param {...Constructor<M>} models - The model constructors to create indexes for
|
|
77
|
+
* @return {Promise<void>} A promise that resolves when all indexes are created
|
|
78
|
+
*/
|
|
16
79
|
protected abstract index<M extends Model>(...models: Constructor<M>[]): Promise<void>;
|
|
17
|
-
|
|
80
|
+
/**
|
|
81
|
+
* @description Executes a raw Mango query against the database
|
|
82
|
+
* @summary Abstract method that must be implemented to execute raw Mango queries
|
|
83
|
+
* @template R - The result type
|
|
84
|
+
* @param {MangoQuery} rawInput - The raw Mango query to execute
|
|
85
|
+
* @param {boolean} docsOnly - Whether to return only the documents or the full response
|
|
86
|
+
* @return {Promise<R>} A promise that resolves to the query result
|
|
87
|
+
*/
|
|
88
|
+
abstract raw<R>(rawInput: MangoQuery, docsOnly: boolean): Promise<R>;
|
|
89
|
+
/**
|
|
90
|
+
* @description Assigns metadata to a model
|
|
91
|
+
* @summary Adds revision metadata to a model as a non-enumerable property
|
|
92
|
+
* @param {Record<string, any>} model - The model to assign metadata to
|
|
93
|
+
* @param {string} rev - The revision string to assign
|
|
94
|
+
* @return {Record<string, any>} The model with metadata assigned
|
|
95
|
+
*/
|
|
18
96
|
protected assignMetadata(model: Record<string, any>, rev: string): Record<string, any>;
|
|
97
|
+
/**
|
|
98
|
+
* @description Assigns metadata to multiple models
|
|
99
|
+
* @summary Adds revision metadata to multiple models as non-enumerable properties
|
|
100
|
+
* @param models - The models to assign metadata to
|
|
101
|
+
* @param {string[]} revs - The revision strings to assign
|
|
102
|
+
* @return The models with metadata assigned
|
|
103
|
+
*/
|
|
19
104
|
protected assignMultipleMetadata(models: Record<string, any>[], revs: string[]): Record<string, any>[];
|
|
105
|
+
/**
|
|
106
|
+
* @description Prepares a record for creation
|
|
107
|
+
* @summary Adds necessary CouchDB fields to a record before creation
|
|
108
|
+
* @param {string} tableName - The name of the table
|
|
109
|
+
* @param {string|number} id - The ID of the record
|
|
110
|
+
* @param {Record<string, any>} model - The model to prepare
|
|
111
|
+
* @return A tuple containing the tableName, id, and prepared record
|
|
112
|
+
*/
|
|
20
113
|
protected createPrefix(tableName: string, id: string | number, model: Record<string, any>): (string | number | Record<string, any>)[];
|
|
21
|
-
|
|
114
|
+
/**
|
|
115
|
+
* @description Creates a new record in the database
|
|
116
|
+
* @summary Abstract method that must be implemented to create a new record
|
|
117
|
+
* @param {string} tableName - The name of the table
|
|
118
|
+
* @param {string|number} id - The ID of the record
|
|
119
|
+
* @param {Record<string, any>} model - The model to create
|
|
120
|
+
* @param {...any[]} args - Additional arguments
|
|
121
|
+
* @return {Promise<Record<string, any>>} A promise that resolves to the created record
|
|
122
|
+
*/
|
|
123
|
+
abstract create(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>>;
|
|
124
|
+
/**
|
|
125
|
+
* @description Prepares multiple records for creation
|
|
126
|
+
* @summary Adds necessary CouchDB fields to multiple records before creation
|
|
127
|
+
* @param {string} tableName - The name of the table
|
|
128
|
+
* @param {string[]|number[]} ids - The IDs of the records
|
|
129
|
+
* @param models - The models to prepare
|
|
130
|
+
* @return A tuple containing the tableName, ids, and prepared records
|
|
131
|
+
* @throws {InternalError} If ids and models arrays have different lengths
|
|
132
|
+
*/
|
|
22
133
|
protected createAllPrefix(tableName: string, ids: string[] | number[], models: Record<string, any>[]): (string | string[] | number[] | Record<string, any>[])[];
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
134
|
+
/**
|
|
135
|
+
* @description Reads a record from the database
|
|
136
|
+
* @summary Abstract method that must be implemented to read a record
|
|
137
|
+
* @param {string} tableName - The name of the table
|
|
138
|
+
* @param {string|number} id - The ID of the record
|
|
139
|
+
* @param {...any[]} args - Additional arguments
|
|
140
|
+
* @return {Promise<Record<string, any>>} A promise that resolves to the read record
|
|
141
|
+
*/
|
|
142
|
+
abstract read(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>>;
|
|
143
|
+
/**
|
|
144
|
+
* @description Prepares a record for update
|
|
145
|
+
* @summary Adds necessary CouchDB fields to a record before update
|
|
146
|
+
* @param {string} tableName - The name of the table
|
|
147
|
+
* @param {string|number} id - The ID of the record
|
|
148
|
+
* @param model - The model to prepare
|
|
149
|
+
* @return A tuple containing the tableName, id, and prepared record
|
|
150
|
+
* @throws {InternalError} If no revision number is found in the model
|
|
151
|
+
*/
|
|
26
152
|
updatePrefix(tableName: string, id: string | number, model: Record<string, any>): (string | number | Record<string, any>)[];
|
|
27
|
-
|
|
153
|
+
/**
|
|
154
|
+
* @description Updates a record in the database
|
|
155
|
+
* @summary Abstract method that must be implemented to update a record
|
|
156
|
+
* @param {string} tableName - The name of the table
|
|
157
|
+
* @param {string|number} id - The ID of the record
|
|
158
|
+
* @param {Record<string, any>} model - The model to update
|
|
159
|
+
* @param {any[]} args - Additional arguments
|
|
160
|
+
* @return A promise that resolves to the updated record
|
|
161
|
+
*/
|
|
162
|
+
abstract update(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>>;
|
|
163
|
+
/**
|
|
164
|
+
* @description Prepares multiple records for update
|
|
165
|
+
* @summary Adds necessary CouchDB fields to multiple records before update
|
|
166
|
+
* @param {string} tableName - The name of the table
|
|
167
|
+
* @param {string[]|number[]} ids - The IDs of the records
|
|
168
|
+
* @param models - The models to prepare
|
|
169
|
+
* @return A tuple containing the tableName, ids, and prepared records
|
|
170
|
+
* @throws {InternalError} If ids and models arrays have different lengths or if no revision number is found in a model
|
|
171
|
+
*/
|
|
28
172
|
protected updateAllPrefix(tableName: string, ids: string[] | number[], models: Record<string, any>[]): (string | string[] | number[] | Record<string, any>[])[];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
173
|
+
/**
|
|
174
|
+
* @description Deletes a record from the database
|
|
175
|
+
* @summary Abstract method that must be implemented to delete a record
|
|
176
|
+
* @param {string} tableName - The name of the table
|
|
177
|
+
* @param {string|number} id - The ID of the record
|
|
178
|
+
* @param {any[]} args - Additional arguments
|
|
179
|
+
* @return A promise that resolves to the deleted record
|
|
180
|
+
*/
|
|
181
|
+
abstract delete(tableName: string, id: string | number, ...args: any[]): Promise<Record<string, any>>;
|
|
182
|
+
/**
|
|
183
|
+
* @description Generates a CouchDB document ID
|
|
184
|
+
* @summary Combines the table name and ID to create a CouchDB document ID
|
|
185
|
+
* @param {string} tableName - The name of the table
|
|
186
|
+
* @param {string|number} id - The ID of the record
|
|
187
|
+
* @return {string} The generated CouchDB document ID
|
|
188
|
+
*/
|
|
32
189
|
protected generateId(tableName: string, id: string | number): string;
|
|
190
|
+
/**
|
|
191
|
+
* @description Parses an error and converts it to a BaseError
|
|
192
|
+
* @summary Converts various error types to appropriate BaseError subtypes
|
|
193
|
+
* @param {Error|string} err - The error to parse
|
|
194
|
+
* @param {string} [reason] - Optional reason for the error
|
|
195
|
+
* @return {BaseError} The parsed error as a BaseError
|
|
196
|
+
*/
|
|
33
197
|
parseError(err: Error | string, reason?: string): BaseError;
|
|
198
|
+
/**
|
|
199
|
+
* @description Checks if an attribute is reserved
|
|
200
|
+
* @summary Determines if an attribute name is reserved in CouchDB
|
|
201
|
+
* @param {string} attr - The attribute name to check
|
|
202
|
+
* @return {boolean} True if the attribute is reserved, false otherwise
|
|
203
|
+
*/
|
|
34
204
|
protected isReserved(attr: string): boolean;
|
|
205
|
+
/**
|
|
206
|
+
* @description Static method to parse an error and convert it to a BaseError
|
|
207
|
+
* @summary Converts various error types to appropriate BaseError subtypes based on error codes and messages
|
|
208
|
+
* @param {Error|string} err - The error to parse
|
|
209
|
+
* @param {string} [reason] - Optional reason for the error
|
|
210
|
+
* @return {BaseError} The parsed error as a BaseError
|
|
211
|
+
* @mermaid
|
|
212
|
+
* sequenceDiagram
|
|
213
|
+
* participant Caller
|
|
214
|
+
* participant parseError
|
|
215
|
+
* participant ErrorTypes
|
|
216
|
+
*
|
|
217
|
+
* Caller->>parseError: err, reason
|
|
218
|
+
* Note over parseError: Check if err is already a BaseError
|
|
219
|
+
* alt err is BaseError
|
|
220
|
+
* parseError-->>Caller: return err
|
|
221
|
+
* else err is string
|
|
222
|
+
* Note over parseError: Extract code from string
|
|
223
|
+
* alt code matches "already exist|update conflict"
|
|
224
|
+
* parseError->>ErrorTypes: new ConflictError(code)
|
|
225
|
+
* ErrorTypes-->>Caller: ConflictError
|
|
226
|
+
* else code matches "missing|deleted"
|
|
227
|
+
* parseError->>ErrorTypes: new NotFoundError(code)
|
|
228
|
+
* ErrorTypes-->>Caller: NotFoundError
|
|
229
|
+
* end
|
|
230
|
+
* else err has code property
|
|
231
|
+
* Note over parseError: Extract code and reason
|
|
232
|
+
* else err has statusCode property
|
|
233
|
+
* Note over parseError: Extract code and reason
|
|
234
|
+
* else
|
|
235
|
+
* Note over parseError: Use err.message as code
|
|
236
|
+
* end
|
|
237
|
+
*
|
|
238
|
+
* Note over parseError: Switch on code
|
|
239
|
+
* alt code is 401, 412, or 409
|
|
240
|
+
* parseError->>ErrorTypes: new ConflictError(reason)
|
|
241
|
+
* ErrorTypes-->>Caller: ConflictError
|
|
242
|
+
* else code is 404
|
|
243
|
+
* parseError->>ErrorTypes: new NotFoundError(reason)
|
|
244
|
+
* ErrorTypes-->>Caller: NotFoundError
|
|
245
|
+
* else code is 400
|
|
246
|
+
* alt code matches "No index exists"
|
|
247
|
+
* parseError->>ErrorTypes: new IndexError(err)
|
|
248
|
+
* ErrorTypes-->>Caller: IndexError
|
|
249
|
+
* else
|
|
250
|
+
* parseError->>ErrorTypes: new InternalError(err)
|
|
251
|
+
* ErrorTypes-->>Caller: InternalError
|
|
252
|
+
* end
|
|
253
|
+
* else code matches "ECONNREFUSED"
|
|
254
|
+
* parseError->>ErrorTypes: new ConnectionError(err)
|
|
255
|
+
* ErrorTypes-->>Caller: ConnectionError
|
|
256
|
+
* else
|
|
257
|
+
* parseError->>ErrorTypes: new InternalError(err)
|
|
258
|
+
* ErrorTypes-->>Caller: InternalError
|
|
259
|
+
* end
|
|
260
|
+
*/
|
|
35
261
|
protected static parseError(err: Error | string, reason?: string): BaseError;
|
|
36
262
|
}
|