Closed Melchyore closed 6 months ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Keep open
A little update, in BelongsTo
class, this.relatedModel
returns a function but this.relatedModel()
returns undefined
.
I did not tried the reproduction you shared so far, but I think this approach will have many other limitations as well.
For example, the default table name for the relationship will be s_models
and not items
. Also, it might break the DB -> Model
hydration layer.
I hope, we had traits like PHP, then all this would have been a lot easier, whereas in case of inheritance, the applied decorators do not know anything about the child model and hence they compute values as per the current (aka parent) model.
Solution
The solution is quite dirty, or maybe not, it depends how much visual clutter can you tolerate.
As the first step, you will be better off if you add properties and relationships to the final model directly. For example, create a function as follows.
function MakeShareable<T extends typeof BaseModel>(superclass: T): T {
superclass.boot()
superclass.$addColumn('userId', {})
superclass.$addRelation('user', 'belongsTo', () => User, {})
return superclass
}
Then use it on the Item
model as follows.
import { MakeShareable } from './some_path'
class Item extends compose(BaseModel, ShareableModel) {
@column.dateTime({ autoCreate: true })
public createdAt: DateTime;
}
export default MakeShareable(Item)
Now, this will take care of the runtime side of things. However, for types you will have to create an interface and export it alongside the function.
interface Shareable {
userId: number
user: BelongsTo<typeof User>
}
Here's the final code will look like.
import { MakeShareable, Shareable } from './some_path'
interface Item extends Shareable {}
class Item extends compose(BaseModel, ShareableModel) {
@column.dateTime({ autoCreate: true })
public createdAt: DateTime;
}
export default MakeShareable(Item)
Okay, scratch my last response. Because, I wrote some tests and it seems to be working fine, because we are cloning and copying the relationships on the child model.
The reason it is not working for you is because of circular imports. If you log the User
model inside the belongsTo decorator, you will find it to be undefined
.
@belongsTo(() => {
console.log(User)
return User
})
Alright, I have to run a few more tests to see how it goes with circular dependencies. Will post updates here
It should be all good in the next release, but that will be the major release targeting v6
It should be all good in the next release, but that will be the major release targeting v6
Many thanks!
Package version
18.3.0
Node.js and npm version
Node.js v18.2.1 npm v8.19.2
Sample Code (to reproduce the issue)
(The full code is in the Bonus section)
Hello, this is a follow-up related to this discussion
Mixin:
Model that extends the above mixin:
When you run
pnpm run dev
you'll see the following errorIf you remove the
belongsTo
relationship, it works. After some investigation, it seems that the decorator is receiving an undefined related model.BONUS (a sample repo to reproduce the issue)
Took the code from here and created the following reproduction repo: https://github.com/Melchyore/lucid-error-reproduction