Supports "createMany" model method
GitHub
- Closes #31
Changes
- [x] Adds
createManymodel method to create multiple random instances of the model. - [ ] Supports multiple randomly created entities that reference the same relational model (#34).
- [ ] Type annotates a
oneOfrelational property when listing theoptions.relationsincreateMany(cannot create an entity without all its relational models specified, sobooleanmakes no sense). - [ ] Supports creation of nested relational properties (user -> post -> category).
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.
@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.
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: '[email protected]' },
{ name: 'Alex', email: '[email protected]' },
])
- I don't think we need to nest initial values under the
dataproperty; - 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.
Hi, I just found this after having noticed a gap in there not being a createMany method, and creating https://github.com/mswjs/data/pull/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.
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?