31
loading...
This website collects cookies to deliver better user experience
npm install fastify typeorm sqlite3
// @src/Models/book.js
import { EntitySchema } from "typeorm";
export const BookEntity = new EntitySchema({
// Some things come here.
});
// @src/Models/book.js
import { EntitySchema } from "typeorm";
export const BookEntity = new EntitySchema({
name: "Books",
columns: {
id: {
type: Number,
primary: true,
generated: true,
},
name: {
type: String,
},
description: {
type: String,
},
format: {
type: String,
},
},
});
// @src/Models/book.js
import { EntitySchema } from "typeorm";
export class Book {
constructor(name, description, format) {
this.name = name;
this.description = description;
this.format = format;
}
}
export const BookEntity = new EntitySchema({
name: "Books",
columns: {
id: {
type: Number,
primary: true,
generated: true,
},
name: {
type: String,
},
description: {
type: String,
},
format: {
type: String,
},
},
});
createConnection()
function from typeorm and then we import our BookEntity from our model.// @src/database/index.js
import { createConnection } from "typeorm";
import { BookEntity } from "../Models/book.js";
// More stuff comes here.
createConnection()
function is asynchronous and from here on there are several approaches that can be taken, in this example I will create an asynchronous function called connection that will return our connection to the database.createConnection()
we will pass our connection settings, such as the dialect, our entities, among other things.// @src/database/index.js
import { createConnection } from "typeorm";
import { BookEntity } from "../Models/book.js";
export const connection = async () => {
return await createConnection({
name: "default",
type: "sqlite",
database: "src/database/dev.db",
entities: [BookEntity],
logging: true,
synchronize: true,
});
};
// @src/main.js
import app from "./app.js";
import { connection } from "./database/index.js";
const start = async () => {
try {
await connection();
await app.listen(3333);
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
getRepository()
function and our model (BookEntity) along with our Book class.// @src/app.js
import Fastify from "fastify";
import { getRepository } from "typeorm";
import { BookEntity, Book } from "./Models/book.js";
const app = Fastify();
// More stuff comes here.
export default app;
.find()
method to get all the data stored in our database table.// @src/app.js
app.get("/books", async (request, reply) => {
const Books = getRepository(BookEntity);
const data = await Books.find();
return reply.send({ data });
});
.save()
method to insert a new book into our database table.// @src/app.js
app.post("/books", async (request, reply) => {
const Books = getRepository(BookEntity);
const book = new Book();
book.name = request.body.name;
book.description = request.body.description;
book.format = request.body.format;
const data = await Books.save(book);
return reply.send({ data });
});
id
..findOne()
method to find just the book with its id
.// @src/app.js
app.get("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
const book = await Books.findOne(id);
return reply.send({ book });
});
.update()
method of the typeorm and we will pass two things, the id
and the updated object of the book.// @src/app.js
app.put("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
await Books.update({ id }, { ...request.body });
const book = await Books.findOne(id);
return reply.send({ book });
});
.findOne()
method and we will have to pass that same book as the only argument to the .remove()
method.// @src/app.js
app.delete("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
const bookToRemove = await Books.findOne(id);
await Books.remove(bookToRemove);
return reply.send({ book: bookToRemove });
});
// @src/app.js
import Fastify from "fastify";
import { getRepository } from "typeorm";
import { BookEntity, Book } from "./Models/book.js";
const app = Fastify();
app.get("/books", async (request, reply) => {
const Books = getRepository(BookEntity);
const data = await Books.find();
return reply.send({ data });
});
app.post("/books", async (request, reply) => {
const Books = getRepository(BookEntity);
const book = new Book();
book.name = request.body.name;
book.description = request.body.description;
book.format = request.body.format;
const data = await Books.save(book);
return reply.send({ data });
});
app.get("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
const book = await Books.findOne(id);
return reply.send({ book });
});
app.put("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
await Books.update({ id }, { ...request.body });
const book = await Books.findOne(id);
return reply.send({ book });
});
app.delete("/books/:id", async (request, reply) => {
const { id } = request.params;
const Books = getRepository(BookEntity);
const bookToRemove = await Books.findOne(id);
await Books.remove(bookToRemove);
return reply.send({ book: bookToRemove });
});
export default app;