@hasna/microservices 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +86 -1
- package/bin/mcp.js +86 -1
- package/dist/index.js +86 -1
- package/microservices/microservice-analytics/package.json +27 -0
- package/microservices/microservice-analytics/src/cli/index.ts +373 -0
- package/microservices/microservice-analytics/src/db/analytics.ts +564 -0
- package/microservices/microservice-analytics/src/db/database.ts +93 -0
- package/microservices/microservice-analytics/src/db/migrations.ts +50 -0
- package/microservices/microservice-analytics/src/index.ts +37 -0
- package/microservices/microservice-analytics/src/mcp/index.ts +334 -0
- package/microservices/microservice-assets/package.json +27 -0
- package/microservices/microservice-assets/src/cli/index.ts +375 -0
- package/microservices/microservice-assets/src/db/assets.ts +370 -0
- package/microservices/microservice-assets/src/db/database.ts +93 -0
- package/microservices/microservice-assets/src/db/migrations.ts +51 -0
- package/microservices/microservice-assets/src/index.ts +32 -0
- package/microservices/microservice-assets/src/mcp/index.ts +346 -0
- package/microservices/microservice-compliance/package.json +27 -0
- package/microservices/microservice-compliance/src/cli/index.ts +467 -0
- package/microservices/microservice-compliance/src/db/compliance.ts +633 -0
- package/microservices/microservice-compliance/src/db/database.ts +93 -0
- package/microservices/microservice-compliance/src/db/migrations.ts +63 -0
- package/microservices/microservice-compliance/src/index.ts +46 -0
- package/microservices/microservice-compliance/src/mcp/index.ts +438 -0
- package/microservices/microservice-habits/package.json +27 -0
- package/microservices/microservice-habits/src/cli/index.ts +315 -0
- package/microservices/microservice-habits/src/db/database.ts +93 -0
- package/microservices/microservice-habits/src/db/habits.ts +451 -0
- package/microservices/microservice-habits/src/db/migrations.ts +46 -0
- package/microservices/microservice-habits/src/index.ts +31 -0
- package/microservices/microservice-habits/src/mcp/index.ts +313 -0
- package/microservices/microservice-health/package.json +27 -0
- package/microservices/microservice-health/src/cli/index.ts +484 -0
- package/microservices/microservice-health/src/db/database.ts +93 -0
- package/microservices/microservice-health/src/db/health.ts +708 -0
- package/microservices/microservice-health/src/db/migrations.ts +70 -0
- package/microservices/microservice-health/src/index.ts +63 -0
- package/microservices/microservice-health/src/mcp/index.ts +437 -0
- package/microservices/microservice-notifications/package.json +27 -0
- package/microservices/microservice-notifications/src/cli/index.ts +349 -0
- package/microservices/microservice-notifications/src/db/database.ts +93 -0
- package/microservices/microservice-notifications/src/db/migrations.ts +62 -0
- package/microservices/microservice-notifications/src/db/notifications.ts +509 -0
- package/microservices/microservice-notifications/src/index.ts +41 -0
- package/microservices/microservice-notifications/src/mcp/index.ts +422 -0
- package/microservices/microservice-products/package.json +27 -0
- package/microservices/microservice-products/src/cli/index.ts +416 -0
- package/microservices/microservice-products/src/db/categories.ts +154 -0
- package/microservices/microservice-products/src/db/database.ts +93 -0
- package/microservices/microservice-products/src/db/migrations.ts +58 -0
- package/microservices/microservice-products/src/db/pricing-tiers.ts +66 -0
- package/microservices/microservice-products/src/db/products.ts +452 -0
- package/microservices/microservice-products/src/index.ts +53 -0
- package/microservices/microservice-products/src/mcp/index.ts +453 -0
- package/microservices/microservice-projects/package.json +27 -0
- package/microservices/microservice-projects/src/cli/index.ts +480 -0
- package/microservices/microservice-projects/src/db/database.ts +93 -0
- package/microservices/microservice-projects/src/db/migrations.ts +65 -0
- package/microservices/microservice-projects/src/db/projects.ts +715 -0
- package/microservices/microservice-projects/src/index.ts +57 -0
- package/microservices/microservice-projects/src/mcp/index.ts +501 -0
- package/microservices/microservice-proposals/package.json +27 -0
- package/microservices/microservice-proposals/src/cli/index.ts +400 -0
- package/microservices/microservice-proposals/src/db/database.ts +93 -0
- package/microservices/microservice-proposals/src/db/migrations.ts +52 -0
- package/microservices/microservice-proposals/src/db/proposals.ts +532 -0
- package/microservices/microservice-proposals/src/index.ts +37 -0
- package/microservices/microservice-proposals/src/mcp/index.ts +375 -0
- package/microservices/microservice-reading/package.json +27 -0
- package/microservices/microservice-reading/src/cli/index.ts +464 -0
- package/microservices/microservice-reading/src/db/database.ts +93 -0
- package/microservices/microservice-reading/src/db/migrations.ts +59 -0
- package/microservices/microservice-reading/src/db/reading.ts +524 -0
- package/microservices/microservice-reading/src/index.ts +51 -0
- package/microservices/microservice-reading/src/mcp/index.ts +368 -0
- package/microservices/microservice-travel/package.json +27 -0
- package/microservices/microservice-travel/src/cli/index.ts +505 -0
- package/microservices/microservice-travel/src/db/database.ts +93 -0
- package/microservices/microservice-travel/src/db/migrations.ts +77 -0
- package/microservices/microservice-travel/src/db/travel.ts +802 -0
- package/microservices/microservice-travel/src/index.ts +60 -0
- package/microservices/microservice-travel/src/mcp/index.ts +495 -0
- package/microservices/microservice-wiki/package.json +27 -0
- package/microservices/microservice-wiki/src/cli/index.ts +345 -0
- package/microservices/microservice-wiki/src/db/database.ts +93 -0
- package/microservices/microservice-wiki/src/db/migrations.ts +55 -0
- package/microservices/microservice-wiki/src/db/wiki.ts +395 -0
- package/microservices/microservice-wiki/src/index.ts +32 -0
- package/microservices/microservice-wiki/src/mcp/index.ts +344 -0
- package/package.json +1 -1
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import {
|
|
5
|
+
createHabit,
|
|
6
|
+
getHabit,
|
|
7
|
+
listHabits,
|
|
8
|
+
updateHabit,
|
|
9
|
+
deleteHabit,
|
|
10
|
+
activateHabit,
|
|
11
|
+
deactivateHabit,
|
|
12
|
+
completeHabit,
|
|
13
|
+
getStreak,
|
|
14
|
+
getAllStreaks,
|
|
15
|
+
getCompletionRate,
|
|
16
|
+
getTodayStatus,
|
|
17
|
+
getWeeklyReport,
|
|
18
|
+
} from "../db/habits.js";
|
|
19
|
+
|
|
20
|
+
const program = new Command();
|
|
21
|
+
|
|
22
|
+
program
|
|
23
|
+
.name("microservice-habits")
|
|
24
|
+
.description("Habit tracking microservice")
|
|
25
|
+
.version("0.0.1");
|
|
26
|
+
|
|
27
|
+
// --- Habits CRUD ---
|
|
28
|
+
|
|
29
|
+
program
|
|
30
|
+
.command("create")
|
|
31
|
+
.description("Create a new habit")
|
|
32
|
+
.requiredOption("--name <name>", "Habit name")
|
|
33
|
+
.option("--description <desc>", "Description")
|
|
34
|
+
.option("--frequency <freq>", "Frequency: daily, weekly, monthly", "daily")
|
|
35
|
+
.option("--target <n>", "Target count per period", "1")
|
|
36
|
+
.option("--category <cat>", "Category")
|
|
37
|
+
.option("--json", "Output as JSON", false)
|
|
38
|
+
.action((opts) => {
|
|
39
|
+
const habit = createHabit({
|
|
40
|
+
name: opts.name,
|
|
41
|
+
description: opts.description,
|
|
42
|
+
frequency: opts.frequency,
|
|
43
|
+
target_count: parseInt(opts.target),
|
|
44
|
+
category: opts.category,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (opts.json) {
|
|
48
|
+
console.log(JSON.stringify(habit, null, 2));
|
|
49
|
+
} else {
|
|
50
|
+
console.log(`Created habit: ${habit.name} (${habit.id})`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
program
|
|
55
|
+
.command("get")
|
|
56
|
+
.description("Get a habit by ID")
|
|
57
|
+
.argument("<id>", "Habit ID")
|
|
58
|
+
.option("--json", "Output as JSON", false)
|
|
59
|
+
.action((id, opts) => {
|
|
60
|
+
const habit = getHabit(id);
|
|
61
|
+
if (!habit) {
|
|
62
|
+
console.error(`Habit '${id}' not found.`);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (opts.json) {
|
|
67
|
+
console.log(JSON.stringify(habit, null, 2));
|
|
68
|
+
} else {
|
|
69
|
+
console.log(`${habit.name}`);
|
|
70
|
+
if (habit.description) console.log(` Description: ${habit.description}`);
|
|
71
|
+
console.log(` Frequency: ${habit.frequency}`);
|
|
72
|
+
console.log(` Target: ${habit.target_count}`);
|
|
73
|
+
if (habit.category) console.log(` Category: ${habit.category}`);
|
|
74
|
+
console.log(` Active: ${habit.active}`);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
program
|
|
79
|
+
.command("list")
|
|
80
|
+
.description("List habits")
|
|
81
|
+
.option("--category <cat>", "Filter by category")
|
|
82
|
+
.option("--active", "Show only active habits")
|
|
83
|
+
.option("--inactive", "Show only inactive habits")
|
|
84
|
+
.option("--frequency <freq>", "Filter by frequency")
|
|
85
|
+
.option("--limit <n>", "Limit results")
|
|
86
|
+
.option("--json", "Output as JSON", false)
|
|
87
|
+
.action((opts) => {
|
|
88
|
+
let active: boolean | undefined;
|
|
89
|
+
if (opts.active) active = true;
|
|
90
|
+
if (opts.inactive) active = false;
|
|
91
|
+
|
|
92
|
+
const habits = listHabits({
|
|
93
|
+
category: opts.category,
|
|
94
|
+
active,
|
|
95
|
+
frequency: opts.frequency,
|
|
96
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (opts.json) {
|
|
100
|
+
console.log(JSON.stringify(habits, null, 2));
|
|
101
|
+
} else {
|
|
102
|
+
if (habits.length === 0) {
|
|
103
|
+
console.log("No habits found.");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
for (const h of habits) {
|
|
107
|
+
const cat = h.category ? ` [${h.category}]` : "";
|
|
108
|
+
const status = h.active ? "" : " (inactive)";
|
|
109
|
+
console.log(` ${h.name} (${h.frequency}, target: ${h.target_count})${cat}${status}`);
|
|
110
|
+
}
|
|
111
|
+
console.log(`\n${habits.length} habit(s)`);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
program
|
|
116
|
+
.command("update")
|
|
117
|
+
.description("Update a habit")
|
|
118
|
+
.argument("<id>", "Habit ID")
|
|
119
|
+
.option("--name <name>", "Habit name")
|
|
120
|
+
.option("--description <desc>", "Description")
|
|
121
|
+
.option("--frequency <freq>", "Frequency")
|
|
122
|
+
.option("--target <n>", "Target count")
|
|
123
|
+
.option("--category <cat>", "Category")
|
|
124
|
+
.option("--json", "Output as JSON", false)
|
|
125
|
+
.action((id, opts) => {
|
|
126
|
+
const input: Record<string, unknown> = {};
|
|
127
|
+
if (opts.name !== undefined) input.name = opts.name;
|
|
128
|
+
if (opts.description !== undefined) input.description = opts.description;
|
|
129
|
+
if (opts.frequency !== undefined) input.frequency = opts.frequency;
|
|
130
|
+
if (opts.target !== undefined) input.target_count = parseInt(opts.target);
|
|
131
|
+
if (opts.category !== undefined) input.category = opts.category;
|
|
132
|
+
|
|
133
|
+
const habit = updateHabit(id, input);
|
|
134
|
+
if (!habit) {
|
|
135
|
+
console.error(`Habit '${id}' not found.`);
|
|
136
|
+
process.exit(1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (opts.json) {
|
|
140
|
+
console.log(JSON.stringify(habit, null, 2));
|
|
141
|
+
} else {
|
|
142
|
+
console.log(`Updated: ${habit.name}`);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
program
|
|
147
|
+
.command("delete")
|
|
148
|
+
.description("Delete a habit")
|
|
149
|
+
.argument("<id>", "Habit ID")
|
|
150
|
+
.action((id) => {
|
|
151
|
+
const deleted = deleteHabit(id);
|
|
152
|
+
if (deleted) {
|
|
153
|
+
console.log(`Deleted habit ${id}`);
|
|
154
|
+
} else {
|
|
155
|
+
console.error(`Habit '${id}' not found.`);
|
|
156
|
+
process.exit(1);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
program
|
|
161
|
+
.command("activate")
|
|
162
|
+
.description("Activate a habit")
|
|
163
|
+
.argument("<id>", "Habit ID")
|
|
164
|
+
.option("--json", "Output as JSON", false)
|
|
165
|
+
.action((id, opts) => {
|
|
166
|
+
const habit = activateHabit(id);
|
|
167
|
+
if (!habit) {
|
|
168
|
+
console.error(`Habit '${id}' not found.`);
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (opts.json) {
|
|
173
|
+
console.log(JSON.stringify(habit, null, 2));
|
|
174
|
+
} else {
|
|
175
|
+
console.log(`Activated: ${habit.name}`);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
program
|
|
180
|
+
.command("deactivate")
|
|
181
|
+
.description("Deactivate a habit")
|
|
182
|
+
.argument("<id>", "Habit ID")
|
|
183
|
+
.option("--json", "Output as JSON", false)
|
|
184
|
+
.action((id, opts) => {
|
|
185
|
+
const habit = deactivateHabit(id);
|
|
186
|
+
if (!habit) {
|
|
187
|
+
console.error(`Habit '${id}' not found.`);
|
|
188
|
+
process.exit(1);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (opts.json) {
|
|
192
|
+
console.log(JSON.stringify(habit, null, 2));
|
|
193
|
+
} else {
|
|
194
|
+
console.log(`Deactivated: ${habit.name}`);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// --- Completions ---
|
|
199
|
+
|
|
200
|
+
program
|
|
201
|
+
.command("complete")
|
|
202
|
+
.description("Record a habit completion")
|
|
203
|
+
.argument("<id>", "Habit ID")
|
|
204
|
+
.option("--notes <notes>", "Completion notes")
|
|
205
|
+
.option("--value <n>", "Numeric value")
|
|
206
|
+
.option("--json", "Output as JSON", false)
|
|
207
|
+
.action((id, opts) => {
|
|
208
|
+
const completion = completeHabit(
|
|
209
|
+
id,
|
|
210
|
+
opts.notes,
|
|
211
|
+
opts.value ? parseFloat(opts.value) : undefined
|
|
212
|
+
);
|
|
213
|
+
if (!completion) {
|
|
214
|
+
console.error(`Habit '${id}' not found.`);
|
|
215
|
+
process.exit(1);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (opts.json) {
|
|
219
|
+
console.log(JSON.stringify(completion, null, 2));
|
|
220
|
+
} else {
|
|
221
|
+
console.log(`Completed habit ${id} at ${completion.completed_at}`);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// --- Status & Analytics ---
|
|
226
|
+
|
|
227
|
+
program
|
|
228
|
+
.command("today")
|
|
229
|
+
.description("Show today's habit status")
|
|
230
|
+
.option("--json", "Output as JSON", false)
|
|
231
|
+
.action((opts) => {
|
|
232
|
+
const status = getTodayStatus();
|
|
233
|
+
|
|
234
|
+
if (opts.json) {
|
|
235
|
+
console.log(JSON.stringify(status, null, 2));
|
|
236
|
+
} else {
|
|
237
|
+
if (status.length === 0) {
|
|
238
|
+
console.log("No active habits.");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
for (const s of status) {
|
|
242
|
+
const check = s.completed_today ? "[x]" : "[ ]";
|
|
243
|
+
console.log(` ${check} ${s.habit.name}`);
|
|
244
|
+
}
|
|
245
|
+
const done = status.filter((s) => s.completed_today).length;
|
|
246
|
+
console.log(`\n${done}/${status.length} completed today`);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
program
|
|
251
|
+
.command("streaks")
|
|
252
|
+
.description("Show all streaks")
|
|
253
|
+
.option("--json", "Output as JSON", false)
|
|
254
|
+
.action((opts) => {
|
|
255
|
+
const streaks = getAllStreaks();
|
|
256
|
+
|
|
257
|
+
if (opts.json) {
|
|
258
|
+
console.log(JSON.stringify(streaks, null, 2));
|
|
259
|
+
} else {
|
|
260
|
+
if (streaks.length === 0) {
|
|
261
|
+
console.log("No streaks.");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
for (const s of streaks) {
|
|
265
|
+
const habit = getHabit(s.habit_id);
|
|
266
|
+
const name = habit ? habit.name : s.habit_id;
|
|
267
|
+
console.log(` ${name}: ${s.current_streak} day(s) (best: ${s.longest_streak})`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
program
|
|
273
|
+
.command("weekly-report")
|
|
274
|
+
.description("Show weekly completion report")
|
|
275
|
+
.option("--json", "Output as JSON", false)
|
|
276
|
+
.action((opts) => {
|
|
277
|
+
const report = getWeeklyReport();
|
|
278
|
+
|
|
279
|
+
if (opts.json) {
|
|
280
|
+
console.log(JSON.stringify(report, null, 2));
|
|
281
|
+
} else {
|
|
282
|
+
if (report.length === 0) {
|
|
283
|
+
console.log("No active habits for report.");
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
for (const r of report) {
|
|
287
|
+
console.log(` ${r.habit.name}: ${r.completions}/${r.target} (${r.rate}%)`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
program
|
|
293
|
+
.command("rate")
|
|
294
|
+
.description("Get completion rate for a habit")
|
|
295
|
+
.argument("<id>", "Habit ID")
|
|
296
|
+
.option("--days <n>", "Number of days to look back", "30")
|
|
297
|
+
.option("--json", "Output as JSON", false)
|
|
298
|
+
.action((id, opts) => {
|
|
299
|
+
const habit = getHabit(id);
|
|
300
|
+
if (!habit) {
|
|
301
|
+
console.error(`Habit '${id}' not found.`);
|
|
302
|
+
process.exit(1);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const days = parseInt(opts.days);
|
|
306
|
+
const rate = getCompletionRate(id, days);
|
|
307
|
+
|
|
308
|
+
if (opts.json) {
|
|
309
|
+
console.log(JSON.stringify({ habit_id: id, days, rate }));
|
|
310
|
+
} else {
|
|
311
|
+
console.log(`${habit.name}: ${rate}% completion rate over ${days} days`);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection for microservice-habits
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Database } from "bun:sqlite";
|
|
6
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { MIGRATIONS } from "./migrations.js";
|
|
9
|
+
|
|
10
|
+
let _db: Database | null = null;
|
|
11
|
+
|
|
12
|
+
function getDbPath(): string {
|
|
13
|
+
// Environment variable override
|
|
14
|
+
if (process.env["MICROSERVICES_DIR"]) {
|
|
15
|
+
return join(process.env["MICROSERVICES_DIR"], "microservice-habits", "data.db");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Check for .microservices in current or parent directories
|
|
19
|
+
let dir = resolve(process.cwd());
|
|
20
|
+
while (true) {
|
|
21
|
+
const candidate = join(dir, ".microservices", "microservice-habits", "data.db");
|
|
22
|
+
const msDir = join(dir, ".microservices");
|
|
23
|
+
if (existsSync(msDir)) return candidate;
|
|
24
|
+
const parent = dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Global fallback
|
|
30
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
|
|
31
|
+
return join(home, ".microservices", "microservice-habits", "data.db");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function ensureDir(filePath: string): void {
|
|
35
|
+
const dir = dirname(resolve(filePath));
|
|
36
|
+
if (!existsSync(dir)) {
|
|
37
|
+
mkdirSync(dir, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getDatabase(): Database {
|
|
42
|
+
if (_db) return _db;
|
|
43
|
+
|
|
44
|
+
const dbPath = getDbPath();
|
|
45
|
+
ensureDir(dbPath);
|
|
46
|
+
|
|
47
|
+
_db = new Database(dbPath);
|
|
48
|
+
_db.exec("PRAGMA journal_mode = WAL");
|
|
49
|
+
_db.exec("PRAGMA foreign_keys = ON");
|
|
50
|
+
|
|
51
|
+
// Create migrations table
|
|
52
|
+
_db.exec(`
|
|
53
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
54
|
+
id INTEGER PRIMARY KEY,
|
|
55
|
+
name TEXT NOT NULL,
|
|
56
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
57
|
+
)
|
|
58
|
+
`);
|
|
59
|
+
|
|
60
|
+
// Apply pending migrations
|
|
61
|
+
const applied = _db
|
|
62
|
+
.query("SELECT id FROM _migrations ORDER BY id")
|
|
63
|
+
.all() as { id: number }[];
|
|
64
|
+
const appliedIds = new Set(applied.map((r) => r.id));
|
|
65
|
+
|
|
66
|
+
for (const migration of MIGRATIONS) {
|
|
67
|
+
if (appliedIds.has(migration.id)) continue;
|
|
68
|
+
|
|
69
|
+
_db.exec("BEGIN");
|
|
70
|
+
try {
|
|
71
|
+
_db.exec(migration.sql);
|
|
72
|
+
_db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
|
|
73
|
+
migration.id,
|
|
74
|
+
migration.name
|
|
75
|
+
);
|
|
76
|
+
_db.exec("COMMIT");
|
|
77
|
+
} catch (error) {
|
|
78
|
+
_db.exec("ROLLBACK");
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return _db;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function closeDatabase(): void {
|
|
89
|
+
if (_db) {
|
|
90
|
+
_db.close();
|
|
91
|
+
_db = null;
|
|
92
|
+
}
|
|
93
|
+
}
|