@@ -3,29 +3,42 @@ import * as Sentry from '@sentry/node'
3
3
4
4
import { redis } from '../util/redis'
5
5
import logger from '../util/logger'
6
+ import { PartialRecord } from '../types'
6
7
7
8
const logError = ( message : string , error : Error ) => {
8
9
logger . error ( `${ message } ${ error . name } , ${ error . message } ` )
9
10
10
11
Sentry . captureException ( error )
11
12
}
12
13
14
+ type AllowedBulkCreateOptionField =
15
+ | 'conflictAttributes'
16
+ | 'updateOnDuplicate'
17
+ | 'ignoreDuplicates'
18
+ type AllowedFallbackCreateOptionField =
19
+ | 'fields'
20
+ | 'conflictFields'
21
+
13
22
interface BulkCreateOptions {
14
23
entityName : string
15
24
bulkCreate : ( entities : object [ ] , options : any ) => Promise < any >
16
25
fallbackCreate : ( entity : object , options : any ) => Promise < any >
17
- options : Record < string , any >
26
+ bulkCreateOptions : PartialRecord < AllowedBulkCreateOptionField , any >
27
+ fallbackCreateOptions : PartialRecord < AllowedFallbackCreateOptionField , any >
18
28
entities : Record < string , any > [ ]
19
29
}
20
30
export const safeBulkCreate = async ( {
21
31
entityName,
22
32
bulkCreate,
23
33
fallbackCreate,
24
- options,
34
+ bulkCreateOptions,
35
+ fallbackCreateOptions,
25
36
entities,
26
37
} : BulkCreateOptions ) => {
27
38
try {
28
- const result = await bulkCreate ( entities , options )
39
+ const result = await bulkCreate ( entities , {
40
+ ...bulkCreateOptions ,
41
+ } )
29
42
return result
30
43
} catch ( bulkCreateError : any ) {
31
44
const result = [ ]
@@ -38,8 +51,7 @@ export const safeBulkCreate = async ({
38
51
try {
39
52
// eslint-disable-next-line no-await-in-loop
40
53
const res = await fallbackCreate ( entity , {
41
- ...options ,
42
- fields : options . updateOnDuplicate ,
54
+ ...fallbackCreateOptions ,
43
55
} )
44
56
result . push ( res )
45
57
} catch ( fallbackCreateError : any ) {
0 commit comments