1
- import { sql } from '@vercel/ postgres' ;
1
+ import postgres from 'postgres' ;
2
2
import {
3
3
CustomerField ,
4
4
CustomersTableType ,
9
9
} from './definitions' ;
10
10
import { formatCurrency } from './utils' ;
11
11
12
+ const sql = postgres ( process . env . POSTGRES_URL ! , { ssl : 'require' } ) ;
13
+
12
14
export async function fetchRevenue ( ) {
13
15
try {
14
16
// Artificially delay a response for demo purposes.
@@ -17,11 +19,11 @@ export async function fetchRevenue() {
17
19
// console.log('Fetching revenue data...');
18
20
// await new Promise((resolve) => setTimeout(resolve, 3000));
19
21
20
- const data = await sql < Revenue > `SELECT * FROM revenue` ;
22
+ const data = await sql < Revenue [ ] > `SELECT * FROM revenue` ;
21
23
22
24
// console.log('Data fetch completed after 3 seconds.');
23
25
24
- return data . rows ;
26
+ return data ;
25
27
} catch ( error ) {
26
28
console . error ( 'Database Error:' , error ) ;
27
29
throw new Error ( 'Failed to fetch revenue data.' ) ;
@@ -30,14 +32,14 @@ export async function fetchRevenue() {
30
32
31
33
export async function fetchLatestInvoices ( ) {
32
34
try {
33
- const data = await sql < LatestInvoiceRaw > `
35
+ const data = await sql < LatestInvoiceRaw [ ] > `
34
36
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
35
37
FROM invoices
36
38
JOIN customers ON invoices.customer_id = customers.id
37
39
ORDER BY invoices.date DESC
38
40
LIMIT 5` ;
39
41
40
- const latestInvoices = data . rows . map ( ( invoice ) => ( {
42
+ const latestInvoices = data . map ( ( invoice ) => ( {
41
43
...invoice ,
42
44
amount : formatCurrency ( invoice . amount ) ,
43
45
} ) ) ;
@@ -66,10 +68,10 @@ export async function fetchCardData() {
66
68
invoiceStatusPromise ,
67
69
] ) ;
68
70
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' ) ;
73
75
74
76
return {
75
77
numberOfCustomers,
@@ -91,7 +93,7 @@ export async function fetchFilteredInvoices(
91
93
const offset = ( currentPage - 1 ) * ITEMS_PER_PAGE ;
92
94
93
95
try {
94
- const invoices = await sql < InvoicesTable > `
96
+ const invoices = await sql < InvoicesTable [ ] > `
95
97
SELECT
96
98
invoices.id,
97
99
invoices.amount,
@@ -112,7 +114,7 @@ export async function fetchFilteredInvoices(
112
114
LIMIT ${ ITEMS_PER_PAGE } OFFSET ${ offset }
113
115
` ;
114
116
115
- return invoices . rows ;
117
+ return invoices ;
116
118
} catch ( error ) {
117
119
console . error ( 'Database Error:' , error ) ;
118
120
throw new Error ( 'Failed to fetch invoices.' ) ;
@@ -121,7 +123,7 @@ export async function fetchFilteredInvoices(
121
123
122
124
export async function fetchInvoicesPages ( query : string ) {
123
125
try {
124
- const count = await sql `SELECT COUNT(*)
126
+ const data = await sql `SELECT COUNT(*)
125
127
FROM invoices
126
128
JOIN customers ON invoices.customer_id = customers.id
127
129
WHERE
@@ -132,7 +134,7 @@ export async function fetchInvoicesPages(query: string) {
132
134
invoices.status ILIKE ${ `%${ query } %` }
133
135
` ;
134
136
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 ) ;
136
138
return totalPages ;
137
139
} catch ( error ) {
138
140
console . error ( 'Database Error:' , error ) ;
@@ -142,7 +144,7 @@ export async function fetchInvoicesPages(query: string) {
142
144
143
145
export async function fetchInvoiceById ( id : string ) {
144
146
try {
145
- const data = await sql < InvoiceForm > `
147
+ const data = await sql < InvoiceForm [ ] > `
146
148
SELECT
147
149
invoices.id,
148
150
invoices.customer_id,
@@ -152,7 +154,7 @@ export async function fetchInvoiceById(id: string) {
152
154
WHERE invoices.id = ${ id } ;
153
155
` ;
154
156
155
- const invoice = data . rows . map ( ( invoice ) => ( {
157
+ const invoice = data . map ( ( invoice ) => ( {
156
158
...invoice ,
157
159
// Convert amount from cents to dollars
158
160
amount : invoice . amount / 100 ,
@@ -167,15 +169,14 @@ export async function fetchInvoiceById(id: string) {
167
169
168
170
export async function fetchCustomers ( ) {
169
171
try {
170
- const data = await sql < CustomerField > `
172
+ const customers = await sql < CustomerField [ ] > `
171
173
SELECT
172
174
id,
173
175
name
174
176
FROM customers
175
177
ORDER BY name ASC
176
178
` ;
177
179
178
- const customers = data . rows ;
179
180
return customers ;
180
181
} catch ( err ) {
181
182
console . error ( 'Database Error:' , err ) ;
@@ -185,7 +186,7 @@ export async function fetchCustomers() {
185
186
186
187
export async function fetchFilteredCustomers ( query : string ) {
187
188
try {
188
- const data = await sql < CustomersTableType > `
189
+ const data = await sql < CustomersTableType [ ] > `
189
190
SELECT
190
191
customers.id,
191
192
customers.name,
@@ -203,7 +204,7 @@ export async function fetchFilteredCustomers(query: string) {
203
204
ORDER BY customers.name ASC
204
205
` ;
205
206
206
- const customers = data . rows . map ( ( customer ) => ( {
207
+ const customers = data . map ( ( customer ) => ( {
207
208
...customer ,
208
209
total_pending : formatCurrency ( customer . total_pending ) ,
209
210
total_paid : formatCurrency ( customer . total_paid ) ,
0 commit comments