How to create relationship in Node.js, using 3 collections in a hierarchical order?

Hi @zakarias :wave: I would like to sum up with you what I understand, and try to answer your question:

I don’t see the reference to the merchant on the customer_stamp_card_logs collection in the UI, do I need to do something more regarding importField ?

Here is your mongoose models:

connection.model(
  'Merchant',
  new mongoose.Schema({}),
);

connection.model(
  'StampCardCampaign',
  new mongoose.Schema({
    merchantId: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Merchant' },
  }),
);

connection.model(
  'CustomerStampCardLogs',
  new mongoose.Schema({
    stampCardCampaignId: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'StampCardCampaign'},
  }),
);

Here is your agent code:

.addDataSource(createMongooseDataSource(connection))
.customizeCollection('CustomerStampCardLogs', collectionCustomizer => {
  collectionCustomizer
    .importField('merchantId', { path: 'stampCardCampaignId__manyToOne:merchantId' })
    // missing code to make the relation fully work
    .addManyToOneRelation('merchant', 'Merchant', { foreignKey: 'merchantId' });
})
.customizeCollection('Merchant', collectionCustomizer => {
  collectionCustomizer.addOneToManyRelation('customerStampCardLogs', 'CustomerStampCardLogs', { originKey: 'merchantId' });
})

If I understand correctly your issue, I thing you can have a look on the code who add merchant ManyToOneRelation on the CustomerStampCardLogs collection.

let me know if that help :pray:

3 Likes