Replies: 4 comments 2 replies
-
Another thought from a recent Echobind team discussion: Could we use the prisma generator approach to automatically generate typesafe functions? For example, given the schema above, we would use a prisma generator to build a import { createUserFactory } from 'prisma-factories'
const chance = new Chance();
const DEFAULT_ATTRIBUTES = {
id: chance.guid(),
slug: chance.word(),
};
export const UserFactory = createUserFactory(DEFAULT_ATTRIBUTES); This feels like the best of both worlds and doesn't require the user to pass in the input type (since we can derive it)! |
Beta Was this translation helpful? Give feedback.
-
There is a list of community generators here for info |
Beta Was this translation helpful? Give feedback.
-
About generator specs, the |
Beta Was this translation helpful? Give feedback.
-
I created factory generator based on this idea. I made it in two days, so it's still lacking some features and documentation, but if anyone is interested in using it, I'd be happy to get feedback! |
Beta Was this translation helpful? Give feedback.
-
Throwing a few ideas out. The goal is a way to setup factories in a project that are automatically fully typed based on the Prisma types.
The createFactory definition
This is more manual, but it's also explicit and clear.
prisma-factory
would expose acreateFactory
function that returns an object withbuild
andcreate
functions.We can also return hooks, like
beforeCreate
that allows us to do things like hash a password.This is our easiest path, and one Echobind has already have implemented in projects with Prisma today.
Automatically Generate via a prisma generator
This would require no user-defined factories unless they want to override the attribute generators. This could make for a nice default, but users might not be clear where things come from.
Prisma docs on generators: https://github.com/prisma/specs/tree/master/generators
Use Field annotation to map functions
This would let the user override functions, but wouldn't handle some of the advanced cases we're considering (like
beforeCreate
hooks)Give it a 🤔 and comment!
Beta Was this translation helpful? Give feedback.
All reactions