35
loading...
This website collects cookies to deliver better user experience
npm install fastify prisma
npx prisma init
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
provider
in this article will change it to sqlite and my database url
will look like this:datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
generator client {
provider = "prisma-client-js"
}
model Country {
// Stuff comes here.
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
generator client {
provider = "prisma-client-js"
}
model Country {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// More stuff comes here.
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
generator client {
provider = "prisma-client-js"
}
model Country {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
language String
population Int
}
npx prisma migrate dev --name initial
prisma
folder, you should have created a file called dev.db
.npm install @prisma/client
app
module (which has yet to be created) and we will create a function that will initialize our process. // @src/main.js
import app from "./app.js";
const start = async () => {
try {
await app.listen(3333);
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
app
module, which will hold all our application logic.// @src/app.js
import Fastify from "fastify";
import { PrismaClient } from "@prisma/client";
const app = Fastify();
const prisma = new PrismaClient();
// More stuff comes here
export default app;
.findMany()
method.app.get("/country", async (request, reply) => {
const countries = await prisma.country.findMany();
return reply.send({ countries });
});
.create()
method and pass the body of the http request.app.post("/country", async (request, reply) => {
const country = await prisma.country.create({ data: { ...request.body } });
return reply.send({ country });
});
id
. Then we will use Prism's .findUnique()
method.app.get("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.findUnique({
where: { id: Number(id) },
});
return reply.send({ country });
});
id
parameter. .update()
method to which we will pass the id of the country we intend to update and the object with the updated data of the respective country.app.put("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.update({
where: { id: Number(id) },
data: { ...request.body },
});
return reply.send({ country });
});
id
. Finally we will use the .delete()
method, to which we will pass the id of the country we want to delete.app.delete("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.delete({ where: { id: Number(id) } });
return reply.send({ country });
});
app
module should look like the following:// @src/app.js
import Fastify from "fastify";
import { PrismaClient } from "@prisma/client";
const app = Fastify();
const prisma = new PrismaClient();
app.get("/country", async (request, reply) => {
const countries = await prisma.country.findMany();
return reply.send({ countries });
});
app.post("/country", async (request, reply) => {
const country = await prisma.country.create({ data: { ...request.body } });
return reply.send({ country });
});
app.get("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.findUnique({
where: { id: Number(id) },
});
return reply.send({ country });
});
app.put("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.update({
where: { id: Number(id) },
data: { ...request.body },
});
return reply.send({ country });
});
app.delete("/country/:id", async (request, reply) => {
const { id } = request.params;
const country = await prisma.country.delete({ where: { id: Number(id) } });
return reply.send({ country });
});
export default app;