Repository navigation
Expand file tree
/
Copy pathdb.ts
More file actions
249 lines (214 loc) · 7.89 KB
/
Copy pathdb.ts
File metadata and controls
249 lines (214 loc) · 7.89 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import fs from 'fs';
import path from 'path';
import Database from 'better-sqlite3';
type Db = Database.Database;
let db: Db | null = null;
function ensureColumn(database: Db, table: string, column: string, definition: string) {
const rows = database.prepare(`PRAGMA table_info(${table})`).all() as Array<{ name: string }>;
const exists = rows.some((r) => r.name === column);
if (!exists) {
database.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
}
}
function initDb(database: Db) {
database.pragma('journal_mode = WAL');
database.pragma('foreign_keys = ON');
database.exec(`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
name TEXT,
verified INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS referral_codes (
code TEXT PRIMARY KEY,
referrer_id TEXT NOT NULL,
active INTEGER NOT NULL DEFAULT 1,
expires_at INTEGER,
created_at INTEGER NOT NULL,
FOREIGN KEY (referrer_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS referrals (
referrer_id TEXT NOT NULL,
referred_id TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (referrer_id, referred_id),
FOREIGN KEY (referrer_id) REFERENCES users(id),
FOREIGN KEY (referred_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS points_adjustments (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
delta INTEGER NOT NULL,
action TEXT,
reason TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS rate_limits (
key TEXT PRIMARY KEY,
count INTEGER NOT NULL,
reset_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS premium_waitlist (
id TEXT PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
email_hash TEXT NOT NULL UNIQUE,
name TEXT,
position INTEGER NOT NULL UNIQUE,
interested_date INTEGER,
is_active INTEGER NOT NULL DEFAULT 1,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS push_subscriptions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
endpoint TEXT NOT NULL UNIQUE,
auth_key TEXT,
p256dh_key TEXT,
subscription_data TEXT NOT NULL,
is_active INTEGER DEFAULT 1,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS notification_preferences (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL UNIQUE,
preferences_data TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS email_jobs (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
email TEXT NOT NULL,
template TEXT NOT NULL,
send_at INTEGER NOT NULL,
sent_at INTEGER,
meta TEXT,
created_at INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE INDEX IF NOT EXISTS idx_email_jobs_send_at ON email_jobs(send_at);
CREATE INDEX IF NOT EXISTS idx_email_jobs_status ON email_jobs(status);
CREATE UNIQUE INDEX IF NOT EXISTS idx_referrals_referred_id ON referrals(referred_id);
CREATE INDEX IF NOT EXISTS idx_referral_codes_referrer_id ON referral_codes(referrer_id);
CREATE TABLE IF NOT EXISTS premium_spot_config (
id INTEGER PRIMARY KEY,
total_spots INTEGER NOT NULL,
spots_taken INTEGER NOT NULL DEFAULT 0,
price_increase_at INTEGER,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS spot_reservations (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
email TEXT NOT NULL,
expires_at INTEGER,
confirmed_at INTEGER,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE INDEX IF NOT EXISTS idx_spot_reservations_expires_at ON spot_reservations(expires_at);
`);
ensureColumn(database, 'users', 'email_hash', 'TEXT');
ensureColumn(database, 'users', 'email_enc', 'TEXT');
ensureColumn(database, 'users', 'name_enc', 'TEXT');
ensureColumn(database, 'users', 'points', 'INTEGER NOT NULL DEFAULT 0');
ensureColumn(database, 'referrals', 'rewarded_at', 'INTEGER');
ensureColumn(database, 'points_adjustments', 'action', 'TEXT');
// Seed the spot config row exactly once
const configRow = database
.prepare('SELECT id FROM premium_spot_config WHERE id = 1')
.get();
if (!configRow) {
const now = Date.now();
// Price increases 30 days from first boot
database
.prepare(
'INSERT INTO premium_spot_config (id, total_spots, spots_taken, price_increase_at, updated_at) VALUES (1, 500, 0, ?, ?)'
)
.run(now + 30 * 24 * 60 * 60 * 1000, now);
}
database.exec(`
CREATE TABLE IF NOT EXISTS email_verification_tokens (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS share_tracking (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
referral_code TEXT,
share_channel TEXT NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email_hash ON users(email_hash) WHERE email_hash IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_rate_limits_reset_at ON rate_limits(reset_at);
CREATE INDEX IF NOT EXISTS idx_referrals_rewarded_at ON referrals(rewarded_at);
CREATE INDEX IF NOT EXISTS idx_points_adjustments_user_id ON points_adjustments(user_id);
CREATE INDEX IF NOT EXISTS idx_email_verification_tokens_user_id ON email_verification_tokens(user_id);
CREATE INDEX IF NOT EXISTS idx_email_verification_tokens_expires_at ON email_verification_tokens(expires_at);
CREATE INDEX IF NOT EXISTS idx_share_tracking_user_id ON share_tracking(user_id);
CREATE INDEX IF NOT EXISTS idx_share_tracking_created_at ON share_tracking(created_at);
CREATE TABLE IF NOT EXISTS unsubscribed_emails (
email TEXT PRIMARY KEY,
reason TEXT,
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_unsubscribed_emails_created_at ON unsubscribed_emails(created_at);
CREATE TABLE IF NOT EXISTS email_preferences (
email TEXT PRIMARY KEY,
preferences_data TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_email_preferences_updated_at ON email_preferences(updated_at);
CREATE TABLE IF NOT EXISTS magic_links (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
expires_at INTEGER NOT NULL,
created_at INTEGER NOT NULL,
used_at INTEGER,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE INDEX IF NOT EXISTS idx_magic_links_expires_at ON magic_links(expires_at);
CREATE INDEX IF NOT EXISTS idx_magic_links_user_id ON magic_links(user_id);
`);
}
export function getDb(): Db {
if (db) return db;
const explicitPath = process.env.PEERX_SQLITE_PATH;
const dbPath = explicitPath
? explicitPath === ':memory:'
? ':memory:'
: path.resolve(explicitPath)
: (() => {
const dataDir = process.env.PEERX_DATA_DIR
? path.resolve(process.env.PEERX_DATA_DIR)
: path.join(process.cwd(), '.data');
fs.mkdirSync(dataDir, { recursive: true });
return path.join(dataDir, 'peerx.sqlite');
})();
const instance = new Database(dbPath);
initDb(instance);
db = instance;
return instance;
}
export function resetDbForTests() {
if (process.env.NODE_ENV !== 'test') {
throw new Error('resetDbForTests is only available in test environment');
}
if (db) {
db.close();
db = null;
}
}