Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
StanleySathler opened this issue Feb 2, 2023 · 8 comments
Open

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

StanleySathler opened this issue Feb 2, 2023 · 8 comments

Comments

@StanleySathler
Copy link

StanleySathler commented Feb 2, 2023

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
Copy link
Author

StanleySathler commented Feb 2, 2023

A simple proposal: #263

@StanleySathler StanleySathler changed the title Can we store a literal object to an entity's field Can we store a literal object to an entity's field? Feb 2, 2023
@StanleySathler
Copy link
Author

StanleySathler commented Feb 2, 2023

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.

@kettanaito
Copy link
Member

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
Copy link
Member

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!

@StanleySathler
Copy link
Author

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
Copy link
Author

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

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

@kettanaito
Copy link
Member

@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.

@MarcLopezAvila
Copy link

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants