@@ -39,6 +39,20 @@ import {
39
39
import { Document } from "./document.ts"
40
40
import { model } from "./model.ts"
41
41
42
+ /**
43
+ * Create a collection builder function.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * collection(model<number>(), {
48
+ * idGenerator: () => ulid()
49
+ * })
50
+ * ```
51
+ *
52
+ * @param model - Model.
53
+ * @param options - Collection options.
54
+ * @returns A collection builder function.
55
+ */
42
56
export function collection < const T1 extends KvValue > (
43
57
model : Model < T1 > ,
44
58
options ?: CollectionOptions < T1 > ,
@@ -347,30 +361,12 @@ export class Collection<
347
361
value : UpdateData < T1 > ,
348
362
options ?: UpdateManyOptions < T1 > ,
349
363
) {
350
- // Initiate result and error list
351
- const result : ( CommitResult < T1 > | Deno . KvCommitError ) [ ] = [ ]
352
- const errors : unknown [ ] = [ ]
353
-
354
364
// Update each document, add commit result to result list
355
- const { cursor } = await this . handleMany ( async ( doc ) => {
356
- try {
357
- const cr = await this . updateDocument ( doc , value , options )
358
- result . push ( cr )
359
- } catch ( e ) {
360
- errors . push ( e )
361
- }
362
- } , options )
363
-
364
- // Throw errors if caught
365
- if ( errors . length > 0 ) {
366
- throw errors
367
- }
368
-
369
- // Return result list and current iterator cursor
370
- return {
371
- result,
372
- cursor,
373
- }
365
+ return await this . handleMany (
366
+ this . _keys . idKey ,
367
+ ( doc ) => this . updateDocument ( doc , value , options ) ,
368
+ options ,
369
+ )
374
370
}
375
371
376
372
/**
@@ -445,7 +441,13 @@ export class Collection<
445
441
*/
446
442
async deleteMany ( options ?: ListOptions < T1 > ) {
447
443
// Execute delete operation for each document entry
448
- return await this . handleMany ( ( doc ) => this . delete ( doc . id ) , options )
444
+ const { cursor } = await this . handleMany (
445
+ this . _keys . idKey ,
446
+ ( doc ) => this . delete ( doc . id ) ,
447
+ options ,
448
+ )
449
+
450
+ return { cursor }
449
451
}
450
452
451
453
/**
@@ -468,20 +470,12 @@ export class Collection<
468
470
* @returns A promise that resovles to an object containing a list of the retrieved documents and the iterator cursor
469
471
*/
470
472
async getMany ( options ?: ListOptions < T1 > ) {
471
- // Initiate result list
472
- const result : Document < T1 > [ ] = [ ]
473
-
474
- // Add each document entry to result list
475
- const { cursor } = await this . handleMany (
476
- ( doc ) => result . push ( doc ) ,
473
+ // Get each document, return result list and current iterator cursor
474
+ return await this . handleMany (
475
+ this . _keys . idKey ,
476
+ ( doc ) => doc ,
477
477
options ,
478
478
)
479
-
480
- // Return result list and current iterator cursor
481
- return {
482
- result,
483
- cursor,
484
- }
485
479
}
486
480
487
481
/**
@@ -505,8 +499,14 @@ export class Collection<
505
499
* @returns A promise that resovles to an object containing the iterator cursor
506
500
*/
507
501
async forEach ( fn : ( doc : Document < T1 > ) => void , options ?: ListOptions < T1 > ) {
508
- // Execute callback function for each document entry
509
- return await this . handleMany ( ( doc ) => fn ( doc ) , options )
502
+ // Execute callback function for each document entry and return cursor
503
+ const { cursor } = await this . handleMany (
504
+ this . _keys . idKey ,
505
+ ( doc ) => fn ( doc ) ,
506
+ options ,
507
+ )
508
+
509
+ return { cursor }
510
510
}
511
511
512
512
/**
@@ -531,24 +531,16 @@ export class Collection<
531
531
* @param options - List options, optional.
532
532
* @returns A promise that resovles to an object containing a list of the callback results and the iterator cursor
533
533
*/
534
- async map < const TMapped > (
535
- fn : ( doc : Document < T1 > ) => TMapped ,
534
+ async map < const T > (
535
+ fn : ( doc : Document < T1 > ) => T ,
536
536
options ?: ListOptions < T1 > ,
537
537
) {
538
- // Initiate result list
539
- const result : TMapped [ ] = [ ]
540
-
541
- // Execute callback function for each document entry, add to result list
542
- const { cursor } = await this . handleMany (
543
- ( doc ) => result . push ( fn ( doc ) ) ,
538
+ // Execute callback function for each document entry, return result and cursor
539
+ return await this . handleMany (
540
+ this . _keys . idKey ,
541
+ ( doc ) => fn ( doc ) ,
544
542
options ,
545
543
)
546
-
547
- // Return result list and current iterator cursor
548
- return {
549
- result,
550
- cursor,
551
- }
552
544
}
553
545
554
546
/**
@@ -571,7 +563,7 @@ export class Collection<
571
563
async count ( options ?: CountOptions < T1 > ) {
572
564
// Initiate count variable, increment for each document entry, return result
573
565
let result = 0
574
- await this . handleMany ( ( ) => result ++ , options )
566
+ await this . handleMany ( this . _keys . idKey , ( ) => result ++ , options )
575
567
return result
576
568
}
577
569
@@ -709,18 +701,24 @@ export class Collection<
709
701
/**
710
702
* Perform operations on lists of documents in the collection.
711
703
*
704
+ * @param prefixKey - Prefix key for list selector.
712
705
* @param fn - Callback function.
713
706
* @param options - List options, optional.
714
707
* @returns Promise that resolves to object with iterator cursor.
715
708
*/
716
- protected async handleMany (
717
- fn : ( doc : Document < T1 > ) => unknown ,
718
- options ?: ListOptions < T1 > ,
709
+ protected async handleMany < const T > (
710
+ prefixKey : KvKey ,
711
+ fn : ( doc : Document < T1 > ) => T ,
712
+ options : ListOptions < T1 > | undefined ,
719
713
) {
720
- // Create list iterator with given options, initiate documents list
721
- const selector = createListSelector ( this . _keys . idKey , options )
714
+ // Create list iterator with given options
715
+ const selector = createListSelector ( prefixKey , options )
722
716
const iter = this . kv . list < T1 > ( selector , options )
717
+
718
+ // Initiate lists
723
719
const docs : Document < T1 > [ ] = [ ]
720
+ const result : Awaited < T > [ ] = [ ]
721
+ const errors : unknown [ ] = [ ]
724
722
725
723
// Loop over each document entry
726
724
for await ( const { key, value, versionstamp } of iter ) {
@@ -744,10 +742,23 @@ export class Collection<
744
742
}
745
743
746
744
// Execute callback function for each document
747
- await allFulfilled ( docs . map ( ( doc ) => fn ( doc ) ) )
745
+ await allFulfilled ( docs . map ( async ( doc ) => {
746
+ try {
747
+ const res = await fn ( doc )
748
+ result . push ( res )
749
+ } catch ( e ) {
750
+ errors . push ( e )
751
+ }
752
+ } ) )
748
753
749
- // Return current iterator cursor
754
+ // Throw any caught errors
755
+ if ( errors . length > 0 ) {
756
+ throw errors
757
+ }
758
+
759
+ // Return result and current iterator cursor
750
760
return {
761
+ result,
751
762
cursor : iter . cursor || undefined ,
752
763
}
753
764
}
0 commit comments