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

Can we have models with multi-fields IDs?

StanleySathler opened this issue Β· comments

Thanks for the hard work - the tool is awesome πŸ‘

One of my entities has two fields to compose a multi-field ID, but I can't use:

export const db = factory({
  timelineItemEffectMetadata: {
    settings: {},
    effectId: primaryKey(String),
    timelineItemId: primaryKey(String),
  },
});

Any way to work around this?

Hey, @StanleySathler. Thanks for the kind words!

The current model API is designed to always have a single primary key. Marking multiple properties with primaryKey() will throw, and that is expected.

Note that you don't have to mark a property as a primary key to query by it. Primary keys are necessary for internal model lookup optimization and to guard against creating multiple entities with the same primary key.

If you wish to query by timelineItemId you can do that at any time:

db. timelineItemEffectMetadata.findFirst({
  where: {
    timelineItemId: { equals: 'abc-123' }
  }
})

Please let me know if I understood your intention wrong here.