[SOLVED] How to process Moralis Streams and add data to MongoDB?

Hey guys,
I’m getting a Streams response back to my webhook from moralis, however there’s not much info available on processing this data and uploading it to mongodb. How exactly can I go about this?

I’ve found two loose examples, however I’m not sure how to implement them:

Assuming I have to use this function, how do I get the data for it to parse and should I put this function in my index.ts?

export function parseEventData(req: any) {
  try {
    const updates: any = {};
    for (const log of req.body.logs) {
      const abi = req.body.abis[log.streamId];
      if (abi) {
        const { filter, update, eventName } = realtimeUpsertParams(abi, log, req.body.confirmed, req.body.block);
        return { data: update, tagName: log.tag, eventName };
      }
    }
  } catch (e: any) {
    console.log(e);
  }
}

Assuming this block of code is what I have to use to update my mongodb, how can I structure the updateDataInMongo() below?

app.post('/webhooks/myevent', async (req: any, res: any) => {
    try {
        verifySignature(req, config.MORALIS_API_KEY);
        const { data, tagName, eventName }: any = parseEventData(req);
        await updateDataInMongo(tagName, data);
        console.log('minted', tagName, eventName)
    } catch (e) {
        console.log(e);
    }
    res.send('ok');
});

Thanks!

What is the event that you received and you were any to add in mongo db?
Asking because there are multiple types of data like nft transfers, token transfers, logs

You can add them directly in mongo db as objects too

I’m receiving event updates of new clone instances. I need them to be added automatically to mongo.

Ok, you can add them directly how you receive them too. In a single column, or you can add them in separate columns

Where do I get the updateInMongo function though?

I’m not sure now where form is that code. Is from the streams example with parse server or form somewhere else?

You can also do simple object updates with Parse sdk or by directly connecting to mongo db

Someone sent me the code, but I just checked with them and the updateDataInMongo function is the same as the parseUpdate function over here: Using event-syncs in Moralis SDK v2 & Parse Server

Thanks for the help, going to do this now!

1 Like