Issue: Relation data between collections from differents databases

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