不要怂,就是干,撸起袖子干!

Commit b33d78eb by JacobLey Committed by GitHub

fix(typings): fix `ignoreDuplicates` option (#13220)

1 parent 6b0b532a
Showing with 88 additions and 11 deletions
...@@ -668,9 +668,14 @@ export interface CreateOptions<TAttributes = any> extends BuildOptions, Logging, ...@@ -668,9 +668,14 @@ export interface CreateOptions<TAttributes = any> extends BuildOptions, Logging,
fields?: (keyof TAttributes)[]; fields?: (keyof TAttributes)[];
/** /**
* On Duplicate * dialect specific ON CONFLICT DO NOTHING / INSERT IGNORE
*/ */
onDuplicate?: string; ignoreDuplicates?: boolean;
/**
* Return the affected rows (only for postgres)
*/
returning?: boolean | (keyof TAttributes)[];
/** /**
* If false, validations won't be run. * If false, validations won't be run.
...@@ -749,7 +754,7 @@ export interface BulkCreateOptions<TAttributes = any> extends Logging, Transacti ...@@ -749,7 +754,7 @@ export interface BulkCreateOptions<TAttributes = any> extends Logging, Transacti
individualHooks?: boolean; individualHooks?: boolean;
/** /**
* Ignore duplicate values for primary keys? (not supported by postgres) * Ignore duplicate values for primary keys?
* *
* @default false * @default false
*/ */
...@@ -1967,16 +1972,14 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut ...@@ -1967,16 +1972,14 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut
/** /**
* Builds a new model instance and calls save on it. * Builds a new model instance and calls save on it.
*/ */
public static create<M extends Model>( public static create<
M extends Model,
O extends CreateOptions<M['_attributes']> = CreateOptions<M['_attributes']>
>(
this: ModelStatic<M>, this: ModelStatic<M>,
values?: M['_creationAttributes'], values?: M['_creationAttributes'],
options?: CreateOptions<M['_attributes']> options?: O
): Promise<M>; ): Promise<O extends { returning: false } | { ignoreDuplicates: true } ? void : M>;
public static create<M extends Model>(
this: ModelStatic<M>,
values: M['_creationAttributes'],
options: CreateOptions<M['_attributes']> & { returning: false }
): Promise<void>;
/** /**
* Find a row that matches the query, or build (but don't save) the row if none is found. * Find a row that matches the query, or build (but don't save) the row if none is found.
......
import { expectTypeOf } from 'expect-type'
import { User } from './models/User';
async () => {
const user = await User.create({
id: 123,
firstName: '<first-name>',
}, {
ignoreDuplicates: false,
returning: true,
});
expectTypeOf(user).toEqualTypeOf<User>()
const voidUsers = await Promise.all([
User.create({
id: 123,
firstName: '<first-name>',
}, {
ignoreDuplicates: true,
returning: false,
}),
User.create({
id: 123,
firstName: '<first-name>',
}, {
ignoreDuplicates: true,
returning: true,
}),
User.create({
id: 123,
firstName: '<first-name>',
}, {
ignoreDuplicates: false,
returning: false,
}),
User.create({
id: 123,
firstName: '<first-name>',
}, { returning: false }),
User.create({
id: 123,
firstName: '<first-name>',
}, { ignoreDuplicates: true }),
]);
expectTypeOf(voidUsers).toEqualTypeOf<[void, void, void, void, void]>()
const emptyUsers = await Promise.all([
User.create(),
User.create(undefined),
User.create(undefined, undefined),
]);
expectTypeOf(emptyUsers).toEqualTypeOf<[User, User, User]>()
const partialUser = await User.create({
id: 123,
firstName: '<first-name>',
lastName: '<last-name>',
}, {
fields: ['firstName'],
returning: ['id'],
});
expectTypeOf(partialUser).toEqualTypeOf<User>()
// @ts-expect-error missing attribute
await User.create({
id: 123,
});
await User.create({
id: 123,
firstName: '<first-name>',
// @ts-expect-error unknown attribute
unknown: '<unknown>',
});
};
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!