11'use strict' ;
22
33import { mapBulk , getDocDescriptor } from '../utils/index' ;
4- import { ElasticsearchServiceParams } from '../types' ;
4+ import { ElasticsearchServiceParams , ElasticAdapterInterface } from '../types' ;
55import { getBulk } from './get-bulk' ;
66
7- function getBulkCreateParams ( service : any , data : any , params : ElasticsearchServiceParams ) {
7+ function getBulkCreateParams (
8+ service : ElasticAdapterInterface ,
9+ data : Record < string , unknown > [ ] ,
10+ params : ElasticsearchServiceParams
11+ ) {
812 const { filters } = service . filterQuery ( params ) ;
913 const index = filters ?. $index || service . index ;
1014
1115 return Object . assign (
1216 {
1317 index,
14- body : data . reduce ( ( result : any , item : any ) => {
18+ body : data . reduce ( ( result : Array < Record < string , unknown > > , item : Record < string , unknown > ) => {
1519 const { id, parent, routing, join, doc } = getDocDescriptor ( service , item ) ;
1620 const method = id !== undefined && ! params . upsert ? 'create' : 'index' ;
1721
1822 if ( join ) {
19- doc [ service . join ] = {
23+ ; ( doc as Record < string , unknown > ) [ service . join as string ] = {
2024 name : join ,
2125 parent
2226 } ;
2327 }
2428
25- const op : any = { [ method ] : { _index : index , _id : id } } ;
29+ const op : Record < string , Record < string , unknown > > = { [ method ] : { _index : index as string , _id : id } } ;
2630 if ( routing ) {
2731 op [ method ] . routing = routing ;
2832 }
@@ -37,47 +41,59 @@ function getBulkCreateParams(service: any, data: any, params: ElasticsearchServi
3741 ) ;
3842}
3943
40- export function createBulk ( service : any , data : any , params : ElasticsearchServiceParams ) {
44+ export function createBulk (
45+ service : ElasticAdapterInterface ,
46+ data : Record < string , unknown > [ ] ,
47+ params : ElasticsearchServiceParams
48+ ) {
4149 const bulkCreateParams = getBulkCreateParams ( service , data , params ) ;
4250
43- return service . Model . bulk ( bulkCreateParams ) . then ( ( results : any ) => {
44- const created = mapBulk ( results . items , service . id , service . meta , service . join ) ;
45- // We are fetching only items which have been correctly created.
46- const docs = created
47- . map ( ( item , index ) =>
48- Object . assign (
49- {
50- [ service . routing ] : data [ index ] [ service . routing ] || data [ index ] [ service . parent ]
51- } ,
52- item
51+ return service . Model . bulk ( bulkCreateParams as never ) . then (
52+ ( results : { items : Array < Record < string , unknown > > } ) => {
53+ const created = mapBulk ( results . items , service . id , service . meta , service . join ) ;
54+ // We are fetching only items which have been correctly created.
55+ const docs = created
56+ . map ( ( item , index ) =>
57+ Object . assign (
58+ {
59+ [ service . routing as string ] :
60+ ( data [ index ] as Record < string , unknown > ) [ service . routing as string ] ||
61+ ( data [ index ] as Record < string , unknown > ) [ service . parent as string ]
62+ } ,
63+ item
64+ )
5365 )
54- )
55- . filter ( ( item ) => item [ service . meta ] . status === 201 )
56- . map ( ( item ) => ( {
57- _id : item [ service . meta ] . _id ,
58- routing : item [ service . routing ]
59- } ) ) ;
66+ . filter (
67+ ( item ) => ( item as Record < string , Record < string , unknown > > ) [ service . meta as string ] . status === 201
68+ )
69+ . map ( ( item ) => ( {
70+ _id : ( item as Record < string , Record < string , unknown > > ) [ service . meta as string ] . _id ,
71+ routing : ( item as Record < string , unknown > ) [ service . routing as string ]
72+ } ) ) ;
6073
61- if ( ! docs . length ) {
62- return created ;
63- }
74+ if ( ! docs . length ) {
75+ return created ;
76+ }
6477
65- return getBulk ( service , docs , params ) . then ( ( fetched : any ) => {
66- let fetchedIndex = 0 ;
78+ return getBulk ( service , docs , params ) . then ( ( fetched : unknown [ ] ) => {
79+ let fetchedIndex = 0 ;
6780
68- // We need to return responses for all items, either success or failure,
69- // in the same order as the request.
70- return created . map ( ( createdItem ) => {
71- if ( ( createdItem as any ) [ service . meta ] . status === 201 ) {
72- const fetchedItem = fetched [ fetchedIndex ] ;
81+ // We need to return responses for all items, either success or failure,
82+ // in the same order as the request.
83+ return created . map ( ( createdItem ) => {
84+ if (
85+ ( createdItem as Record < string , Record < string , unknown > > ) [ service . meta as string ] . status === 201
86+ ) {
87+ const fetchedItem = fetched [ fetchedIndex ] ;
7388
74- fetchedIndex += 1 ;
89+ fetchedIndex += 1 ;
7590
76- return fetchedItem ;
77- }
91+ return fetchedItem ;
92+ }
7893
79- return createdItem ;
94+ return createdItem ;
95+ } ) ;
8096 } ) ;
81- } ) ;
82- } ) ;
97+ }
98+ ) ;
8399}
0 commit comments