Skip to content

Commit

Permalink
feat: upgrade to fastify v5, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
theogravity committed Sep 22, 2024
1 parent dcd3774 commit ca0f5ea
Show file tree
Hide file tree
Showing 37 changed files with 1,128 additions and 628 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "theogravity/boilerplate-typescript-monorepo" }],
"changelog": ["@changesets/changelog-github", { "repo": "theogravity/fastify-starter-turbo-monorepo" }],
"access": "public",
"baseBranch": "main"
}
37 changes: 37 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Lint

on:
pull_request:

jobs:
lint:
name: Linting
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Use pnpm
uses: pnpm/action-setup@v3
with:
version: 9.11.0

- name: Install Dependencies
run: pnpm install --frozen-lockfile --prefer-offline

- name: Build workspace packages
run: pnpm build

- name: Run package checking
run: pnpm run lint:packages

- name: Run type checking
run: pnpm run typecheck

- name: Run linting
run: pnpm run lint
52 changes: 52 additions & 0 deletions .github/workflows/release-snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Snapshot Release

on:
workflow_dispatch:

env:
PNPM_CACHE_FOLDER: .pnpm-store

jobs:
snapshot:
name: Snapshot Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Use pnpm
uses: pnpm/action-setup@v3
with:
version: 9.11.0

- name: setup pnpm config
run: pnpm config set store-dir $PNPM_CACHE_FOLDER

- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Build workspace packages
run: pnpm build

- name: Run package checking
run: pnpm run lint:packages

- name: Run type checking
run: pnpm run typecheck

- name: Run linting
run: pnpm run lint

- name: Create Snapshot Release
run: |
pnpm run version-packages --snapshot "${{ github.ref_name }}"
echo '---'
echo 'Detected Changes:'
git diff
echo '---'
pnpm run release --tag "${{ github.ref_name }}" --no-git-tag
58 changes: 58 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Release

on:
push:
branches:
- main

env:
PNPM_CACHE_FOLDER: .pnpm-store

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Releasing
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Use pnpm
uses: pnpm/action-setup@v3
with:
version: 9.11.0

- name: setup pnpm config
run: pnpm config set store-dir $PNPM_CACHE_FOLDER

- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Build workspace packages
run: pnpm build

- name: Run package checking
run: pnpm run lint:packages

- name: Run type checking
run: pnpm run typecheck

- name: Run linting
run: pnpm run lint

- name: Create Release Pull Request / Publish Packages
uses: changesets/action@v1
with:
publish: pnpm run release
version: pnpm run version-packages
commit: 'chore: release package(s)'
title: 'chore: release package(s)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test

on:
pull_request:

jobs:
test:
name: Testing
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 2

- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Use pnpm
uses: pnpm/action-setup@v3
with:
version: 9.11.0

- name: Install Dependencies
run: pnpm install --frozen-lockfile --prefer-offline

- name: Build workspace packages
run: pnpm build

- name: Branch Information
run: |
echo "Git Branch: $(git branch)"
echo "Git Log: $(git log --oneline)"
echo "HEAD SHA: $(git rev-parse HEAD)"
echo "HEAD^1 SHA: $(git rev-parse HEAD^1)"
echo "Git Diff: $(git diff HEAD^1)"
- name: Run Package(s) Tests
run: |
pnpm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ dist
.pnp.*
.idea/
.turbo/
.hashes.json
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# boilerplate-typescript-monorepo

## Sept-21-2024

- Upgrade fastify to v5, all packages to latest versions
- Better error handling for the backend
- Fixed bugs around logs not showing in certain cases during tests
- Add package publishing support
- Add dev build caching for faster builds during dev
- Removed AJV sanitize plugin. In real-world usage, you wouldn't want to sanitize the input to your API immediately, but later on.


## Sept-07-2024

- Added better formatting for logs
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Fastify Turbo Monorepo Starter

This is a starter project for building an API server using Typescript, Fastify and Kysely with Postgres.
This is a starter project for building an API server using Typescript, Fastify v5 and Kysely with Postgres.

## Features

- Fastify v5 with Typescript.
- It is setup as a monorepo using [`turbo`](https://turbo.build/) and [`pnpm`](https://pnpm.io/).
- Outputs OpenAPI schema for the API and has a web UI for viewing it.
- A sample REST test is included using Vitest.
Expand All @@ -20,6 +21,7 @@ This is a starter project for building an API server using Typescript, Fastify a
- [`commitlint`](https://commitlint.js.org/) for commit message linting
- [`turbo`](https://turbo.build/) for monorepo management
- [`fastify`](https://www.fastify.io/) for the API server framework
- [`hash-runner`](https://github.com/theogravity/hash-runner) for caching builds
- [`kysely`](https://kysely.dev/) for the database query builder
- [`postgres`](https://www.postgresql.org/) + pgAdmin for the database
- [`testcontainers`](https://www.testcontainers.org/) for testing with a sandboxed postgres instance
Expand All @@ -45,6 +47,14 @@ Perform database migrations:

`pnpm db:migrate:lastest`

In Github settings (to publish packages and changelogs):
- Edit `.changeset/config.json` to your repository
- `Code and Automation > Actions > Workflow permissions`
* `Read and write permissions`
* `Allow Github Actions to create and approve pull requests`
- `Secrets and variables > Actions`
* `Repository Secrets > Actions > create NPM_TOKEN > your npm publish token`

## Development

`turbo watch dev`
Expand Down Expand Up @@ -83,15 +93,16 @@ Generators for the following:

`pnpm syncpack:update`

## Add a new CHANGELOG.md entry + package versioning

Create a new changeset entry:

`pnpm changeset`
## Development workflow / Add a new CHANGELOG.md entry + package versioning

Update all package versions and generate changelogs:
- Create a branch and make changes.
- Create a new changeset entry: `pnpm changeset`
- Commit your changes and create a pull request.
- Merge the pull request
- A new PR will be created with the changeset entry/ies.
- When the PR is merged, the package versions will be bumped and published and the changelog updated.

`pnpm changeset version`
**note: To publish a package, `private: false` must be set in the package.json**

## Troubleshooting

Expand Down
5 changes: 5 additions & 0 deletions apps/backend/.hash-runnerrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": ["src/**", "package.json"],
"execOnChange": "pnpm run build",
"hashFile": ".hashes.json"
}
46 changes: 24 additions & 22 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,57 @@
"db:migrate:latest": "kysely migrate latest",
"db:migrate:undo": "kysely migrate down",
"generate": "NODE_OPTIONS=\"--import=./loader.js\" IS_GENERATING_CLIENT=true fastify generate-swagger --yaml=true dist/api/index.js > ../../packages/backend-client/openapi.yml | tee ../../packages/backend-client/openapi.yml",
"build:dev": "hash-runner",
"build": "rm -rf dist && tsc -p tsconfig.json && pnpm run generate",
"clean": "rm -rf .turbo node_modules dist",
"dev": "tsx --inspect=9230 ./src/index.ts",
"lint": "biome check --write --unsafe src && biome format src --write && biome lint src --fix",
"prod": "NODE_OPTIONS=\"--import=./loader.js\" node ./dist/index.js",
"typecheck": "tsc --noEmit",
"test": "vitest --watch",
"test:ci": "vitest"
"test": "NODE_OPTIONS=\"--no-deprecation\" IS_TEST=true TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 vitest --watch",
"test:debug": "NODE_OPTIONS=\"--no-deprecation\" IS_TEST=true TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 vitest --inspect-brk --pool forks --poolOptions.forks.singleFork --no-file-parallelism",
"test:ci": "NODE_OPTIONS=\"--no-deprecation\" IS_TEST=true TESTCONTAINERS_HOST_OVERRIDE=127.0.0.1 vitest --watch=false"
},
"dependencies": {
"@dotenvx/dotenvx": "1.14.0",
"@fastify/cors": "9.0.1",
"@fastify/jwt": "8.0.1",
"@fastify/swagger": "8.15.0",
"@fastify/swagger-ui": "4.1.0",
"@fastify/type-provider-typebox": "4.1.0",
"@dotenvx/dotenvx": "1.14.1",
"@fastify/cors": "10.0.0",
"@fastify/jwt": "9.0.0",
"@fastify/swagger": "9.0.0",
"@fastify/swagger-ui": "5.0.1",
"@fastify/type-provider-typebox": "5.0.0",
"@internal/backend-errors": "workspace:*",
"@sinclair/typebox": "0.33.9",
"@sinclair/typebox": "0.33.12",
"ajv-formats": "3.0.1",
"ajv-sanitizer": "1.2.1",
"asyncforge": "0.5.0",
"bcrypt": "5.1.1",
"chalk": "5.3.0",
"dotenv": "16.4.5",
"env-var": "7.5.0",
"fastify": "4.28.1",
"fastify-cli": "6.3.0",
"fastify-plugin": "4.5.1",
"fastify": "5.0.0",
"fastify-cli": "7.0.0",
"fastify-plugin": "5.0.1",
"kysely": "0.27.4",
"loglayer": "4.7.0",
"nanoid": "5.0.7",
"pg": "8.12.0",
"pg": "8.13.0",
"pino": "9.4.0",
"pino-pretty": "11.2.2",
"serialize-error": "11.0.3",
"tsc-module-loader": "0.0.1",
"uuid": "10.0.0"
},
"devDependencies": {
"@faker-js/faker": "9.0.0",
"@faker-js/faker": "9.0.1",
"@internal/tsconfig": "workspace:*",
"@testcontainers/postgresql": "10.13.0",
"@turbo/gen": "2.1.1",
"@types/node": "22.5.4",
"@types/pg": "8.11.8",
"@testcontainers/postgresql": "10.13.1",
"@turbo/gen": "2.1.2",
"@types/node": "22.5.5",
"@types/pg": "8.11.10",
"hash-runner": "2.0.1",
"kysely-ctl": "0.9.0",
"ts-node": "10.9.2",
"tsx": "4.19.0",
"typescript": "5.5.4",
"vitest": "2.0.5"
"tsx": "4.19.1",
"typescript": "5.6.2",
"vitest": "2.1.1"
}
}
3 changes: 0 additions & 3 deletions apps/backend/src/api-lib/ajv-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import ajvFormats from "ajv-formats";
import ajvSanitizer from "ajv-sanitizer";

export const ajvPlugins = [
// https://www.npmjs.com/package/ajv-sanitizer
ajvSanitizer,
// https://ajv.js.org/packages/ajv-formats.html
ajvFormats,
];
Loading

0 comments on commit ca0f5ea

Please sign in to comment.