Giter Club home page Giter Club logo

api-lambda-example's Introduction

API Lambda Example

This project contains a simple API for practice purpose and maybe for new learners like me who explores the aws serverless world.

A user can have N of posts and has a daily limit of posts that we can attach.

Techs Used

Main library used in Node js

  • Joi used for API validation
  • Typegoose used to define Mongoose models using TypeScript classes
  • Mongoose to interact with database
  • Inversify an IoC container uses a class constructor to identify and inject its dependencies.

Architecture

I used the single purpose function in lambda trilogy. Functions are grouped into module and each module are attached to an API gateway and glued together using custom domain.

I used AWS CodeArtifact to make a private repository called ksr/shared where all common codes reside, we can used it in each module to avoid repetition of codes and also this is where all the queries abstracted using repository pattern belonged.

  • One question that comes into my mind when making this is what if I needed the createPost method in the createUser method, well I just make a service in each module and just import it if needed in the future. By doing this I can avoid using the lambda invoke (aws-sdk) which will make the request more slower because it will initiate a new connection on the database.

image

Folder structure

Top folder is represented by module name (except for repositories), each files under src are lambda functions.

└── posts/
    ├── src/
    │   ├── createPost.ts
    │   └── getAllPosts.ts
    └── tests/
        └── ...
└── repositories/
    ├── di
    ├── modules
    ├── shared
    └── tests

repositories folder

repositories / @ksr/shared folder or the private repository that represents the common codes used by each modules (users, posts, users posts)

  • di has files that are needed for dependency injection e.g. container.ts
  • modules has files that represent each module (I used repository pattern)
  • shared has files that are common and might be used in each lambda fns.

Notes: if we build or test the project there's additional folder included like coverage and dist

API reference

Get all users

  • <API_GATEWAY_URL>/user-management/users (GET)

Create User

  • <API_GATEWAY_URL>/user-management/user (POST)
    {
      "name": "test",
      "dailyLimit": 2
    }
    

Get all posts

  • <API_GATEWAY_URL>/post-management/posts (GET)

Create post

  • <API_GATEWAY_URL>/post-management/post (POST)
    {
      "title": "test6",
      "description": "desc6"
    }
    

Get all users posts

  • <API_GATEWAY_URL>/user-post-management/users-posts (GET)

Attach posts to user

  • <API_GATEWAY_URL>/user-post-management/user-post (POST)
    {
      posts: ["62aace7f0406a73d0c3742ec", "62aad0631e145e6b829d2625"],
      userId: "62aae23a7e64ab93f1b6eedc"
    }
    

Todos maybe

  • CI/CD for each module and if we commit our code only the changed files under a specific module should be deployed (monorepo), I'm still exploring this and still not sure how to implement it lol.
  • AWS cognito for auth purpose and to protect the API's
  • multi environment setup

api-lambda-example's People

Contributors

ksromero avatar

Stargazers

 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.