Skip to content

Commit

Permalink
Merge pull request #29 from Quramy/rename_init
Browse files Browse the repository at this point in the history
feat: Rename initialze option parameters
  • Loading branch information
Quramy committed Nov 23, 2022
2 parents 4b6f6bf + 8be973e commit 5fe76e5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { PrismaClient } from "@prisma/client";
import { initialize, defineUserFactory } from "./__generated__/fabbrica";

const prisma = new PrismaClient();
initialize({ client: prisma });
initialize({ prisma });

async function seed() {
const UserFactory = defineUserFactory();
Expand Down
6 changes: 5 additions & 1 deletion packages/prisma-fabbrica/src/clientHolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ export type PrismaClientLike = {
$disconnect: () => PromiseLike<unknown>;
};

let _client: () => PrismaClientLike;
let _client: undefined | (() => PrismaClientLike);

export function resetClient() {
_client = undefined;
}

export function setClient<T extends PrismaClientLike>(client: T | (() => T)) {
_client = typeof client === "function" ? client : () => client;
Expand Down
10 changes: 7 additions & 3 deletions packages/prisma-fabbrica/src/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { setClient, PrismaClientLike } from "./clientHolder";
import { resetClient, setClient, PrismaClientLike } from "./clientHolder";

export type InitializeOptions = {
readonly client: PrismaClientLike | (() => PrismaClientLike);
readonly prisma: PrismaClientLike | (() => PrismaClientLike);
};

export function reset() {
resetClient();
}

export function initialize(options: InitializeOptions) {
setClient(options.client);
setClient(options.prisma);
}
2 changes: 1 addition & 1 deletion packages/prisma-fabbrica/src/scripts/jest-prisma/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initialize } from "../../initialize";
beforeAll(() => {
if (typeof jestPrisma === "object") {
initialize({
client: () => jestPrisma.client,
prisma: () => jestPrisma.client,
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-compile-testing/fixtures/simple-model/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client";
import { initialize, defineUserFactory } from "./__generated__/fabbrica";

const prisma = new PrismaClient();
initialize({ client: prisma });
initialize({ prisma });

async function seed() {
const UserFactory = defineUserFactory();
Expand Down

0 comments on commit 5fe76e5

Please sign in to comment.