Modelling Objects #193
-
|
I have a model instance in the form of: {
"things": [ "hey" ],
"stuff": {
"a16422d1": {
"foo": "bar"
}
}
}Anybody know how I would specify the Right now I have: export const db = factory({
modelName: {
things: Array
stuff: ???
},
})Maybe I need to convert it into an export const db = factory({
modelName: {
things: Array
stuff: Array
},
})Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hey, @mauriceoc. I assume the keys in the You can pass a function to model property to generate its value. import { factory } from '@mswjs/data'
export const db = factory({
modelName: {
things: Array,
stuff: () => {
const randomKey = generateKey()
return {
[randomKey]: {
foo: 'bar'
}
}
}
}
})I think currently you can't nest model definitions this way, so Would this achieve your goal? If not, please let me know more about it. |
Beta Was this translation helpful? Give feedback.
-
UpdateSince v1.0, Data no longer provides an object modeling syntax. Instead, it relies on a Standard Schema that you provide, using any Standard Schema-compliant libraries out there. For example, here's how you would describe your original factory using Zod: const users = new Collection({
schema: z.object({
things: z.array(z.string()),
// Use `z.record()` in Zod to describe an object
// with dynamic keys.
stuff: z.record(
z.string(),
z.object({
foo: z.string(),
}),
),
}),
})Data will respect this schema whenever you create/update/query properties. |
Beta Was this translation helpful? Give feedback.
Update
Since v1.0, Data no longer provides an object modeling syntax. Instead, it relies on a Standard Schema that you provide, using any Standard Schema-compliant libraries out there. For example, here's how you would describe your original factory using Zod:
Data will respect this schema whenever you create/update/query properties.