diff --git a/app/lib/data.ts b/app/lib/data.ts index bc9154d0..94b4180c 100644 --- a/app/lib/data.ts +++ b/app/lib/data.ts @@ -1,4 +1,4 @@ -import { sql } from '@neondatabase/serverless'; +import postgres from 'postgres'; import { CustomerField, CustomersTable, @@ -9,7 +9,7 @@ import { Revenue, } from './definitions'; import { formatCurrency } from './utils'; - +const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' }); export async function fetchRevenue() { // Add noStore() here prevent the response from being cached. // This is equivalent to in fetch(..., {cache: 'no-store'}). @@ -21,11 +21,11 @@ export async function fetchRevenue() { // console.log('Fetching revenue data...'); // await new Promise((resolve) => setTimeout(resolve, 3000)); - const data = await sql`SELECT * FROM revenue`; + const data = sql`SELECT * FROM revenue`; // console.log('Data fetch complete after 3 seconds.'); - return data.rows; + return data; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch revenue data.'); @@ -34,14 +34,14 @@ export async function fetchRevenue() { export async function fetchLatestInvoices() { try { - const data = await sql` + const data = await sql` SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id FROM invoices JOIN customers ON invoices.customer_id = customers.id ORDER BY invoices.date DESC LIMIT 5`; - const latestInvoices = data.rows.map((invoice) => ({ + const latestInvoices = data.map((invoice) => ({ ...invoice, amount: formatCurrency(invoice.amount), })); @@ -70,10 +70,10 @@ export async function fetchCardData() { invoiceStatusPromise, ]); - const numberOfInvoices = Number(data[0].rows[0].count ?? '0'); - const numberOfCustomers = Number(data[1].rows[0].count ?? '0'); - const totalPaidInvoices = formatCurrency(data[2].rows[0].paid ?? '0'); - const totalPendingInvoices = formatCurrency(data[2].rows[0].pending ?? '0'); + const numberOfInvoices = Number(data[0][0].count ?? '0'); + const numberOfCustomers = Number(data[1][0].count ?? '0'); + const totalPaidInvoices = formatCurrency(data[2][0].paid ?? '0'); + const totalPendingInvoices = formatCurrency(data[2][0].pending ?? '0'); return { numberOfCustomers, @@ -95,7 +95,7 @@ export async function fetchFilteredInvoices( const offset = (currentPage - 1) * ITEMS_PER_PAGE; try { - const invoices = await sql` + const invoices = await sql` SELECT invoices.id, invoices.amount, @@ -116,7 +116,7 @@ export async function fetchFilteredInvoices( LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset} `; - return invoices.rows; + return invoices; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch invoices.'); @@ -136,7 +136,7 @@ export async function fetchInvoicesPages(query: string) { invoices.status ILIKE ${`%${query}%`} `; - const totalPages = Math.ceil(Number(count.rows[0].count) / ITEMS_PER_PAGE); + const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE); return totalPages; } catch (error) { console.error('Database Error:', error); @@ -146,7 +146,7 @@ export async function fetchInvoicesPages(query: string) { export async function fetchInvoiceById(id: string) { try { - const data = await sql` + const data = await sql` SELECT invoices.id, invoices.customer_id, @@ -156,7 +156,7 @@ export async function fetchInvoiceById(id: string) { WHERE invoices.id = ${id}; `; - const invoice = data.rows.map((invoice) => ({ + const invoice = data.map((invoice) => ({ ...invoice, // Convert amount from cents to dollars amount: invoice.amount / 100, @@ -170,7 +170,7 @@ export async function fetchInvoiceById(id: string) { export async function fetchCustomers() { try { - const data = await sql` + const data = await sql` SELECT id, name @@ -178,7 +178,7 @@ export async function fetchCustomers() { ORDER BY name ASC `; - const customers = data.rows; + const customers = data; return customers; } catch (err) { console.error('Database Error:', err); @@ -188,7 +188,7 @@ export async function fetchCustomers() { export async function fetchFilteredCustomers(query: string) { try { - const data = await sql` + const data = await sql` SELECT customers.id, customers.name, @@ -206,7 +206,7 @@ export async function fetchFilteredCustomers(query: string) { ORDER BY customers.name ASC `; - const customers = data.rows.map((customer) => ({ + const customers = data.map((customer) => ({ ...customer, total_pending: formatCurrency(customer.total_pending), total_paid: formatCurrency(customer.total_paid), @@ -222,9 +222,9 @@ export async function fetchFilteredCustomers(query: string) { export async function getUser(email: string) { try { const user = await sql`SELECT * from USERS where email=${email}`; - return user.rows[0] as User; + return user[0] as User; } catch (error) { console.error('Failed to fetch user:', error); throw new Error('Failed to fetch user.'); } -} +} \ No newline at end of file