DEV Community

Raihanul Islam Sharif
Raihanul Islam Sharif

Posted on

PostgreSQL-CRUD OPERATION

PostgreSQL, often referred to as "Postgres," is a powerful open-source relational database management system (RDBMS). It is known for its reliability, extensibility, and compliance with SQL standards

GET ALL BOOK FROM book Collection

app.get('/books', async(req,res)=>{
    try {
        const books = await pool.query("SELECT * FROM book")
        res.status(200).json({message: "data recived",data:books.rows})
    } catch (error) {
        res.json({error: error.message})
    }
})
Enter fullscreen mode Exit fullscreen mode

Get Specific Book by Id

app.get('/books/:id',async(req,res)=>{
    try {
        const {id} = req.params;
        // console.log(id)
        const book = await pool.query("SELECT * FROM book WHERE id=$1",[id])
        res.status(200).json({text:"book paisi", data:book.rows})
    } catch (error) {
        res.json({error: error.message})
    }
})

Enter fullscreen mode Exit fullscreen mode

delete specific Book by id

app.delete('/books/:id',async(req,res)=>{
    try {
        const {id} = req.params;
        const book = await pool.query("DELETE FROM book WHERE id=$1",[id])
        res.status(200).json({text:'Deleted'})

    } catch (error) {
        res.json({error: error.message})
    }
})
Enter fullscreen mode Exit fullscreen mode

Update Book

app.put('/books/:id',async(req,res)=>{
    try {
        const {id} = req.params;
        const {name,description} = req.body;
        const updatedBook = await pool.query("UPDATE book SET name=$1,description=$2 WHERE id=$3 RETURNING *",[name,description,id])
        res.status(200).json({text: 'Book data Updated',data:updatedBook.rows})
    } catch (error) {
        res.json({error: error.message})
    }
})
Enter fullscreen mode Exit fullscreen mode

POST Book on book Collection

app.post('/books',async(req,res)=>{
    try {
        const {name,description} = req.body;
        const id = uuidv4();
        // Post on the database 
        const newBook = await pool.query(
            "INSERT INTO book (id,name,description) VALUES ($1,$2,$3) RETURNING *",
            [id,name,description]
        )
        res.status(201).json({data: newBook.rows})
    } catch (error) {
        res.json({error: error.message})
    }
})
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
raihanuldev profile image
Raihanul Islam Sharif

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay