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

Feature request: defaultData that depends on input #382

Open
EvHaus opened this issue Sep 1, 2024 · 0 comments
Open

Feature request: defaultData that depends on input #382

EvHaus opened this issue Sep 1, 2024 · 0 comments

Comments

@EvHaus
Copy link
Contributor

EvHaus commented Sep 1, 2024

Describe the feature

Some of my Prisma models have unique indexes which require conditional defaults based on the input. It seems like the defaultData function in prisma-fabbrica doesn't provide any info about the given input.

Ideally I'd like to do something like this:

const UserFactory = defineUserFactory({
	defaultData: async ({input}) => {
		// This is the feature I want. To be able to set different defaults
		// based on what the given input is.
		if (input.type === 'admin') return { permissions: '*' };
		return { permissions: null }
	}
});

With that factory defined, I could then do this:

const regularUser = await UserFactory.create({type: 'normal'});
// {id: '123', permissions: null, type: 'normal'};

const adminUser = await UserFactory.create({type: 'admin'});
// {id: '123', permissions: '*', type: 'normal'};

Is something like this possible with prisma-fabbrica today?

I thought maybe Transient Fields would do this, but this limitation:

You can't use model field names defined in your schema.prisma as transient parameters because they're not passed to prisma.user.create method.

means that it doesn't work in my case.

@EvHaus EvHaus changed the title defaultData that depends on input Feature request: defaultData that depends on input Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant