Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Oct 8, 2024
0 parents commit 8c57d76
Show file tree
Hide file tree
Showing 36 changed files with 12,624 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .cursorrules
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
9 changes: 9 additions & 0 deletions .editorconfig
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
41 changes: 41 additions & 0 deletions .env.example
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
77 changes: 77 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
8 changes: 8 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"lines": 95,
"branches": "82",
"statements": "95",
"exclude": [
"src/core/**"
]
}
21 changes: 21 additions & 0 deletions LICENSE
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.
105 changes: 105 additions & 0 deletions README.md
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
9 changes: 9 additions & 0 deletions drizzle.config.js
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,
})
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { standard } from 'eslint-config-standard-ext'

export default standard()
20 changes: 20 additions & 0 deletions migrations/0000_omniscient_malice.sql
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 $$;
Loading

0 comments on commit 8c57d76

Please sign in to comment.