We use Prisma ORM to handle the database. You can create new models from the schema.prisma file located in the app/database/schema.prisma
directory. You can also export the models, for easier use, from app/database/index.ts
.
In the .env file, the DB_URL variable changes according to the bank you choose, being:
DB_URL=postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}
DB_URL=mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}
DB_URL="file:./dev.db"
If you need to execute raw queries, you can use the Database handler located in app/database/database.ts
. This handler provides an abstraction over Prisma’s native functions, making it easier to work with database operations.
For example, instead of doing:
const [posts, totalPosts] = await prisma.$transaction([
prisma.post.findMany({ where: { title: { contains: "prisma" } } }),
prisma.post.count(),
]);
You can use the Database handler like this:
const [posts, totalPosts] = await Database.transaction([
Post.findMany({ where: { title: { contains: "prisma" } } }),
Post.count(),
]);
This abstraction allows you to interact with your models in a more streamlined way, making your code cleaner and easier to maintain.
The Database handler provides the following methods for interacting with the database:
transaction
: Executes multiple Prisma operations in a transaction.queryRaw
: Executes a raw SQL query (returns data).queryRawUnsafe
: Executes a raw SQL query with potentially unsafe queries (use with caution).executeRaw
: Executes a raw SQL command (does not return data).executeRawUnsafe
: Executes a raw SQL command with potentially unsafe queries (use with caution).Each of these methods is bound to Prisma’s corresponding functions, allowing for consistent use across your application.