@@ -5,21 +5,27 @@ import type { Email, EmailSummary } from "@/schemas/emails/schema";
55 * Insert an email into the database
66 */
77export async function insertEmail ( db : D1Database , emailData : Email ) {
8- return await db
9- . prepare (
10- `INSERT INTO emails (id, from_address, to_address, subject, received_at, html_content, text_content)
8+ try {
9+ const { success, error, meta } = await db
10+ . prepare (
11+ `INSERT INTO emails (id, from_address, to_address, subject, received_at, html_content, text_content)
1112 VALUES (?, ?, ?, ?, ?, ?, ?)` ,
12- )
13- . bind (
14- emailData . id ,
15- emailData . from_address ,
16- emailData . to_address ,
17- emailData . subject ,
18- emailData . received_at ,
19- emailData . html_content ,
20- emailData . text_content ,
21- )
22- . run ( ) ;
13+ )
14+ . bind (
15+ emailData . id ,
16+ emailData . from_address ,
17+ emailData . to_address ,
18+ emailData . subject ,
19+ emailData . received_at ,
20+ emailData . html_content ,
21+ emailData . text_content ,
22+ )
23+ . run ( ) ;
24+ return { success, error, meta } ;
25+ } catch ( e : unknown ) {
26+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
27+ return { success : false , error : error , meta : undefined } ;
28+ }
2329}
2430
2531/**
@@ -31,67 +37,97 @@ export async function getEmailsByRecipient(
3137 limit : number ,
3238 offset : number ,
3339) {
34- const { results } = await db
35- . prepare (
36- `SELECT id, from_address, to_address, subject, received_at
40+ try {
41+ const { results } = await db
42+ . prepare (
43+ `SELECT id, from_address, to_address, subject, received_at
3744 FROM emails
3845 WHERE to_address = ?
3946 ORDER BY received_at DESC
4047 LIMIT ? OFFSET ?` ,
41- )
42- . bind ( emailAddress , limit , offset )
43- . all ( ) ;
44- return results as EmailSummary [ ] ;
48+ )
49+ . bind ( emailAddress , limit , offset )
50+ . all ( ) ;
51+ return { results : results as EmailSummary [ ] , error : undefined } ;
52+ } catch ( e : unknown ) {
53+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
54+ return { results : [ ] , error : error } ;
55+ }
4556}
4657
4758/**
4859 * Get an email by ID
4960 */
5061export async function getEmailById ( db : D1Database , emailId : string ) {
51- const emailResult = await db . prepare ( "SELECT * FROM emails WHERE id = ?" ) . bind ( emailId ) . first ( ) ;
52- return emailResult as Email | null ;
62+ try {
63+ const emailResult = await db . prepare ( "SELECT * FROM emails WHERE id = ?" ) . bind ( emailId ) . first ( ) ;
64+ return { result : emailResult as Email | null , error : undefined } ;
65+ } catch ( e : unknown ) {
66+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
67+ return { result : null , error : error } ;
68+ }
5369}
5470
5571/**
5672 * Delete emails older than a specific timestamp
5773 */
5874export async function deleteOldEmails ( db : D1Database , timestamp : number ) {
59- const { success, error, meta } = await db
60- . prepare ( "DELETE FROM emails WHERE received_at < ?" )
61- . bind ( timestamp )
62- . run ( ) ;
63- return { success, error, meta } ;
75+ try {
76+ const { success, error, meta } = await db
77+ . prepare ( "DELETE FROM emails WHERE received_at < ?" )
78+ . bind ( timestamp )
79+ . run ( ) ;
80+ return { success, error, meta } ;
81+ } catch ( e : unknown ) {
82+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
83+ return { success : false , error : error , meta : undefined } ;
84+ }
6485}
6586
6687/**
6788 * Delete emails by recipient email address
6889 */
6990export async function deleteEmailsByRecipient ( db : D1Database , emailAddress : string ) {
70- const { success, error, meta } = await db
71- . prepare ( "DELETE FROM emails WHERE to_address = ?" )
72- . bind ( emailAddress )
73- . run ( ) ;
74- return { success, error, meta } ;
91+ try {
92+ const { success, error, meta } = await db
93+ . prepare ( "DELETE FROM emails WHERE to_address = ?" )
94+ . bind ( emailAddress )
95+ . run ( ) ;
96+ return { success, error, meta } ;
97+ } catch ( e : unknown ) {
98+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
99+ return { success : false , error : error , meta : undefined } ;
100+ }
75101}
76102
77103/**
78104 * Delete an email by ID
79105 */
80106export async function deleteEmailById ( db : D1Database , emailId : string ) {
81- const { success, error, meta } = await db
82- . prepare ( "DELETE FROM emails WHERE id = ?" )
83- . bind ( emailId )
84- . run ( ) ;
85- return { success, error, meta } ;
107+ try {
108+ const { success, error, meta } = await db
109+ . prepare ( "DELETE FROM emails WHERE id = ?" )
110+ . bind ( emailId )
111+ . run ( ) ;
112+ return { success, error, meta } ;
113+ } catch ( e : unknown ) {
114+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
115+ return { success : false , error : error , meta : undefined } ;
116+ }
86117}
87118
88119/**
89120 * Count emails by recipient email address
90121 */
91122export async function countEmailsByRecipient ( db : D1Database , emailAddress : string ) {
92- const result = await db
93- . prepare ( "SELECT count(*) as count FROM emails WHERE to_address = ?" )
94- . bind ( emailAddress )
95- . first < { count : number } > ( ) ;
96- return result ?. count || 0 ;
123+ try {
124+ const result = await db
125+ . prepare ( "SELECT count(*) as count FROM emails WHERE to_address = ?" )
126+ . bind ( emailAddress )
127+ . first < { count : number } > ( ) ;
128+ return { count : result ?. count || 0 , error : undefined } ;
129+ } catch ( e : unknown ) {
130+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
131+ return { count : 0 , error : error } ;
132+ }
97133}
0 commit comments