-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: db migration #97
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||||||||
import "server-only"; | ||||||||||||||
import { createServerSupabaseClient } from "@repo/database/server"; | ||||||||||||||
import type { cookies } from "next/headers"; | ||||||||||||||
|
||||||||||||||
export const getAllPosts = async (cookieStore?: Awaited<ReturnType<typeof cookies>>) => { | ||||||||||||||
const supabase = await createServerSupabaseClient(cookieStore); | ||||||||||||||
const { data, error } = await supabase.from("posts").select("*"); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding pagination and column selection The current implementation fetches all posts and all columns at once, which could lead to performance issues as the dataset grows. Consider:
- const { data, error } = await supabase.from("posts").select("*");
+ const { data, error } = await supabase
+ .from("posts")
+ .select('id, title, slug, category, created_at, updated_at')
+ .order('created_at', { ascending: false })
+ .range(0, 9); 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
if (error) { | ||||||||||||||
throw error; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return data; | ||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import "server-only"; | ||
import { createServerSupabaseClient } from "@repo/database/server"; | ||
import type { cookies } from "next/headers"; | ||
|
||
export const getPostBySlug = async (slug: string, cookieStore?: Awaited<ReturnType<typeof cookies>>) => { | ||
const supabase = await createServerSupabaseClient(cookieStore); | ||
const { data, error } = await supabase.from("posts").select("*").eq("slug", slug).single(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
return data; | ||
}; | ||
Comment on lines
+5
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add type safety and input validation The function lacks type safety for the returned data and input validation for the slug parameter. + import { type Post } from './types';
+
+ const isValidSlug = (slug: string) => {
+ return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(slug);
+ };
+
- export const getPostBySlug = async (slug: string, cookieStore?: Awaited<ReturnType<typeof cookies>>) => {
+ export const getPostBySlug = async (
+ slug: string,
+ cookieStore?: Awaited<ReturnType<typeof cookies>>
+ ): Promise<Post> => {
+ if (!isValidSlug(slug)) {
+ throw new Error('Invalid slug format');
+ }
+
const supabase = await createServerSupabaseClient(cookieStore);
- const { data, error } = await supabase.from("posts").select("*").eq("slug", slug).single();
+ const { data, error } = await supabase
+ .from("posts")
+ .select('id, title, slug, category, content, created_at, updated_at')
+ .eq("slug", slug)
+ .single();
if (error) {
- throw error;
+ throw new Error(`Failed to fetch post with slug ${slug}: ${error.message}`);
}
return data;
};
|
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling and caching strategy
The sitemap generation lacks error handling and could benefit from caching to improve performance.