This project is a basic Node.js API built with Express and Prisma. It includes basic functionality for fetching users from a database and tests for the API endpoints.
- Node.js: JavaScript runtime environment.
- Express: Web framework for building APIs.
- Prisma: ORM for interacting with the database.
- Jest: Testing framework for writing unit and integration tests.
- TypeScript: Typed superset of JavaScript.
- Supertest: HTTP assertions library used for testing API endpoints.
Ensure you have Node.js (v18 or above) installed. You can download it from the official website: Node.js.
-
Clone the repository:
git clone <repository-url> cd <project-directory>
-
Install the dependencies:
npm install
-
Set up the Prisma database schema. Make sure you have a running database and have set up the connection in the
.envfile. -
To generate Prisma client and perform initial setup:
npx prisma generate
To start the development server:
npm run devThis will start the server at http://localhost:3000. You can use this endpoint to test the API:
- GET /api/users: Fetches a list of users from the database.
You can seed the database with sample data by running the following command:
npm run prisma seedThis will execute the prisma/seed.ts file and populate the database with initial data.
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
await prisma.user.create({
data: {
name: 'John Doe',
email: 'john.doe@example.com',
},
});
console.log('User created');
}
main()
.catch(e => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});To run tests, simply run:
npm testimport request from 'supertest';
import { app } from '../src/app'; // import the app
describe("API Test", () => {
it("should return users", async () => {
const res = await request(app).get("/api/users");
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("users");
});
});Your jest.config.ts should look like this to properly work with TypeScript:
import type { JestConfigWithTsJest } from 'ts-jest';
const config: JestConfigWithTsJest = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
};
export default config;-
src: Source code for the application.controllers: Contains logic for handling requests and interacting with the database.routes: Defines API routes and routes to controllers.app.ts: Main entry point of the application, initializes Express and routes.
-
prisma: Contains Prisma schema and seed file.schema.prisma: Defines the database schema and models.seed.ts: Seeds the database with sample data.
-
__tests__: Contains the test files for the application.
- Prisma Client: After modifying the Prisma schema, remember to run
npx prisma generateto regenerate the Prisma client. - Environment Variables: Ensure that your
.envfile is set up with the correct database connection.
This project is licensed under the ISC License - see the LICENSE file for details.
This README.md includes all the information you need to run the project, seed the database, and run tests. Let me know if you'd like to modify or add anything else!