interface-forge icon indicating copy to clipboard operation
interface-forge copied to clipboard

feat(factory): add built-in persistence adapters (Prisma, Mongoose, TypeORM, custom)

Open simplicityf opened this issue 10 months ago • 6 comments

Description:

This PR adds first-class support for persisting factory-generated objects via adapters. You can now wire up your Factory to any ORM or API client and call:

const UserFactory = new Factory<User>((f) => ({
  email: f.internet.email(),
  name:  f.person.fullName(),
}))
.persist({
  adapter: 'prisma',      // or 'mongoose' | 'typeorm' | custom adapter object
  model:   prisma.user,   // your Prisma Client model, Mongoose Model, or TypeORM repository
});

const user  = await UserFactory.create();      // builds + saves single record
const users = await UserFactory.createMany(5); // builds + saves batch

What Changed

  • PersistenceAdapter<T> interface with create(data: T): Promise<T> and createMany(data: T[]): Promise<T[]>.
  • MongooseAdapter, PrismaAdapter, TypeORMAdapter, PersistenceAdapter implementations.
  • Factory’s .persist() method to wire up the adapter.
  • .create() and .createMany() on Factory now build and persist.
  • Graceful error if you call create() before persist().

#Tests

  • Added specs for each adapter:
    • single & batch for Mongoose (model.create / insertMany)
    • single & batch for Prisma (model.create({data}) / createMany({data}))
    • single & batch for TypeORM (repository.save)
    • custom adapter shape
    • error if no adapter configured
  • All new tests passing
Screenshot 2025-06-08 at 8 41 08 PM

Related Issues: closes #45

simplicityf avatar Jun 08 '25 19:06 simplicityf

@Goldziher

simplicityf avatar Jun 08 '25 20:06 simplicityf

I used it for the test before, I was kind of used to it. I will remove it

simplicityf avatar Jun 09 '25 09:06 simplicityf

@Goldziher I have remove unused dependencies and unused devdependencies, including the one i met there 🙃 I also ran all test, nothing is broken

simplicityf avatar Jun 09 '25 09:06 simplicityf

@Goldziher, i think we are good to go now, all check passed

simplicityf avatar Jun 10 '25 00:06 simplicityf

@Goldziher, please review the code, if there will be any changes to make.

I am installing the dependencies, and my network is a bit bad. I will push once i install everything. But pending the time, if there is another changes i need to make.

Thanks boss

simplicityf avatar Jun 10 '25 17:06 simplicityf

@Goldziher we are good to go please

simplicityf avatar Jun 10 '25 23:06 simplicityf

@Goldziher, please check. Sorry for the late response, i was pretty busy

simplicityf avatar Jun 13 '25 07:06 simplicityf

@simplicityf please rebase your PR on main, or merge main and resolve the conflicts. Main has substantially changed, so be careful.

Goldziher avatar Jun 14 '25 10:06 Goldziher

@Goldziher

simplicityf avatar Jun 16 '25 08:06 simplicityf

@Goldziher they are inside exampleas folder, check the last commits

simplicityf avatar Jun 16 '25 10:06 simplicityf

ok, thanks. I'll merge the PR and re-work it a bit. I'll make you a reviewer.

Goldziher avatar Jun 17 '25 13:06 Goldziher