• Stars
    star
    374
  • Rank 113,764 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The best practice of building Koa2 with TypeScript

koa-ts

The best practice of building Koa2 with TypeScript. ไธญๆ–‡


Usage

  1. Run npm init koa-ts

  2. Install dependencies: yarn

  3. Rename .env.example to .env, and run prisma db push to synchronize the data model

  4. Start the server: yarn dev. visit: http://127.0.0.1:3000/apis/sessions

(Optional) the project has built-in a docker compose, run yarn dev:db to run database automatic.


Project Layout

โ”œโ”€โ”€ app
โ”‚ย ย  โ”œโ”€โ”€ controllers         ---  server controllers
โ”‚ย ย  โ”œโ”€โ”€ helpers             ---  helper func (interceptor / error handler / validator...)
โ”‚ย ย  โ”œโ”€โ”€ jobs                ---  task (periodic task / trigger task / email server...)
โ”‚ย ย  โ”œโ”€โ”€ entities            ---  database entities/models
โ”‚ย ย  โ””โ”€โ”€ services            ---  adhesive controller and model
โ”œโ”€โ”€ config
โ”‚ย ย  โ”œโ”€โ”€ constants        ---  environment variable
โ”‚ย   โ”œโ”€โ”€ koa.middlewares     ---  middlewares for Koa
โ”‚ย   โ”œโ”€โ”€ routing.middlewares ---  middlewares for Routing Controller
โ”‚ย   โ”œโ”€โ”€ routing.options     ---  configs for Routing Controller
โ”‚ย ย  โ”œโ”€โ”€ bootstrap           ---  lifecycle
โ”‚ย ย  โ””โ”€โ”€ interceptors        ---  global interceptor
โ”‚ย ย  โ””โ”€โ”€ utils               ---  pure functions for help
โ””โ”€โ”€ test                    ---  utils for testcase
โ”œโ”€โ”€ .env           ---  environment file

Feature

  • Separation configuration and business logic.

  • Export scheme model and interface, follow style of TypeScript.

  • Test cases and lint configuration.

  • The best practice for Dependency Injection in Koa project.

  • Get constraints on your data model with Prisma.

  • TypeScript hotload.


Lifecycle

  1. app.ts -> collect env vars constants -> collect env files variables.env

  2. envs ready, call bootstrap.before()

  3. lift routing-controllers -> lift Koa middlewares -> register Container for DI

  4. start Koa & invoke bootstrap.after() after startup


Databases

The project uses Prisma as the intelligent ORM tool by default. Supports PostgreSQL, MySQL and SQLite.

  • You can change the data type and connection method in the .env file
  • After each modification to file /prisma/schema.prisma, you need to run prisma migrate dev to migrate the database.
  • After each modification to file /prisma/schema.prisma, you need to run prisma generate to sync types.

About Environments

When nodejs is running, ENV does not mean NODE_ENV:

  • After NodeJS project is built, we always run it as NODE_ENV=PRODUCTION, which may affect some framework optimizations.
  • NODE_ENV only identifies the NodeJS runtime, independent of the business.
  • You should use ENV to identify the environment.

For the data settings of each environment, you can refer to the following:

  • Development Mode (ENV=development): read configurations from configs/constants/development.ts file, but it will still be overwritten by .env file.

  • Production Mode (ENV=production): read configurations from configs/constants/production.ts file, but it will still be overwritten by .env file.


Reference


LICENSE

This project is licensed under the MIT License. See the LICENSE file for more info.

More Repositories

1

func

Modern Command Line Framework.
TypeScript
408
star
2

unix.bio

My static blog template.
TypeScript
223
star
3

simpler-paper

elegant document generation tool.
TypeScript
149
star
4

v2ex-cli

v2ex-cli
JavaScript
91
star
5

secret

TypeScript
64
star
6

touch-dog

A modern translation tool.
TypeScript
63
star
7

tcome

NodeJS & Angular4 Blog
JavaScript
54
star
8

views

show your page views without server.
TypeScript
52
star
9

todo-live

A todo list manager in command line.
TypeScript
50
star
10

tcome-frontend

NodeJS&Angular4 BLOG
TypeScript
49
star
11

news-feed

Angular + Electron ๆกŒ้ขๅบ”็”จ็ณปๅˆ—ๆ•™็จ‹
TypeScript
37
star
12

want

commands for humans.
Shell
31
star
13

v-decorators

decorators for vue
JavaScript
31
star
14

euv

IoC with vue
TypeScript
30
star
15

react-native-smartbar

Maybe this is the best tabbar , support android and ios !
JavaScript
29
star
16

cdn-viewer

cdn viewer of packages.
JavaScript
25
star
17

poc.css

Lightweight style file.
SCSS
24
star
18

sendAlert

ๆ•ๆท,ไผ˜้›…็š„ๅผนๆก†ๆ’ไปถ
CSS
22
star
19

DungeonWatchDog

A dungeon finder filter with World of Warcraft
Lua
21
star
20

cool-scroll

cool-scroll
CSS
16
star
21

Nioro

Blizzard Raid frame enhancement.
Lua
14
star
22

vue-auto

Use Vue in a simpler way.
JavaScript
14
star
23

cobot

A user-experience-focused middleware for building Gitlab applications
TypeScript
14
star
24

backset

Minimalist state tool for React apps.
TypeScript
13
star
25

img

Transform images for better display on social site.
JavaScript
12
star
26

next-password

Use password to protect your Next.js application.
TypeScript
11
star
27

back-loader

preload resource repo.
TypeScript
10
star
28

koa-custom-response

standard and convenient KOA2 custom responses.
TypeScript
9
star
29

done

Releaser for semantic version.
TypeScript
9
star
30

pages

make home pages from `readme.md`
JavaScript
8
star
31

aliyun-logs

้˜ฟ้‡Œไบ‘ๆ—ฅๅฟ—ๆœๅŠก SDK
TypeScript
8
star
32

blog

archived
7
star
33

taobaoScroller

ๆจกไปฟๆ‰‹ๆœบๆท˜ๅฎ็š„ไธŠไธ‹ๆ‹‰ๅŠ ่ฝฝ
JavaScript
6
star
34

vuepress-theme-zeit

a vuepress theme for zeit design
Vue
6
star
35

easy-jsdelivr

A web tool for jsdelivr cdn.
Vue
6
star
36

exciting-segmented

ios&android segmented
JavaScript
6
star
37

angular-mobile-tips

ไธ€ไธชๅŸบไบŽangular1.x็š„็งปๅŠจ็ซฏๅผน็ช—ๆ’ไปถ
JavaScript
5
star
38

objectSnake

ไธ€ไธช็ฎ€ๅ•็š„js่ดชๅƒ่›‡
JavaScript
4
star
39

nest-prisma-template

A template for creating API services based on NestJS and Prisma.
TypeScript
3
star
40

unix

2
star
41

docs

Programming documentation
JavaScript
2
star
42

docs-cli

The command-line tool for docs
TypeScript
2
star
43

release2

git release generator.
TypeScript
1
star
44

unix.bio-migrate

TypeScript
1
star
45

func-service

Standard tooling for `func` development.
TypeScript
1
star
46

create-vono

TypeScript
1
star
47

create-unix-bio

Installer for unix.bio.
TypeScript
1
star
48

curseforge-release

This is just a template project and nothing is done.
Lua
1
star
49

api-fold-cookies

TypeScript
1
star
50

rehype-join-line

Resolve line breaks in chinese paragraphs.
JavaScript
1
star
51

vono

Vue project template. Geek style.
Vue
1
star
52

notion-board-box

1
star
53

store-now

File store
TypeScript
1
star
54

touch-dog-api

JavaScript
1
star