Reusing a schema for addExternalRelation

Feature(s) impacted

I would like to use addExternalRelation to build a complex one-to-many relationship.

If we were a concert app, I would like to have a relationship between users and their unclaimed concert tickets. “Unclaimed” tickets here would be a ticket with an Email but no ClaimedAt field for example. (Using addExternalRelation might be an XY issue here)

Tickets and users live in two different Sequelize data sources (there are two databases).

Observed behavior

addExternalRelation needs a schema to be aware of what data is returned.

Because this is pointing to an existing collection, I would really like to reuse its schema with some Picking on the fields I will project in the body.

Here is some example code:

    users.addExternalRelation('UnclaimedTickets', {
      schema: { }, // TODO plug the Pick<>ed Tickets schema somehow
      dependencies: ['...'],
      listRecords(record, context) {
        return context.dataSource.getCollection('Tickets').list(
          {
            conditionTree: { /* one giant mess */ },
          },
          [/* a few projected fields */],
        )
      },
    })

Is there any way to get the schema from a collection?

Context

  • Agent technology: nodejs
  • Agent (forest package) name & version: 1.40.1 with sequelize 6.37.3 and sequelize-typescript 2.1.6
  • Database type: MSSQL
  • Recent changes made on your end if any: Migrating from old agent.

Hello @kll,

Sorry for the late reply, the forum has been quite active these last few days and I’ve been cooking something for you :wink:

I would not recommend the use of addExternalRelation in your case since the data you want to link to is present in your Forest models. The addExternalRelation was designed to enrich data from elsewhere, making the UI treat it as a JSONArray, you would not be able to use any of the Forest Admin UI features (relations, filter, search and so on…)

So we made a new plugin, we hope you’ll like it and find it useful @forestadmin-experimental/plugin-filtered-relationship :slight_smile:

This plugin allows you to create a filtered oneToMany relationship to collection present in your Forest models

Best regards,

3 Likes

Oooh that’s a tasty-sounding plugin, I’m checking it out immediately! :slight_smile:

2 Likes

@dogan.ay Thanks for the plugin, this seems perfect! I’ve filed an issue on the repo directly :smiley:

2 Likes

I’ve solved the issue and everything should be released on npm, tell me if it works out for you :smiley:

1 Like

Thank you very much! It works perfectly :smiley:

3 Likes