Nuxt3 + vercel postgressQL slow in production

25 views Asked by At

I'm trying to create a simple CRUD system using Vercel's posgresql database with my nuxt3 project. At first I found it easy to get started and didn't think there would be any problems. On the system I created an API with nuxt3 features in the server directory, I deployed the template from Vercel and at first I didn't have any database speed issues. I thought it might be because of the caching of the useFetch function. Later, as I tried more things, I had to use fetch without caching anything to keep it up to date. But then I found that querying data in this format is quite slow,At first I thought it might be due to the website being too slow when the product was produced with Vercel. In one click I found that every API call

import { createPool } from "@vercel/postgres";
const db = createPool();

At the same time, I also tested creating an api to pull fake data that was larger than calling createPool. I also found it to be quite fast.

import { createPool } from "@vercel/postgres";
const db = createPool();
export default defineEventHandler(async (req) => {
  try {
    const params = getQuery(req);
    const postId = params.id;
    if (!postId) {
      throw new Error("Post ID is required.");
    }
    const { rows: articles } = await db.query(
      "SELECT body FROM articles WHERE post_id = $1",
      [postId]
    );
    if (articles.length === 0) {
      return {
        status: 404,
        message: "Post not found.",
      };
    }
    return articles;
  } catch (error: any) {
    return {
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    };
  }
});

this is/server/api/get-full-post.ts (my api test)

enter image description here

And from this picture, I'm pretty sure that calling

import { createPool } from "@vercel/postgres";
const db = createPool();

It may not be appropriate for my project. And I might be running it the wrong way. I may not need a lot of code or solutions, but if anyone can answer my question it is. 1.Is it appropriate to use this method? 2. Will it be faster if I try using an ORM to help deal with this problem? Is it worth it to try? Because I'm still very new to trying ORMs and I have limited time to learn it, That will be applied to the project right away but if it's the best and only way then I'm ready to start. Thank you very much for the answer.

0

There are 0 answers