mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Faker definition for relationship properties

SalahAdDin opened this issue · comments

It is very annoying to have to create a whole object just cause one of the properties, the relationship, is not filling by default:

  continueArticle: {
    id: primaryKey(() => _continueId++),
    title: () => faker.lorem.words(),
    slug: () => faker.helpers.slugify(title),
    publishedDate: () => faker.date.anytime(),
    category: oneOf("category"),
    thumbnail: oneOf("image"),
  },
export const createContinueArticle = (relatedArticle = {}) =>
db.continueArticle.create(relatedArticle);

image

Is there any away to make it getting any existing object to fill the relation?

Hi, @SalahAdDin. Not sure what you are trying to achieve.

The category is missing because you've never provided one in your example. You have to provide an initial value to the relation or update the entity with a relation for the value to appear.

db.continueArticle.create({
  category: db.category.create() // or reference an existing category.
})

Once you do, the value will be set correctly.

Hi, @SalahAdDin. I'm not sure what you are trying to achieve.

The category is missing because you've never provided one in your example. You must provide an initial value to the relation or update the entity concerning the value to appear.

db.continueArticle.create({
  category: db.category.create() // or reference an existing category.
})

Once you do, the value will be set correctly.

That's the issue, why isn't msw/data able to create the category entity by itself?