Accessing Atlas database over REST API

Hi,
I am using MongoDB for my project. I’m thinking of using AWS Lambda functions which would be calling MongoDB for CRUD operations. As Atlas has connection limits, I’m looking for some alternatives in form of performing CRUD operations over REST protocol instead.
Here are some things I found -

  1. Hosting RestHeart & accessing MongoDB through it.
  2. Using MongoDB Realm Database service through SDK
  3. Using MongoDB Realm GraphQL service

Option 1 seems to fit the requirement, but I’m looking for server-less solution for it.

Option 2 - Will there be any time cost of initializing Realm App ? & Does it has any no. of concurrent connection limits? Is it over REST or Wire protocol?

Option 3- This seems to fulfill the requirements as It would be using Rest api - No connection limit issue.

What would be the ideal solution here ?

Thanks

I’d pick option 2. You’d need your Lambda function to log into Realm (using anonymous authentication if you prefer) – I’d suggest doing that outside of the handler so that you aren’t logging in every time that the function runs. Here’s a completely unrelated Lambda function that I’ve written – but it does does show how to do some pre-work outside of the handler:

const {WebClient} = require('@slack/web-api');
const AWS = require('aws-sdk');

// This work is done outside of the handler so that it's only run when the
// function is instantiated, not on every execution

const secretName = process.env.secretName;

let slackToken = "";
let channelId = "";
let secretsManager = new AWS.SecretsManager();

const initPromise = new Promise((resolve, reject) => {
    secretsManager.getSecretValue(
        { SecretId: secretName },
        function(err, data) {
            if(err) {
                console.error(`Failed to fetch secrets: ${err}`);
                reject();
            } else {
                const secrets = JSON.parse(data.SecretString);
                slackToken = secrets.slackToken;
                channelId = secrets.channelId;
                resolve()
            }
        }
    )
});

exports.handler = async (event) => {
    await initPromise;
    const client = new WebClient({ token: slackToken });
    const blocks = [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": `*${event.detail.fullDocument.author} said...*\n\n${event.detail.fullDocument.text}`
            },
            "accessory": {
                "type": "image",
                "image_url": "https://stitch-statichosting-prod.s3.amazonaws.com/5fd9ede61919a2df04497366/RChat%20Icon%20-%20180.png",
                // "image_url": "https://cdn.dribbble.com/users/27903/screenshots/4327112/69chat.png?compress=1&resize=800x600",
                "alt_text": "Chat logo"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": `Sent from <https://github.com/ClusterDB/RChat|RChat>`
            }
        },
        {
            "type": "divider"
        }
    ]

    await publishMessage(
        channelId, `Sent from RChat: ${event.detail.fullDocument.author} said "${event.detail.fullDocument.text}"`,
        blocks);

    const response = {
        statusCode: 200,
        body: JSON.stringify('Slack message sent')
    };
    return response;

    async function publishMessage(id, text, blocks) {
        try {
            const result = await client.chat.postMessage({
                token: slackToken,
                channel: id,
                text: text,
                blocks: blocks
            });
        }
        catch (error) {
            console.error(error);
        }
    }
};

Thanks for the response @Andrew_Morgan,

One more question - Why is option 2 over option 3 & Is there any connection limit/concurrent request limits while accessing MongoDB database from Realm SDK ?

Using GraphQL is a good option - especially if you’re already using it for other data sources. Personally, I find working with the SDK easier.

1 Like

Another option is also to use Webhooks (HTTP Endpoints) which allow you to invoke a Realm Function as an HTTP request. There are tradeoffs between each (e.g. SDK is easier to work with because it comes with built in methods, but there may not be an SDK for Realm based on the language you’re using in your Lambda function)

We’re looking to invest some time in improving the Webhooks/API developer experience and if anyone else is interested in providing feedback on the service, please send me a message or email me at sumedha.mehta@mongodb.com

Sumedha

3 Likes

For anyone following this who is still interested, please go to MongoDB Atlas Data API | MongoDB to opt-in to news and release announcements surrounding the feature.

1 Like