prisma-factory
Version:
Generates factories for your prisma models
25 lines (22 loc) • 1.41 kB
TypeScript
declare type ObjectWithMaybeCallbacks<Type> = {
[Property in keyof Type]: Type[Property] extends keyof any ? Type[Property] | (() => Type[Property]) : ObjectWithMaybeCallbacks<Type[Property]> | (() => ObjectWithMaybeCallbacks<Type[Property]>);
};
interface CreateFactoryOptions {
client?: string;
}
interface CreateFactoryHooks<CreateInputType, ReturnModelType> {
beforeCreate?: (attrs: CreateInputType) => CreateInputType;
afterCreate?: (data: ReturnModelType) => ReturnModelType;
}
interface CreateFactoryReturn<CreateInputType, ReturnModelType> {
build: (attrs?: Partial<CreateInputType>) => CreateInputType;
create: (attrs?: Partial<CreateInputType>) => Promise<ReturnModelType>;
}
/**
* Creates a new Prisma Factory based on a provided model name and set of default attributes.
* @example
* const REQUIRED_ATTRIBUTES = { email: chance.email(), firstName: 'Dave', password: 'test1234' };
* createFactory<Prisma.UserCreateInput>('User', REQUIRED_ATTRIBUTES);
*/
declare function createFactory<CreateInputType, ReturnModelType>(modelName: string, defaultAttrs: ObjectWithMaybeCallbacks<CreateInputType>, options?: CreateFactoryOptions, hooks?: CreateFactoryHooks<CreateInputType, ReturnModelType>): CreateFactoryReturn<CreateInputType, ReturnModelType>;
export { CreateFactoryHooks, CreateFactoryOptions, CreateFactoryReturn, ObjectWithMaybeCallbacks, createFactory };