-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdb.js
522 lines (470 loc) · 14.9 KB
/
db.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/**
* Prism database handler
*/
/**
* @module PrismDB
* @version 0.4.0
* @description SQLite database adapter for prism Next 4.x with big fancy features or something.
*/
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const winston = require('winston');
// Configure Winston logger
const dbLogger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'logs/db.log' })
]
});
/**
* @class PrismDB
* @description Main database class that handles all database operations with queuing and TTL support
*/
class PrismDB {
/**
* @constructor
* @param {string} dbPath - Path to SQLite database file (obviously)
* @throws {Error} If database path is not provided or connection fails...
*/
constructor(dbPath) {
if (!dbPath) {
throw new Error('Database path is required');
}
const resolvedPath = path.resolve(dbPath.replace('sqlite://', ''));
this.db = new sqlite3.Database(resolvedPath, (err) => {
if (err) {
throw new Error(`Failed to connect to database: ${err.message}`);
}
});
this.namespace = 'prism';
this.ttlSupport = false;
this.queue = [];
this.isProcessing = false;
this.totalOperationTime = 0;
this.operationCount = 0;
this.maxQueueSize = 10000; // Prevent unbounded queue growth
this.tableName = 'prism'; // Default table name
// Enable WAL mode for better concurrency
this.db.run('PRAGMA journal_mode = WAL');
// Initialize the database table
this.initializeDatabase().catch(err => {
console.error('Failed to initialize database:', err);
});
// Log queue stats every 5 seconds
setInterval(() => this.logQueueStats(), 5000);
// Cleanup expired entries periodically if TTL is supported
if (this.ttlSupport) {
setInterval(() => this.cleanupExpired(), 60000);
}
}
/**
* @async
* @method initializeDatabase
* @description Initializes database tables and indexes
* @returns {Promise<void>}
*/
async initializeDatabase() {
return this.executeQuery(() => new Promise((resolve, reject) => {
// First check if keyv table exists
this.db.get("SELECT name FROM sqlite_master WHERE type='table' AND name='keyv'", (err, row) => {
if (err) {
reject(err);
return;
}
if (row) {
console.log('Using prism Legacy compatibility mode - Found existing keyv database');
this.tableName = 'keyv';
this.namespace = 'keyv';
resolve();
return;
}
// Create prism table if keyv doesn't exist
const createTableSQL = `
CREATE TABLE IF NOT EXISTS prism (
[key] TEXT PRIMARY KEY,
value TEXT NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now'))
)`;
this.db.serialize(() => {
this.db.run('BEGIN TRANSACTION');
this.db.run(createTableSQL, (err) => {
if (err) {
this.db.run('ROLLBACK');
reject(err);
return;
}
this.db.run('CREATE INDEX IF NOT EXISTS idx_prism_key ON prism ([key])', (indexErr) => {
if (indexErr) {
this.db.run('ROLLBACK');
reject(indexErr);
return;
}
this.db.run('COMMIT', (commitErr) => {
if (commitErr) {
reject(commitErr);
} else {
resolve();
}
});
});
});
});
});
}));
}
/**
* @async
* @method executeQuery
* @description Executes a database operation with queuing and timeout
* @param {Function} operation - Database operation to execute
* @returns {Promise<any>} Result of the operation
* @throws {Error} If queue is full or operation times out
*/
async executeQuery(operation) {
if (this.queue.length >= this.maxQueueSize) {
throw new Error('Database queue is full');
}
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('Database operation timed out'));
}, 30000); // 30 second timeout
this.queue.push({
operation,
resolve: (result) => {
clearTimeout(timeout);
resolve(result);
},
reject: (error) => {
clearTimeout(timeout);
reject(error);
}
});
this.processQueue();
});
}
/**
* @async
* @method processQueue
* @description Processes the next operation in the queue
* @private
*/
async processQueue() {
if (this.isProcessing || this.queue.length === 0) return;
this.isProcessing = true;
const { operation, resolve, reject } = this.queue.shift();
const startTime = Date.now();
try {
const result = await operation();
const operationTime = Date.now() - startTime;
this.updateStats(operationTime);
// Log successful transaction
dbLogger.info('Database transaction completed', {
operationTime,
queueLength: this.queue.length
});
resolve(result);
} catch (error) {
// Log failed transaction
dbLogger.error('Database transaction failed', {
error: error.message,
queueLength: this.queue.length
});
console.error('Database operation failed:', error);
reject(error);
} finally {
this.isProcessing = false;
setImmediate(() => this.processQueue());
}
}
/**
* @method updateStats
* @description Updates operation statistics
* @param {number} operationTime - Time taken for operation in milliseconds
* @private
*/
updateStats(operationTime) {
this.totalOperationTime += operationTime;
this.operationCount++;
// Reset stats periodically to prevent overflow
if (this.operationCount > 1000000) {
this.totalOperationTime = operationTime;
this.operationCount = 1;
}
}
/**
* @method logQueueStats
* @description Logs queue statistics
* @private
*/
logQueueStats() {
const avgOperationTime = this.operationCount > 0 ? this.totalOperationTime / this.operationCount : 0;
dbLogger.info('Queue statistics', {
queueLength: this.queue.length,
averageOperationTime: avgOperationTime.toFixed(2)
});
}
/**
* @async
* @method cleanupExpired
* @description Removes expired entries from database
* @returns {Promise<void>}
*/
async cleanupExpired() {
if (!this.ttlSupport) return;
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.run(`DELETE FROM ${this.tableName} WHERE json_extract(value, "$.expires") < ?`, [Date.now()], (err) => {
if (err) reject(err);
else resolve();
});
}));
}
/**
* @async
* @method get
* @description Retrieves a value by key
* @param {string} key - Key to retrieve
* @returns {Promise<any>} Retrieved value
* @throws {Error} If key is not provided or value parsing fails
*/
async get(key) {
if (!key) throw new Error('Key is required');
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.get(`SELECT value FROM ${this.tableName} WHERE [key] = ?`, [`${this.namespace}:${key}`], (err, row) => {
if (err) {
reject(err);
} else {
if (row) {
try {
const parsed = JSON.parse(row.value);
if (this.ttlSupport && parsed.expires && parsed.expires < Date.now()) {
this.delete(key).catch(console.error);
resolve(undefined);
} else {
resolve(parsed.value);
}
} catch (e) {
reject(new Error(`Failed to parse stored value: ${e.message}`));
}
} else {
resolve(undefined);
}
}
});
}));
}
/**
* @async
* @method set
* @description Sets a value with optional TTL
* @param {string} key - Key to set
* @param {any} value - Value to store
* @param {number} [ttl] - Time-to-live in milliseconds
* @returns {Promise<void>}
* @throws {Error} If key is not provided
*/
async set(key, value, ttl) {
if (!key) throw new Error('Key is required');
const expires = this.ttlSupport && ttl ? Date.now() + ttl : undefined;
const data = JSON.stringify({
value,
expires
});
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.run(`INSERT OR REPLACE INTO ${this.tableName} ([key], value) VALUES (?, ?)`, [`${this.namespace}:${key}`, data], (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}));
}
/**
* @async
* @method delete
* @description Deletes a value by key
* @param {string} key - Key to delete
* @returns {Promise<void>}
* @throws {Error} If key is not provided
*/
async delete(key) {
if (!key) throw new Error('Key is required');
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.run(`DELETE FROM ${this.tableName} WHERE [key] = ?`, [`${this.namespace}:${key}`], (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}));
}
/**
* @async
* @method clear
* @description Clears all values in the current namespace
* @returns {Promise<void>}
*/
async clear() {
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.run(`DELETE FROM ${this.tableName} WHERE [key] LIKE ?`, [`${this.namespace}:%`], (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}));
}
/**
* @async
* @method has
* @description Checks if a key exists
* @param {string} key - Key to check
* @returns {Promise<boolean>} True if key exists
* @throws {Error} If key is not provided
*/
async has(key) {
if (!key) throw new Error('Key is required');
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.get(`SELECT 1 FROM ${this.tableName} WHERE [key] = ?`, [`${this.namespace}:${key}`], (err, row) => {
if (err) {
reject(err);
} else {
resolve(!!row);
}
});
}));
}
/**
* @async
* @method close
* @description Closes database connection
* @returns {Promise<void>}
*/
close() {
return new Promise((resolve, reject) => {
this.db.close((err) => {
if (err) reject(err);
else resolve();
});
});
}
/**
* @async
* @method getAll
* @description Retrieves all key-value pairs in the current namespace
* @returns {Promise<Object>} Object containing all key-value pairs
*/
async getAll() {
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.all(`SELECT [key], value FROM ${this.tableName} WHERE [key] LIKE ?`, [`${this.namespace}:%`], (err, rows) => {
if (err) {
reject(err);
} else {
const result = {};
rows.forEach(row => {
const key = row.key.replace(`${this.namespace}:`, '');
try {
const parsed = JSON.parse(row.value);
if (!(this.ttlSupport && parsed.expires && parsed.expires < Date.now())) {
result[key] = parsed.value;
}
} catch (e) {
console.error(`Failed to parse value for key ${key}:`, e);
}
});
resolve(result);
}
});
}));
}
/**
* @async
* @method increment
* @description Increments a numeric value by the specified amount
* @param {string} key - Key to increment
* @param {number} [amount=1] - Amount to increment by
* @returns {Promise<number>} New value after increment
*/
async increment(key, amount = 1) {
const currentValue = await this.get(key) || 0;
if (typeof currentValue !== 'number') {
throw new Error('Value must be a number to increment');
}
const newValue = currentValue + amount;
await this.set(key, newValue);
return newValue;
}
/**
* @async
* @method decrement
* @description Decrements a numeric value by the specified amount
* @param {string} key - Key to decrement
* @param {number} [amount=1] - Amount to decrement by
* @returns {Promise<number>} New value after decrement
*/
async decrement(key, amount = 1) {
return this.increment(key, -amount);
}
/**
* @async
* @method search
* @description Searches for keys matching a pattern
* @param {string} pattern - Search pattern (SQL LIKE pattern)
* @returns {Promise<string[]>} Array of matching keys
*/
async search(pattern) {
return this.executeQuery(() => new Promise((resolve, reject) => {
this.db.all(
`SELECT [key] FROM ${this.tableName} WHERE [key] LIKE ?`,
[`${this.namespace}:${pattern}`],
(err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows.map(row => row.key.replace(`${this.namespace}:`, '')));
}
}
);
}));
}
/**
* @async
* @method setMultiple
* @description Sets multiple key-value pairs at once
* @param {Object} entries - Object containing key-value pairs to set
* @param {number} [ttl] - Optional TTL for all entries
* @returns {Promise<void>}
*/
async setMultiple(entries, ttl) {
return this.executeQuery(() => new Promise((resolve, reject) => {
const stmt = this.db.prepare(`INSERT OR REPLACE INTO ${this.tableName} ([key], value) VALUES (?, ?)`);
this.db.serialize(() => {
this.db.run('BEGIN TRANSACTION');
try {
for (const [key, value] of Object.entries(entries)) {
const data = JSON.stringify({
value,
expires: this.ttlSupport && ttl ? Date.now() + ttl : undefined
});
stmt.run(`${this.namespace}:${key}`, data);
}
this.db.run('COMMIT', (err) => {
if (err) reject(err);
else resolve();
});
} catch (err) {
this.db.run('ROLLBACK');
reject(err);
} finally {
stmt.finalize();
}
});
}));
}
}
module.exports = PrismDB;