-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
bulk-create.ts
60 lines (54 loc) · 1.41 KB
/
bulk-create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
Model,
InferAttributes,
CreationOptional,
InferCreationAttributes,
} from 'sequelize';
import { sequelize } from './connection';
import type { MakeNullishOptional } from 'sequelize/types/utils';
class TestModel extends Model<
InferAttributes<TestModel>,
InferCreationAttributes<TestModel>
> {
declare id: CreationOptional<number>;
declare testString: CreationOptional<string | null>;
declare testEnum: CreationOptional<'d' | 'e' | 'f' | null>;
}
type wat = InferCreationAttributes<TestModel>;
sequelize.transaction(async trx => {
const newItems: Array<
MakeNullishOptional<InferCreationAttributes<TestModel>>
> = [
{
testEnum: 'e',
testString: 'abc',
},
{
testEnum: null,
testString: undefined,
},
];
const res1: Array<TestModel> = await TestModel.bulkCreate(newItems, {
benchmark: true,
fields: ['testEnum'],
hooks: true,
logging: true,
returning: true,
transaction: trx,
validate: true,
ignoreDuplicates: true,
});
const res2: Array<TestModel> = await TestModel.bulkCreate(newItems, {
benchmark: true,
fields: ['testEnum'],
hooks: true,
logging: true,
returning: false,
transaction: trx,
validate: true,
updateOnDuplicate: ['testEnum', 'testString'],
});
const res3: Array<TestModel> = await TestModel.bulkCreate(newItems, {
conflictAttributes: ['testEnum', 'testString'],
});
});