Repository navigation
Expand file tree
/
Copy pathdb.js
More file actions
128 lines (114 loc) · 3.79 KB
/
Copy pathdb.js
File metadata and controls
128 lines (114 loc) · 3.79 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
const fs = require('fs').promises;
const path = require('path');
const DB_FILE = path.join(__dirname, 'data', 'db.json');
let mutationQueue = Promise.resolve();
// Ensure database directory and file exist
async function initDb() {
const dir = path.dirname(DB_FILE);
try {
await fs.mkdir(dir, { recursive: true });
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
try {
await fs.access(DB_FILE);
} catch (err) {
// File doesn't exist, create it with empty structure
await fs.writeFile(DB_FILE, JSON.stringify({ todos: [] }, null, 2), 'utf8');
}
}
async function readDbUnlocked() {
await initDb();
const data = await fs.readFile(DB_FILE, 'utf8');
return JSON.parse(data);
}
async function readDb() {
await mutationQueue;
return readDbUnlocked();
}
async function writeDb(data) {
await initDb();
await fs.writeFile(DB_FILE, JSON.stringify(data, null, 2), 'utf8');
}
function enqueueMutation(operation) {
const result = mutationQueue.then(operation, operation);
mutationQueue = result.catch(() => {});
return result;
}
function normalizeAttachments(value) {
if (!Array.isArray(value)) return [];
return value.slice(0, 6).map(item => ({
id: String(item?.id || '').slice(0, 100),
name: String(item?.name || '첨부 이미지').slice(0, 200),
url: String(item?.url || '').slice(0, 500),
mimeType: String(item?.mimeType || '').slice(0, 100),
size: Math.max(0, Number(item?.size) || 0)
})).filter(item => item.id && item.url.startsWith('/api/todo-attachments/'));
}
// CRUD Operations
async function getAllTodos() {
const db = await readDb();
return db.todos;
}
async function getTodoById(id) {
const db = await readDb();
return db.todos.find(todo => todo.id === id);
}
async function createTodo(todoData) {
return enqueueMutation(async () => {
const db = await readDbUnlocked();
const newTodo = {
id: Date.now().toString(36) + Math.random().toString(36).substr(2, 5),
title: todoData.title || 'Untitled Schedule',
content: todoData.content || '',
startDate: todoData.startDate || new Date().toISOString(),
endDate: todoData.endDate || todoData.startDate || new Date().toISOString(),
allDay: todoData.allDay === true,
color: todoData.color || '#4f46e5',
priority: todoData.priority || 'medium',
scheduleType: todoData.scheduleType === 'kidsnote' ? 'kidsnote' : 'general',
completed: todoData.completed || false,
category: todoData.category || 'general',
dateReason: todoData.dateReason || '',
evidence: todoData.evidence || '',
confidence: Number.isFinite(todoData.confidence) ? todoData.confidence : null,
attachments: normalizeAttachments(todoData.attachments),
createdAt: new Date().toISOString()
};
db.todos.push(newTodo);
await writeDb(db);
return newTodo;
});
}
async function updateTodo(id, updateData) {
return enqueueMutation(async () => {
const db = await readDbUnlocked();
const index = db.todos.findIndex(todo => todo.id === id);
if (index === -1) return null;
const normalizedUpdate = { ...updateData };
if (Object.prototype.hasOwnProperty.call(normalizedUpdate, 'attachments')) {
normalizedUpdate.attachments = normalizeAttachments(normalizedUpdate.attachments);
}
db.todos[index] = { ...db.todos[index], ...normalizedUpdate, id };
await writeDb(db);
return db.todos[index];
});
}
async function deleteTodo(id) {
return enqueueMutation(async () => {
const db = await readDbUnlocked();
const initialLength = db.todos.length;
db.todos = db.todos.filter(todo => todo.id !== id);
if (db.todos.length === initialLength) return false;
await writeDb(db);
return true;
});
}
module.exports = {
initDb,
getAllTodos,
getTodoById,
createTodo,
updateTodo,
deleteTodo
};