@decaf-ts/for-pouch 0.2.2 → 0.2.4

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.
@@ -6,21 +6,421 @@ import Database = PouchDB.Database;
6
6
  import { Constructor, Model } from "@decaf-ts/decorator-validation";
7
7
  import { PouchFlags } from "./types";
8
8
  import { PouchRepository } from "./PouchRepository";
9
+ /**
10
+ * @description Sets the creator ID on a model during creation or update operations
11
+ * @summary This function is used as a decorator handler to automatically set the creator ID field on a model
12
+ * when it's being created or updated. It extracts the UUID from the context and assigns it to the specified key.
13
+ * @template M - The model type that extends Model
14
+ * @template R - The repository type that extends PouchRepository<M>
15
+ * @template V - The relations metadata type that extends RelationsMetadata
16
+ * @param {R} this - The repository instance
17
+ * @param {Context<PouchFlags>} context - The operation context containing flags
18
+ * @param {V} data - The relations metadata
19
+ * @param key - The property key to set on the model
20
+ * @param {M} model - The model instance to modify
21
+ * @return {Promise<void>} A promise that resolves when the operation is complete
22
+ * @function createdByOnPouchCreateUpdate
23
+ * @memberOf module:for-pouch
24
+ */
9
25
  export declare function createdByOnPouchCreateUpdate<M extends Model, R extends PouchRepository<M>, V extends RelationsMetadata>(this: R, context: Context<PouchFlags>, data: V, key: keyof M, model: M): Promise<void>;
26
+ /**
27
+ * @description PouchDB implementation of the CouchDBAdapter
28
+ * @summary This class provides a concrete implementation of the CouchDBAdapter for PouchDB.
29
+ * It handles all database operations like create, read, update, delete (CRUD) for both
30
+ * single documents and bulk operations. It also provides methods for querying and indexing.
31
+ * @template Database - The PouchDB database type
32
+ * @template PouchFlags - The flags specific to PouchDB operations
33
+ * @template Context<PouchFlags> - The context type with PouchDB flags
34
+ * @param {Database} scope - The PouchDB database instance
35
+ * @param {string} [alias] - Optional alias for the database
36
+ * @class PouchAdapter
37
+ * @example
38
+ * ```typescript
39
+ * import PouchDB from 'pouchdb';
40
+ * import { PouchAdapter } from '@decaf-ts/for-pouch';
41
+ *
42
+ * // Create a new PouchDB instance
43
+ * const db = new PouchDB('my-database');
44
+ *
45
+ * // Create a PouchAdapter with the database
46
+ * const adapter = new PouchAdapter(db);
47
+ *
48
+ * // Use the adapter for database operations
49
+ * const result = await adapter.read('users', 'user-123');
50
+ * ```
51
+ * @mermaid
52
+ * sequenceDiagram
53
+ * participant Client
54
+ * participant PouchAdapter
55
+ * participant PouchDB
56
+ * participant CouchDB
57
+ *
58
+ * Client->>PouchAdapter: new PouchAdapter(db)
59
+ * PouchAdapter->>CouchDBAdapter: super(scope, PouchFlavour, alias)
60
+ *
61
+ * Client->>PouchAdapter: create(table, id, model)
62
+ * PouchAdapter->>PouchDB: put(model)
63
+ * PouchDB->>CouchDB: HTTP PUT
64
+ * CouchDB-->>PouchDB: Response
65
+ * PouchDB-->>PouchAdapter: Response
66
+ * PouchAdapter-->>Client: Updated model
67
+ *
68
+ * Client->>PouchAdapter: read(table, id)
69
+ * PouchAdapter->>PouchDB: get(id)
70
+ * PouchDB->>CouchDB: HTTP GET
71
+ * CouchDB-->>PouchDB: Document
72
+ * PouchDB-->>PouchAdapter: Document
73
+ * PouchAdapter-->>Client: Model
74
+ */
10
75
  export declare class PouchAdapter extends CouchDBAdapter<Database, PouchFlags, Context<PouchFlags>> {
11
76
  constructor(scope: Database, alias?: string);
77
+ /**
78
+ * @description Generates operation flags for PouchDB operations
79
+ * @summary Creates a set of flags for a specific operation, including a UUID for identification.
80
+ * This method extracts the user ID from the database URL or generates a random UUID if not available.
81
+ * @template M - The model type that extends Model
82
+ * @param {OperationKeys} operation - The operation key (create, read, update, delete)
83
+ * @param {Constructor<M>} model - The model constructor
84
+ * @param {Partial<PouchFlags>} flags - Partial flags to be merged
85
+ * @return {PouchFlags} The complete set of flags for the operation
86
+ */
12
87
  protected flags<M extends Model>(operation: OperationKeys, model: Constructor<M>, flags: Partial<PouchFlags>): PouchFlags;
88
+ /**
89
+ * @description Creates database indexes for the given models
90
+ * @summary Generates and creates indexes in the PouchDB database based on the provided model constructors.
91
+ * This method uses the generateIndexes utility to create index definitions and then creates them in the database.
92
+ * @template M - The model type that extends Model
93
+ * @param models - The model constructors to create indexes for
94
+ * @return {Promise<void>} A promise that resolves when all indexes are created
95
+ */
13
96
  protected index<M extends Model>(...models: Constructor<M>[]): Promise<void>;
97
+ /**
98
+ * @description Creates a new document in the database
99
+ * @summary Inserts a new document into the PouchDB database using the put operation.
100
+ * This method handles error parsing and ensures the operation was successful.
101
+ * @param {string} tableName - The name of the table/collection
102
+ * @param {string|number} id - The document ID
103
+ * @param {Record<string, any>} model - The document data to insert
104
+ * @return {Promise<Record<string, any>>} A promise that resolves to the created document with metadata
105
+ * @mermaid
106
+ * sequenceDiagram
107
+ * participant Client
108
+ * participant PouchAdapter
109
+ * participant PouchDB
110
+ *
111
+ * Client->>PouchAdapter: create(tableName, id, model)
112
+ * PouchAdapter->>PouchDB: put(model)
113
+ * alt Success
114
+ * PouchDB-->>PouchAdapter: Response with ok=true
115
+ * PouchAdapter->>PouchAdapter: assignMetadata(model, response.rev)
116
+ * PouchAdapter-->>Client: Updated model with metadata
117
+ * else Error
118
+ * PouchDB-->>PouchAdapter: Error
119
+ * PouchAdapter->>PouchAdapter: parseError(e)
120
+ * PouchAdapter-->>Client: Throws error
121
+ * end
122
+ */
14
123
  create(tableName: string, id: string | number, model: Record<string, any>): Promise<Record<string, any>>;
124
+ /**
125
+ * @description Creates multiple documents in the database in a single operation
126
+ * @summary Inserts multiple documents into the PouchDB database using the bulkDocs operation.
127
+ * This method handles error parsing and ensures all operations were successful.
128
+ * @param {string} tableName - The name of the table/collection
129
+ * @param {string[]|number[]} ids - The document IDs
130
+ * @param models - The document data to insert
131
+ * @return A promise that resolves to the created documents with metadata
132
+ * @mermaid
133
+ * sequenceDiagram
134
+ * participant Client
135
+ * participant PouchAdapter
136
+ * participant PouchDB
137
+ *
138
+ * Client->>PouchAdapter: createAll(tableName, ids, models)
139
+ * PouchAdapter->>PouchDB: bulkDocs(models)
140
+ * alt Success
141
+ * PouchDB-->>PouchAdapter: Array of responses with ok=true
142
+ * PouchAdapter->>PouchAdapter: assignMultipleMetadata(models, revs)
143
+ * PouchAdapter-->>Client: Updated models with metadata
144
+ * else Error
145
+ * PouchDB-->>PouchAdapter: Array with errors
146
+ * PouchAdapter->>PouchAdapter: Check for errors
147
+ * PouchAdapter-->>Client: Throws InternalError
148
+ * end
149
+ */
15
150
  createAll(tableName: string, ids: string[] | number[], models: Record<string, any>[]): Promise<Record<string, any>[]>;
151
+ /**
152
+ * @description Retrieves a document from the database by ID
153
+ * @summary Fetches a document from the PouchDB database using the get operation.
154
+ * This method generates the document ID based on the table name and ID, then retrieves the document.
155
+ * @param {string} tableName - The name of the table/collection
156
+ * @param {string|number} id - The document ID
157
+ * @return {Promise<Record<string, any>>} A promise that resolves to the retrieved document with metadata
158
+ * @mermaid
159
+ * sequenceDiagram
160
+ * participant Client
161
+ * participant PouchAdapter
162
+ * participant PouchDB
163
+ *
164
+ * Client->>PouchAdapter: read(tableName, id)
165
+ * PouchAdapter->>PouchAdapter: generateId(tableName, id)
166
+ * PouchAdapter->>PouchDB: get(_id)
167
+ * alt Success
168
+ * PouchDB-->>PouchAdapter: Document
169
+ * PouchAdapter->>PouchAdapter: assignMetadata(record, record._rev)
170
+ * PouchAdapter-->>Client: Document with metadata
171
+ * else Error
172
+ * PouchDB-->>PouchAdapter: Error
173
+ * PouchAdapter->>PouchAdapter: parseError(e)
174
+ * PouchAdapter-->>Client: Throws error
175
+ * end
176
+ */
16
177
  read(tableName: string, id: string | number): Promise<Record<string, any>>;
178
+ /**
179
+ * @description Retrieves multiple documents from the database by their IDs
180
+ * @summary Fetches multiple documents from the PouchDB database using the bulkGet operation.
181
+ * This method generates document IDs based on the table name and IDs, then retrieves the documents.
182
+ * @param {string} tableName - The name of the table/collection
183
+ * @param {Array<string|number|bigint>} ids - The document IDs
184
+ * @return A promise that resolves to the retrieved documents with metadata
185
+ * @mermaid
186
+ * sequenceDiagram
187
+ * participant Client
188
+ * participant PouchAdapter
189
+ * participant PouchDB
190
+ *
191
+ * Client->>PouchAdapter: readAll(tableName, ids)
192
+ * PouchAdapter->>PouchAdapter: Map ids to generateId(tableName, id)
193
+ * PouchAdapter->>PouchDB: bulkGet({docs})
194
+ * alt Success
195
+ * PouchDB-->>PouchAdapter: BulkGetResponse
196
+ * PouchAdapter->>PouchAdapter: Process results
197
+ * PouchAdapter->>PouchAdapter: assignMetadata for each doc
198
+ * PouchAdapter-->>Client: Documents with metadata
199
+ * else Error
200
+ * PouchAdapter->>PouchAdapter: parseError(error)
201
+ * PouchAdapter-->>Client: Throws error
202
+ * end
203
+ */
17
204
  readAll(tableName: string, ids: (string | number | bigint)[]): Promise<Record<string, any>[]>;
205
+ /**
206
+ * @description Updates an existing document in the database
207
+ * @summary Updates a document in the PouchDB database using the put operation.
208
+ * This method handles error parsing and ensures the operation was successful.
209
+ * @param {string} tableName - The name of the table/collection
210
+ * @param {string|number} id - The document ID
211
+ * @param {Record<string, any>} model - The updated document data
212
+ * @return {Promise<Record<string, any>>} A promise that resolves to the updated document with metadata
213
+ * @mermaid
214
+ * sequenceDiagram
215
+ * participant Client
216
+ * participant PouchAdapter
217
+ * participant PouchDB
218
+ *
219
+ * Client->>PouchAdapter: update(tableName, id, model)
220
+ * PouchAdapter->>PouchDB: put(model)
221
+ * alt Success
222
+ * PouchDB-->>PouchAdapter: Response with ok=true
223
+ * PouchAdapter->>PouchAdapter: assignMetadata(model, response.rev)
224
+ * PouchAdapter-->>Client: Updated model with metadata
225
+ * else Error
226
+ * PouchDB-->>PouchAdapter: Error
227
+ * PouchAdapter->>PouchAdapter: parseError(e)
228
+ * PouchAdapter-->>Client: Throws error
229
+ * end
230
+ */
18
231
  update(tableName: string, id: string | number, model: Record<string, any>): Promise<Record<string, any>>;
232
+ /**
233
+ * @description Updates multiple documents in the database in a single operation
234
+ * @summary Updates multiple documents in the PouchDB database using the bulkDocs operation.
235
+ * This method handles error parsing and ensures all operations were successful.
236
+ * @param {string} tableName - The name of the table/collection
237
+ * @param {string[]|number[]} ids - The document IDs
238
+ * @param models - The updated document data
239
+ * @return A promise that resolves to the updated documents with metadata
240
+ * @mermaid
241
+ * sequenceDiagram
242
+ * participant Client
243
+ * participant PouchAdapter
244
+ * participant PouchDB
245
+ *
246
+ * Client->>PouchAdapter: updateAll(tableName, ids, models)
247
+ * PouchAdapter->>PouchDB: bulkDocs(models)
248
+ * alt Success
249
+ * PouchDB-->>PouchAdapter: Array of responses with ok=true
250
+ * PouchAdapter->>PouchAdapter: assignMultipleMetadata(models, revs)
251
+ * PouchAdapter-->>Client: Updated models with metadata
252
+ * else Error
253
+ * PouchDB-->>PouchAdapter: Array with errors
254
+ * PouchAdapter->>PouchAdapter: Check for errors
255
+ * PouchAdapter-->>Client: Throws InternalError
256
+ * end
257
+ */
19
258
  updateAll(tableName: string, ids: string[] | number[], models: Record<string, any>[]): Promise<Record<string, any>[]>;
259
+ /**
260
+ * @description Deletes a document from the database by ID
261
+ * @summary Removes a document from the PouchDB database using the remove operation.
262
+ * This method first retrieves the document to get its revision, then deletes it.
263
+ * @param {string} tableName - The name of the table/collection
264
+ * @param {string|number} id - The document ID
265
+ * @return {Promise<Record<string, any>>} A promise that resolves to the deleted document with metadata
266
+ * @mermaid
267
+ * sequenceDiagram
268
+ * participant Client
269
+ * participant PouchAdapter
270
+ * participant PouchDB
271
+ *
272
+ * Client->>PouchAdapter: delete(tableName, id)
273
+ * PouchAdapter->>PouchAdapter: generateId(tableName, id)
274
+ * PouchAdapter->>PouchDB: get(_id)
275
+ * PouchDB-->>PouchAdapter: Document with _rev
276
+ * PouchAdapter->>PouchDB: remove(_id, record._rev)
277
+ * alt Success
278
+ * PouchDB-->>PouchAdapter: Success response
279
+ * PouchAdapter->>PouchAdapter: assignMetadata(record, record._rev)
280
+ * PouchAdapter-->>Client: Deleted document with metadata
281
+ * else Error
282
+ * PouchDB-->>PouchAdapter: Error
283
+ * PouchAdapter->>PouchAdapter: parseError(e)
284
+ * PouchAdapter-->>Client: Throws error
285
+ * end
286
+ */
20
287
  delete(tableName: string, id: string | number): Promise<Record<string, any>>;
288
+ /**
289
+ * @description Deletes multiple documents from the database by their IDs
290
+ * @summary Removes multiple documents from the PouchDB database in a single operation.
291
+ * This method first retrieves all documents to get their revisions, then marks them as deleted.
292
+ * @param {string} tableName - The name of the table/collection
293
+ * @param {Array<string|number|bigint>} ids - The document IDs
294
+ * @return A promise that resolves to the deleted documents with metadata
295
+ * @mermaid
296
+ * sequenceDiagram
297
+ * participant Client
298
+ * participant PouchAdapter
299
+ * participant PouchDB
300
+ *
301
+ * Client->>PouchAdapter: deleteAll(tableName, ids)
302
+ * PouchAdapter->>PouchAdapter: Map ids to generateId(tableName, id)
303
+ * PouchAdapter->>PouchDB: bulkGet({docs})
304
+ * PouchDB-->>PouchAdapter: BulkGetResponse with documents
305
+ * PouchAdapter->>PouchAdapter: Mark documents as deleted
306
+ * PouchAdapter->>PouchDB: bulkDocs(marked documents)
307
+ * alt Success
308
+ * PouchDB-->>PouchAdapter: Success responses
309
+ * PouchAdapter->>PouchAdapter: Process results
310
+ * PouchAdapter->>PouchAdapter: assignMetadata for each doc
311
+ * PouchAdapter-->>Client: Deleted documents with metadata
312
+ * else Error
313
+ * PouchAdapter->>PouchAdapter: Check for errors
314
+ * PouchAdapter-->>Client: Throws InternalError
315
+ * end
316
+ */
21
317
  deleteAll(tableName: string, ids: (string | number | bigint)[]): Promise<Record<string, any>[]>;
318
+ /**
319
+ * @description Executes a raw Mango query against the database
320
+ * @summary Performs a direct find operation using a Mango query object.
321
+ * This method allows for complex queries beyond the standard CRUD operations.
322
+ * @template V - The return type
323
+ * @param {MangoQuery} rawInput - The Mango query to execute
324
+ * @param {boolean} [process=true] - Whether to process the response (true returns just docs, false returns full response)
325
+ * @return {Promise<V>} A promise that resolves to the query results
326
+ * @mermaid
327
+ * sequenceDiagram
328
+ * participant Client
329
+ * participant PouchAdapter
330
+ * participant PouchDB
331
+ *
332
+ * Client->>PouchAdapter: raw<V>(rawInput, process)
333
+ * PouchAdapter->>PouchDB: find(rawInput)
334
+ * alt Success
335
+ * PouchDB-->>PouchAdapter: FindResponse
336
+ * alt process=true
337
+ * PouchAdapter-->>Client: response.docs as V
338
+ * else process=false
339
+ * PouchAdapter-->>Client: response as V
340
+ * end
341
+ * else Error
342
+ * PouchDB-->>PouchAdapter: Error
343
+ * PouchAdapter->>PouchAdapter: parseError(e)
344
+ * PouchAdapter-->>Client: Throws error
345
+ * end
346
+ */
22
347
  raw<V>(rawInput: MangoQuery, process?: boolean): Promise<V>;
348
+ /**
349
+ * @description Parses and converts errors from PouchDB to application-specific errors
350
+ * @summary Converts PouchDB errors to the application's error hierarchy.
351
+ * This instance method delegates to the static parseError method.
352
+ * @param {Error|string} err - The error object or message to parse
353
+ * @param {string} [reason] - Optional reason for the error
354
+ * @return {BaseError} The converted error object
355
+ */
23
356
  parseError(err: Error | string, reason?: string): BaseError;
357
+ /**
358
+ * @description Static method to parse and convert errors from PouchDB to application-specific errors
359
+ * @summary Converts PouchDB errors to the application's error hierarchy based on error codes and messages.
360
+ * This method analyzes the error type, status code, or message to determine the appropriate error class.
361
+ * @param {Error|string} err - The error object or message to parse
362
+ * @param {string} [reason] - Optional reason for the error
363
+ * @return {BaseError} The converted error object
364
+ * @mermaid
365
+ * sequenceDiagram
366
+ * participant Caller
367
+ * participant PouchAdapter
368
+ *
369
+ * Caller->>PouchAdapter: parseError(err, reason)
370
+ * alt err is BaseError
371
+ * PouchAdapter-->>Caller: Return err as is
372
+ * else err is string
373
+ * alt contains "already exist" or "update conflict"
374
+ * PouchAdapter-->>Caller: ConflictError
375
+ * else contains "missing" or "deleted"
376
+ * PouchAdapter-->>Caller: NotFoundError
377
+ * end
378
+ * else err has status
379
+ * alt status is 401, 412, 409
380
+ * PouchAdapter-->>Caller: ConflictError
381
+ * else status is 404
382
+ * PouchAdapter-->>Caller: NotFoundError
383
+ * else status is 400
384
+ * alt message contains "No index exists"
385
+ * PouchAdapter-->>Caller: IndexError
386
+ * else
387
+ * PouchAdapter-->>Caller: InternalError
388
+ * end
389
+ * else message contains "ECONNREFUSED"
390
+ * PouchAdapter-->>Caller: ConnectionError
391
+ * else
392
+ * PouchAdapter-->>Caller: InternalError
393
+ * end
394
+ * end
395
+ */
24
396
  static parseError(err: Error | string, reason?: string): BaseError;
397
+ /**
398
+ * @description Sets up decorations for PouchDB-specific model properties
399
+ * @summary Configures decorators for createdBy and updatedBy fields in models.
400
+ * This method defines how these fields should be automatically populated during create and update operations.
401
+ * @mermaid
402
+ * sequenceDiagram
403
+ * participant Caller
404
+ * participant PouchAdapter
405
+ * participant Decoration
406
+ *
407
+ * Caller->>PouchAdapter: decoration()
408
+ * PouchAdapter->>Repository: key(PersistenceKeys.CREATED_BY)
409
+ * Repository-->>PouchAdapter: createdByKey
410
+ * PouchAdapter->>Repository: key(PersistenceKeys.UPDATED_BY)
411
+ * Repository-->>PouchAdapter: updatedByKey
412
+ *
413
+ * PouchAdapter->>Decoration: flavouredAs(PouchFlavour)
414
+ * Decoration-->>PouchAdapter: DecoratorBuilder
415
+ * PouchAdapter->>Decoration: for(createdByKey)
416
+ * PouchAdapter->>Decoration: define(onCreate, propMetadata)
417
+ * PouchAdapter->>Decoration: apply()
418
+ *
419
+ * PouchAdapter->>Decoration: flavouredAs(PouchFlavour)
420
+ * Decoration-->>PouchAdapter: DecoratorBuilder
421
+ * PouchAdapter->>Decoration: for(updatedByKey)
422
+ * PouchAdapter->>Decoration: define(onCreate, propMetadata)
423
+ * PouchAdapter->>Decoration: apply()
424
+ */
25
425
  static decoration(): void;
26
426
  }