Skip to content

v2.2.0

Latest
Compare
Choose a tag to compare
@Quramy Quramy released this 25 Jun 03:24
· 6 commits to main since this release

New Features

  • Transient fields (#326)
  • Callback functions (#254)
  • Improve createList signature (#335)

Transient fields

Transient fields allows to define arbitrary parameters to factories.

const UserFactory = defineUserFactory.withTransientFields({
  loginCount: 0,
})({
  defaultData: async ({ loginCount }) => ({
    /* Do something using `loginCount` parameter */
  }),
});

await UserFactory.create({
  name: "Bob", // `name` field is defined in scehma.primsma
  loginCount: 100, // `loginCount` field is NOT defined in schema but defined in factory
});

If you want more details, See docs

Callback functions

Allow to define callback functions when to define factories like this:

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

await UserFactory.create();

Available callback functions are onAfterBuild, onBeforeCreate, and onAfterCreate.

If you want more details, See docs

Improve createList signature

createList (or buildList) method in factory interface accepts the 2nd argument.
The 2nd argument can be an object assignable to Partial<Prisma.MODEL.CreateInput>.

// Insert 10 LoginHistory model records with the same `userId` field.
await LoginHistoryFactory.createList(10, { userId: "user001" });

Full Changelog: v2.1.5...v2.2.0