# Migrating to Node.js backend, how do I create relationships from an array of IDs? Previously done with route

**URL:** <https://community.forestadmin.com/t/migrating-to-node-js-backend-how-do-i-create-relationships-from-an-array-of-ids-previously-done-with-route/6493>\
**Category:** Help me!\
**Tags:** howtos, smart-relationship\
**Created:** [July 13, 2023, 11:30am UTC](https://community.forestadmin.com/t/migrating-to-node-js-backend-how-do-i-create-relationships-from-an-array-of-ids-previously-done-with-route/6493 "2023-07-13T11:30:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakarias](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/zakarias/32/6313_2.png) [@zakarias](https://community.forestadmin.com/u/zakarias)\
**Post date:** [July 13, 2023, 11:30am UTC](https://community.forestadmin.com/t/migrating-to-node-js-backend-how-do-i-create-relationships-from-an-array-of-ids-previously-done-with-route/6493/1 "2023-07-13T11:30:44Z")

</div>

## Info

We are migrating our server from `forest-express-mongoose` to `Node.js`. We have not yet figured out a way to create a relationship from an array of IDs, previously done with a route. I will give an example below and also show some code snippets.

Based on the code explanation and code snippets below, the question is:

**How do we achieve the same thing as before without adding userId to venue documents?**

## Setup explanation

We have 3 different collections, Users, Merchants, and Venues.

Users → Merchant (many → 1). Reference = `user.manages.merchantId`  
Venues → Merchant (many → 1). Reference = `venue.merchantId`

We wish to create a smart relationship from Users to Venues, which would become an array of references to multiple venues that we manually have to calculate.

## Solution before migrating, using routes

We created a custom smart relationship between User and Venues, returning an array of references.

```javascript
router.get('/users/:userId/relationships/venues', async (req, res, next) => {
    const user = await users.findOne({
      _id: mongoose.Types.ObjectId(req.params.userId),
    });

    let venuesList = [];
    const merchant = await merchants.findOne({
      _id: user.manages.merchantId,
    });

    venuesList.push(...(await venues.find({ merchantId: merchant._id })));
    
    const serializer = new RecordSerializer({ name: 'venues' });

    res.send(
      await serializer.serialize(venuesList, {
        count: venues.length,
      }),
    );
});

```

## How we would like to do it now in the new Node.js version

We get the following error when running the code below: `Error Types from 'User.venuesIds' and 'Venue._id' do not match.`

```javascript
.customizeCollection('User', collection => {
    collection.addField('venuesIds', {
      columnType: ['String'],
      dependencies: ['manages@@@merchantId'],
      getValues: async (users, context) => {
        const venues = context.dataSource.getCollection('Venue');

        const relatedVenues = await venues.list(
          { conditionTree: { field: 'merchantId', operator: 'In', value: users.map(m => m['manages@@@merchantId']) } },
          ['_id', 'merchantId'],
        );

        return users.map(record => {
          return relatedVenues.filter(ven => ven.merchantId === record['manages@@@merchantId']).map(ven => ven._id);
        });
      },
    });

    collection.replaceFieldOperator('venuesIds', 'In', async (venueIds, context) => {
      const records = await context.dataSource
        .getCollection('Venue')
        .list({ conditionTree: { field: '_id', operator: 'In', value: venueIds } }, ['_id']);

      return { field: 'venuesIds', operator: 'In', value: records.map(r => r._id) };
    });

    collection.addManyToOneRelation('venues', 'Venue', {
      foreignKey: 'venuesIds',
      foreignKeyTarget: '_id',
    });
  })

```

## Context

- Project name: PayAtt Internal Admin Portal
- Team name: All teams
- Environment name: All environments
- Agent (old): forest-express-mongoose: ^8.7.9
- Agent (new): `"@forestadmin/agent": "^1.13.4",` & `"@forestadmin/datasource-mongoose": "^1.4.2",`
- Database type: MongoDB Atlas

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 13, 2023, 12:17pm UTC](https://community.forestadmin.com/t/migrating-to-node-js-backend-how-do-i-create-relationships-from-an-array-of-ids-previously-done-with-route/6493/2 "2023-07-13T12:17:44Z")

</div>

> [@zakarias](#):
>
> Users → Merchant (many → 1). Reference = `user.manages.merchantId`  
> Venues → Merchant (many → 1). Reference = `venue.merchantId`

Ok so something like this?

```javascript
merchant = { _id: 'merchant1' }
user = { _id: 'user1', manages: { merchantId: 'merchant1' } };
venues = { _id: 'venue1', merchantId: 'merchant1' };

```

> [@zakarias](#):
>
> We wish to create a smart relationship from Users to Venues

I don’t think you need to query any intermediary collection.  
As I understand, you are just trying to get venues where `venue.merchantId === user.manages.merchantId`

Are you using `flattenMode: 'auto'`? If not tell me, we’ll write a more complete answer

```javascript
agent.customizeCollection('users', collection => {
  collection.addOneToManyRelationship('userVenus', 'venue', {
    originKey: 'merchantId',
    originKeyTarget: 'manages@@@merchantId' // name of the flattened field, this should show on the autocompletion
  });
});

```

> **[To multiple records](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/multiple-records#one-to-many-relations)**

> **[Relationships](https://docs.forestadmin.com/developer-guide-agents-nodejs/under-the-hood/data-model/relationships)**

---

<div class="post-metadata">

**Author:** ![zakarias](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/zakarias/32/6313_2.png) [@zakarias](https://community.forestadmin.com/u/zakarias)\
**Post date:** [July 13, 2023, 12:27pm UTC](https://community.forestadmin.com/t/migrating-to-node-js-backend-how-do-i-create-relationships-from-an-array-of-ids-previously-done-with-route/6493/5 "2023-07-13T12:27:59Z")

</div>

Thank you @anon39940173, this solved the issue!

It just takes some time to wrap my head around the new way of working with the system. I’m glad the solution was so easy. 🙂
