-
Notifications
You must be signed in to change notification settings - Fork 68
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does spraypaint support incremental loading? #59
Comments
You are correct - you cannot fetch incrementally and have to use I do it by adding a method on the Model to ensure the relationship is there: const Employee = ApplicationRecord.extend({
static: {
jsonapiType: 'employees',
},
attrs: {
name: attr(),
manager: hasOne(),
},
methods: {
async ensureManager() {
// Will throw if this.manager is unset, like when creating a new Employee()!
// Ensure the manager has id and return if there are any attributes
if (!this.manager.id || Object.keys(this.manager.attributes).length > 0) return;
this.manager = await Manager.find(this.manager.id).data;
},
},
}); So you do: const e = (await Employee.first()).data;
await e.ensureManager();
console.log(e.manager.name) You could batch create these helper methods, iterating over your relationships and creating a method for each, but it is quite abstract and less readable (too DRY): const rels = {
manager: hasOne(),
};
const Employee = ...
Object.keys(rels).forEach((key) => {
const klass = Employee.attributeList[key].klass;
Object.defineProperty(Employee.prototype, `ensure${key[0].toUpperCase()}${key.slice(1)}`, {
value: async function() {
if (!this[key].id || Object.keys(this[key].attributes).length > 0) return;
this[key] = (await klass.find(this[key].id)).data;
},
});
}); |
Thanks for the code. I realized later than having to |
I think it is not the |
AFAIK simply using |
Thanks for the good conversation here! I agree we should support eager AND lazy-loading, the latter with links. I previously wrote a (internal) python client that did this, where you could follow the links but also add additional parameters: const post = await Post.find(1)
// fetch 3 active comments via the link
await post.commentsLink().merge(Comment.where({ active: true }).per(3)).all()
// access as usual
post.comments // [<Comment>, <Comment>, ...] I would LOVE to see this in Spraypaint but it's not an immediate need for me so I'm not sure when I'd get to it. If anyone would like to PR I'd be happy to help and share some of the python code. |
+1 for lazy loading |
If I define a hasOne relationship like:
I want to be able to fetch an employee, and the later, optionally fetch the manger, e.g.
Spraypaint doesn't seem to support following links that JSON:API returns in the original request. The ability to load sub-records incrementally is one of the great promises of JSON:API, but it seems that in the current implementation I need to fetch all the data in my original request, using
include('manager')
.Am I missing how to do incremental loading?
The text was updated successfully, but these errors were encountered: