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

Supports "createMany" model method #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Supports "createMany" model method #33

wants to merge 2 commits into from

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Dec 23, 2020

GitHub

Changes

  • Adds createMany model method to create multiple random instances of the model.
  • Supports multiple randomly created entities that reference the same relational model (Support unique/shared relational models #34).
  • Type annotates a oneOf relational property when listing the options.relations in createMany (cannot create an entity without all its relational models specified, so boolean makes no sense).
  • Supports creation of nested relational properties (user -> post -> category).

src/factory.ts Outdated Show resolved Hide resolved
Copy link
Sponsor Collaborator

@Aprillion Aprillion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add to README (model methods, but also in the Getting started section - I found it hard to get started without any data, getAll() is returning an empty array which is probably fine, but I kind of expected that factory() would populate some mock data automagically)

@kettanaito
Copy link
Member Author

Thanks, that's a good point. We need to mention that factory() just creates an API to create/query the data, but doesn't actually populate anything.

@tomaszgil
Copy link

@kettanaito we've recently integrated msw and @mswjs/data, thanks for all the contributions 🎉

Creating larger sets of models based on factories is something that we've been looking for - I also saw that prisma supports a similar case with prisma.user.createMany({ data: [...] }). Would be awesome to have something similar in the library.

Is this PR still being worked on? I saw some todos left, but there wasn't a lot of activity recently.

@kettanaito
Copy link
Member Author

Hey, @tomaszgil. Thank you for your kinds words! I'm excited that MSW and its ecosystem helps you achieve your goals 🎉

I'm not working on this feature currently. That being said, I still find this a crucial API to have, so if you're willing to help me out with this I'd be thankful.

I think some of the pre-requisites for createMany were implemented (like #34) but it may still need some discovery along the way. I haven't rebased this feature branch for some time, so it may make more sense to create a new one not to deal with the changes that have been introduced since it had been open (cherry-picking commits should work fine as we're adding a brand new feature here).

I like that Prisma reference! This library is inspired by Prisma a lot, and modeling our createMany API around it would also be a good direction to follow:

const users = db.user.createMany([
  { name: 'Sonali', email: 'sonali@prisma.io' },
  { name: 'Alex', email: 'alex@prisma.io' },
])
  • I don't think we need to nest initial values under the data property;
  • Each item in the initial values array is identical to calling db.user.create(initialValues) individually. This includes the optional properties, relations, and using value getters from the model definition to generate the values that were not provided upon creation.

@themagickoala
Copy link
Contributor

Hi, I just found this after having noticed a gap in there not being a createMany method, and creating #237 to address it. Feel free to close my PR if this is going to supercede it. Alternatively, if you like the route I've taken and would like me to adjust it to use an array format like Prisma, I'm happy to tweak it so this can get moved along.

@themagickoala
Copy link
Contributor

Hi, I just found this after having noticed a gap in there not being a createMany method, and creating #237 to address it. Feel free to close my PR if this is going to supercede it. Alternatively, if you like the route I've taken and would like me to adjust it to use an array format like Prisma, I'm happy to tweak it so this can get moved along.

Have updated #237 to reflect the prisma-like syntax. Perhaps your initial approach with this branch could be satisfied in future by a createList model method akin to FactoryBot's syntax?

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

Successfully merging this pull request may close these issues.

Support seeding the database
4 participants