Create Has many relationship

Hi Team, I am going through the below doc to create has_many smart relationship, in the doc for creating a product has_many customer’s smart relationship, seems the sample snippet doesn’t use limit and offset variable itself, then how customer records are limited? let’s a product has 1000 customers, then we need to limit the records and provide pagination support right?

And one more doubt is, inside the spread function, there is code that like this count.orders_count basically what it does and can you explain how the orders_count method became available in the first place? below is the snippet.
return serializer.serialize(customers, { count: count.orders_count });

Also the above snippet uses bluebird package all and spread methods, kindly give a brief overview of what it does and how it works or share some documentation regarding the same.

Hi Giri_S!

To make thing simple, when using the “classical” agents (forest-express-*) and implementing smart relationships, nothing is automatic:

Customers are in charge of implementing all routes that the frontend will call for that relationship. This gives you a lot of flexibility but requires a lot of code in the agent to be written. The newer agents (released in July) work quite differently for that feature.

With that being said, it’s easier to understand the answers to the specific question in your thread

There are two code samples in the documentation for has-many smart relationships on the link that you have provided.

In both offset and limit are extracted from the query string and used (either as parameters to the customers.findAll method, or directly in the SQL query).

When the smart collection is displayed on the frontend, the GUI needs to know the total number of records so that the pagination widget can be populated.

That means that the route has two different objectives:

  • count the total number of records (with the countQuery SQL query)
  • retrieve the records of a given page (with the dataQuery SQL query)

Boths queries are run against the database, and both results are returned to the frontend

I did not find any usage of bluebird in the documentation page that you provided.

Promise.all() is a standard function and allows to wait for multiple promises to finish.

Those three code samples are equivalent:

const promise1 = function1();
const promise2 = function2();
const result1 = await promise1;
const result2 = await promise2;
// more work
const [result1, result2] = await Promise.all([function1(), function2()]);
// more work
Promise.all([function1(), function2()]).then(([result1, result2]) => {
  // more work