@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.
Files changed (90) hide show
  1. package/bin/index.js +86 -1
  2. package/bin/mcp.js +86 -1
  3. package/dist/index.js +86 -1
  4. package/microservices/microservice-analytics/package.json +27 -0
  5. package/microservices/microservice-analytics/src/cli/index.ts +373 -0
  6. package/microservices/microservice-analytics/src/db/analytics.ts +564 -0
  7. package/microservices/microservice-analytics/src/db/database.ts +93 -0
  8. package/microservices/microservice-analytics/src/db/migrations.ts +50 -0
  9. package/microservices/microservice-analytics/src/index.ts +37 -0
  10. package/microservices/microservice-analytics/src/mcp/index.ts +334 -0
  11. package/microservices/microservice-assets/package.json +27 -0
  12. package/microservices/microservice-assets/src/cli/index.ts +375 -0
  13. package/microservices/microservice-assets/src/db/assets.ts +370 -0
  14. package/microservices/microservice-assets/src/db/database.ts +93 -0
  15. package/microservices/microservice-assets/src/db/migrations.ts +51 -0
  16. package/microservices/microservice-assets/src/index.ts +32 -0
  17. package/microservices/microservice-assets/src/mcp/index.ts +346 -0
  18. package/microservices/microservice-compliance/package.json +27 -0
  19. package/microservices/microservice-compliance/src/cli/index.ts +467 -0
  20. package/microservices/microservice-compliance/src/db/compliance.ts +633 -0
  21. package/microservices/microservice-compliance/src/db/database.ts +93 -0
  22. package/microservices/microservice-compliance/src/db/migrations.ts +63 -0
  23. package/microservices/microservice-compliance/src/index.ts +46 -0
  24. package/microservices/microservice-compliance/src/mcp/index.ts +438 -0
  25. package/microservices/microservice-habits/package.json +27 -0
  26. package/microservices/microservice-habits/src/cli/index.ts +315 -0
  27. package/microservices/microservice-habits/src/db/database.ts +93 -0
  28. package/microservices/microservice-habits/src/db/habits.ts +451 -0
  29. package/microservices/microservice-habits/src/db/migrations.ts +46 -0
  30. package/microservices/microservice-habits/src/index.ts +31 -0
  31. package/microservices/microservice-habits/src/mcp/index.ts +313 -0
  32. package/microservices/microservice-health/package.json +27 -0
  33. package/microservices/microservice-health/src/cli/index.ts +484 -0
  34. package/microservices/microservice-health/src/db/database.ts +93 -0
  35. package/microservices/microservice-health/src/db/health.ts +708 -0
  36. package/microservices/microservice-health/src/db/migrations.ts +70 -0
  37. package/microservices/microservice-health/src/index.ts +63 -0
  38. package/microservices/microservice-health/src/mcp/index.ts +437 -0
  39. package/microservices/microservice-notifications/package.json +27 -0
  40. package/microservices/microservice-notifications/src/cli/index.ts +349 -0
  41. package/microservices/microservice-notifications/src/db/database.ts +93 -0
  42. package/microservices/microservice-notifications/src/db/migrations.ts +62 -0
  43. package/microservices/microservice-notifications/src/db/notifications.ts +509 -0
  44. package/microservices/microservice-notifications/src/index.ts +41 -0
  45. package/microservices/microservice-notifications/src/mcp/index.ts +422 -0
  46. package/microservices/microservice-products/package.json +27 -0
  47. package/microservices/microservice-products/src/cli/index.ts +416 -0
  48. package/microservices/microservice-products/src/db/categories.ts +154 -0
  49. package/microservices/microservice-products/src/db/database.ts +93 -0
  50. package/microservices/microservice-products/src/db/migrations.ts +58 -0
  51. package/microservices/microservice-products/src/db/pricing-tiers.ts +66 -0
  52. package/microservices/microservice-products/src/db/products.ts +452 -0
  53. package/microservices/microservice-products/src/index.ts +53 -0
  54. package/microservices/microservice-products/src/mcp/index.ts +453 -0
  55. package/microservices/microservice-projects/package.json +27 -0
  56. package/microservices/microservice-projects/src/cli/index.ts +480 -0
  57. package/microservices/microservice-projects/src/db/database.ts +93 -0
  58. package/microservices/microservice-projects/src/db/migrations.ts +65 -0
  59. package/microservices/microservice-projects/src/db/projects.ts +715 -0
  60. package/microservices/microservice-projects/src/index.ts +57 -0
  61. package/microservices/microservice-projects/src/mcp/index.ts +501 -0
  62. package/microservices/microservice-proposals/package.json +27 -0
  63. package/microservices/microservice-proposals/src/cli/index.ts +400 -0
  64. package/microservices/microservice-proposals/src/db/database.ts +93 -0
  65. package/microservices/microservice-proposals/src/db/migrations.ts +52 -0
  66. package/microservices/microservice-proposals/src/db/proposals.ts +532 -0
  67. package/microservices/microservice-proposals/src/index.ts +37 -0
  68. package/microservices/microservice-proposals/src/mcp/index.ts +375 -0
  69. package/microservices/microservice-reading/package.json +27 -0
  70. package/microservices/microservice-reading/src/cli/index.ts +464 -0
  71. package/microservices/microservice-reading/src/db/database.ts +93 -0
  72. package/microservices/microservice-reading/src/db/migrations.ts +59 -0
  73. package/microservices/microservice-reading/src/db/reading.ts +524 -0
  74. package/microservices/microservice-reading/src/index.ts +51 -0
  75. package/microservices/microservice-reading/src/mcp/index.ts +368 -0
  76. package/microservices/microservice-travel/package.json +27 -0
  77. package/microservices/microservice-travel/src/cli/index.ts +505 -0
  78. package/microservices/microservice-travel/src/db/database.ts +93 -0
  79. package/microservices/microservice-travel/src/db/migrations.ts +77 -0
  80. package/microservices/microservice-travel/src/db/travel.ts +802 -0
  81. package/microservices/microservice-travel/src/index.ts +60 -0
  82. package/microservices/microservice-travel/src/mcp/index.ts +495 -0
  83. package/microservices/microservice-wiki/package.json +27 -0
  84. package/microservices/microservice-wiki/src/cli/index.ts +345 -0
  85. package/microservices/microservice-wiki/src/db/database.ts +93 -0
  86. package/microservices/microservice-wiki/src/db/migrations.ts +55 -0
  87. package/microservices/microservice-wiki/src/db/wiki.ts +395 -0
  88. package/microservices/microservice-wiki/src/index.ts +32 -0
  89. package/microservices/microservice-wiki/src/mcp/index.ts +344 -0
  90. package/package.json +1 -1
@@ -0,0 +1,464 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from "commander";
4
+ import {
5
+ createBook,
6
+ getBook,
7
+ listBooks,
8
+ updateBook,
9
+ deleteBook,
10
+ searchBooks,
11
+ startBook,
12
+ finishBook,
13
+ abandonBook,
14
+ getCurrentlyReading,
15
+ getBookProgress,
16
+ getReadingStats,
17
+ getReadingPace,
18
+ } from "../db/reading.js";
19
+ import {
20
+ createHighlight,
21
+ listHighlights,
22
+ searchHighlights,
23
+ } from "../db/reading.js";
24
+ import {
25
+ createReadingSession,
26
+ listReadingSessions,
27
+ } from "../db/reading.js";
28
+
29
+ const program = new Command();
30
+
31
+ program
32
+ .name("microservice-reading")
33
+ .description("Reading tracker microservice")
34
+ .version("0.0.1");
35
+
36
+ // --- Books ---
37
+
38
+ const bookCmd = program
39
+ .command("book")
40
+ .description("Book management");
41
+
42
+ bookCmd
43
+ .command("add")
44
+ .description("Add a new book")
45
+ .requiredOption("--title <title>", "Book title")
46
+ .option("--author <author>", "Author name")
47
+ .option("--isbn <isbn>", "ISBN")
48
+ .option("--category <category>", "Category/genre")
49
+ .option("--pages <pages>", "Total pages")
50
+ .option("--cover-url <url>", "Cover image URL")
51
+ .option("--json", "Output as JSON", false)
52
+ .action((opts) => {
53
+ const book = createBook({
54
+ title: opts.title,
55
+ author: opts.author,
56
+ isbn: opts.isbn,
57
+ category: opts.category,
58
+ pages: opts.pages ? parseInt(opts.pages) : undefined,
59
+ cover_url: opts.coverUrl,
60
+ });
61
+
62
+ if (opts.json) {
63
+ console.log(JSON.stringify(book, null, 2));
64
+ } else {
65
+ console.log(`Added book: ${book.title}${book.author ? ` by ${book.author}` : ""} (${book.id})`);
66
+ }
67
+ });
68
+
69
+ bookCmd
70
+ .command("list")
71
+ .description("List books")
72
+ .option("--status <status>", "Filter by status (to_read, reading, completed, abandoned)")
73
+ .option("--category <category>", "Filter by category")
74
+ .option("--author <author>", "Filter by author")
75
+ .option("--limit <n>", "Limit results")
76
+ .option("--json", "Output as JSON", false)
77
+ .action((opts) => {
78
+ const books = listBooks({
79
+ status: opts.status,
80
+ category: opts.category,
81
+ author: opts.author,
82
+ limit: opts.limit ? parseInt(opts.limit) : undefined,
83
+ });
84
+
85
+ if (opts.json) {
86
+ console.log(JSON.stringify(books, null, 2));
87
+ } else {
88
+ if (books.length === 0) {
89
+ console.log("No books found.");
90
+ return;
91
+ }
92
+ for (const b of books) {
93
+ const author = b.author ? ` by ${b.author}` : "";
94
+ const status = ` [${b.status}]`;
95
+ const progress = b.pages ? ` (${b.current_page}/${b.pages})` : "";
96
+ console.log(` ${b.title}${author}${status}${progress}`);
97
+ }
98
+ console.log(`\n${books.length} book(s)`);
99
+ }
100
+ });
101
+
102
+ bookCmd
103
+ .command("get")
104
+ .description("Get a book by ID")
105
+ .argument("<id>", "Book ID")
106
+ .option("--json", "Output as JSON", false)
107
+ .action((id, opts) => {
108
+ const book = getBook(id);
109
+ if (!book) {
110
+ console.error(`Book '${id}' not found.`);
111
+ process.exit(1);
112
+ }
113
+
114
+ if (opts.json) {
115
+ console.log(JSON.stringify(book, null, 2));
116
+ } else {
117
+ console.log(`${book.title}`);
118
+ if (book.author) console.log(` Author: ${book.author}`);
119
+ if (book.isbn) console.log(` ISBN: ${book.isbn}`);
120
+ console.log(` Status: ${book.status}`);
121
+ if (book.pages) console.log(` Progress: ${book.current_page}/${book.pages} pages`);
122
+ if (book.rating) console.log(` Rating: ${book.rating}/5`);
123
+ if (book.category) console.log(` Category: ${book.category}`);
124
+ if (book.started_at) console.log(` Started: ${book.started_at}`);
125
+ if (book.finished_at) console.log(` Finished: ${book.finished_at}`);
126
+ }
127
+ });
128
+
129
+ bookCmd
130
+ .command("update")
131
+ .description("Update a book")
132
+ .argument("<id>", "Book ID")
133
+ .option("--title <title>", "Title")
134
+ .option("--author <author>", "Author")
135
+ .option("--isbn <isbn>", "ISBN")
136
+ .option("--category <category>", "Category")
137
+ .option("--pages <pages>", "Total pages")
138
+ .option("--current-page <page>", "Current page")
139
+ .option("--rating <rating>", "Rating (1-5)")
140
+ .option("--cover-url <url>", "Cover URL")
141
+ .option("--json", "Output as JSON", false)
142
+ .action((id, opts) => {
143
+ const input: Record<string, unknown> = {};
144
+ if (opts.title !== undefined) input.title = opts.title;
145
+ if (opts.author !== undefined) input.author = opts.author;
146
+ if (opts.isbn !== undefined) input.isbn = opts.isbn;
147
+ if (opts.category !== undefined) input.category = opts.category;
148
+ if (opts.pages !== undefined) input.pages = parseInt(opts.pages);
149
+ if (opts.currentPage !== undefined) input.current_page = parseInt(opts.currentPage);
150
+ if (opts.rating !== undefined) input.rating = parseInt(opts.rating);
151
+ if (opts.coverUrl !== undefined) input.cover_url = opts.coverUrl;
152
+
153
+ const book = updateBook(id, input);
154
+ if (!book) {
155
+ console.error(`Book '${id}' not found.`);
156
+ process.exit(1);
157
+ }
158
+
159
+ if (opts.json) {
160
+ console.log(JSON.stringify(book, null, 2));
161
+ } else {
162
+ console.log(`Updated: ${book.title}`);
163
+ }
164
+ });
165
+
166
+ bookCmd
167
+ .command("delete")
168
+ .description("Delete a book")
169
+ .argument("<id>", "Book ID")
170
+ .action((id) => {
171
+ const deleted = deleteBook(id);
172
+ if (deleted) {
173
+ console.log(`Deleted book ${id}`);
174
+ } else {
175
+ console.error(`Book '${id}' not found.`);
176
+ process.exit(1);
177
+ }
178
+ });
179
+
180
+ bookCmd
181
+ .command("search")
182
+ .description("Search books")
183
+ .argument("<query>", "Search term")
184
+ .option("--json", "Output as JSON", false)
185
+ .action((query, opts) => {
186
+ const results = searchBooks(query);
187
+
188
+ if (opts.json) {
189
+ console.log(JSON.stringify(results, null, 2));
190
+ } else {
191
+ if (results.length === 0) {
192
+ console.log(`No books matching "${query}".`);
193
+ return;
194
+ }
195
+ for (const b of results) {
196
+ console.log(` ${b.title}${b.author ? ` by ${b.author}` : ""} [${b.status}]`);
197
+ }
198
+ }
199
+ });
200
+
201
+ bookCmd
202
+ .command("start")
203
+ .description("Start reading a book")
204
+ .argument("<id>", "Book ID")
205
+ .option("--json", "Output as JSON", false)
206
+ .action((id, opts) => {
207
+ const book = startBook(id);
208
+ if (!book) {
209
+ console.error(`Book '${id}' not found.`);
210
+ process.exit(1);
211
+ }
212
+
213
+ if (opts.json) {
214
+ console.log(JSON.stringify(book, null, 2));
215
+ } else {
216
+ console.log(`Started reading: ${book.title}`);
217
+ }
218
+ });
219
+
220
+ bookCmd
221
+ .command("finish")
222
+ .description("Mark a book as finished")
223
+ .argument("<id>", "Book ID")
224
+ .option("--rating <rating>", "Rating (1-5)")
225
+ .option("--json", "Output as JSON", false)
226
+ .action((id, opts) => {
227
+ const rating = opts.rating ? parseInt(opts.rating) : undefined;
228
+ const book = finishBook(id, rating);
229
+ if (!book) {
230
+ console.error(`Book '${id}' not found.`);
231
+ process.exit(1);
232
+ }
233
+
234
+ if (opts.json) {
235
+ console.log(JSON.stringify(book, null, 2));
236
+ } else {
237
+ console.log(`Finished: ${book.title}${rating ? ` (rated ${rating}/5)` : ""}`);
238
+ }
239
+ });
240
+
241
+ bookCmd
242
+ .command("abandon")
243
+ .description("Abandon a book")
244
+ .argument("<id>", "Book ID")
245
+ .option("--json", "Output as JSON", false)
246
+ .action((id, opts) => {
247
+ const book = abandonBook(id);
248
+ if (!book) {
249
+ console.error(`Book '${id}' not found.`);
250
+ process.exit(1);
251
+ }
252
+
253
+ if (opts.json) {
254
+ console.log(JSON.stringify(book, null, 2));
255
+ } else {
256
+ console.log(`Abandoned: ${book.title}`);
257
+ }
258
+ });
259
+
260
+ // --- Highlights ---
261
+
262
+ const highlightCmd = program
263
+ .command("highlight")
264
+ .description("Highlight management");
265
+
266
+ highlightCmd
267
+ .command("add")
268
+ .description("Add a highlight to a book")
269
+ .requiredOption("--book <id>", "Book ID")
270
+ .requiredOption("--text <text>", "Highlight text")
271
+ .option("--page <page>", "Page number")
272
+ .option("--chapter <chapter>", "Chapter name")
273
+ .option("--color <color>", "Highlight color", "yellow")
274
+ .option("--notes <notes>", "Notes about the highlight")
275
+ .option("--json", "Output as JSON", false)
276
+ .action((opts) => {
277
+ const highlight = createHighlight({
278
+ book_id: opts.book,
279
+ text: opts.text,
280
+ page: opts.page ? parseInt(opts.page) : undefined,
281
+ chapter: opts.chapter,
282
+ color: opts.color,
283
+ notes: opts.notes,
284
+ });
285
+
286
+ if (opts.json) {
287
+ console.log(JSON.stringify(highlight, null, 2));
288
+ } else {
289
+ console.log(`Added highlight (${highlight.id})`);
290
+ }
291
+ });
292
+
293
+ highlightCmd
294
+ .command("list")
295
+ .description("List highlights for a book")
296
+ .argument("<book-id>", "Book ID")
297
+ .option("--json", "Output as JSON", false)
298
+ .action((bookId, opts) => {
299
+ const highlights = listHighlights(bookId);
300
+
301
+ if (opts.json) {
302
+ console.log(JSON.stringify(highlights, null, 2));
303
+ } else {
304
+ if (highlights.length === 0) {
305
+ console.log("No highlights found.");
306
+ return;
307
+ }
308
+ for (const h of highlights) {
309
+ const page = h.page ? ` (p.${h.page})` : "";
310
+ const chapter = h.chapter ? ` [${h.chapter}]` : "";
311
+ console.log(` "${h.text}"${page}${chapter}`);
312
+ if (h.notes) console.log(` Note: ${h.notes}`);
313
+ }
314
+ console.log(`\n${highlights.length} highlight(s)`);
315
+ }
316
+ });
317
+
318
+ highlightCmd
319
+ .command("search")
320
+ .description("Search all highlights")
321
+ .argument("<query>", "Search term")
322
+ .option("--json", "Output as JSON", false)
323
+ .action((query, opts) => {
324
+ const results = searchHighlights(query);
325
+
326
+ if (opts.json) {
327
+ console.log(JSON.stringify(results, null, 2));
328
+ } else {
329
+ if (results.length === 0) {
330
+ console.log(`No highlights matching "${query}".`);
331
+ return;
332
+ }
333
+ for (const h of results) {
334
+ console.log(` "${h.text}" — ${h.book_title}`);
335
+ if (h.notes) console.log(` Note: ${h.notes}`);
336
+ }
337
+ }
338
+ });
339
+
340
+ // --- Sessions ---
341
+
342
+ const sessionCmd = program
343
+ .command("session")
344
+ .description("Reading session management");
345
+
346
+ sessionCmd
347
+ .command("log")
348
+ .description("Log a reading session")
349
+ .requiredOption("--book <id>", "Book ID")
350
+ .option("--pages <pages>", "Pages read")
351
+ .option("--duration <minutes>", "Duration in minutes")
352
+ .option("--date <date>", "Date (ISO string)", new Date().toISOString())
353
+ .option("--json", "Output as JSON", false)
354
+ .action((opts) => {
355
+ const session = createReadingSession({
356
+ book_id: opts.book,
357
+ pages_read: opts.pages ? parseInt(opts.pages) : undefined,
358
+ duration_min: opts.duration ? parseInt(opts.duration) : undefined,
359
+ logged_at: opts.date,
360
+ });
361
+
362
+ if (opts.json) {
363
+ console.log(JSON.stringify(session, null, 2));
364
+ } else {
365
+ const pages = session.pages_read ? `${session.pages_read} pages` : "";
366
+ const duration = session.duration_min ? `${session.duration_min} min` : "";
367
+ const parts = [pages, duration].filter(Boolean).join(", ");
368
+ console.log(`Logged session: ${parts || "no details"} (${session.id})`);
369
+ }
370
+ });
371
+
372
+ sessionCmd
373
+ .command("list")
374
+ .description("List reading sessions for a book")
375
+ .argument("<book-id>", "Book ID")
376
+ .option("--json", "Output as JSON", false)
377
+ .action((bookId, opts) => {
378
+ const sessions = listReadingSessions(bookId);
379
+
380
+ if (opts.json) {
381
+ console.log(JSON.stringify(sessions, null, 2));
382
+ } else {
383
+ if (sessions.length === 0) {
384
+ console.log("No sessions found.");
385
+ return;
386
+ }
387
+ for (const s of sessions) {
388
+ const pages = s.pages_read ? `${s.pages_read} pages` : "";
389
+ const duration = s.duration_min ? `${s.duration_min} min` : "";
390
+ const parts = [pages, duration].filter(Boolean).join(", ");
391
+ console.log(` ${s.logged_at}: ${parts || "no details"}`);
392
+ }
393
+ console.log(`\n${sessions.length} session(s)`);
394
+ }
395
+ });
396
+
397
+ // --- Top-level convenience commands ---
398
+
399
+ program
400
+ .command("stats")
401
+ .description("Show reading statistics")
402
+ .option("--year <year>", "Filter by year")
403
+ .option("--json", "Output as JSON", false)
404
+ .action((opts) => {
405
+ const year = opts.year ? parseInt(opts.year) : undefined;
406
+ const stats = getReadingStats(year);
407
+
408
+ if (opts.json) {
409
+ console.log(JSON.stringify(stats, null, 2));
410
+ } else {
411
+ console.log(`Reading Stats${year ? ` (${year})` : ""}:`);
412
+ console.log(` Books read: ${stats.books_read}`);
413
+ console.log(` Pages read: ${stats.pages_read}`);
414
+ console.log(` Total sessions: ${stats.total_sessions}`);
415
+ console.log(` Avg rating: ${stats.avg_rating ?? "N/A"}`);
416
+ if (Object.keys(stats.by_category).length > 0) {
417
+ console.log(" By category:");
418
+ for (const [cat, count] of Object.entries(stats.by_category)) {
419
+ console.log(` ${cat}: ${count}`);
420
+ }
421
+ }
422
+ }
423
+ });
424
+
425
+ program
426
+ .command("currently-reading")
427
+ .description("Show books currently being read")
428
+ .option("--json", "Output as JSON", false)
429
+ .action((opts) => {
430
+ const books = getCurrentlyReading();
431
+
432
+ if (opts.json) {
433
+ console.log(JSON.stringify(books, null, 2));
434
+ } else {
435
+ if (books.length === 0) {
436
+ console.log("Not reading any books right now.");
437
+ return;
438
+ }
439
+ for (const b of books) {
440
+ const progress = b.pages ? ` (${b.current_page}/${b.pages} pages)` : "";
441
+ console.log(` ${b.title}${b.author ? ` by ${b.author}` : ""}${progress}`);
442
+ }
443
+ }
444
+ });
445
+
446
+ program
447
+ .command("pace")
448
+ .description("Show reading pace")
449
+ .option("--json", "Output as JSON", false)
450
+ .action((opts) => {
451
+ const pace = getReadingPace();
452
+
453
+ if (opts.json) {
454
+ console.log(JSON.stringify(pace, null, 2));
455
+ } else {
456
+ console.log("Reading Pace:");
457
+ console.log(` Pages/day: ${pace.pages_per_day}`);
458
+ console.log(` Books/month: ${pace.books_per_month}`);
459
+ console.log(` Avg pages/session: ${pace.avg_session_pages}`);
460
+ console.log(` Avg minutes/session: ${pace.avg_session_minutes ?? "N/A"}`);
461
+ }
462
+ });
463
+
464
+ program.parse(process.argv);
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Database connection for microservice-reading
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-reading", "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-reading", "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-reading", "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
+ }
@@ -0,0 +1,59 @@
1
+ export interface MigrationEntry {
2
+ id: number;
3
+ name: string;
4
+ sql: string;
5
+ }
6
+
7
+ export const MIGRATIONS: MigrationEntry[] = [
8
+ {
9
+ id: 1,
10
+ name: "initial_schema",
11
+ sql: `
12
+ CREATE TABLE IF NOT EXISTS books (
13
+ id TEXT PRIMARY KEY,
14
+ title TEXT NOT NULL,
15
+ author TEXT,
16
+ isbn TEXT,
17
+ status TEXT NOT NULL DEFAULT 'to_read' CHECK(status IN ('to_read','reading','completed','abandoned')),
18
+ rating INTEGER,
19
+ category TEXT,
20
+ pages INTEGER,
21
+ current_page INTEGER NOT NULL DEFAULT 0,
22
+ started_at TEXT,
23
+ finished_at TEXT,
24
+ cover_url TEXT,
25
+ metadata TEXT NOT NULL DEFAULT '{}',
26
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
27
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
28
+ );
29
+
30
+ CREATE TABLE IF NOT EXISTS highlights (
31
+ id TEXT PRIMARY KEY,
32
+ book_id TEXT NOT NULL REFERENCES books(id) ON DELETE CASCADE,
33
+ text TEXT NOT NULL,
34
+ page INTEGER,
35
+ chapter TEXT,
36
+ color TEXT NOT NULL DEFAULT 'yellow',
37
+ notes TEXT,
38
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
39
+ );
40
+
41
+ CREATE TABLE IF NOT EXISTS reading_sessions (
42
+ id TEXT PRIMARY KEY,
43
+ book_id TEXT NOT NULL REFERENCES books(id) ON DELETE CASCADE,
44
+ pages_read INTEGER,
45
+ duration_min INTEGER,
46
+ logged_at TEXT NOT NULL,
47
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
48
+ );
49
+
50
+ CREATE INDEX IF NOT EXISTS idx_books_status ON books(status);
51
+ CREATE INDEX IF NOT EXISTS idx_books_author ON books(author);
52
+ CREATE INDEX IF NOT EXISTS idx_books_category ON books(category);
53
+ CREATE INDEX IF NOT EXISTS idx_books_isbn ON books(isbn);
54
+ CREATE INDEX IF NOT EXISTS idx_highlights_book ON highlights(book_id);
55
+ CREATE INDEX IF NOT EXISTS idx_reading_sessions_book ON reading_sessions(book_id);
56
+ CREATE INDEX IF NOT EXISTS idx_reading_sessions_logged ON reading_sessions(logged_at);
57
+ `,
58
+ },
59
+ ];