Skip to content

cham11ng/typescript-api-starter

Repository files navigation

Typescript API Starter

Build Status

This is a API starter template for building a Node.js and Express.js using Typescript and PostgreSQL as database. It includes popular tools such as jsonwebtoken, joi, Knex, Objection.js, and more.

Requirements

Getting Started

# Clone repository
$ git clone git@github.com:cham11ng/typescript-api-starter.git <application-name>

$ cd <application-name>

# Update database credentials
$ cp .env.example .env

# Install dependencies
$ yarn install

$ yarn migrate
# Load fake data in database.
$ yarn load:fake <FactoryName> <Number>

Start the application.

# For production
$ yarn build

# For development
$ yarn dev

Using Docker

$ docker compose up -d api

# Make sure server is started checking logs before running this command
$ docker compose exec api sh yarn migrate
# View logs of the container.
$ docker compose logs -f api

# To stop the services.
$ docker compose stop api postgres

Generating Migrations and Seeds

# To create migration use `make:migration`
$ yarn make:migration create_{table_name}_table

# To create seed use `make:seeder`
$ yarn make:seeder {table_name}_table_seeder
# Example
$ yarn make:migration create_posts_table
$ yarn make:seeder post_table_seeder

Modify migration and seeder file as per the requirement. Then finally:

# to migrate
$ yarn migrate

# to seed
$ yarn seed

Setting up REST Client

Create a file or add following lines in .vscode > settings.json and switch an environment Cmd/Ctrl + Shift + P > REST Client: Switch Environment. Then, you can request APIs from api.rest file.

{
  "rest-client.environmentVariables": {
    "$shared": {
      "refreshToken": "foo",
      "accessToken": "bar",
      "email": "sgr.raee@gmail.com",
      "password": "secret"
    },
    "local": {
      "host": "localhost",
      "refreshToken": "{{$shared refreshToken}}",
      "accessToken": "{{$shared accessToken}}",
      "email": "{{$shared email}}",
      "password": "{{$shared password}}"
    }
  }
}

Contributing

Feel free to send pull requests.

License

typescript-api-starter is under MIT License.