提问人:gitaddBella 提问时间:11/3/2023 更新时间:11/3/2023 访问量:7
如何在迁移表中创建/添加镜像(cloudinary)?
How to create/add a image (cloudinary) in a migration table?
问:
我目前正在开发一个旅游网页,但我不知道如何在迁移表上创建或添加图像。我想像任何其他社交媒体平台一样创建一个帖子部分,您可以在其中添加图像,并且我想使用 cloudinary。
由于我是一名初级开发人员,我不知道如何为我的帖子部分实现 cloudinary 和时间戳。如果有人有想法或可以帮助我解决这个问题,我会很高兴。这是我目前所拥有的:
这是我的迁移表:
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
`;
}
这是我的数据库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;
});
答: 暂无答案
评论