Smart relation ships
I want to create a smart relationship.
Observed behavior
On forest-express-mongoose 7.8.2, I used the RecordsGetter like this to create my relationships (+ a smart field):
// smart relationship controller:
{
...
const payments: IPayment[] = await Payment.find({ orderId })
const paymentsGetter = new RecordsGetter(Payment)
return paymentsGetter.serialize(payments)
}
// Smart field:
export const paymentsSmartField = {
field: 'payments',
type: ['String'],
reference: 'payment._id'
}
On forest-express-mongoose 8.6.4, it seems things have changed:
- the RecordsGetter does not seem to work the same way than before:
and you use the Liana.RecordSerializer
in the doc:
So, my questions is: how to create a simple smart relationships for account that have many transactions and a transaction that only belongs to one account ?
Context
"liana": "forest-express-mongoose",
"liana_version": "8.6.4",
"stack": {
"database_type": "MongoDB",
"engine": "nodejs",
"engine_version": "16.13.0",
"orm_version": "6.2.5"
}
Hey @guillaumejauffret,
v8 introduced a breaking change on the API you are mentionning. You can find more infos here. Most of our controller now uses user
and query
as extra parameters.
Outside of this (major) change, everything should work as expected, so let me know if you still encounter your issue after upgrading
Hi @jeffladiray ,
thanks.
Indeed, if I use the user and the query it works better. However, I still have this issue:
If I do not use the .limit(query.page.size)
function on my query to get my records from my DB, the RecordsGetter tries to serialize too much records and the request is stuck and freezes the browser.
To avoid the freezing, I did that:
const transactions = await Transaction.find({ account }).limit(query.page.size)
const transactionsGetter = new RecordsGetter(Transaction, user, query)
return transactionsGetter.serialize(transactions)
…and then I get my 15 records in the Related Data.
I think it’s not the right way to do…
Moreover, why don’t you use the RecordsGetter
in the example of your doc ? I don’t really understand how to use the serializer.serialize(...)
with the count property inside.
const transactions = await Transaction.find({ account }).limit(query.page.size)
const transactionsGetter = new RecordsGetter(Transaction, user, query)
return transactionsGetter.serialize(transactions)
In your case, you should be able to directly import RecordSerializer
(see here) if you want to avoid instanciating a RecordsGetter
.
Moreover, why don’t you use the RecordsGetter in the example of your doc ? I don’t really understand how to use the serializer.serialize(…) with the count property inside.
RecordsGetter
was implemented in order to ease route overriding (As described here), but is definitely not required for everything to work as expected.
You can also find a few example with { meta: { count: xxx } }
here for example
If I do not use the .limit(query.page.size) function on my query to get my records from my DB, the RecordsGetter tries to serialize too much records and the request is stuck and freezes the browser.
Let me know if that helps
It’s ok. Thanks @jeffladiray
1 Like