Database
We are using mongodb
with Prisma ORM
to make database
operations if you are not familiar with Prisma
it has
a very easy learning curve, follow the guide below.
We are planning to support postgresql
in the future.
Setup
- Create an account on atlas
- Create a database guide here
- Select Atlas UI tab
- Copy your database URL to
.env
at the root of the project folder,URL
looks similar to theURL
below.- Make sure to add username, password and rename your database by replacing
project_name
in theURL
below
- Make sure to add username, password and rename your database by replacing
.env
DATABASE_URL=mongodb+srv://username:password@cluster0.q8e4oaz.mongodb.net/project_name?retryWrites=true&w=majority&appName=Cluster0
Using Prisma
- Import
db
fromlib/db
to interact with the database on the server
/src/app/(site)/example_page/page.tsx
import { db } from "lib/db";
- Let's fetch all active products from the database
/src/app/(site)/example_page/page.tsx
const ExamplePage = async () => {
const products = await db.findMany({
where: { active: true },
});
return (
<div>
{products.map((product) => (
<p key={product.id}>{product.title}</p>
))}
</div>
);
};
export default ExamplePage;
That's it!
Learn Prisma
Follow the guide here