Hi team,
I am currently rewriting the way in which I am populating the smart fields using the AfterFind hook technique as shown at: https://docs.forestadmin.com/woodshop/how-tos/load-smart-fields-using-sequelizes-afterfind-hook.
It seems to be a very good way to improve the performances as it will generate far fewer queries to the database.
I made it work for the vast majority of my smart fields.
However, I have a problem with smart relationships.
Let’s take my example in a few words. I have the following native relationships:
db.Users.hasOne(db.kyc);
db.Users.hasOne(db.cards);
In the kyc collection, I have defined the smart relationship to get a hasOne or BelongsTo between db.kyc and db.cards
{ field: 'card', // belongsTo association from kyc to cards
type: 'String',
reference: 'cards.user_id', // initialized in model afterFind hook
relationship: 'BelongsTo',
},
Before my changes, I had implemented a get() method that was doing the job.
get: (kyc) => {
return models.cards
.findOne({ where: { user_id: kyc.user_id } })
.then((card) => { return card; });
},
Using that old method, the smart field displayed as a Reference in the Cards collection was correctly populated when displaying the Kyc rows.
Now, when using the AfterFind hook, I don’t know what I have to return in the kyc.card member.
I tried to set it like this:
kyc.card: { id: 'd6a07fac-eaa9-4f37-9c9a-42cd991ce26f' },
When doing that, the card id is displayed in the Kyc rows, instead of the smart field defined as a Reference in the Cards collection.
So, it seems that:
- either something is missing in the returned value
- or the smart field of the Cards collection (used as a Reference) is not populated
Could you please help me?
Thanks and regards
Here is my setup
"database_type": "postgres",
"liana": "forest-express-sequelize",
"liana_version": "6.5.0",
"engine": "nodejs",
"engine_version": "12.13.1",
"orm_version": "5.22.3"