chore: add nextjs template
This commit is contained in:
22
frontend-nextjs/lib/api.ts
Normal file
22
frontend-nextjs/lib/api.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export type Product = {
|
||||
id: number;
|
||||
title: string;
|
||||
price_cents: number;
|
||||
image_url: string;
|
||||
};
|
||||
|
||||
export async function fetchProducts(): Promise<Product[]> {
|
||||
const res = await fetch("/api/products", { cache: "no-store" });
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to fetch products");
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function fetchProduct(productId: string): Promise<Product> {
|
||||
const res = await fetch(`/api/products/${productId}`, { cache: "no-store" });
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to fetch product");
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
29
frontend-nextjs/lib/db.ts
Normal file
29
frontend-nextjs/lib/db.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Pool } from "pg";
|
||||
|
||||
// Singleton database connection pool
|
||||
// Uses environment variables for configuration
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST || "127.0.0.1",
|
||||
port: parseInt(process.env.DB_PORT || "5432"),
|
||||
user: process.env.DB_USER || "myuser",
|
||||
password: process.env.DB_PASSWORD || "mypassword",
|
||||
database: process.env.DB_DATABASE || "mydb",
|
||||
});
|
||||
|
||||
export default pool;
|
||||
|
||||
// Helper function for queries
|
||||
export async function query<T>(text: string, params?: unknown[]): Promise<T[]> {
|
||||
const result = await pool.query(text, params);
|
||||
return result.rows as T[];
|
||||
}
|
||||
|
||||
// Helper function for single row
|
||||
export async function queryOne<T>(
|
||||
text: string,
|
||||
params?: unknown[]
|
||||
): Promise<T | null> {
|
||||
const result = await pool.query(text, params);
|
||||
return result.rows[0] as T | null;
|
||||
}
|
||||
Reference in New Issue
Block a user