Skip to content

This repository is a template for creating a modular monolith on Fastify, designed for small projects. It combines best practices and serves as an efficient alternative to Nest.js.

Notifications You must be signed in to change notification settings

256Taras/fastify-drizzle-orm-starter-kit

Repository files navigation

⬢ Node.js Application Development Starter Kit ⬢

The Starter Kit offers tools and features tailored for efficient application development using Node.js. It aims to streamline the process, especially for MVPs and scalable applications.

🗝️ Key Features & Benefits:

  • Modular Structure: Organized pattern for application development.
  • Latest Node.js Capabilities: Utilize contemporary Node.js features without relying on Babel or TypeScript.
  • Fastify Integration: Fast and efficient HTTP servers with asynchronous support.
  • Drizzle-orm Integration: Simplified database connectivity and operations.
  • Architectural Flexibility: Suitable for MVPs, with easy adaptability for future architectural shifts.
  • Alternative to Nest.js: Offers core functionalities without additional complexities.
  • Optimized Infrastructure: Prioritizes efficiency and performance.
  • Clear Code Base: Organized structure for easier navigation and modification.
  • Adaptable Components: Components designed for customization based on project requirements.

📚 Prerequisites

We assume that everyone who comes here is programmer with intermediate knowledge and we also need to understand more before we begin in order to reduce the knowledge gap.

  1. Understand Fastify Fundamental, Main Framework. Fast and low overhead web framework, for Node.js
  2. Understand Express Fundamental, NodeJs Base Framework. Since the project is based on fastify, which is trying to solve problems specifically express
  3. Understand The YAGNI Principle and KISS Principle for better write the code.
  4. Optional. UnderstandTypescript Fundamental, Programming Language. It will help to write us contacts of types
  5. Optional. Understanding The Twelve Factor Apps. It can help to serve the project.
  6. Optional. Understanding Docker. It can help to run the project.

⚠️ Requirements

Name Version
Node v20.11.x
Typescript v5.4.x
PostgreSQL v15.0.x
Yarn v1.22.x
NPM v8.19.x
Docker v18.10.x
Docker Compose v1.23.x

📌 Getting Started

Before start, we need to install some packages and tools. The recommended version is the LTS version for every tool and package.

Make sure to check that the tools have been installed successfully.

Clone Repo

  1. Clone the project with git.
git clone https://github.com/256Taras/fastify-drizzle-orm-starter-kit

Install Dependencies

Optional: Use nvm use command which automatically will setup node of specified version in .nvmrc file.

  1. This project needs some dependencies. Let's go install it.
yarn install

Create environment

  1. Create .environment file in configs folder. Use .environment.example as example. .environment.example has already fully functional settings, so server can be started with it without changes
cp .environment.example .environment

Database Migration

  1. Run migrations
yarn typeorm:migration:run

🏃 Run Project

  1. Run infrastructure in docker (db, etc.):
  yarn docker:infra:up
  1. Run server on local machine without docker:
  yarn start

Finally, Cheers 🍻🍻 !!! you passed all steps.

API Reference

You can check The ApiSpec after running this project. here

🧪 Testing

  1. Run infrastructure in docker (db, etc.):
yarn docker:infra:up

🤌 Development culture

  • split by modules (scalability & better dev experience)
  • store as much as can boilerplate code in shared
  • has additional layers for easy cognitive perception:
    • handlers
    • services
    • scopes (extracted from infra)

🔐 Git & Commit culture

Prefer to use Git Flow process.

Commit culture is based on this article, here are seven simple rules:

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters
  3. Capitalize the subject line
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how

Commit Convention is described in this article, here are seven simple rules:

  1. Each commit message consists of a header, a body, and a footer.
   <header>
   <BLANK LINE>
   <body>
   <BLANK LINE>
   <footer>
  • The header is mandatory and must conform to the Commit Message Header format.
  • The body is mandatory for all commits except for those of type "docs". When the body is present it must be at least 20 characters long and must conform to the Commit Message Body format.
  • The footer is optional. The Commit Message Footer format describes what the footer is used for and the structure it must have.
  1. Commit Message Header

Format of the commit message

<type>(<scope>): <short summary>
│       │             │
│       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
│       │
│       └─⫸ Commit Scope: common|core|account-crud|migrations
│
└─⫸ Commit Type: build|docs|feat|fix|refactor|test

The and

fields are mandatory, the () field is optional.

  1. Type

    Must be one of the following:

  • build: Changes that affect the build system or external dependencies (example scopes: npm, infra)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • refactor: A code change that neither fixes a bug nor adds a feature
  • test: Adding missing tests or correcting existing tests
  1. Scope

    Usually the scope should be the name of task.

  • none/empty string: useful for test and refactor changes that are done across all packages (e.g. test: add missing unit tests) and for docs changes that are not related to a specific package (e.g. docs: fix typo in tutorial).
  1. Summary

    Use the summary field to provide a succinct description of the change:

  • use the imperative, present tense: "change" not "changed" nor "changes"
  • don't capitalize the first letter
  • no dot (.) at the end
  1. Commit Message Body

    Use the body to explain what and why vs. how

  2. Commit Message Footer

    The footer can contain information about breaking changes and deprecations and is also the place to reference GitHub issues, Jira tickets, and other PRs that this commit closes or is related to.

💡 Features

  • production ready
  • automatic loading of modules and plugins
  • validate environment:
  • main script: initialize infra for proper server start, start server, add server stop handlers
  • graceful shutdown
  • configs: separate folder, split by files, setup using process environment
  • fastify application server
  • auto documentation: fastify-swagger. *Require fastify input & output schemas
  • routers with input/output validation
  • separate router handler
  • logger powered by 'pino'
    • console interface
    • log levels
    • trace ID
  • loggerService service implementation with LOG_LEVELs: wrapped pino into console.log interface
  • containerize in docker server and all 3rd parties if possible:
    • server
    • db (postgres)
  • DI: At the file system level
  • TDD: node:test or tap, DI, unit-tests, e2e
  • [N-layer architecture] (#architecture-description-&-maintain-instructions)
  • [] db:
    • drizzle-orm
    • postgres-js
    • separate loggerService for db logging
    • cursor pagination
  • local code style support:
    • esLint setup
    • prettier setup
    • editorconfig
    • optional: words spelling checker (VScode plugin)
  • [] built-in Auth:
    • bearer token (JWT) (Manually)
    • [] API_KEY (Manually)
  • permissions checks:
  • rate limiting (https://www.npmjs.com/package/fastify-rate-limit)
    • global rate limit
    • rate limit per route
  • request timeouts (implement manually)
  • request-scoped storage support, based on Asynchronous Local Storage to receive data without mutation request
  • templates rendering support
  • ability to run CPU intensive tasks in the Piscina worker pool
  • covering utilities with .d.ts files
  • support Docker installation

🙋 FAQ

View FAQ

📝 Docs

About

This repository is a template for creating a modular monolith on Fastify, designed for small projects. It combines best practices and serves as an efficient alternative to Nest.js.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages