feat(factory): add built-in persistence adapters (Prisma, Mongoose, TypeORM, custom)
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
Related Issues: closes #45
@Goldziher
I used it for the test before, I was kind of used to it. I will remove it
@Goldziher I have remove unused dependencies and unused devdependencies, including the one i met there 🙃 I also ran all test, nothing is broken
@Goldziher, i think we are good to go now, all check passed
@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
@Goldziher we are good to go please
@Goldziher, please check. Sorry for the late response, i was pretty busy
@simplicityf please rebase your PR on main, or merge main and resolve the conflicts. Main has substantially changed, so be careful.
@Goldziher
@Goldziher they are inside exampleas folder, check the last commits
ok, thanks. I'll merge the PR and re-work it a bit. I'll make you a reviewer.