Database Integration with Prisma
A web application is rarely complete without a database. This recipe will show you how to integrate a database with your Minima.js application using Prisma, a popular open-source database toolkit for Node.js and TypeScript.
We will use Prisma to connect to a SQLite database, but the same principles apply to other databases supported by Prisma (like PostgreSQL, MySQL, etc.).
Prerequisites
First, you need to install the required packages:
npm install prisma @prisma/client1. Setting up Prisma
The first step is to initialize Prisma in your project.
npx prisma init --datasource-provider sqliteThis command will create a prisma directory with a schema.prisma file. This file contains your database schema.
Let's define a simple User model in prisma/schema.prisma:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}Now, you can use the Prisma CLI to create the database and generate the Prisma Client.
npx prisma migrate dev --name initThis command will:
- Create your SQLite database file.
- Create the
Usertable in your database. - Generate the Prisma Client library in
node_modules/@prisma/client.
2. Connecting to the Database
We need to connect to the database when the application starts and disconnect when it stops. The hook.lifespan utility is perfect for this. It is best to register this in your root module.
import { hook, logger } from "@minimajs/server";
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
export const dbLifespan = hook.lifespan(async () => {
await prisma.$connect();
logger.info("Database connected");
return async () => {
await prisma.$disconnect();
logger.info("Database disconnected");
};
});import { type Meta } from "@minimajs/server";
import { dbLifespan } from "./database.js";
// Global database connection management
export const meta: Meta = {
plugins: [dbLifespan],
};import { createApp } from "@minimajs/server/bun";
const app = createApp(); // Auto-discovers root module with dbLifespan
await app.listen({ port: 3000 });3. Using the Prisma Client in Routes
Now that we have connected to the database, we can use the Prisma Client in our route handlers to query the database.
import { body, type Routes } from "@minimajs/server";
import { prisma } from "../database.js";
async function listUsers() {
const users = await prisma.user.findMany();
return users;
}
async function createUser() {
const { name, email } = body<{ name: string; email: string }>();
const newUser = await prisma.user.create({
data: {
name,
email,
},
});
return newUser;
}
export const routes: Routes = {
"GET /": listUsers,
"POST /": createUser,
};4. REST API Example
To keep our code organized, we encapsulate the database-related logic in a feature-based module structure.
src/
├── database.ts # Prisma instance & lifespan hook
├── module.ts # Root module (global config)
├── index.ts # Entry point
└── users/
└── module.ts # CRUD for /usersThis approach makes our code more modular and easier to maintain. You now have a solid foundation for building database-driven applications with Minima.js and Prisma!