Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: next ver #285

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,7 @@ Prisma generator for model factories.
- [has-many / has-one relation](#has-many--has-one-relation)
- [Custom scalar field generation](#custom-scalar-field-generation)
- [Traits](#traits)
- [Callbacks](#callbacks)
- [Field value precedence](#field-value-precedence)
- [More examples](#more-examples)
- [Generator configuration](#generator-configuration)
Expand Down Expand Up @@ -375,6 +376,64 @@ Multiple traits are also available:
await UserFactory.use("someTrait", "anotherTrait").create();
```

### Callbacks

You can set callback function before or after factory execution.

```ts
const UserFactory = defineUserFactory({
onAfterCreate: async user => {
await PostFactory.craete({
author: { connect: uesr },
});
},
});

await UserFactory.create();
```

Callback functions are also available within trait definition.

```ts
const UserFactory = defineUserFactory({
traits: {
withComment: {
onAfterCreate: async user => {
await PostFactory.craete({
author: { connect: uesr },
});
},
},
},
});

await UserFactory.create();
await UserFactory.use("withComment").create();
```

Note: The above code is to explain the callback. If you want to create association, first consider to use `defaultData` and `trait.data` option as in [has-many / has-one relation](#has-many--has-one-relation).

The following three types are available as callback function:

```ts
const UserFactory = defineUserFactory({
onAfterBuild: async createInput => {
// do something
},
onBeforeCreate: async createInput => {
// do something
},
onAfterCreate: async createdData => {
// do something
},
});
```

And here, the parameter types are:

- `createInput` is assignable to model create function parameter (e.g. `Prsima.UserCreateInput`).
- `createdData` is resolved object by model create function (e.g. `User` model type)

### Field value precedence

Each field is determined in the following priority order(lower numbers have higher priority):
Expand Down
41 changes: 25 additions & 16 deletions examples/example-prj/src/__generated__/fabbrica/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 72 additions & 8 deletions examples/example-prj/src/__generated__/fabbrica/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions examples/example-prj/src/trait.test.ts
Expand Up @@ -2,12 +2,28 @@ import {
defineUserFactory,
definePostFactory,
defineCommentFactory,
CommentFactoryInterface,
type PostFactoryInterface,
type CommentFactoryInterface,
} from "./__generated__/fabbrica";

const prisma = jestPrisma.client;

export const UserFactory = defineUserFactory();
export const UserFactory = defineUserFactory({
traits: {
withSelfCommentedPost: {
onAfterCreate: async user => {
await getPostFactory().create({
author: {
connect: user,
},
comments: {
create: [await getCommentFactory().build({ author: { connect: user } })],
},
});
},
},
},
});

export const PostFactory = definePostFactory({
defaultData: {
Expand Down Expand Up @@ -43,6 +59,10 @@ export const CommentFactory = defineCommentFactory({
},
});

function getPostFactory(): PostFactoryInterface {
return PostFactory;
}

function getCommentFactory(): CommentFactoryInterface {
return CommentFactory;
}
Expand Down Expand Up @@ -77,5 +97,12 @@ describe("factories", () => {
await expect(prisma.comment.count({ where: { postId: post2.id } })).resolves.toBe(1);
});
});

describe("trait and callback", () => {
test("Execute other factory in callback", async () => {
const { id: userId } = await UserFactory.use("withSelfCommentedPost").create();
await expect(prisma.comment.count({ where: { authorId: userId } })).resolves.toBe(1);
});
});
});
});