Code Monkey home page Code Monkey logo

nx-serverless's Introduction


Nx Serverless

The Ultimate Monorepo Starter for Node.js Serverless Applications

✅  First-Class Typescript Support
✅  DynamoDB Single Table Design
✅  Shared API Gateway
✅  Environments Configuration
✅  CORS
✅  JWT Auth Middleware
✅  Http Params Validation
✅  Typed Proxy Handlers
✅  Auto Generators
✅  Localstack
✅  ESLint
✅  Jest
✅  Github Actions


serverless esbuild npm peer dependency version (scoped) code style: prettier GitHub license PRs Welcome Maintained

Prerequisites

  • Docker
  • Node.js

Getting Started

About the App

The application contains three services:

Auth Service:

The auth service is responsible for authentication. It exposes one route for signing up:

curl --request POST 'http://localhost:3001/dev/auth/sign-up' \
--data-raw '{
    "email": "[email protected]",
    "name": "Netanel Basal"
}'

The request returns a JWT, which is used for accessing protected routes.

Users Service:

The users service is responsible for managing users. It exposes one route:

curl 'http://localhost:3003/dev/user' --header 'Authorization: token TOKEN'

The request returns the logged-in user.

Todos Service:

The todos service is responsible for managing todos. A user has many todos. It exposes CRUD routes:

// Get user todos
curl 'http://localhost:3005/dev/todos' --header 'Authorization: token TOKEN'

// Get a single todo
curl 'http://localhost:3005/dev/todos/:id' --header 'Authorization: token TOKEN'

// Create a todo
curl --request POST 'http://localhost:3005/dev/todos' \
--header 'Authorization: token TOKEN'
--data-raw '{
    "title": "Learn Serverless"
}'

// Update a todo
curl  --request PUT 'http://localhost:3005/dev/todos/:id' \
--header 'Authorization: token TOKEN' \
--data-raw '{
    "completed": true
}'

DynamoDB GUI

Download NoSQL Workbench for DynamoDB and connect to http://localhost:4566.

Commands

nx serve <service-name>
nx deploy <service-name>
nx remove <service-name>
nx build <service-name>
nx lint <service-name>
nx test <service-name>

// Use different enviroment
NODE_ENV=prod nx deploy <service-name> 
NODE_ENV=stg nx deploy <service-name> 

// Run only affected
nx affected:test
nx affected:deploy

Generators

// Generate a service
yarn g:service tags

// Generate handler
yarn g:handler handler-name

// Generate http handler
yarn g:http-handler create-tag

// Generate a model
yarn g:model tag


CI/CD Pipeline with Github Actions

The pipeline has been configured to run everytime a push/pull_request is made to the main branch. You should uncomment the ci.yml workflow.

Workflow Steps

  • Checkout: The checkout action is used to checkout the source code.

  • Node setup: The setup-node action is used to optionally download and cache distribution of the requested Node.js version.

  • lint and test: The lint and test runs only on affected projects.

  • Configure AWS credentials: The credentials needed are AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY and should be set as Github secrets.

  • Each branch should be prefixed with the environment name. For example, if we have a stg-feature-name branch and open a pull request to the main branch, it will set NODE_ENV to stg and deploy to this environment.

By merging the pull request to the main branch, NODE_ENV is set to prod, and the deployment is done to production.

The workflow file can have as many environments as you need.

Further help

Contribution

Found an issue? feel free to raise an issue with information to reproduce.

Pull requests are welcome to improve.

License

MIT

This project is a fork of nx-serverless

Monster icons created by Smashicons - Flaticon

nx-serverless's People

Contributors

damola12345 avatar netanelbasal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nx-serverless's Issues

Dependency Injection?

Hi

Many thanks for putting this repo together, it is an excellent example.

What are your thoughts on using a Dependency Injection library like tsyringe? Could 'newing up' several dependencies in your todo service lead to a lot of boilerplate setup for each handler when more abstractions are created around IO and Business Logic?

I've forked your repo, created a branch and refactored the todo service with a few tests as a basic example - https://github.com/stottle-uk/nx-serverless/tree/dependency_injection.

Any thoughts or ideas you have would be much appreciated.

Kind regards

Dynamo

Issue with Table generation

POST /dev/auth/sign-up (λ: sign-up)
One of the tables was not found, verify table exists before retrying. Error: Cannot do operations on a non-existent table

Run the create user curl and get the above

NB// Also add docs about setting up AWS credentials and the defaults needed perhaps

Frontend?

Hello,

First off, as a long time Serverless Framework user, I love what has been done here!

So much so, I am planning on using this as a PoC at my job. I have 2 quick question before I do though:

  1. Would I easily be able to integrate a React frontend into the repo? - I have looked over the Nx Documentation and believe it should be possible but I would like your opinion as you definitely have more experience in Nx than myself.

  2. If a frontend service is possible, would you be able to share types across both?

Thanks,
Connor

Document yarn usage

README.md lists both yarn and npm, and it's not clear which is the supported way. Also, there are Yarn Classic and Yarn Berry, which of them are known to work?

Serverless Compose?

Hi, Serverless Framework maintainer here. I stumbled upon this repository and I'm wondering if Serverless Framework Compose could be useful here?

Furthermore we're exploring possible integrations with Learn/Nx/Turborepo, looking to bounce ideas about that. So I thought I'd open an issue ^^

NPM Error

Dependency error occurred using npm 8.11.0, with npm 8.5.0 it installs everything fine

Steps to reproduce:

  • Use npm version = 8.11.0

It got me this error over here:

While resolving: @nrwl/[email protected]
Found: @typescript-eslint/[email protected]
node_modules/@typescript-eslint/parser
  dev @typescript-eslint/parser@"5.19.0" from the root project
  peer @typescript-eslint/parser@"^5.0.0" from @typescript-eslint/[email protected]
  node_modules/@typescript-eslint/eslint-plugin
    dev @typescript-eslint/eslint-plugin@"5.19.0" from the root project

Could not resolve dependency:
peer @typescript-eslint/parser@"~5.18.0" from @nrwl/[email protected]
node_modules/@nrwl/eslint-plugin-nx
  dev @nrwl/eslint-plugin-nx@"14.1.2" from the root project

Conflicting peer dependency: @typescript-eslint/[email protected]
node_modules/@typescript-eslint/parser
  peer @typescript-eslint/parser@"~5.18.0" from @nrwl/[email protected]
  node_modules/@nrwl/eslint-plugin-nx
    dev @nrwl/eslint-plugin-nx@"14.1.2" from the root project

What we can do in this case?

Usually I would just start to bump versions from those dependencies. I'm new to open source projects and I would love to know what approach we use in these dependency error cases.

And I'm curious why this happens, all of the versions are set to the exact match in package.json

ERESOLVE could not resolve

After clone I was running npm install and I got this error:

npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @nrwl/[email protected]
npm ERR! Found: @typescript-eslint/[email protected]
npm ERR! node_modules/@typescript-eslint/parser
npm ERR!   dev @typescript-eslint/parser@"5.19.0" from the root project
npm ERR!   peer @typescript-eslint/parser@"^5.0.0" from @typescript-eslint/[email protected]
npm ERR!   node_modules/@typescript-eslint/eslint-plugin
npm ERR!     dev @typescript-eslint/eslint-plugin@"5.19.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @typescript-eslint/parser@"~5.18.0" from @nrwl/[email protected]
npm ERR! node_modules/@nrwl/eslint-plugin-nx
npm ERR!   dev @nrwl/eslint-plugin-nx@"14.1.2" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @typescript-eslint/[email protected]
npm ERR! node_modules/@typescript-eslint/parser
npm ERR!   peer @typescript-eslint/parser@"~5.18.0" from @nrwl/[email protected]
npm ERR!   node_modules/@nrwl/eslint-plugin-nx
npm ERR!     dev @nrwl/eslint-plugin-nx@"14.1.2" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Github Actions

Add GitHub actions for testing, linting, and deploying.

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.