Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting Started
    • Quick start
    • Development workflow
  • Reference Guide
    • How it works
      • Environments
      • Developing on Forest Admin
        • Using branches
        • Deploying your changes
        • Forest CLI commands
          • init
          • login
          • branch
          • switch
          • set-origin
          • push
          • environments:reset
          • environments:create
          • deploy
          • schema:diff [beta]
        • Express packages
    • Models
      • Enrich your models
      • Relationships
        • Create a Smart relationship
          • GetIdsFromRequest
        • Smart Relationship Examples
          • Smart hasMany relationship in mongoDB
    • Actions
      • Create and manage Smart Actions
        • Use a Smart Action Form
        • Use a Smart Action Intent
      • Smart Action Examples
        • Calculate the distance between two string addresses
        • Impersonate a user
        • Create a record with a multiselect through a many-to-many relationship
        • Handle enums with alias labels in a smart action
        • Dropdown with list of values in smart action form
        • Custom dynamic dropdown in a form using smart collections
        • Refresh hasMany relationship in smart action
        • Smart segment to restrict access to an action on a record details view
        • BelongsToMany edition through smart collection
        • Upload files to amazon s3
        • Upload several files with the File Picker
        • Retrieve smart field info in a smart action
        • Smart action to create several records from the input of a single smart action form
        • Add many existing records at the same time (hasMany-belongsTo relationship)
        • Call a webhook with record ids
        • Bulk update records
    • Smart Fields
      • Smart Field Examples
        • Add an HTML credit card as a smart field in a summary view
        • Display field with complex info in html format (rich text editor)
        • Generate signed urls to display S3 files in a smart field
        • Print a status object in a single line field
        • Sort by smart field
        • Sort by smart field that includes value from a belongsTo relationship
        • Add fields destined to the create form
        • Add validation to a smart field edition
        • Display smart field as progress bar using rich text editor
        • Update point geometry field using a smart field and algolia api
    • Smart Collections
      • Examples
        • Create a Smart Collection with Amazon S3
        • Smart relationship between model and stripe cards
        • Create records from a Smart collection
        • Searchable smart collection with records fetched from hubspot API
      • Serializing your records
    • Routes
      • Default routes
      • Extend a route
      • Override a route
    • Integrations
      • Stripe
      • Mixpanel
      • Intercom
      • Elasticsearch
        • Interact with your Elasticsearch data
        • Elasticsearch service/utils
        • Another example
      • Zendesk
        • Authentication, Filtering & Sorting
        • Display Zendesk tickets
        • Display Zendesk users
        • View tickets related to a user
        • Bonus: Direct link to Zendesk + change priority of a ticket
      • Dwolla
        • Display Dwolla customers
        • Display Dwolla funding sources
        • Display Dwolla transfers
        • Link users and Dwolla customers
        • Dwolla Service
      • Razorpay
      • Hubspot
        • Create a Hubspot company
        • Display Hubspot companies
      • Twilio
        • Send an SMS with Twilio and Zapier
      • Azure Table Storage
      • Slack
        • Send Smart Action notifications to Slack
      • Algolia
        • Geocode an address with Algolia
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a dynamic calendar view for an event-booking use case
      • Create a custom moderation view
    • Smart Segments
    • Scopes
      • Create a scope more than one level away based on a Smart field
      • Scope on a smart field extracting a json's column attribute
    • Performance
    • Charts
      • Create an API-based Chart
      • Create a Smart Chart
      • Create Charts with AWS Redshift
  • Extra help
    • Setup
      • Install
      • Connecting Forest Admin to Your Database (Forest Cloud)
      • Forest Admin IP white-listing (Forest Cloud)
      • Why HTTPS is necessary even locally
      • Troubleshooting
      • Prevent permission errors at installation
      • Deploy your admin backend to AWS
      • Deploy your admin backend on Heroku
      • Deploy your admin backend to Ubuntu server
      • Deploy your admin backend to Google Cloud Platform
      • Install Forest Admin on a remote machine
      • Use Forest Admin with a read-only database
      • Configuring CORS headers
      • Running Forest Admin on multiple servers
      • Flatten nested fields (MongoDB)
    • Upgrade
      • Upgrade notes (Rails)
        • Upgrade to v9
        • Upgrade to v8
        • Upgrade to v7
        • Upgrade to v6
        • Upgrade to v5
        • Upgrade to v4
        • Upgrade to v3
      • Upgrade notes (SQL, Mongodb)
        • Upgrade to v9
        • Upgrade to v8
        • Upgrade to v7
        • Upgrade to v6
        • Upgrade to v5
        • Upgrade to v4
        • Upgrade to v3
      • Update your models' definition
      • Monitor your Forest's status
      • Manage your Forest Admin environments programmatically
      • Changing your domain name
      • Migrate to the new role system
      • Push your new version to production
    • Databases
      • Use a demo SQL database
      • Use a demo MongoDB database
      • Populate a postgreSQL database on Heroku
      • Connect to a read replica database
      • Plug multiple schemas
      • Add new databases
      • Manage SQL views
    • Settings
      • Customize your /forest folder
      • Disable automatic Forest Admin schema update
      • Include/exclude models
      • Display extensive logs
      • Laravel specific settings
    • Releases Support
    • Other documentations
Powered by GitBook
On this page

Was this helpful?

  1. Reference Guide
  2. Models
  3. Relationships

Create a Smart relationship

PreviousRelationshipsNextGetIdsFromRequest

Last updated 1 year ago

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.

This is still the latest Ruby on Rails documentation of the forest_liana agent, you’re at the right place, please read on.

This is the documentation of the django-forestadmin Django agent that will soon reach end-of-support.

If you’re using a Django agent, notice that django-forestadmin v1 is replaced by v1.

If you’re using a Flask agent, go to the v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.

This is the documentation of the forestadmin/laravel-forestadmin Laravel agent that will soon reach end-of-support.

If you’re using a Laravel agent, notice that forestadmin/laravel-forestadmin v1 is replaced by v3.

If you’re using a Symfony agent, go to the v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.

Create a Smart relationship

What is a Smart Relationship?

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.

Create a BelongsTo Smart Relationship

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.

/forest/orders.js
const { collection } = require('forest-express-sequelize');
const models = require('../models');
​
collection('orders', {
  fields: [{
    field: 'delivery_address',
    type: 'String',
    reference: 'addresses.id',
    get: function (order) {
      return models.addresses
        .findAll({
          include: [{
            model: models.customers,
            where: { id: order.customer_id },
            include: [{
              model: models.orders,
              where: { ref: order.ref }
            }]
          }],
        })
        .then((addresses) => {
          if (addresses) { return addresses[0]; }
        });
    }
  }]
});
/forest/orders.js
const { collection } = require('forest-express-mongoose');
const Address = require('../models/addresses');

collection('Order', {
  fields: [
    {
      field: 'delivery_address',
      type: 'String',
      reference: 'Address._id',
      get: function (order) {
        return Address.aggregate([
          {
            $lookup: {
              from: 'orders',
              localField: 'customer_id',
              foreignField: 'customer_id',
              as: 'orders_docs',
            },
          },
          {
            $match: {
              'orders_docs._id': order._id,
            },
          },
        ]).then((addresses) => {
          if (addresses) {
            return addresses[0]._id;
          }
        });
      },
    },
  ],
});
lib/forest_liana/collections/order.rb
class Forest::Order
  include ForestLiana::Collection

  collection :Order

  search_delivery_address = lambda do |query, search|

    query.joins(customer: :address).or(Order.joins(customer: :address).where("addresses.country ILIKE ?", "%#{search}%"))

  end

  belongs_to :delivery_address, reference: 'Address.id', search: search_delivery_address do
    object.customer.address
  end
end
app/forest/order.py
from django_forest.utils.collection import Collection
from app.models import Order, Address


class OrderForest(Collection):
    def load(self):
        self.fields = [
            {
                'field': 'delivery_address',
                'reference': 'app_addresses.id',
                'type': 'String',
                'get': self.get_delivery_address,
            }
        ]

    def get_delivery_address(self, obj):
        queryset = Address.objects.filter(customer=obj.customer)
        if len(queryset):
            return queryset[0]

Collection.register(OrderForest, Order)

Ensure the file app/forest/__init__.py exists and contains the import of the previous defined class :

app/forest/__init__.py
from app.forest.orders import OrderForest
app/Models/Order.php
<?php

namespace App\Models;

use ForestAdmin\LaravelForestAdmin\Services\Concerns\ForestCollection;
use ForestAdmin\LaravelForestAdmin\Services\SmartFeatures\SmartRelationship;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Order
 */
class Order extends Model
{
    use HasFactory, ForestCollection;

    /**
     * @return SmartRelationship
     */
    public function deliveryAddress(): SmartRelationship
    {
        return $this->smartRelationship(
            [
                'type' => 'String',
                'reference' => 'address.id'
            ]
        )
            ->get(
                function () {
                    return Order::join('customers', 'customers.id', '=', 'orders.customer_id')
                        ->join('addresses', 'addresses.customer_id', '=', 'customers.id')
                        ->where('orders.id', $this->id)
                        ->first();
                }
            );
    }
}

Create a HasMany Smart Relationship

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.

/forest/products.js
const { collection } = require('forest-express-sequelize');

collection('products', {
  fields: [
    {
      field: 'buyers',
      type: ['String'],
      reference: 'customers.id',
    },
  ],
});
/forest/products.js
const { collection } = require('forest-express-mongoose');

collection('products', {
  fields: [
    {
      field: 'buyers',
      type: ['String'],
      reference: 'Customer._id',
    },
  ],
});
lib/forest_liana/collections/product.rb
class Forest::Product
  include ForestLiana::Collection

  collection :Product

  has_many :buyers, type: ['String'], reference: 'Customer.id'
end
app/forest/product.py
from django_forest.utils.collection import Collection
from app.models import Product


class ProductForest(Collection):
    def load(self):
        self.fields = [
            {
                'field': 'buyers',
                'reference': 'app_customer.id',
                'type': ['String'],
            }
        ]


Collection.register(ProductForest, Product)
app/Models/Product.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


/**
 * Class Product
 */
class Product extends Model
{
    use HasFactory;

    /**
     * @return SmartRelationship
     */
    public function buyers(): SmartRelationship
    {
        return $this->smartRelationship(
            [
                'type' => ['String'],
                'reference' => 'customer.id'
            ]
        );
    }
}

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.

/routes/products.js
const express = require('express');
const {
  PermissionMiddlewareCreator,
  RecordSerializer,
} = require('forest-express-sequelize');
const { products, customers, orders } = require('../models');

const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('products');

router.get(
  '/products/:product_id/relationships/buyers',
  (request, response, next) => {
    const productId = request.params.product_id;
    const limit = parseInt(request.query.page.size, 10) || 20;
    const offset = (parseInt(request.query.page.number, 10) - 1) * limit;
    const include = [
      {
        model: orders,
        as: 'orders',
        where: { product_id: productId },
      },
    ];

    // find the customers for the requested page and page size
    const findAll = customers.findAll({
      include,
      offset,
      limit,
    });

    // count all customers for pagination
    const count = customers.count({ include });

    // resolve the two promises and serialize the response
    const serializer = new RecordSerializer(customers);
    Promise.all([findAll, count])
      .then(([customersFound, customersCount]) =>
        serializer.serialize(customersFound, { count: customersCount })
      )
      .then((recordsSerialized) => response.send(recordsSerialized))
      .catch(next);
  }
);

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.

/routes/products.js
const express = require('express');
const router = express.Router();
const models = require('../models');

router.get('/products/:product_id/relationships/buyers', (req, res, next) => {
  let limit = parseInt(req.query.page.size) || 10;
  let offset = (parseInt(req.query.page.number) - 1) * limit;
  let queryType = models.sequelize.QueryTypes.SELECT;

  let countQuery = `
    SELECT COUNT(*)
    FROM customers
    JOIN orders ON orders.customer_id = customers.id
    JOIN products ON orders.product_id = products.id
    WHERE product_id = ${req.params.product_id};
  `;

  let dataQuery = `
    SELECT customers.*
    FROM customers
    JOIN orders ON orders.customer_id = customers.id
    JOIN products ON orders.product_id = products.id
    WHERE product_id = ${req.params.product_id}
    LIMIT ${limit}
    OFFSET ${offset}
  `;

  const serializer = new RecordSerializer(customers);
  Promise.all([
    // Since support to multiple db connections was added you have to use the connection name defined in config/databases.js
    // here using default
    models.connections.default.query(countQuery, { type: queryType }),
    models.connections.default.query(dataQuery, { type: queryType }),
  ])
    .then(([count, queryResult]) =>
      serializer.serialize(queryResult[0], { count: count[0].count })
    )
    .then((serializedResult) => res.send(serializedResult))
    .catch((err) => next(err));
});

module.exports = router;

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”

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 /Product/:product_id/relationships/buyers.

We use the $lookup operator of the aggregate pipeline. Since there's a many-to-many relationship between Product and Customer, the $lookup operator needs to look into orders which is an array we have to flatten first using $unwind.

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). You can access to the serializer through the Liana.ResourceSerializer object.

/forest/products.js
const { collection } = require('forest-express-mongoose');

collection('products', {
  fields: [
    {
      field: 'buyers',
      type: ['String'],
      reference: 'Customer._id',
    },
  ],
});
/routes/products.js
const P = require('bluebird');
const express = require('express');
const router = express.Router();
const Liana = require('forest-express-mongoose');
const { Customers } = require('../models');
const mongoose = require('mongoose');

router.get('/Product/:product_id/relationships/buyers', (req, res, next) => {
  let limit = parseInt(req.query.page.size) || 10;
  let offset = (parseInt(req.query.page.number) - 1) * limit;

  let countQuery = Customers.aggregate([
    {
      $lookup: {
        from: 'orders',
        localField: 'orders',
        foreignField: '_id',
        as: 'orders_docs',
      },
    },
    {
      $unwind: '$orders_docs',
    },
    {
      $lookup: {
        from: 'products',
        localField: 'orders_docs._id',
        foreignField: 'orders',
        as: 'products_docs',
      },
    },
    {
      $match: {
        'products_docs._id': mongoose.Types.ObjectId(req.params.product_id),
      },
    },
    {
      $count: 'products_docs',
    },
  ]);

  let dataQuery = Customers.aggregate([
    {
      $lookup: {
        from: 'orders',
        localField: 'orders',
        foreignField: '_id',
        as: 'orders_docs',
      },
    },
    {
      $unwind: '$orders_docs',
    },
    {
      $lookup: {
        from: 'products',
        localField: 'orders_docs._id',
        foreignField: 'orders',
        as: 'products_docs',
      },
    },
    {
      $match: {
        'products_docs._id': mongoose.Types.ObjectId(req.params.product_id),
      },
    },
  ]);

  return P.all([countQuery, dataQuery])
    .spread((count, customers) => {
      const serializer = new Liana.RecordSerializer(Customers);
      return serializer.serialize(customers, { count: count.orders_count });
    })
    .then((products) => {
      res.send(products);
    })
    .catch((err) => next(err));
});

module.exports = router;

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 /Product/:product_id/buyers.

Then, you should handle pagination in order to avoid performance issue. The API call has a querystring 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 (Customer in this example). You can access to the serializer through the serialize_models() function.

Rails.application.routes.draw do
  # MUST be declared before the mount ForestLiana::Engine.
  namespace :forest do
    get '/Product/:product_id/buyers' => 'orders#buyers'
  end

  mount ForestLiana::Engine => '/forest'
end
class Forest::ProductsController < ForestLiana::ApplicationController
  def buyers
    limit = params['page']['size'].to_i
    offset = (params['page']['number'].to_i - 1) * limit

    product = Product.find(params['product_id'])
    customers = Customer.where(order_id: product.orders.ids)

    render json: serialize_models(customers.limit(limit).offset(offset), meta: {count: customers.count})
  end
end

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 /app_product/:product_pk/relationships/buyers. You will have to declare this route in your app urls.py file

app/urls.py
from django.urls import path
from django.views.decorators.csrf import csrf_exempt

from . import views

app_name = 'app'
urlpatterns = [
    path('/app_product/<pk>/relationships/buyers', views.BuyersView.as_view(), name='product-buyers'),
]

Then create the pertained view

app/views.py
from django.http import JsonResponse
from django.views import generic

from django_forest.resources.utils.queryset import PaginationMixin
from django_forest.utils.schema.json_api_schema import JsonApiSchema

class BuyersView(PaginationMixin, generic.ListView):

    def get(self, request, pk, *args, **kwargs):
        params = request.GET.dict()

        # queryset
        queryset = Customer.objects.filter(order__product_id=pk).distinct()

        # pagination
        queryset = self.get_pagination(params, queryset)

        # json api serializer
        Schema = JsonApiSchema.get('app_customer')
        data = Schema().dump(queryset, many=True)

        return JsonResponse(data, safe=False)

Then, you should handle pagination in order to avoid performance issue. The API call has a querystring available which gives you all the necessary parameters you need to enable pagination.

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 /product/{id}/relationships/buyers.

Then, you should handle pagination in order to avoid performance issue. The API call has a querystring 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 (Customer in this example). You can access to the serializer through the render() function of JsonApi facade.

routes/web.php
<?php

use App\Http\Controllers\ProductsController;
use Illuminate\Support\Facades\Route;


Route::get('forest/product/{id}/relationships/buyers', [ProductsController::class, 'buyers']);
app/Http/controllers/ProductsController.php
<?php

namespace App\Http\Controllers;

use App\Models\Customer;
use ForestAdmin\LaravelForestAdmin\Facades\JsonApi;
use ForestAdmin\LaravelForestAdmin\Http\Controllers\ForestController;
use Illuminate\Http\JsonResponse;

/**
 * Class ProductsController
 */
class ProductsController extends ForestController
{
    /**
     * @param int $id
     * @return JsonResponse
     */
    public function buyers(int $id): JsonResponse
    {
        $query = Customer::whereHas('orders.products', fn ($query) => $query->where('products.id', $id))
            ->paginate($pageParams['size'] ?? null, '*', 'page', $pageParams['number'] ?? null);

        return response()->json(
            JsonApi::render($query, 'customers', ['count' => $query->total()])
        );
    }
}

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.

Smart Field
Sequelize
Sequelize
Active Record
Django ORM
marshmallow-jsonapi
Active Record
@forestadmin/agent
forestadmin-agent-django
forestadmin-agent-flask
forestadmin/laravel-forestadmin
forestadmin/symfony-forestadmin
Smart Field