npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-mongo-proxy

v1.0.0

Published

A serverless plugin that creates a mongodb proxy

Downloads

18

Readme

serverless-mongo-proxy

A serverless plugin that creates a mongodb proxy

Installation

Install via npm in the root of your Serverless service:

npm install serverless-mongo-proxy

Add the plugin to the plugins array in your Serverless serverless.yaml:

plugins:
  - serverless-mongo-proxy  # add before serverless-offline if present

Add Lambda invoke permissions in your Serverless serverless.yaml:

provider:
  iamRoleStatements:
    - Effect: Allow
      Action:
        - lambda:InvokeFunction
      Resource: '*'

Custom parameters

It is possible to specify custom parameters via the following object:

custom:
  mongo-proxy:
    mongoUri: 'mongodb://localhost:27017'
    mongoDbName: 'myDatabase'

If different configuration is needed between different stages, it is possible to specify stage-specific custom objects:

custom:
  mongo-proxy-prod:
    functionFields:
      vpc:
        securityGroupIds:
          - sg-xxxxxxxx
        subnetIds:
          - subnet-xxxxxxxx
          - subnet-xxxxxxxx
custom:
  mongo-proxy-dev:
    mongoUri: 'mongodb://localhost:27017'
    mongoDbName: 'myDatabase'

Environment Variables

Mongodb

Mongodb endpoint and database name can be set respectively through the environment variables MONGO_URI and MONGO_DB_NAME or under the custom section of your Serverless serverless.yaml:

custom:
  mongo-proxy:
    mongoUri: 'mongodb://localhost:27017'
    mongoDbName: 'myDatabase'

Stage name

Stage name can be set under the provider section of your Serverless serverless.yaml:

provider:
  stage: 'dev'

or through the environment variable STAGE_NAME

Note: all variables can be set also through a .env file

Additional proxy function fields

Other fields can be specified under functionFields.

For example to use the plugin with serverless-plugin-warmup add these fields on your Serverless serverless.yaml:

custom:
  mongo-proxy:
    functionFields:
      warmup:
        enabled: true

VPC configuration

If you need to put the proxy inside a VPC, for example to work with Mongo Atlas after a Peering connection, you can use:

custom:
  mongo-proxy-prod:
    functionFields:
      vpc:
        securityGroupIds:
          - sg-xxxxxxxx
        subnetIds:
          - subnet-xxxxxxxx
          - subnet-xxxxxxxx

Usage

The plugin creates a lambda function that can be invoked through the aws-sdk:

const { Lambda } = require('aws-sdk')
...
const lambda = new Lambda()
lambda.invoke({
  FunctionName: `${serviceName}-${stage}-_serverless-mongo-proxy`,
  InvocationType: 'RequestResponse',
  Payload: JSON.stringify({ requestBufferValues }),
}).then(...)

see aws docs for details.

Payload

The proxy uses bson to serialize/deserialize data between the proxy itself and the caller.

The expected stringified payload has shape: { requestBufferValues: number[] } where number[] contains the values of the buffer representing the request, serialized using bson.

Example: const requestBufferValues = Array.from(bson.serialize({...}).values())

Proxy Request

The actual proxy request has shape:

interface ProxyRequest {
  collectionName: string
  operation: string // a mongodb operation (find, insert...)
  args: any[]
}
Proxy Response

The proxy response has shape:

interface ProxyResponse {
  responseBufferValues?: number[]
  error?: string
}

Full example

const { Lambda } = require('aws-sdk')
const Bson = require('bson')
...
const lambda = new Lambda()
const proxyRequest = {
  collectionName: 'test-collection',
  operation: 'insertOne',
  args: [{ name: 'Arthur', surname: 'Dent' }]
}
const bufferValues = Array.from(bson.serialize(proxyRequest).values())
lambda.invoke({
  FunctionName: `${serviceName}-${stage}-_serverless-mongo-proxy`,
  InvocationType: 'RequestResponse',
  Payload: JSON.stringify({ requestBufferValues }),
}).then(response => {
  const { requestBufferValues, error } = response
  const { result } = bson.deserialize(Buffer.from(requestBufferValues))
  console.log(result.insertedCount) // output: 1
})

Note: the deserialized payload has shape { result: any } and represents the mongodb operation result

See @restlessness/dao-mongo package for an example of usage.