mswjs / data

Data modeling and relation library for testing JavaScript applications.
https://npm.im/@mswjs/data
MIT License
827 stars 52 forks source link

Support modeling of non-queryable data #161

Open kettanaito opened 3 years ago

kettanaito commented 3 years ago

Motivation

Not all the modeled data are meant to be queryable. In certain cases, it's required to only describe the objects and have an API to seed them on demand. The required primaryKey at the moment prevents such usage of the library.

API

I suggest adding a new public API designed for modeling objects in general, without querying capabilities. The factory could then reuse this new API internally, appending querying support.

import { model } from '@mswjs/data'

const formData = model({
  username: String,
  password: faker.random.password,
})

formData.create()
formData.create({ username: 'john.maverick' })
formData.create({ username: 'joe', password: 'secret+123' })

Specification

ericchernuka commented 3 years ago

I was thinking about this the other day and thought it would be a great addition. In the Ruby world with factory_bot they had a method called build which allowed for creating non-persisted entities. https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#build-strategies