Skip to content

Commit b33d78e

Browse files
authored
fix(typings): fix ignoreDuplicates option (#13220)
1 parent 6b0b532 commit b33d78e

2 files changed

Lines changed: 88 additions & 11 deletions

File tree

types/lib/model.d.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,14 @@ export interface CreateOptions<TAttributes = any> extends BuildOptions, Logging,
668668
fields?: (keyof TAttributes)[];
669669

670670
/**
671-
* On Duplicate
671+
* dialect specific ON CONFLICT DO NOTHING / INSERT IGNORE
672672
*/
673-
onDuplicate?: string;
673+
ignoreDuplicates?: boolean;
674+
675+
/**
676+
* Return the affected rows (only for postgres)
677+
*/
678+
returning?: boolean | (keyof TAttributes)[];
674679

675680
/**
676681
* If false, validations won't be run.
@@ -749,7 +754,7 @@ export interface BulkCreateOptions<TAttributes = any> extends Logging, Transacti
749754
individualHooks?: boolean;
750755

751756
/**
752-
* Ignore duplicate values for primary keys? (not supported by postgres)
757+
* Ignore duplicate values for primary keys?
753758
*
754759
* @default false
755760
*/
@@ -1967,16 +1972,14 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut
19671972
/**
19681973
* Builds a new model instance and calls save on it.
19691974
*/
1970-
public static create<M extends Model>(
1975+
public static create<
1976+
M extends Model,
1977+
O extends CreateOptions<M['_attributes']> = CreateOptions<M['_attributes']>
1978+
>(
19711979
this: ModelStatic<M>,
19721980
values?: M['_creationAttributes'],
1973-
options?: CreateOptions<M['_attributes']>
1974-
): Promise<M>;
1975-
public static create<M extends Model>(
1976-
this: ModelStatic<M>,
1977-
values: M['_creationAttributes'],
1978-
options: CreateOptions<M['_attributes']> & { returning: false }
1979-
): Promise<void>;
1981+
options?: O
1982+
): Promise<O extends { returning: false } | { ignoreDuplicates: true } ? void : M>;
19801983

19811984
/**
19821985
* Find a row that matches the query, or build (but don't save) the row if none is found.

types/test/create.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { expectTypeOf } from 'expect-type'
2+
import { User } from './models/User';
3+
4+
async () => {
5+
const user = await User.create({
6+
id: 123,
7+
firstName: '<first-name>',
8+
}, {
9+
ignoreDuplicates: false,
10+
returning: true,
11+
});
12+
expectTypeOf(user).toEqualTypeOf<User>()
13+
14+
const voidUsers = await Promise.all([
15+
User.create({
16+
id: 123,
17+
firstName: '<first-name>',
18+
}, {
19+
ignoreDuplicates: true,
20+
returning: false,
21+
}),
22+
User.create({
23+
id: 123,
24+
firstName: '<first-name>',
25+
}, {
26+
ignoreDuplicates: true,
27+
returning: true,
28+
}),
29+
User.create({
30+
id: 123,
31+
firstName: '<first-name>',
32+
}, {
33+
ignoreDuplicates: false,
34+
returning: false,
35+
}),
36+
User.create({
37+
id: 123,
38+
firstName: '<first-name>',
39+
}, { returning: false }),
40+
User.create({
41+
id: 123,
42+
firstName: '<first-name>',
43+
}, { ignoreDuplicates: true }),
44+
]);
45+
expectTypeOf(voidUsers).toEqualTypeOf<[void, void, void, void, void]>()
46+
47+
const emptyUsers = await Promise.all([
48+
User.create(),
49+
User.create(undefined),
50+
User.create(undefined, undefined),
51+
]);
52+
expectTypeOf(emptyUsers).toEqualTypeOf<[User, User, User]>()
53+
54+
const partialUser = await User.create({
55+
id: 123,
56+
firstName: '<first-name>',
57+
lastName: '<last-name>',
58+
}, {
59+
fields: ['firstName'],
60+
returning: ['id'],
61+
});
62+
expectTypeOf(partialUser).toEqualTypeOf<User>()
63+
64+
// @ts-expect-error missing attribute
65+
await User.create({
66+
id: 123,
67+
});
68+
await User.create({
69+
id: 123,
70+
firstName: '<first-name>',
71+
// @ts-expect-error unknown attribute
72+
unknown: '<unknown>',
73+
});
74+
};

0 commit comments

Comments
 (0)