Is it possible for one property in a model to re-use the value of another? #157
-
I'm writing some tests and would like to model a REST API out of my control. GET requests to that API return JSON objects like: {
"id": 534342,
"object": "review",
"url": "https://api.wanikani.com/v2/reviews/534342",
"data_updated_at": "2017-12-20T01:00:59.255427Z",
"data": {
"created_at": "2017-12-20T01:00:59.255427Z",
...
}
} The URL value always includes the ID value, which is redundant and not normalized but I'd like to model it nonetheless. Is there a reliable and idiomatic way for a factory to return two properties with the same value within both? I'd like to use the moral equivalent of const db = factory({
user: {
id: primaryKey(() => datatype.number({max: 999999})),
url: () => `https://api.wanikani.com/v2/reviews/{this.id}`, // doesn't work, of course
...
},
}) Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey, @wrex. Thank you for raising this! Currently, we don't have an API to allow referencing other properties of the same entity in the value getters. There's, however, a suggestion in #95. We would like to support this, so please share with us any ideas/suggestions on how you want this API to work in the said issue. Ideally, I'd like to see an API like this: factory({
user: {
id: primaryKey(...),
url: (entity) => entity.id
}
}) |
Beta Was this translation helpful? Give feedback.
Hey, @wrex. Thank you for raising this!
Currently, we don't have an API to allow referencing other properties of the same entity in the value getters. There's, however, a suggestion in #95. We would like to support this, so please share with us any ideas/suggestions on how you want this API to work in the said issue.
Ideally, I'd like to see an API like this: