Skip to content

A Koa js API template made with typescript, with all the cool things you need.

License

Notifications You must be signed in to change notification settings

meretamal/koa-mongodb-template

Repository files navigation

Koa MongoDB Template

A Koa template with all the cool things you need.

Requirements

  • Node 16
  • Yarn
  • MongoDB

Setup

First of all, make a copy of this project by clicking on "Use this template".

Now that you have your own repository, clone it and install its dependencies:

yarn install

Create a .env file using the .env.example file as a template:

cp .env.example .env

and fill the necessary variables.

Finally, build the docker image and and run your app:

docker compose build
docker compose up

and visit http://localhost:3000/ (you can change the port by changing the PORT variable in your .env file).

Environment

There are some variables your are required to use for this template to work:

  • APP_NAME: name of the app you are building (defaults to Koa MongoDB Starter).
  • DATABASE_URL: url of your MongoDB service.
  • JWT_SECRET: secret used to generate auth tokens.
  • JWT_EXPIRATION: expiration time for auth tokens (tokens don't expire by default).
  • PORT: port in which the app will run (defaults to 3000).

Tools and packages

This template comes with different tools that you will definetly love:

  • Bcrypt to hash user passwords.
  • Dotenv to read .env files.
  • Jest + Supertest to develop unit tests.
  • Eslint + Prettier to check for syntax errors and enforce a common code style.
  • Jsonwebtoken to generate auth tokens.
  • Koa to run the server and manage requests.
  • Prisma to to facilitate the use of MongoDB.
  • Yup to validate data.

Using Prisma

To able to use Prisma correctly, every time you make a change to your schema file, run:

yarn prisma generate

to update your prisma client code.

CI

This project is configured with GitHub Actions so that each pull request is checked for eslint offenses and runs e2e tests.

For this to work you must configure a ci environment (in the repository settings), and add a DATABASE_URL secret pointing to a database to can you use for testing purposes.

In a near future I want to configure a docker image to be able to run the tests, so that this step is not necessary.

About

A Koa js API template made with typescript, with all the cool things you need.

Topics

Resources

License

Stars

Watchers

Forks