29
loading...
This website collects cookies to deliver better user experience
npm install fastify sequelize sqlite3
## If you don't use SQLite, replace sqlite3 with the following:
# MySQL => mysql2
# PostgreSQL => pg
// @src/database/index.js
import Sequelize from "sequelize";
const connection = new Sequelize({
dialect: "sqlite",
storage: "src/database/dev.db",
});
export default connection;
// @src/models/product.js
import Sequelize from "sequelize";
import database from "../database/index.js";
// More stuff comes here.
product
and let's export it before we define each of its attributes.// @src/models/product.js
import Sequelize from "sequelize";
import database from "../database/index.js";
const Product = database.define("product", {
// More stuff comes here.
});
export default Product;
id
which is going to be a primary key, auto-incrementing and which is an integer. name
and description
. price
which is going to be a decimal number.// @src/models/product.js
import Sequelize from "sequelize";
import database from "../database/index.js";
const Product = database.define("product", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
price: {
type: Sequelize.DECIMAL,
allowNull: false,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
});
export default Product;
// @src/main.js
import app from "./app.js";
import database from "./database/index.js";
const start = async () => {
try {
await database.sync();
await app.listen(3333);
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
// @src/main.js
import "./models/product.js";
import app from "./app.js";
import database from "./database/index.js";
const start = async () => {
try {
await database.sync();
await app.listen(3333);
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
// @src/app.js
import Fastify from "fastify";
import Product from "./models/product.js";
const app = Fastify();
// More stuff comes here.
export default app;
.findAll()
to get all the records we have in the product
table.// @src/app.js
app.get("/product", async (request, reply) => {
const post = await Product.findAll();
return reply.send({ post });
});
.create()
method to add each of the properties present in the http request body. And then we will return the product data that was entered into the database.// @src/app.js
app.post("/product", async (request, reply) => {
const post = await Product.create({ ...request.body });
return reply.send({ post });
});
id
. .findByPk()
method which will fetch a product in the database table according to the primary key.// @src/app.js
app.get("/product/:id", async (request, reply) => {
const { id } = request.params;
const post = await Product.findByPk(id);
return reply.send({ post });
});
.update()
and we will pass two arguments, the first will be the updated object
and the second will be its id
to perform the update.// @src/app.js
app.put("/product/:id", async (request, reply) => {
const { id } = request.params;
await Product.update({ ...request.body }, { where: { id } });
const post = await Product.findByPk(id);
return reply.send({ post });
});
.destroy()
method and we will pass only the id
of the product we want to delete.// @src/app.js
app.delete("/product/:id", async (request, reply) => {
const { id } = request.params;
const post = await Product.findByPk(id);
await Product.destroy({ where: { id } });
return reply.send({ post });
});
// @src/app.js
import Fastify from "fastify";
import Product from "./models/product.js";
const app = Fastify();
app.get("/product", async (request, reply) => {
const post = await Product.findAll();
return reply.send({ post });
});
app.post("/product", async (request, reply) => {
const post = await Product.create({ ...request.body });
return reply.send({ post });
});
app.get("/product/:id", async (request, reply) => {
const { id } = request.params;
const post = await Product.findByPk(id);
return reply.send({ post });
});
app.put("/product/:id", async (request, reply) => {
const { id } = request.params;
await Product.update({ ...request.body }, { where: { id } });
const post = await Product.findByPk(id);
return reply.send({ post });
});
app.delete("/product/:id", async (request, reply) => {
const { id } = request.params;
const post = await Product.findByPk(id);
await Product.destroy({ where: { id } });
return reply.send({ post });
});
export default app;