Skip to content
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

Different between using @lazy decorator vs getter? #1736

Open
garygcchiu opened this issue Jan 18, 2024 · 0 comments
Open

Different between using @lazy decorator vs getter? #1736

garygcchiu opened this issue Jan 18, 2024 · 0 comments

Comments

@garygcchiu
Copy link

garygcchiu commented Jan 18, 2024

Hey,

I am running into the following error when trying to import my models in my Node.js (node v20.10.0) application: #1337

/node_modules/@nozbe/watermelondb/decorators/lazy/index.js:16
    configurable: configurable,
                  ^
TypeError: Cannot destructure property 'configurable' of 'descriptor' as it is undefined.
    at lazy (/node_modules/@nozbe/watermelondb/decorators/lazy/index.js:16:19)

I was able to get a workaround by changing how I set up my @lazy methods in my models:

Instead of:

@lazy commenters = this.collections.get('users').query(
    Q.on('comments', 'post_id', this.id)
)

Do:

get commenters () {
     return this.collections.get('users').query(
        Q.on('comments', 'post_id', this.id)
    )
}

Note the removal of the @lazy decorator. If I kept it, then my application would give this error when these queries are eventually called with .fetch(), e.g. await post.commenters.fetch():

 WARN  Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'fetch' of undefined

Since TypeScript/JavaScript getters are already lazily evaluated, I think this should be OK. I'm not too familiar with the internals of the decorators here, but I wanted to start a discussion and see if there are any concerns with doing this, or if there is an alternative workaround to either of the above issues.

Looking at the code, my understanding it that it would hinder performance a bit, as each subsequent call is evaluated lazily instead of using the precomputed value. Is that it?

@garygcchiu garygcchiu changed the title Different between using @lazy decorator vs getter? Different between using @lazy decorator vs getter? Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant