Giter Site home page Giter Site logo

cham11ng / typescript-api-starter Goto Github PK

View Code? Open in Web Editor NEW
98.0 4.0 17.0 2.8 MB

Node.js (๐Ÿค–) Express.js / PostgreSQL API in TypeScript

License: MIT License

TypeScript 98.23% JavaScript 0.42% Makefile 1.09% Shell 0.26%
nodejs typescript express api knex knexjs winston migration seed chai

typescript-api-starter's Introduction

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 [email protected]: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": "[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.

typescript-api-starter's People

Contributors

cham11ng avatar dependabot[bot] avatar omept 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

Watchers

 avatar  avatar  avatar  avatar

typescript-api-starter's Issues

Adapt logging to Heroku's logging technique

Firstly, this is an awesome project setup and it was very useful for an app I built recently. Thank you for making it.

The only way to read the logs from heroku is via an addon service or heroku's cli. I deployed the application on Heroku and I had to add logFileGenarationSupport config variable in src\config\config.ts that tells me if my deployment destination supports log file generation.

logFileGenarationSupport: process.env.LOG_FILE_GENERATION_SUPPORT || 'false',

I also modified the src\utils\logger.ts file to look like this:

let trans: any = [];
let logger: Logger;

if (logFileGenarationSupport == 'false') {
  logger = createLogger({
    level: logging.level,
    format: combine(splat(), colorize(), timestamp(), formatter),
    transports: [new transports.Console()]
  });
} else {
  if (!fs.existsSync('logs')) {
    fs.mkdirSync('logs');
  }

  if (environment === 'development') {
    trans = [new transports.Console()];
  }

  logger = createLogger({
    level: logging.level,
    format: combine(splat(), colorize(), timestamp(), formatter),
    transports: [
      ...trans,
      new DailyRotateFile({
        maxSize: logging.maxSize,
        maxFiles: logging.maxFiles,
        datePattern: logging.datePattern,
        zippedArchive: true,
        filename: `logs/${logging.level}-%DATE%.log`
      })
    ]
  });
}

I think it would be nice to have this option added.

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.