How to create/add a image (cloudinary) in a migration table?

I am currently working on a travel Webpage and I don’t know how to create or add images on my migration table. I want to create a Posts section like any other social media platform where you can add images and I want to work with cloudinary.

Since I’m a junior developer I don’t know how to implement cloudinary and a timestamp for my posts section. I would be so happy if anyone has a idea or could help me with this issue. This is what I currently have:

This is my migration table:

import { Sql } from 'postgres';

export type Post = {
  id: number;
  userId: number;
  title: string;
  content: string;
};

export async function up(sql: Sql) {
  await sql`
    CREATE TABLE posts (
      id serial PRIMARY KEY,
      user_id integer NOT NULL REFERENCES users (id) ON DELETE CASCADE,
      title varchar(80) NOT NULL UNIQUE,
      content text NOT NULL
    );
  `;
}

export async function down(sql: Sql) {
  await sql`
    DROP TABLE posts
  `;
}

This is my database posts.ts:

import 'server-only';
import { cache } from 'react';
import { Post } from '../migrations/00002-createTablePosts';
import { sql } from './connect';
import { UserBlogPostWithoutUserId } from './users';

export const getPosts = cache(async () => {
  const posts = await sql<Post[]>`
    SELECT * FROM posts
  `;
  return posts;
});

export const deletePostByUserId = cache(async (userId: number) => {
  const [post] = await sql<Post[]>`
    DELETE FROM
      posts
    WHERE
      user_id = ${userId}
    RETURNING *
  `;

  return post;
});

export const createBlogPost = cache(
  async (userId: number, title: string, post: string) => {
    const [posts] = await sql<Post[]>`
      INSERT INTO posts
      (user_id, title, post)
      VALUES
        (${userId},${title}, ${post})
      RETURNING *
    `;

    return posts;
  },
);

export const getAllBlogPosts = cache(async () => {
  const notes = await sql<UserBlogPostWithoutUserId[]>`
    SELECT
      posts.id AS post_id,
      posts.title AS title,
      posts.post AS post,
      users.username AS username
    FROM
      posts
    INNER JOIN
      users ON posts.user_id = users.id
  `;
  return notes;
});

export const getBlogPostsById = cache(async (id: number) => {
  const notes = await sql<UserBlogPostWithoutUserId[]>`
    SELECT
      posts.id AS post_id,
      posts.title AS title,
      posts.post AS post,
      users.username AS username
    FROM
      posts
    INNER JOIN
      users ON posts.user_id = users.id
    WHERE
      posts.id = ${id}
  `;
  return notes;
});

Leave a Comment