typescript-api-starter icon indicating copy to clipboard operation
typescript-api-starter copied to clipboard

Node.js (🤖) Express.js / PostgreSQL API in TypeScript

Typescript API Starter

Build Status

Starter for Node.js Express API in Typescript with jsonwebtoken, joi, Knex, Objection.js and many other popular tools.

Requirements

Getting Started

Clone the repository, install the dependencies.

$ git clone [email protected]:cham11ng/typescript-api-starter.git <application-name>

$ cd <application-name>

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

$ yarn migrate

Load fake data in database.

$ yarn load:fake <FactoryName> <Number>

Start the application.

$ yarn build # For production

$ yarn start # For development

Using Docker

Make a copy of .env.docker and save as .env.

$ cp .env.docker .env

Install dependencies and run the application locally.

$ docker compose up -d postgres

$ docker compose up -d api

$ docker compose exec api sh yarn migrate # Make sure server is started checking logs before running this command

View logs of the container.

$ docker compose logs -f

To stop the services.

$ docker compose stop api postgres

Generating Migrations and Seeds

To create migration use make:migration and seed use make:seeder:

$ yarn make:migration create_{table_name}_table

$ 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:

$ yarn migrate # to migrate

$ yarn seed # to 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": "[email protected]",
      "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.