InstanceDefaults and relationships for populated data
See original GitHub issueI would like to make use of the Relationships for populated data as described in the documentation. Unfortunately, I’ve been having trouble getting it to work. Nor do I really understand how it could work as shown in the documentation.
Actual behavior
While the model name for my service is recognized (gets set to null), no data is populated. After going through the code, I wasn’t sure how it would work as it is written in the documentation. My id fields are called “user_id” instead of “userId” as in the example, though I couldn’t find a reason for that not to work. The services own id fields are simply “id”. From what I understood of the feathers-vuex sourcecode (I’m somewhat new to javascript) seemed to indicate that the foreign id field should get the default value of the model. That got me further, but then the following code in model.js (lines 182-189) have me confused on their function.
const model = new Model(item)
const id = model[idField]_
_const storedModel = store.state[model.constructor.namespace].keyedById[id]_
…results in id being undefined and not doing anything on the last line.
By changing that line to:
const storedModel = store.state[model.constructor.namespace].keyedById**[item]**
…I actually managed to get it to populate the id field with the service data. I’m not too sure that is a real solution though. I’m not sure what a reference to another model looks like… So I was hoping someone could shed some light on what I’m doing wrong.
Expected behavior
I would like to add a property that refers to another model as shown in the documentation on relationships for populated data. So I’d like to keep the original “user_id” property and add a “user” property that references the actual user model. Currently a get an object in user_id that is a full representation of a user. Not sure how to check if it’s a reference or a copy…
Steps to reproduce
I think it’s hard to make a reproduction, with so many parts working together. But all the services work seperately, it’s just that I can’t get the populated data to work.
service('api/todos', {
instanceDefaults: {
user_id: 'User',
}
}),
Is my current service declaration. The ‘api/’ is an unfortunate choice made when this project was started. Could that have anything to do with it? It seems to be irrelevant on every other functionality I’ve seen.
System configuration
The services are KnexJs connecting to a PostgreSQL db.
Module versions (especially the part that’s not working): Feathers-vuex 1.4.8 Feathersjs 3.1.7 Feathers-knex: 3.3.0 Knex: 0.15.0
NodeJS version: v8.11.3
Operating System: Windows 10
Browser Version: Chrome: 68.0.3440.106
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
It’s much more efficient to populate on the server than it is to make all of the requests from the client. I’d recommend looking into the feathers-plus/cli and looking into the GraphQL features. They allow the client to specify to the server which records to populate with each query. It’s the future, and it’s here. 😃
Another way to populate data is now available. See docs for instanceDefaults, here: https://feathers-plus.github.io/v1/feathers-vuex/model-classes.html#instanceDefaults-Function-pre-release
It’s intended to do one-off requests to get related data for a single record.