question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better support for nested models and collections

See original GitHub issue

At the moment the support for nested structures is limited. We’d like to improve this as it’s a common situation and should in theory work as you would expect. This issue is a place to collect all the things we’d like to achieve when we get to improving this support.

  • Errors should be set on the child, not with dot notation on the parent. (or both?)
  • Determine the difference between parent.saved('child.attr') and parent.child.saved('attr')
  • Provide a nice mechanism to convert nested objects to models

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:11
  • Comments:14

github_iconTop GitHub Comments

1reaction
Sarkecommented, Jun 30, 2019

I extended Model like this for a simple relation:

export default class extends Model {

    /**
     * Creates a new instance, called when using 'new'.
     *
     * @param  {Object}     [attributes]  Model attributes
     * @param  {Collection} [collection]  Collection that this model belongs to.
     * @param  {Object}     [options]     Options to set on the model.
     */
    constructor(attributes = {}, collection = null, options = {}) {
        super(attributes, collection, options);
        this._relations = {};
        this.init();
    }

    init() {
        ;
    }

    relation(relationName, findCallback) {
        if (typeof this._relations[relationName] === 'undefined') {
            let storeData = store.get(relationName);
            if (storeData.length === 0) {
                return null;
            }

            this._relations[relationName] = storeData.find(findCallback);
            if (this._relations[relationName] === undefined) {
                this._relations[relationName] = null;
            }
        }

        return this._relations[relationName];
    }
}

The init() is just there so you can more easily add things to the constructor. All I really need to do is add this._relations = {};.

Then, in the model for a particular resource, I can just do this:

    get categoryModel() {
        return this.relation('categories', Category => Category.id === this.categoryId);
    }

Nothing crazy, just a simple way to do it. The relation is cached, and there’s nothing invalidating that cache on update, but other than that it’s works well.

1reaction
rtheunissencommented, Nov 9, 2018

@MichMich There are two stages here: 1) plan it, 2) implement it. I’m not confident that we know what the solution is yet, so while the implementation might be quick, it’s difficult to estimate how long it would take to come up with a solution.

Can you give me an example of one of your model schema’s?

The API for this should be as simple as:

delegates() {
    "a.b": ModelA,
    "x.y.*": ModelB,
}

I think if we can come up with an API that is “acceptable”, we’re good to go. This would only solve 1 of the 3 points laid out in the opening statement. I don’t believe the other two are difficult to solve though:

Errors should be set on the child, not with dot notation on the parent. (or both?)

Should be set on the child, such that parent.errors is empty but parent.child.errors is populated.

Determine the difference between parent.saved(‘child.prop’) and parent.child.saved(‘prop’)

These should both be supported because they are not equivalent. This first uses the saved child’s property, and the second uses the active child’s saved property. This is a bit confusing but it will be very difficult to avoid that, unless we disallow nesting that isn’t delegated (I don’t want to do this though because it could force a lot of boilerplate).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested BackboneJS Models - Andrew Goldis - Medium
The Views (and Collection / Layout views) are designed to work with simple, flat models. Having a unified but complicated nested model and...
Read more >
MongoDB schema design - multiple collections vs nested ...
In your example it probably makes more sense to create two separate collections. Embedding documents is very useful in some cases, ...
Read more >
Body - Nested Models - FastAPI
Editor support (completion, etc), even for nested models; Data conversion ... You can use more complex singular types that inherit from str ....
Read more >
Collection-oriented workflows represent nested data ...
... addition, input data outside of an actor's scope is automatically forwarded downstream. where collection-aware actors can concurrently process collections ...
Read more >
create contextapp with single model and nested collection
Not currently no. Pretty much everything in Konstrukt is based around the collections so it's more about managing groups of data than individual ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found