Giter Site home page Giter Site logo

meetdave3 / vercel-mongodb-rest-api Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 21 KB

Serverless, scalable and secure MongoDB CRUD starter using REST API in Node.js with Zeit Now

License: The Unlicense

TypeScript 100.00%
zeit-now zeit-nodejs zeit-api serverless-examples serverless-functions now-sh zeit-microservices serverless zeit helpers

vercel-mongodb-rest-api's Introduction

zeit-mongodb-rest-api

Serverless REST API in Node.js with Zeit Now

Support

  • You will need the Now CLI to run this project. (Refer: https://zeit.co/download)
  • Currently the API supports GET, PUT, POST, DELETE methods on /api/index.
  • With the help of these 4 endpoints, CRUD operations can be performed on any given Mongodb database collection.
  • The REST API, is protected with Basic Authentication (HTTP header where 'username:password' is encoded in base64.)
  • Secrets must be added to the secrets & env variables before starting off. (Refer: https://zeit.co/docs/v2/build-step#adding-secrets).
  • The HTTP header auth username & password is matched to the username and password provided in the secrets.

Usage

  • To get started you will need

    • MongoDB connection uri to connect to your database.
    • username and password (pre-defined in the secrets). This is used to authenticate all incoming API requests.
  • Install the dependencies yarn

  • Add the secrets

    • mongodb_uri
    • username
    • password
  • Running locally: now dev

    • Any changes inside the /src folder will rebuild the serverless functions.
  • For production deployment, run: now

REST API Examples

  • GET: Returns all the documents for any given collection

    curl --request GET \
      --url http://localhost:3000/api/index \
      --header 'authorization: Basic base64-encoded-username:password' \
      --header 'content-type: application/json' \
      --data '{
        "collectionName": "comments"
      }'
    
  • PUT: Updates a document for any given collection, based on the filter object (query)

    curl --request PUT \
      --url http://localhost:3000/api/index \
      --header 'authorization: Basic base64-encoded-username:password' \
      --header 'content-type: application/json' \
      --data '{
        "collectionName": "users",
        "filter": {
          "contact": "1234567890"
        },
        "document": {
          "firstName": "Salva",
          "lastName: "Marquina",
          "city": "Madrid",
          "contact": "987654321"
        }
      }'
    
  • POST: Inserts a document in any given collection

    curl --request POST \
      --url http://localhost:3000/api/index \
      --header 'authorization: Basic base64-encoded-username:password' \
      --header 'content-type: application/json' \
      --data '{
        "collectionName": "comments",
        "document": {
          "creator": "331123454",
          "name": "Sergio",
          "comment": "This is a random comment"
        }
      }'
    
  • DELETE: Deletes a document in any given collection based on filter object (query)

    curl --request DELETE \
      --url http://localhost:3000/api/index \
      --header 'authorization: Basic base64-encoded-username:password' \
      --header 'content-type: application/json' \
      --data '{
        "collectionName": "users",
        "filter": {
          "firstName": "Salva"
        }
      }'
    

Inspiration

Since I've been using Zeit for frontend deployments, I wanted an equally faster way to setup database CRUD operations with a predefined Mongodb database. (Get a free Mongodb cluster here: https://www.mongodb.com/download-center/cloud)

Personally, I didn't want to create models / services / controllers / serverless API endpoints based on the database model everytime I start a new project. This setup should be ideal for a minimum viable product or a proof of concept.

Therefore, this solution is a secure, quicker and scalable way of managing and deploying database functions with Zeit.

License

MIT

vercel-mongodb-rest-api's People

Contributors

meetdave3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.