Load external related data via API

Hi,

I want to show data from an external API to the user.
Is it possible to have model, which is not in database but loaded from an external API?
My idea was to have a smart related data for a “virtual” model, which does not really exist in database.

Would that be possible or is there even a better way to load external related data?

Hi @bef :wave: unfortunately it is not easy to do it today.
But maybe i can help you to do it.
To do that, can you give me more context please.
Do you need all the externals data as collections inside your project? Or just some datas you want to plug inside an existing collection?

Hi @Arnaud_Moncel ,

Thanks for your fast reply.

I want to add log entries to a model.
Lets say I have a user model and an API to load all the log entries by user-id.

That means a user can have multiple log entries.
Each log entry will have its own properties (date, type, text, …)

My idea was to add it a as relation to the “related data” but I would be fine with other options.
I don’t need any filtering on these entries (this would just be a nice to have).
So even a text view in the user summary would be OK.

Ok so, I suggest you to have a look on this documentation related to smart relation ship feature.

Adapted to your goal,
You need to create a smart collection who represent your log entries.
Like this :point_down:

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

collection('log', {
  actions: [],
  fields: [{
    field: 'content',
    type: 'String',
  }, {
    field: 'log creation',
    type: 'Date',
  }, {
    ...
  }],
  segments: [],
});

Next You need to define a smart relationship between your user model and your new log model.
like this :point_down:

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

collection('user', {
  actions: [],
  fields: [{
    field: 'logs',
    type: ['Number'],
    reference: 'log.id'
  }],
  segments: [],
});

After that you will be able to see, in the ui, the associated related data under user detail.
The last thing you need is to fill data now.

You mus create the new relationship route like this. :point_down:

router.get('/user/:userId/relationships/logs', async (request, response, next) => {
  const recordSerializer = new RecordSerializer({ name: 'log' });
  const records = await yourApiCall();// call your api with request.params.userId
  // WRNING! you need to have id inside your records.
  // here we want to have plain object who represent the log collection you declared before
  const serializedRecords = await recordSerializer.serialize(records, { count: records.length });
  response.send(serializedRecords);
});

Now you should be able to see inside user related data your logs.

I let you play with our smart feature and the little exemple i provided.
Please don’t hesitate to come back if you have any question. :pray:

1 Like

Thanks a lot. This does the job.

The only thing is, that I have to pass the user and the query from the request to the serizalizer.
const recordSerializer = new RecordSerializer({ name: 'log' }, request.user, request.query);

If not it results in an error.

Oh! yes I forgot it, sorry about that :pray:.

No Problem. I’m glad you helped me out :pray:
Just wanted to leaf it, if somebody needs this later :wink:

1 Like