-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8c57d76
Showing
36 changed files
with
12,624 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
This a starter project to build libraries for node.js. | ||
|
||
You are a programmer that uses several tools and best practices. | ||
|
||
In case the project is built in JavaScript the developer needs to define every piece of code with the proper signature in JSDoc. | ||
|
||
The project is set up to use: | ||
|
||
- node:test for the testing framework | ||
- node:assert for the assertions | ||
- prefers to use `describe` and `it` for the tests | ||
- JavaScript with stricter JSDoc or TypeScript | ||
- eslint for linting | ||
- eslint-config-standard-ext for the style guide | ||
- tsc for the compiler | ||
- @sinclair/typebox to define any JSON schema | ||
- fastify for the server | ||
- @fastify/type-provider-typebox for the type-safe Fastify | ||
- @fastify/autoload for the auto-loading plugins and routes | ||
- drizzle-orm for the ORM | ||
- scalar for the documentation UI | ||
- @snaplet/copycat to help to generate random data seeds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Node environment: development, production, or test | ||
NODE_ENV=production | ||
|
||
# Server host | ||
HOST=127.0.0.1 | ||
|
||
# Server port | ||
PORT=3000 | ||
|
||
# Log level (optional): info, error, debug, fatal, warn, trace, child | ||
# LOG_LEVEL=info | ||
|
||
# Database configuration | ||
# For in-memory database: | ||
DATABASE_IN_MEMORY=true | ||
|
||
# For external database: | ||
# DATABASE_HOST=localhost | ||
# DATABASE_PORT=5432 | ||
# DATABASE_USER=your_username | ||
# DATABASE_PASSWORD=your_password | ||
# DATABASE_DB=your_database_name | ||
# DATABASE_PATH=/optional/path | ||
# DATABASE_SSL=false | ||
# DATABASE_MAX=10 | ||
# DATABASE_MAX_LIFETIME=3600000 | ||
# DATABASE_IDLE_TIMEOUT=30000 | ||
# DATABASE_CONNECT_TIMEOUT=5000 | ||
|
||
# Database operations | ||
DATABASE_RUN_SEED=false | ||
DATABASE_RUN_MIGRATE=false | ||
|
||
# JWT configuration | ||
JWT_SECRET=your_jwt_secret_key | ||
# JWT_CACHE=false | ||
# JWT_CACHE_TTL=60000 | ||
|
||
# OpenAPI configuration | ||
OPENAPI_ENABLED=false | ||
# OPENAPI_UI=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Dependency directories | ||
node_modules/ | ||
|
||
# TypeScript compiled output | ||
dist/ | ||
build/ | ||
|
||
# Environment variables | ||
.env | ||
.env.local | ||
.env.*.local | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# IDEs and editors | ||
.idea/ | ||
.vscode/ | ||
*.swp | ||
*.swo | ||
.DS_Store | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.* | ||
!.env.example | ||
|
||
# Database files | ||
*.sqlite | ||
*.sqlite3 | ||
|
||
# Temporary files | ||
tmp/ | ||
temp/ | ||
|
||
# OS generated files | ||
.DS_Store | ||
.DS_Store? | ||
._* | ||
.Spotlight-V100 | ||
.Trashes | ||
ehthumbs.db | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"lines": 95, | ||
"branches": "82", | ||
"statements": "95", | ||
"exclude": [ | ||
"src/core/**" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Martin Acosta | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Fastify TypeScript Starter | ||
|
||
A robust and modern starter template for building high-performance web applications using Fastify, TypeScript, and Drizzle ORM. | ||
|
||
## 🚀 Features | ||
|
||
- **[Fastify](https://www.fastify.io/)**: High-performance web framework for Node.js | ||
- **[TypeScript](https://www.typescriptlang.org/)**: Strongly typed programming language that builds on JavaScript | ||
- **[Fastify UWS](https://github.com/geut/fastify-uws)**: Push your Fastify server performance to the next level | ||
- **[Drizzle ORM](https://github.com/drizzle-team/drizzle-orm)**: TypeScript ORM for SQL databases with a focus on type safety | ||
- **[PostgreSQL](https://www.postgresql.org/)**: Powerful, open-source relational database | ||
- **[node:test](https://nodejs.org/api/test.html)**: Built-in testing framework for Node.js | ||
- **[pglite](https://github.com/electric-sql/pglite)**: In-memory PostgreSQL database for testing | ||
- **[Standard Ext](https://github.com/tinchoz49/eslint-config-standard-ext)**: JavaScript style guide, linter, and formatter | ||
- **[Typebox](https://github.com/sinclair/typebox)**: JSON schema based validation and generation | ||
- **[Copycat](https://github.com/snaplet/copycat)**: Deterministic data generation for testing and seeding | ||
- **[typebox-env](https://github.com/lucasvieirasilva/typebox-env)**: Environment variables parsing and validation | ||
|
||
## Fastify Plugins | ||
|
||
- **[@fastify/auth](https://github.com/fastify/fastify-auth)**: Authentication plugin for Fastify | ||
- **[@fastify/cors](https://github.com/fastify/fastify-cors)**: Enable CORS | ||
- **[@fastify/helmet](https://github.com/fastify/fastify-helmet)**: Secure the application by setting various HTTP headers | ||
- **[@fastify/swagger](https://github.com/fastify/fastify-swagger)**: Document the API using Swagger | ||
- **[@scalar/fastify-api-reference](https://github.com/scalar/scalar/tree/main/packages/fastify-api-reference)**: OpenAPI/Swagger interactive API documentation | ||
- **[@fastify/type-provider-typebox](https://github.com/fastify/fastify-type-provider-typebox)**: Type-safe schema definition language | ||
- **[fastify-better-error](https://github.com/tinchoz49/fastify-better-error)**: Supercharge Fastify error handling! Streamline definitions, automate schemas, and boost productivity with built-in HTTP errors and robust TypeScript support. Make errors work for you! | ||
|
||
## 🧰 Commands | ||
|
||
- `npm run dev`: Start the development server. | ||
- `npm run build`: Build the application. | ||
- `npm run start`: Start the application. | ||
- `npm run test`: Run the test cases. | ||
- `npm run coverage`: Generate the coverage report. | ||
- `npm run lint`: Run the linter. | ||
- `npm run db:generate`: Generate the migration file. | ||
- `npm run db:migrate`: Apply the migration to the database. | ||
- `npm run db:check`: Check the migration file. | ||
|
||
## 📁 Structure | ||
|
||
- `src/`: Source code. | ||
- `src/app.ts`: Fastify application. | ||
- `src/serve.ts`: Entry point. | ||
- `src/env.ts`: TypeBox schema for environment variables. | ||
- `src/errors.ts`: Custom errors. | ||
- `src/plugins/`: User defined Fastify plugins. | ||
- `src/routes/`: Fastify API routes. | ||
- `src/schemas/`: TypeBox schemas. | ||
- `src/db/`: Drizzle ORM. | ||
- `dist/`: Build output. | ||
- `test/`: Test cases. | ||
- `migrations/`: Drizzle migrations. | ||
|
||
## Development | ||
|
||
Start by creating a new `.env` file and setting the correct environment variables. | ||
|
||
```bash | ||
cp .env.example .env | ||
``` | ||
|
||
```dotenv | ||
# Node environment: development, production, or test | ||
NODE_ENV=development | ||
# Server host | ||
HOST=127.0.0.1 | ||
# Server port | ||
PORT=3000 | ||
# Log level (optional): info, error, debug, fatal, warn, trace, child | ||
LOG_LEVEL=info | ||
# Database configuration | ||
# For in-memory database: | ||
DATABASE_IN_MEMORY=true | ||
# Database operations | ||
DATABASE_RUN_SEED=true | ||
DATABASE_RUN_MIGRATE=true | ||
# JWT configuration | ||
JWT_SECRET=your_jwt_secret_key | ||
# JWT_CACHE=false | ||
# JWT_CACHE_TTL=60000 | ||
# OpenAPI configuration | ||
OPENAPI_ENABLED=true | ||
OPENAPI_UI=true | ||
``` | ||
|
||
```bash | ||
$ npm run dev | ||
``` | ||
|
||
## Issues | ||
|
||
:bug: If you found an issue we encourage you to report it on [github](https://github.com/tinchoz49/typebox-env/issues). Please specify your OS and the actions to reproduce it. | ||
|
||
## License | ||
|
||
MIT © 2024 Martin Acosta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'drizzle-kit' | ||
|
||
export default defineConfig({ | ||
schema: './src/db/schema.js', | ||
out: './migrations', | ||
dialect: 'postgresql', | ||
verbose: true, | ||
strict: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { standard } from 'eslint-config-standard-ext' | ||
|
||
export default standard() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
CREATE TABLE IF NOT EXISTS "posts" ( | ||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
"title" varchar(256) NOT NULL, | ||
"content" varchar(256) NOT NULL, | ||
"author_id" uuid NOT NULL | ||
); | ||
--> statement-breakpoint | ||
CREATE TABLE IF NOT EXISTS "users" ( | ||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
"username" varchar(256) NOT NULL, | ||
"password_hash" text NOT NULL, | ||
"email" varchar(256) NOT NULL, | ||
CONSTRAINT "users_username_unique" UNIQUE("username") | ||
); | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "posts" ADD CONSTRAINT "posts_author_id_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; |
Oops, something went wrong.