Repository navigation
Expand file tree
/
Copy pathdb.js
More file actions
138 lines (120 loc) · 3.26 KB
/
Copy pathdb.js
File metadata and controls
138 lines (120 loc) · 3.26 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
const fs = require('fs');
const path = require('path');
const DB_DIR = path.join(__dirname, 'data');
const SESSIONS_FILE = path.join(DB_DIR, 'sessions.json');
const TRANSCRIPTS_FILE = path.join(DB_DIR, 'transcripts.json');
// Ensure data directory exists
if (!fs.existsSync(DB_DIR)) {
fs.mkdirSync(DB_DIR, { recursive: true });
}
// Initialize files if they don't exist
function initFile(filePath, defaultData = []) {
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, JSON.stringify(defaultData, null, 2));
}
}
initFile(SESSIONS_FILE, []);
initFile(TRANSCRIPTS_FILE, []);
// Generic read/write operations
function readData(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(`Error reading ${filePath}:`, err.message);
return [];
}
}
function writeData(filePath, data) {
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
return true;
} catch (err) {
console.error(`Error writing ${filePath}:`, err.message);
return false;
}
}
// App Sessions operations
const sessions = {
getAll() {
return readData(SESSIONS_FILE);
},
getById(sessionId) {
const all = this.getAll();
return all.find(s => s.id === sessionId);
},
create(sessionData) {
const all = this.getAll();
const newSession = {
id: sessionData.id || Date.now().toString(),
created_at: new Date().toISOString(),
...sessionData
};
all.push(newSession);
writeData(SESSIONS_FILE, all);
return newSession;
},
update(sessionId, updates) {
const all = this.getAll();
const index = all.findIndex(s => s.id === sessionId);
if (index !== -1) {
all[index] = { ...all[index], ...updates, updated_at: new Date().toISOString() };
writeData(SESSIONS_FILE, all);
return all[index];
}
return null;
},
delete(sessionId) {
const all = this.getAll();
const filtered = all.filter(s => s.id !== sessionId);
if (filtered.length < all.length) {
writeData(SESSIONS_FILE, filtered);
return true;
}
return false;
}
};
// Transcript Entries operations
const transcripts = {
getAll() {
return readData(TRANSCRIPTS_FILE);
},
getBySessionId(sessionId) {
const all = this.getAll();
return all.filter(t => t.app_session_id === sessionId);
},
create(transcriptData) {
const all = this.getAll();
const newTranscript = {
id: transcriptData.id || `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
created_at: new Date().toISOString(),
...transcriptData
};
all.push(newTranscript);
writeData(TRANSCRIPTS_FILE, all);
return newTranscript;
},
delete(transcriptId) {
const all = this.getAll();
const filtered = all.filter(t => t.id !== transcriptId);
if (filtered.length < all.length) {
writeData(TRANSCRIPTS_FILE, filtered);
return true;
}
return false;
},
deleteBySessionId(sessionId) {
const all = this.getAll();
const filtered = all.filter(t => t.app_session_id !== sessionId);
const deleted = all.length - filtered.length;
if (deleted > 0) {
writeData(TRANSCRIPTS_FILE, filtered);
return deleted;
}
return 0;
}
};
module.exports = {
sessions,
transcripts
};