data icon indicating copy to clipboard operation
data copied to clipboard

Can we store a literal object to an entity's field?

Open StanleySathler opened this issue 2 years ago • 8 comments

Great work guys! 👏

One of my fields should represent a JSON column supported by some DBMS. They're able to store any valid JSON object.

My model:

factory({
  timelineItemEffectMetadata: {
    id: primaryKey(v4),
    settings: Object,
    effectId: String,
  },
})

Then:

const effect = db.timelineItemEffectMetadata.create({
  effectId: 'string',
  settings: { intensity: 1 },
});

For some reason settings is always an empty object when I retrieve it.

StanleySathler avatar Feb 02 '23 19:02 StanleySathler

A simple proposal: https://github.com/mswjs/data/pull/263

StanleySathler avatar Feb 02 '23 20:02 StanleySathler

Another way is to support:

factory({
  timelineItemEffectMetadata: {
    id: primaryKey(v4),
    settings: (initialValue) => Object(initialValue), // note it now gets the initialValue param
  },
})

Which gives us extra flexibility.

StanleySathler avatar Feb 02 '23 20:02 StanleySathler

Hey, @StanleySathler.

Nested objects are supported but you have to describe those objects as well, meaning let Data know which properties you expect them to have and their respective value types.

import { nullable } from '@mswjs/data'

const db = factory({
  timelineItemEffectMetadata: {
    settings: nullable({
      intensity: Number
    })
  }
})

I assume here that .settings is nullable and not always present so I'm marking it with nullable(). This way the .create() method of the model shouldn't populate the "settings" unless you provide an explicit initial value.

Supporting Object as model value type

I think this may be a good idea. I wonder how well TS will help us here, and whether we can guard against false-positives as many things in JavaScript land are objects. I do recall us having something like isObject() that we can utilize for that check.

kettanaito avatar Feb 03 '23 11:02 kettanaito

To give you a little context, I've been rewriting Data from scratch for about a year now (not because it's super complex but simply because I have very little time to dedicate to this library). I've addressed a lot of issues in the new version by accounting for various scenarios from the conception phase. Seeing how active you are with your feedback, would you be interested in joining forced with me and finally shipping that new Data that's going to absolutely blow everybody away?

@StanleySathler, let me know!

kettanaito avatar Feb 03 '23 11:02 kettanaito

Hey @kettanaito 👋 Thanks for replying.

Sorry for the late response. I'm fully active again.

Nested objects are supported but you have to describe those objects as well

In my case, the problem is that the object doesn't have a known structure. It's a SQL JSON column, which means it can store any valid object.

[...] would you be interested in joining forced with me [...]

Sure thing! I mean, we've been adopting mswjs/data in some of our repos at work, so would be fair if I could help somehow. Let me know how we can make this happen.

StanleySathler avatar Mar 31 '23 22:03 StanleySathler

[...] I've been rewriting Data from scratch for about a year now

So I assume we should drop #263 in favor of this rewrite?

StanleySathler avatar Mar 31 '23 22:03 StanleySathler

@StanleySathler, we can still follow through with your proposal. The rewrite I mentioned is not landing any time soon, I've got plenty of work to do in other areas around MSW. Meanwhile, many would benefit from plain Object support in the schemas.

I think I left a few comments last time I reviewed your pull request. Let me know what is the state of it, I will do my best to help you ship this feature.

kettanaito avatar Apr 11 '23 13:04 kettanaito

@StanleySathler @kettanaito Hi all, any updates about this, we really need this feature and we are doing dark magic to make it work but it would be great if the library could handle it!

MarcLopezAvila avatar Sep 08 '23 07:09 MarcLopezAvila