• Stars
    star
    639
  • Rank 70,436 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 2 months ago

Reviews

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

Repository Details

A template repository for TypeScript backend server

Backend

1. Outline

1.1. Introduction

Nestia Logo

GitHub license Build Status Guide Documents

Template repository for NestJS + Prisma stack with FP and TDD.

@samchon/backend is a template repsitory of backend project utilizing NestJS and Prisma. It has been prepared to educate and spread how to adapt FP (Functional Programming) in the NestJS development. Also, @samchon/backend guides how to utilize those 3rd party libraries (what I've developed) in the production, especially helpful for TDD (Test Driven Development) with dramatic productivity enhancement.

  • typia: Superfast runtime validator
  • nestia: NestJS helper libaries like SDK generation
  • prisma-markdown: Markdown generator of Prisma (ERD + documentation)

Additionally, I've prepared a couple of example backend projects leveraging this template repository. Reading this README.md document and traveling below example projects, you may understand how to develop the TypeScript backend server of NestJS following the FP and TDD paradigm with my 3rd party libraries.

If you've already developed a TypeScript backend server with NestJS + Prisma, and its quality seems like enough good to be a good example for the backend programming learners with FP and TDD paradigm, please leave an issue or a pull request.

1.2. Specializations

Transform this template project to be yours.

When you've created a new backend project through this template project, you can specialize it to be suitable for you by changing some words. Replace below words through IDE specific function like Edit > Replace in Files (Ctrl + Shift + H), who've been supported by the VSCode.

Before After
ORGANIZATION Your account or corporation name
PROJECT Your own project name
AUTHOR Author name
db_name Database to connnect
db_schema Database schema to use
db_account Database account to use, not root account
https://github.com/samchon/backend Your repository URL

After those replacements, you should specialize the src/Configuration.ts, .github/workflows/build.yml files. Open those files and change constant values of these files to be suitable for your project. Also, open markdown files like this README.md and write your specific project story. Below is list of the markdown files.

2. Installation

2.1. NodeJS

This backend server has implemented through TypeScript and it runs on the NodeJS. Therefore, to mount this backend server on your local machine, you've to install the NodeJS.

Also as you can see from the package.json file, this project requires the private npm module @ORGANIZATION, provided from the Github. Therefore, to develop this backend server, you've configure the .npmrc file. Open the below link and complete the configuration.

2.2. PostgreSQL

bash postgres.sh

If you've installed Docker, then run the script above.

Otherwise, visit below PostgreSQL official site and install it manually.

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

After that, run the npm run schema <root-account> <password> command.

Database schema for BBS backend system would be automatically constructed.

npm run schema postgres root

2.3. Repository

From now on, you can start the backend server development, right now.

Just download this project through the git clone command and install dependencies by the npm install command. After those preparations, you can start the development by typing the npm run dev command.

# CLONE REPOSITORY
git clone https://github.com/samchon/backend
cd backend

# INSTALL DEPENDENCIES
npm install

# START DEVELOPMENT
npm run dev

3. Development

  • A. Definition only
    • Design prisma schema file
    • Build and Share ERD document with your companions
    • Write DTO structures
    • Declare controller method only
  • B. Software Development Kit
    • Build SDK from the declaration only controller files
    • SDK supports mockup simulator, boosting up frontend development
    • SDK is type safe, so development be much safer
  • C. Test Automation Program
    • Build test program earlier than main program development
    • Utilize SDK library in the test program development
    • This is the TDD (Test Driven Development)
  • D. Main Program Development

3.1. Definition

ERD

Before developing the main program, define it before.

At first, design the DB architecture on the Prisma Schema file (prisma/schema.prisma).

Writing the schema definitions, don't forget to write the detailed descriptions on each tables and properties. After that, build ERD (Enterprise Relationship Diagram) document through npm run build:prisma command. The ERD document will be generated on the docs/ERD.md path. If you share the ERD document with your companions, your team can enjoy increased productivity by standardizing words and entities.

At second, write DTO structures under the src/api/structures directory and declare API endpoint specs under the src/controllers directory. Note that, do not implement the function body of the controller. Just write declaration only. Below code is never pseudo code, but actual code for current step.

@Controlleer("bbs/articles")
export class BbsArticleController {
  @TypedRoute.Patch()
  public async index(
    @TypedBody() input: IBbsArticle.IRequest
  ): Promise<IPage<IBbsArticle.ISummary>> {
    input;
    return null!;
  }
}

3.2. Software Development Kit

nestia-sdk-demo

@samchon/backend provides SDK (Software Development Kit) for convenience.

SDK library means a collection of fetch functions with proper types, automatically generated by nestia. As you can see from the above gif image, SDK library boosts up client developments, by providing type hints and auto completions.

Furthermore, the SDK library supports Mockup Simulator.

If client developer configures simulate option to be true, the SDK library will not send HTTP request to your backend server, but simulate the API endpoints by itself. With that feature, frontend developers can directly start the interaction development, even when the main program development has not started.

# BUILD SDK IN LOCAL
npm run build:sdk

# BUILD SDK AND PUBLISH IT TO THE NPM
npm run package:api

3.3. Test Automation Program

TDD (Test Driven Development)

After the Definition and client SDK generation, you've to design the use-case scenarios and implement a test automation program who represents those use-case scenarios and guarantees the Main Program.

To add a new test function in the Test Automation Program, create a new TS file under the test/features directory following the below category and implement the test scenario function with representative function name and export symbol.

Note that, the Test Automation Program resets the local DB schema whenever being run. Therefore, you've to be careful if import data has been stored in the local DB server. To avoid the resetting the local DB, configure the reset option like below.

Also, the Test Automation Program runs all of the test functions placed into the test/features directory. However, those full testing may consume too much time. Therefore, if you want to reduce the testing time by specializing some test functions, use the include option like below.

  • supported options
    • include: test only restricted functions who is containing the special keyword.
    • exclude: exclude some functions who is containing the special keyword.
    • reset: do not reset the DB
# test without db reset
npm run test -- --reset false

# include or exclude some features
npm run test -- --include something
npm run test -- --include cart order issue
npm run test -- --include cart order issue --exclude index deposit

3.4. Main Program

After Definition, client SDK building and Test Automation Program are all prepared, finally you can develop the Main Program. Also, when you complete the Main Program implementation, it would better to validate the implementation through the pre-built SDK and Test Automation Program.

However, do not commit a mistake that writing source codes only in the controller classes. The API Controller must have a role that only intermediation. The main source code should be write down separately following the directory categorizing. For example, source code about DB I/O should be written into the src/providers directory.

4. Appendix

4.1. NPM Run Commands

List of the run commands defined in the package.json are like below:

  • Test
  • Build
    • build: Build every below programs
    • build:sdk: Build SDK library, but only for local
    • build:test: Build Test Automation Program
    • build:main: Build main program
    • dev: Incremental builder of the Test Automation Program
    • eslint: EsLint validator runner
    • pretter: Adjust prettier to every source codes
    • webpack: Run webpack bundler
  • Deploy
    • package:api: Build and deploy the SDK library to the NPM
    • schema: Create DB, users and schemas on local database
    • start: Start the backend server
  • Webpack
    • webpack: Run webpack bundler
    • webpack:start: Start the backend server built by webpack
    • webpack:test: Run test program to the webpack built

4.2. Directories

4.3. Related Repositories

Write the related repositories down.

More Repositories

1

typia

Super-fast/easy runtime validators and serializers via transformation
TypeScript
4,547
star
2

nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
TypeScript
1,814
star
3

tstl

TypeScript-STL (Standard Template Library, migrated from C++)
TypeScript
603
star
4

prisma-markdown

Markdown generator of Prisma, including ERD and descriptions
TypeScript
432
star
5

payments

Korean Payment System with Mockup servers of PGs
TypeScript
332
star
6

safe-typeorm

TypeORM helper library enhancing safety in the compilation level
TypeScript
227
star
7

tgrid

TypeScript RPC (Remote Procedure Call) for WebSocket and Worker protocols
TypeScript
142
star
8

fake-iamport-server

Fake Iamport Server with Real SDK
93
star
9

framework

Deprecated, use TGrid instead
C++
71
star
10

resume

CSS
53
star
11

astl

Standard Template Library for AssemblyScript
TypeScript
38
star
12

nestia-helper

NestJS helper with Type level
TypeScript
34
star
13

mutex

Mutex Server using WebSocket
TypeScript
29
star
14

openapi

OpenAPI definitions, converters and LLM function calling application composer.
TypeScript
28
star
15

bbs-backend

Simple Bullet-in Board System Backend
TypeScript
24
star
16

sxml

Simple XML Library for TypeScript (JavaScript)
TypeScript
16
star
17

nestia-start

Nestia template project installed by "npx nestia start"
TypeScript
16
star
18

fast-object

Fast object creator, via JSON.parse(), but type safe
TypeScript
16
star
19

shopping-backend

Shopping Mall Backend Server made by NestJS + Prisma for Education
TypeScript
11
star
20

websocket-polyfill

WebSocket class from Browser to NodeJS
TypeScript
11
star
21

ecol

Event Collections
TypeScript
9
star
22

samchon

8
star
23

json-translator

Translate JSON file via Google Translate API with optimization strategies
TypeScript
6
star
24

cagen

Number of Case Generator
TypeScript
5
star
25

schedules

Open source development schedule of Samchon
5
star
26

big-associative

Big associative containers who can store over 14M elements.
TypeScript
5
star
27

encrypted-fetcher

Rest API Fetcher with AES Encryption
TypeScript
4
star
28

typedoc-plugin-exclude-references

temporary plug-in
TypeScript
4
star
29

environments

My development environments
4
star
30

import2

lazy import function who can prevent transpiled to require
JavaScript
4
star
31

tgrid.project.chat

Demo Project - Chat Application
TypeScript
4
star
32

safeorm

Ultimate Safe ORM for the TypeScript
TypeScript
4
star
33

nestjs-websocket-example

Example WebSocket server of NestJS with Nestia
TypeScript
3
star
34

nestia-fetcher

Fetcher library for the Nestia
TypeScript
3
star
35

oss-2019

κ³΅κ°œμ†Œν”„νŠΈμ›¨μ–΄ κ°œλ°œμžλŒ€νšŒ 2019
CSS
2
star
36

URLVariables

URLVariables class for TypeScript
TypeScript
2
star
37

nestjs-custom-decorator-exception-filter

2
star
38

nestjs-study-router-module

To support RouterModule in Nestia
TypeScript
1
star
39

prisma-bug-report-postgres-timestampz

A prisma bug reproduction repo for reporting about `timestampz` type in Postgres
1
star
40

nestia-auto-crud-example

For https://github.com/samchon/nestia/issues/670
TypeScript
1
star
41

tgrid.com

Guide Documents for TGrid
HTML
1
star
42

tgrid.projects.market

Demo Project - Grid Market
TypeScript
1
star
43

gitbook-plugin-hide-navigation-buttons

Gitbook Plugin: Hide Navigation Buttons
CSS
1
star
44

timetable

Hansung Timetable
ActionScript
1
star
45

tgrid.examples

Example Source Codes for TGrid
TypeScript
1
star
46

samchon.github.io

My homepage
HTML
1
star
47

gitbook-plugin-scroll-to-top

JavaScript
1
star
48

ts-patch-typescript-v5.3-test

JavaScript
1
star
49

nestia-sdk-template

Nestia SDK Template Repository for `@nestia/migrate`
TypeScript
1
star