Issue: Relation data between collections from differents databases

Hi :grinning: I am working on my project where I have integrated Forest Admin with MongoDB, which creates a second backend app. I am working with two databases, which I have already made the connection and everything works fine.

What I would like to know is how to relate a collection from database 1 with a collection from database 2.

Context: I have db1 and db2, in db1 I have the collection portfolio and in db2 I have the collection charities. I would like to relate these collections in such a way that a portfolio can have many charities (related by charity_id).
I suppose that it is possible, the issue is that it is complicating me to make the relation following the documentation presented in the page since the models are in different databases.

I would appreciate if anyone could help me to make such a relationship,
Thank you very much!

Here are my schemas! :


Set up information:
“express”: “~4.17.1”,
“forest-express-mongoose”: “^8.0.0”,
“mongoose”: “~5.8.2”,

Hi @usr1vang :wave:, to reach your goal i think you can create a smart relation ship between your two databases. Have a look on this documentation Create a Smart relationship - Documentation.

Let me know if you need more help.

I think that the relationship is correctly done, because in forest I can see the charities related to my portfolio as shown below.

What I would like to do is to be able to click on the charity_id inside my portfolio and go to the information of this charity that is in another db. The problem is that I can’t mark the charity_id field as link because as I show in the previous post, my schema is defined as JSON.
( …
charities: [
{
charity_id: Mongoose.Schema.Types.ObjectId,
reference_type: String,
reference_id: String,
_id: false,
},
],
…)

My question is if there is a way to do this, to be able to click on the charity_id and go to the information of that same charity.

I look forward to your comments, thank you!

Hi @usr1vang, unfortunately we doesn’t support this yet :confused: I push your request in our product-board.

1 Like

Oh ok!

Do you know of any other way that maybe can be implemented to achieve what i need? maybe with a smart relationship?

My schemas are the pictures that i attached above, is it possible to create a smart relationship where i get the charities information from the charities id that are inside the [charity_id] field in portfolio?

I mean, the problem I have with the documentation that explains the smart relationships is that here in my case, the chartity_id inside portfolio, are in a json format, so I do not know how to get my information from the Charity database from those id.

I have found a workaround for you. Please follow steps below. :pray:

1. Create a smart collection forest/smartCharities.js

const { collection } = require('forest-express-mongoose');

collection('smartCharities', {
  actions: [],
  fields: [{
    field: 'id',
    type: 'String',
  }, {
    field: 'charity_id',
    type: 'String',
    reference: 'charity._id'
  }, {
    field: 'amount',
    type: 'Number',
  }, {
    field: 'reference_type',
    type: 'String',
  }, {
    field: 'reference_id',
    type: 'String',
  }],
  segments: [],
  fieldsToFlatten: [],
});

2. Create a hasmany smart relationship inside forest/portfolios.js

const { collection } = require('forest-express-mongoose');

collection('portfolios', {
  actions: [],
  fields: [{
    field: 'smartCharities',
    type: ['String'],
    reference: 'smartCharities.id'
  }],
  segments: [],
  fieldsToFlatten: [],
});

3. Create get relationships route inside routes/portfolios.js

const { PermissionMiddlewareCreator, RecordGetter, RecordSerializer } = require('forest-express-mongoose');
const { portfolios, objectMapping } = require('../models');

router.get('/portfolios/:recordId/relationships/smartCharities', permissionMiddlewareCreator.list(), async (request, response, next) => {
  const { params, query, user } = request;
  const recordGetter = new RecordGetter(portfolios, user, query);
  const record = await recordGetter.get(params.recordId);

  const { charities } = record;
  charities.forEach(charity => charity.id = objectMapping.Types.ObjectId());

  const recordSerializer = new RecordSerializer({ name: 'smartCharities' });
  const serializedCharities = await recordSerializer.serialize(charities);
  response.send(serializedCharities);
});

4. Restart your server

5. Hide charities related data from portfolios details records

Let me know if that help.

1 Like

Yes!!! that works perfecty!! I am very grateful for your help!
Thank you very much for your time.
Greetings.

1 Like