14
14
15
15
import { normalizeSort } from './utils' ;
16
16
import { FindCursor } from '@/src/data-api/cursor' ;
17
- import { Db , SomeDoc , SomeId } from '@/src/data-api' ;
17
+ import { DataAPIDetailedErrorDescriptor , Db , SomeDoc , SomeId } from '@/src/data-api' ;
18
18
import {
19
19
BulkWriteError ,
20
20
CollectionNotFoundError ,
21
21
DataAPIResponseError ,
22
- DeleteManyError ,
23
- InsertManyError ,
22
+ DeleteManyError , InsertManyError ,
24
23
mkRespErrorFromResponse ,
25
24
mkRespErrorFromResponses ,
26
25
TooManyDocumentsToCountError ,
@@ -73,7 +72,7 @@ import { FindOneAndReplaceCommand } from '@/src/data-api/types/find/find-one-rep
73
72
import { DeleteOneCommand } from '@/src/data-api/types/delete/delete-one' ;
74
73
import { FindOneAndDeleteCommand } from '@/src/data-api/types/find/find-one-delete' ;
75
74
import { FindOneAndUpdateCommand } from '@/src/data-api/types/find/find-one-update' ;
76
- import { InsertManyCommand } from '@/src/data-api/types/insert/insert-many' ;
75
+ import { InsertManyCommand , InsertManyDocumentResponse } from '@/src/data-api/types/insert/insert-many' ;
77
76
import { Mutable } from '@/src/data-api/types/utils' ;
78
77
import { CollectionSpawnOptions } from '@/src/data-api/types/collections/spawn-collection' ;
79
78
@@ -474,7 +473,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
474
473
commonResult . matchedCount += desc . rawResponse . status ?. matchedCount ?? 0 ;
475
474
commonResult . upsertedCount = desc . rawResponse . status ?. upsertedCount ?? 0 ;
476
475
477
- throw mkRespErrorFromResponse ( UpdateManyError , command , desc . rawResponse , commonResult ) ;
476
+ throw mkRespErrorFromResponse ( UpdateManyError , command , desc . rawResponse , { partialResult : commonResult } ) ;
478
477
}
479
478
480
479
return ( resp . status ?. upsertedId )
@@ -677,8 +676,14 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
677
676
if ( ! ( e instanceof DataAPIResponseError ) ) {
678
677
throw e ;
679
678
}
679
+
680
680
const desc = e . detailedErrorDescriptors [ 0 ] ;
681
- throw mkRespErrorFromResponse ( DeleteManyError , command , desc . rawResponse , { deletedCount : numDeleted + ( desc . rawResponse . status ?. deletedCount ?? 0 ) } )
681
+
682
+ throw mkRespErrorFromResponse ( DeleteManyError , command , desc . rawResponse , {
683
+ partialResult : {
684
+ deletedCount : numDeleted + ( desc . rawResponse . status ?. deletedCount ?? 0 ) ,
685
+ } ,
686
+ } ) ;
682
687
}
683
688
684
689
return {
@@ -1516,35 +1521,37 @@ const coalesceVectorSpecialsIntoSort = <T extends OptionsWithSort>(options: T |
1516
1521
1517
1522
// -- Insert Many ------------------------------------------------------------------------------------------
1518
1523
1519
- const insertManyOrdered = async < Schema > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , chunkSize : number , timeoutManager : TimeoutManager ) : Promise < IdOf < Schema > [ ] > => {
1524
+ const insertManyOrdered = async < Schema extends SomeDoc > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , chunkSize : number , timeoutManager : TimeoutManager ) : Promise < IdOf < Schema > [ ] > => {
1520
1525
const insertedIds : IdOf < Schema > [ ] = [ ] ;
1521
1526
1522
1527
for ( let i = 0 , n = documents . length ; i < n ; i += chunkSize ) {
1523
1528
const slice = documents . slice ( i , i + chunkSize ) ;
1524
1529
1525
- try {
1526
- const inserted = await insertMany < Schema > ( httpClient , slice , true , timeoutManager ) ;
1527
- insertedIds . push ( ...inserted ) ;
1528
- } catch ( e ) {
1529
- if ( ! ( e instanceof DataAPIResponseError ) ) {
1530
- throw e ;
1531
- }
1532
- const desc = e . detailedErrorDescriptors [ 0 ] ;
1530
+ const [ docResp , inserted , errDesc ] = await insertMany < Schema > ( httpClient , slice , true , timeoutManager ) ;
1531
+ insertedIds . push ( ...inserted ) ;
1533
1532
1534
- insertedIds . push ( ...desc . rawResponse . status ?. insertedIds ?? [ ] ) ;
1535
- throw mkRespErrorFromResponse ( InsertManyError , desc . command , desc . rawResponse , { insertedIds : insertedIds as SomeId [ ] , insertedCount : insertedIds . length } )
1533
+ if ( errDesc ) {
1534
+ throw mkRespErrorFromResponse ( InsertManyError , errDesc . command , errDesc . rawResponse , {
1535
+ partialResult : {
1536
+ insertedIds : insertedIds as SomeId [ ] ,
1537
+ insertedCount : insertedIds . length ,
1538
+ } ,
1539
+ documentResponses : docResp ,
1540
+ failedCount : docResp . length - insertedIds . length ,
1541
+ } ) ;
1536
1542
}
1537
1543
}
1538
1544
1539
1545
return insertedIds ;
1540
1546
}
1541
1547
1542
- const insertManyUnordered = async < Schema > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , concurrency : number , chunkSize : number , timeoutManager : TimeoutManager ) : Promise < IdOf < Schema > [ ] > => {
1548
+ const insertManyUnordered = async < Schema extends SomeDoc > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , concurrency : number , chunkSize : number , timeoutManager : TimeoutManager ) : Promise < IdOf < Schema > [ ] > => {
1543
1549
const insertedIds : IdOf < Schema > [ ] = [ ] ;
1544
1550
let masterIndex = 0 ;
1545
1551
1546
1552
const failCommands = [ ] as Record < string , any > [ ] ;
1547
1553
const failRaw = [ ] as Record < string , any > [ ] ;
1554
+ const docResps = [ ] as InsertManyDocumentResponse < SomeDoc > [ ] ;
1548
1555
1549
1556
const workers = Array . from ( { length : concurrency } , async ( ) => {
1550
1557
while ( masterIndex < documents . length ) {
@@ -1558,42 +1565,72 @@ const insertManyUnordered = async <Schema>(httpClient: DataAPIHttpClient, docume
1558
1565
1559
1566
const slice = documents . slice ( localI , endIdx ) ;
1560
1567
1561
- try {
1562
- const inserted = await insertMany < Schema > ( httpClient , slice , false , timeoutManager ) ;
1563
- insertedIds . push ( ...inserted ) ;
1564
- } catch ( e ) {
1565
- if ( ! ( e instanceof DataAPIResponseError ) ) {
1566
- throw e ;
1567
- }
1568
- const desc = e . detailedErrorDescriptors [ 0 ] ;
1569
-
1570
- const justInserted = desc . rawResponse . status ?. insertedIds ?? [ ] ;
1571
- insertedIds . push ( ...justInserted ) ;
1568
+ const [ docResp , inserted , errDesc ] = await insertMany < Schema > ( httpClient , slice , false , timeoutManager ) ;
1569
+ insertedIds . push ( ...inserted ) ;
1570
+ docResps . push ( ...docResp ) ;
1572
1571
1573
- failCommands . push ( desc . command ) ;
1574
- failRaw . push ( desc . rawResponse ) ;
1572
+ if ( errDesc ) {
1573
+ failCommands . push ( errDesc . command ) ;
1574
+ failRaw . push ( errDesc . rawResponse ) ;
1575
1575
}
1576
1576
}
1577
1577
} ) ;
1578
1578
await Promise . all ( workers ) ;
1579
1579
1580
1580
if ( failCommands . length > 0 ) {
1581
- throw mkRespErrorFromResponses ( InsertManyError , failCommands , failRaw , { insertedIds : insertedIds as SomeId [ ] , insertedCount : insertedIds . length } ) ;
1581
+ throw mkRespErrorFromResponses ( InsertManyError , failCommands , failRaw , {
1582
+ partialResult : {
1583
+ insertedIds : insertedIds as SomeId [ ] ,
1584
+ insertedCount : insertedIds . length ,
1585
+ } ,
1586
+ documentResponses : docResps ,
1587
+ failedCount : docResps . length - insertedIds . length ,
1588
+ } ) ;
1582
1589
}
1583
1590
1584
1591
return insertedIds ;
1585
1592
}
1586
1593
1587
- const insertMany = async < Schema > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , ordered : boolean , timeoutManager : TimeoutManager ) : Promise < IdOf < Schema > [ ] > => {
1594
+ const insertMany = async < Schema extends SomeDoc > ( httpClient : DataAPIHttpClient , documents : unknown [ ] , ordered : boolean , timeoutManager : TimeoutManager ) : Promise < [ InsertManyDocumentResponse < Schema > [ ] , IdOf < Schema > [ ] , DataAPIDetailedErrorDescriptor | undefined ] > => {
1588
1595
const command : InsertManyCommand = {
1589
1596
insertMany : {
1590
1597
documents,
1591
- options : { ordered } ,
1598
+ options : {
1599
+ returnDocumentResponses : true ,
1600
+ ordered,
1601
+ } ,
1592
1602
}
1593
1603
}
1594
1604
1595
- const resp = await httpClient . executeCommand ( command , { timeoutManager } ) ;
1596
- return resp . status ?. insertedIds ?? [ ] ;
1605
+ let resp , err : DataAPIResponseError | undefined ;
1606
+
1607
+ try {
1608
+ resp = await httpClient . executeCommand ( command , { timeoutManager } ) ;
1609
+ } catch ( e ) {
1610
+ if ( ! ( e instanceof DataAPIResponseError ) ) {
1611
+ throw e ;
1612
+ }
1613
+ resp = e . detailedErrorDescriptors [ 0 ] . rawResponse ;
1614
+ err = e ;
1615
+ }
1616
+
1617
+ const documentResponses = resp . status ! . documentResponses ;
1618
+ const errors = resp . errors ! ;
1619
+
1620
+ const insertedIds = [ ] ;
1621
+
1622
+ for ( let i = 0 , n = documentResponses . length ; i < n ; i ++ ) {
1623
+ const resp = documentResponses [ i ] ;
1624
+
1625
+ if ( resp . status === "OK" ) {
1626
+ insertedIds . push ( resp . _id ) ;
1627
+ } else if ( resp . errorIdx ) {
1628
+ resp . error = errors [ resp . errorIdx ] ;
1629
+ delete resp . errorIdx ;
1630
+ }
1631
+ }
1632
+
1633
+ return [ documentResponses , insertedIds , err ?. detailedErrorDescriptors [ 0 ] ] ;
1597
1634
}
1598
1635
1599
1636
// -- Bulk Write ------------------------------------------------------------------------------------------
@@ -1616,7 +1653,7 @@ const bulkWriteOrdered = async <Schema extends SomeDoc>(httpClient: DataAPIHttpC
1616
1653
addToBulkWriteResult ( results , desc . rawResponse . status , i )
1617
1654
}
1618
1655
1619
- throw mkRespErrorFromResponse ( BulkWriteError , desc . command , desc . rawResponse , results ) ;
1656
+ throw mkRespErrorFromResponse ( BulkWriteError , desc . command , desc . rawResponse , { partialResult : results } ) ;
1620
1657
}
1621
1658
1622
1659
return results ;
@@ -1654,7 +1691,7 @@ const bulkWriteUnordered = async <Schema extends SomeDoc>(httpClient: DataAPIHtt
1654
1691
await Promise . all ( workers ) ;
1655
1692
1656
1693
if ( failCommands . length > 0 ) {
1657
- throw mkRespErrorFromResponses ( BulkWriteError , failCommands , failRaw , results ) ;
1694
+ throw mkRespErrorFromResponses ( BulkWriteError , failCommands , failRaw , { partialResult : results } ) ;
1658
1695
}
1659
1696
1660
1697
return results ;
0 commit comments