1- import { sql } from '@vercel/ postgres' ;
1+ import postgres from 'postgres' ;
22import {
33 CustomerField ,
44 CustomersTableType ,
99} from './definitions' ;
1010import { formatCurrency } from './utils' ;
1111
12+ const sql = postgres ( process . env . POSTGRES_URL ! , { ssl : 'require' } ) ;
13+
1214export async function fetchRevenue ( ) {
1315 try {
1416 // Artificially delay a response for demo purposes.
@@ -17,11 +19,11 @@ export async function fetchRevenue() {
1719 // console.log('Fetching revenue data...');
1820 // await new Promise((resolve) => setTimeout(resolve, 3000));
1921
20- const data = await sql < Revenue > `SELECT * FROM revenue` ;
22+ const data = await sql < Revenue [ ] > `SELECT * FROM revenue` ;
2123
2224 // console.log('Data fetch completed after 3 seconds.');
2325
24- return data . rows ;
26+ return data ;
2527 } catch ( error ) {
2628 console . error ( 'Database Error:' , error ) ;
2729 throw new Error ( 'Failed to fetch revenue data.' ) ;
@@ -30,14 +32,14 @@ export async function fetchRevenue() {
3032
3133export async function fetchLatestInvoices ( ) {
3234 try {
33- const data = await sql < LatestInvoiceRaw > `
35+ const data = await sql < LatestInvoiceRaw [ ] > `
3436 SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
3537 FROM invoices
3638 JOIN customers ON invoices.customer_id = customers.id
3739 ORDER BY invoices.date DESC
3840 LIMIT 5` ;
3941
40- const latestInvoices = data . rows . map ( ( invoice ) => ( {
42+ const latestInvoices = data . map ( ( invoice ) => ( {
4143 ...invoice ,
4244 amount : formatCurrency ( invoice . amount ) ,
4345 } ) ) ;
@@ -66,10 +68,10 @@ export async function fetchCardData() {
6668 invoiceStatusPromise ,
6769 ] ) ;
6870
69- const numberOfInvoices = Number ( data [ 0 ] . rows [ 0 ] . count ?? '0' ) ;
70- const numberOfCustomers = Number ( data [ 1 ] . rows [ 0 ] . count ?? '0' ) ;
71- const totalPaidInvoices = formatCurrency ( data [ 2 ] . rows [ 0 ] . paid ?? '0' ) ;
72- const totalPendingInvoices = formatCurrency ( data [ 2 ] . rows [ 0 ] . pending ?? '0' ) ;
71+ const numberOfInvoices = Number ( data [ 0 ] . count ?? '0' ) ;
72+ const numberOfCustomers = Number ( data [ 1 ] . count ?? '0' ) ;
73+ const totalPaidInvoices = formatCurrency ( data [ 2 ] [ 0 ] . paid ?? '0' ) ;
74+ const totalPendingInvoices = formatCurrency ( data [ 2 ] [ 0 ] . pending ?? '0' ) ;
7375
7476 return {
7577 numberOfCustomers,
@@ -91,7 +93,7 @@ export async function fetchFilteredInvoices(
9193 const offset = ( currentPage - 1 ) * ITEMS_PER_PAGE ;
9294
9395 try {
94- const invoices = await sql < InvoicesTable > `
96+ const invoices = await sql < InvoicesTable [ ] > `
9597 SELECT
9698 invoices.id,
9799 invoices.amount,
@@ -112,7 +114,7 @@ export async function fetchFilteredInvoices(
112114 LIMIT ${ ITEMS_PER_PAGE } OFFSET ${ offset }
113115 ` ;
114116
115- return invoices . rows ;
117+ return invoices ;
116118 } catch ( error ) {
117119 console . error ( 'Database Error:' , error ) ;
118120 throw new Error ( 'Failed to fetch invoices.' ) ;
@@ -121,7 +123,7 @@ export async function fetchFilteredInvoices(
121123
122124export async function fetchInvoicesPages ( query : string ) {
123125 try {
124- const count = await sql `SELECT COUNT(*)
126+ const data = await sql `SELECT COUNT(*)
125127 FROM invoices
126128 JOIN customers ON invoices.customer_id = customers.id
127129 WHERE
@@ -132,7 +134,7 @@ export async function fetchInvoicesPages(query: string) {
132134 invoices.status ILIKE ${ `%${ query } %` }
133135 ` ;
134136
135- const totalPages = Math . ceil ( Number ( count . rows [ 0 ] . count ) / ITEMS_PER_PAGE ) ;
137+ const totalPages = Math . ceil ( Number ( data [ 0 ] . count ) / ITEMS_PER_PAGE ) ;
136138 return totalPages ;
137139 } catch ( error ) {
138140 console . error ( 'Database Error:' , error ) ;
@@ -142,7 +144,7 @@ export async function fetchInvoicesPages(query: string) {
142144
143145export async function fetchInvoiceById ( id : string ) {
144146 try {
145- const data = await sql < InvoiceForm > `
147+ const data = await sql < InvoiceForm [ ] > `
146148 SELECT
147149 invoices.id,
148150 invoices.customer_id,
@@ -152,7 +154,7 @@ export async function fetchInvoiceById(id: string) {
152154 WHERE invoices.id = ${ id } ;
153155 ` ;
154156
155- const invoice = data . rows . map ( ( invoice ) => ( {
157+ const invoice = data . map ( ( invoice ) => ( {
156158 ...invoice ,
157159 // Convert amount from cents to dollars
158160 amount : invoice . amount / 100 ,
@@ -167,15 +169,14 @@ export async function fetchInvoiceById(id: string) {
167169
168170export async function fetchCustomers ( ) {
169171 try {
170- const data = await sql < CustomerField > `
172+ const customers = await sql < CustomerField [ ] > `
171173 SELECT
172174 id,
173175 name
174176 FROM customers
175177 ORDER BY name ASC
176178 ` ;
177179
178- const customers = data . rows ;
179180 return customers ;
180181 } catch ( err ) {
181182 console . error ( 'Database Error:' , err ) ;
@@ -185,7 +186,7 @@ export async function fetchCustomers() {
185186
186187export async function fetchFilteredCustomers ( query : string ) {
187188 try {
188- const data = await sql < CustomersTableType > `
189+ const data = await sql < CustomersTableType [ ] > `
189190 SELECT
190191 customers.id,
191192 customers.name,
@@ -203,7 +204,7 @@ export async function fetchFilteredCustomers(query: string) {
203204 ORDER BY customers.name ASC
204205 ` ;
205206
206- const customers = data . rows . map ( ( customer ) => ( {
207+ const customers = data . map ( ( customer ) => ( {
207208 ...customer ,
208209 total_pending : formatCurrency ( customer . total_pending ) ,
209210 total_paid : formatCurrency ( customer . total_paid ) ,
0 commit comments