mswjs / data

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

Can we have models with multi-fields IDs? #260

Closed StanleySathler closed 1 year ago

StanleySathler commented 1 year ago

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?

kettanaito commented 1 year ago

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.