-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
177 lines (155 loc) · 4.91 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const Database = require('better-sqlite3');
const path = require('path');
const fs = require('fs');
// Use different data directories for production and development
const dataDir = process.env.NODE_ENV === 'production' ? '/data' : path.join(__dirname, 'data');
// Create directory if it doesn't exist (and we're not in production)
if (process.env.NODE_ENV !== 'production' && !fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Use a specific path for the database file
const dbPath = path.join(dataDir, 'birthdays.db');
// Initialize database with options for better error handling
const db = new Database(dbPath, {
verbose: console.log, // This will log all SQL queries (remove in production)
fileMustExist: false // Create the file if it doesn't exist
});
// Enable foreign keys and WAL mode for better performance
db.pragma('foreign_keys = ON');
db.pragma('journal_mode = WAL');
// Add this to your .gitignore
const gitignore = path.join(__dirname, '.gitignore');
if (!fs.existsSync(gitignore) || !fs.readFileSync(gitignore, 'utf8').includes('data/')) {
fs.appendFileSync(gitignore, '\n# Database\ndata/\n');
}
// Initialize tables with more robust schema
db.exec(`
CREATE TABLE IF NOT EXISTS birthdays (
user_id TEXT PRIMARY KEY,
birth_date TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
notification_sent BOOLEAN DEFAULT FALSE,
last_notification_date TEXT
);
CREATE TABLE IF NOT EXISTS birthday_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
celebrant_id TEXT NOT NULL,
sender_id TEXT NOT NULL,
sender_name TEXT NOT NULL,
message TEXT NOT NULL,
media_url TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
sent_in_thread BOOLEAN DEFAULT FALSE,
FOREIGN KEY (celebrant_id) REFERENCES birthdays(user_id)
);
CREATE TABLE IF NOT EXISTS description_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
celebrant_id TEXT NOT NULL,
sender_id TEXT NOT NULL,
sender_name TEXT NOT NULL,
message TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
sent_in_thread BOOLEAN DEFAULT FALSE,
FOREIGN KEY (celebrant_id) REFERENCES birthdays(user_id)
);
-- Add any indexes we need
CREATE INDEX IF NOT EXISTS idx_birth_date ON birthdays(birth_date);
CREATE INDEX IF NOT EXISTS idx_celebrant_messages ON birthday_messages(celebrant_id);
CREATE INDEX IF NOT EXISTS idx_description_messages ON description_messages(celebrant_id);
`);
// Add a function to check database connection
function checkDatabase() {
try {
// Try a simple query
const test = db.prepare('SELECT 1').get();
if (test[1] === 1) {
console.log('✅ Database connection successful');
return true;
}
} catch (error) {
console.error('❌ Database connection failed:', error);
return false;
}
}
// Export what we need
module.exports = {
db,
checkDatabase,
// Add your prepared statements here
statements: {
// Modify insertBirthday to be more flexible
insertBirthday: db.prepare(`
INSERT OR REPLACE INTO birthdays (
user_id,
birth_date,
updated_at,
notification_sent,
last_notification_date
)
VALUES (
?,
COALESCE(?, '1900-01-01'), /* Temporary date if none provided */
CURRENT_TIMESTAMP,
FALSE,
NULL
)
`),
getBirthday: db.prepare(`
SELECT * FROM birthdays WHERE user_id = ?
`),
getAllBirthdays: db.prepare(`
SELECT * FROM birthdays
`),
updateNotificationSent: db.prepare(`
UPDATE birthdays
SET notification_sent = ?,
last_notification_date = CURRENT_TIMESTAMP
WHERE user_id = ?
`),
insertBirthdayMessage: db.prepare(`
INSERT INTO birthday_messages (
celebrant_id,
sender_id,
sender_name,
message,
media_url
) VALUES (?, ?, ?, ?, ?)
`),
getBirthdayMessages: db.prepare(`
SELECT * FROM birthday_messages
WHERE celebrant_id = ?
AND sent_in_thread = FALSE
ORDER BY created_at ASC
`),
markMessagesAsSent: db.prepare(`
UPDATE birthday_messages
SET sent_in_thread = TRUE
WHERE celebrant_id = ?
AND sent_in_thread = FALSE
`),
insertDescriptionMessage: db.prepare(`
INSERT INTO description_messages (
celebrant_id,
sender_id,
sender_name,
message
) VALUES (?, ?, ?, ?)
`),
getDescriptionMessages: db.prepare(`
SELECT * FROM description_messages
WHERE celebrant_id = ?
AND sent_in_thread = FALSE
ORDER BY created_at ASC
`),
markDescriptionMessagesAsSent: db.prepare(`
UPDATE description_messages
SET sent_in_thread = TRUE
WHERE celebrant_id = ?
AND sent_in_thread = FALSE
`),
checkUserExists: db.prepare(`
SELECT COUNT(*) as count FROM birthdays WHERE user_id = ?
`),
}
};