Skip to content

Commit 7585f53

Browse files
committed
model
1 parent b89d601 commit 7585f53

File tree

2 files changed

+78
-34
lines changed

2 files changed

+78
-34
lines changed

src/config/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ const SUPABASE_URL = process.env.SUPABASE_URL; // URL da sua instância do Supab
55
const SUPABASE_ANON_KEY = process.env.SUPABASE_KEY; // Chave pública anônima (encontrada no painel do Supabase)
66

77
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
8-
export default supabase
8+
export default supabase

src/models/Post.ts

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,81 @@
1-
import { DataTypes, Model } from "sequelize";
2-
import sequelize from "../config/database";
3-
4-
class Post extends Model {
5-
public id!: string;
6-
public title!: string;
7-
public desc!: string;
8-
public date!: Date;
1+
import supabase from "../config/database"; // Caminho para o seu cliente Supabase
2+
3+
interface Post {
4+
id: string;
5+
title: string;
6+
desc: string;
7+
date: string; // O Supabase geralmente lida com datas como strings em formato ISO
98
}
109

11-
Post.init(
12-
{
13-
id: {
14-
type: DataTypes.STRING,
15-
primaryKey: true,
16-
},
17-
title: {
18-
type: DataTypes.STRING,
19-
allowNull: false,
20-
},
21-
desc: {
22-
type: DataTypes.TEXT,
23-
allowNull: false,
24-
},
25-
date: {
26-
type: DataTypes.DATE,
27-
allowNull: false,
28-
},
29-
},
30-
{
31-
sequelize,
32-
modelName: "Post",
33-
tableName: "posts",
10+
class PostModel {
11+
// Função para obter todos os posts
12+
static async getAllPosts(): Promise<Post[]> {
13+
const { data, error } = await supabase
14+
.from("posts")
15+
.select("*");
16+
17+
if (error) {
18+
throw new Error(error.message);
19+
}
20+
21+
return data as Post[];
22+
}
23+
24+
// Função para criar um novo post
25+
static async createPost(title: string, desc: string): Promise<Post> {
26+
const { data, error } = await supabase
27+
.from("posts")
28+
.insert([{ title, desc, date: new Date().toISOString() }])
29+
.single();
30+
31+
if (error) {
32+
throw new Error(error.message);
33+
}
34+
35+
return data as Post;
3436
}
35-
);
3637

37-
export default Post;
38+
// Função para obter um post por ID
39+
static async getPostById(postId: string): Promise<Post | null> {
40+
const { data, error } = await supabase
41+
.from("posts")
42+
.select("*")
43+
.eq("id", postId)
44+
.single();
45+
46+
if (error) {
47+
throw new Error(error.message);
48+
}
49+
50+
return data as Post | null;
51+
}
52+
53+
// Função para atualizar um post
54+
static async updatePost(postId: string, title: string, desc: string): Promise<Post> {
55+
const { data, error } = await supabase
56+
.from("posts")
57+
.update({ title, desc })
58+
.eq("id", postId)
59+
.single();
60+
61+
if (error) {
62+
throw new Error(error.message);
63+
}
64+
65+
return data as Post;
66+
}
67+
68+
// Função para deletar um post
69+
static async deletePost(postId: string): Promise<void> {
70+
const { error } = await supabase
71+
.from("posts")
72+
.delete()
73+
.eq("id", postId);
74+
75+
if (error) {
76+
throw new Error(error.message);
77+
}
78+
}
79+
}
80+
81+
export default PostModel;

0 commit comments

Comments
 (0)