Create a Smart relationship
Last updated
Was this helpful?
Last updated
Was this helpful?
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize
and forest-express-mongoose
Node.js agents that will soon reach end-of-support.
forest-express-sequelize
v9 and forest-express-mongoose
v9 are replaced by v1.
Please check your agent type and version and read on or switch to the right documentation.
Sometimes, you want to create a virtual relationship between two set of data that does not exist in your database. A concrete example could be creating a relationship between two collections available in two different databases. Creating a Smart Relationship allows you to customize with code how your collections are linked together.
On the Live Demo example, we have an order which belongsTo
a customer which belongsTo
a delivery address. We’ve created here a BelongsTo Smart Relationship that acts like a shortcut between the order and the delivery address.
A BelongsTo Smart Relationship is created like a with the reference
option to indicate on which collection the Smart Relationship points to. You will also need to code the logic of the search query.
On the Live Demo example, we have a product hasMany
orders and an order belongsTo
customer. We’ve created a Smart Relationship that acts like a shortcut: product hasMany
customers.
Upon browsing, an API call is triggered when accessing the data of the HasMany relationships in order to fetch them asynchronously. In the following example, the API call is a GET on /products/:product_id/relationships/buyers
.
Option 1: using Sequelize ORM
Then, you should handle pagination in order to avoid performance issue. The API call has a query string available which gives you all the necessary parameters you need to enable pagination.
Finally, you don’t have to serialize the data yourself. The Forest Admin agent already knows how to serialize your collection (customers
in this example). You can access to the serializer through the recordsGetter.serialize
function.
Option2: using raw SQL
Then, you should handle pagination in order to avoid performance issue. The API call has a query string available which gives you all the necessary parameters you need to enable pagination.
Finally, you don’t have to serialize the data yourself. The Forest Admin agent already knows how to serialize your collection (customers
in this example). You can access to the serializer through the recordsGetter.serialize
function.
If your primary key column name (customer_id
) is different than the model field name (customerId
), you must alias the primary key column with the name of the model field in the dataQuery.
Ex: SELECT customers.*, customers.customer_id AS “customerId”
A HasMany Smart Relationship is created like a with the reference
option to indicates on which collection the Smart Relationship points to.
We’ll use the findAll and count methods provided by to find and count all customers who bought the current product (buyers).
We’ll use raw SQL query and to count and find all customers who bought the current product (buyers).
We’ve built the right SQL query using to count and find all customers who bought the current product.
We’ve built the right SQL query using to find all customers who bought the current product.
Finally, you don’t have to serialize the data yourself. The Forest Admin agent already knows how to serialize your collection (Customer
in this example, with the table name app_customer
). You can access to the serializer through the Schema().dump
function (using internally).
We’ve built the right SQL query using to count and find all customers who bought the current product.