avatar
setup MongoDB Atlas PostgreSQL

> First and foremost, you will see default cluster from MongoDB Atlas dashboard if you register new account successfully.

> For Cluster0 are running in M0 Sandbox environment, if you attach your credit card to activate. Then, you will see Cluster1 as below

Note: Access link to get all clusters in your account and billing link to track from your usage.

> You can totally terminate your Cluster1 if you aren't ready for using it.

> Press Connect button to establish configuration for Cluster1. The next step, Choose connection a method.

Database User: flagtick
Database Password: YiiSHeSJL0rVYpJN

Click Connect your application.

> Create Database by clicking on Browser Collections as below:

Note: Collection name was that means Table name. In here, we will create table named users. We can move to Network Access and add 0.0.0.0/0  (includes your current IP address) to allow anywhere from access.

> Get reference code for Node.js and handler in lambda function.

const { ObjectID } = require('bson');
const client = require('mongoose');
const Schema = client.Schema;

async function mongodbConnect() {
  const URL = 'mongodb+srv://flagtick:[email protected]/flagtick?retryWrites=true&w=majority';
  client.set('strictQuery', false);
  return await new Promise((resolve) => {
    client.connect(URL,
        { 
          useNewUrlParser: true, 
          useUnifiedTopology: true 
        }
      ).then(function(data) {
        /** Refer document here: https://mongoosejs.com/docs/models.html */
        const id = Schema.ObjectId;
        const UserModel = client.model('User', 
          new Schema(
            { _id: ObjectID, name: String, slug: String }
          ), 
          'users'
        );    
        UserModel.find({}, function(err, data) { 
          if (err !== null) {
              resolve(err);
          } else {
              resolve(data);
          }
        });
      })
      .catch(function (err) {
        console.error('Could not connect to mongo DB', err)
      });
  });
}

exports.handler = async (event) => {
    let res = await mongodbConnect();
    const response = {
        statusCode: 200,
        body: JSON.stringify(res),
    };
    return response;
};

> Run node -r esbuild-register run.ts to test lambda function locally.

node -r esbuild-register run.ts
{
  statusCode: 200,
  body: '[{"_id":"639ff1c7fe256706160bd9ce","name":"Flagtick Group","slug":"flagtick-group"}]'
}

Note: 'mongodb+srv://flagtick:[email protected]/?retryWrites=true&w=majority'; should update as below to ensure which database target?

const URL = 'mongodb+srv://flagtick:[email protected]/flagtick?retryWrites=true&w=majority';

> Reference link here and document for mongoose here.

You need to login to do this manipulation!