A simple RESTful API for managing financial transactions, built with Fastify, Knex.js, and SQLite. This project demonstrates session-based transaction management, environment configuration, and automated testing.
- Create, list, and retrieve transactions
- Session-based transaction isolation using cookies
- Transaction summary endpoint
- SQLite database with migrations via Knex.js
- Environment variable management with dotenv and zod validation
- Automated tests with Vitest and Supertest
- Linting and formatting with ESLint and Prettier
-
Clone the repository:
git clone https://github.com/your-username/api_rest_node_fastfy.git cd api_rest_node_fastfy -
Install dependencies:
npm install
-
Copy the example environment file and configure as needed:
cp .env.example .env
Edit
.envto set yourDATABASE_URLand other variables.
Run the database migrations:
npm run knex migrate:latestStart the development server:
npm run devThe server will run at http://localhost:3333 by default.
To run the test suite:
npm testAll endpoints are prefixed with /transactions.
- POST
/transactions - Body:
{ "title": "New transaction", "amount": 5000, "type": "credit" // or "debit" } - Response:
201 Created
- GET
/transactions - Response:
{ "transactions": [ { "id": "...", "title": "...", "amount": 5000, "created_at": "...", "session_id": "..." } ] }
- GET
/transactions/:id - Response:
{ "transaction": { "id": "...", "title": "...", "amount": 5000, "created_at": "...", "session_id": "..." } }
- GET
/transactions/summary - Response:
{ "summary": { "amount": 3000 } }
.
├── src/
│ ├── app.ts
│ ├── server.ts
│ ├── database.ts
│ ├── routes/
│ │ └── transactions.ts
│ ├── middlewares/
│ │ └── check-session-id-exists.ts
│ ├── env/
│ │ └── index.ts
│ ├── test/
│ │ └── transactions.spec.ts
│ └── @types/
│ └── knex.d.ts
├── db/
│ ├── migrations/
│ └── app.db
├── .env.example
├── .env.test.example
├── package.json
├── tsconfig.json
└── README.md
npm run dev— Start the server in development modenpm run build— Build the projectnpm run knex— Run Knex CLI commands (e.g., migrations)npm run lint— Lint and fix codenpm test— Run tests
This project is licensed under the ISC License.
Made with ❤️ using Fastify, Knex, and SQLite.