working progress

This commit is contained in:
Sam Chau
2025-10-17 00:56:39 +10:30
commit 2440b36fae
18 changed files with 643 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"recommendations": [
"denoland.vscode-deno"
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"deno.enablePaths": [
"./"
],
"editor.inlayHints.enabled": "off"
}
+6
View File
@@ -0,0 +1,6 @@
{
"version": "0.1",
"dependencies": {
"base": "0.1"
}
}
+7
View File
@@ -0,0 +1,7 @@
INSERT INTO profiles (name, description) VALUES
('banana-test', 'Test profile for banana database');
INSERT INTO profile_items (profile_id, quality_name, allowed, position) VALUES
(1, 'WEBDL-2160p', 1, 1),
(1, 'WEBDL-1080p', 1, 2),
(1, 'Bluray-2160p', 1, 3);
+3
View File
@@ -0,0 +1,3 @@
{
"version": "0.1"
}
+15
View File
@@ -0,0 +1,15 @@
CREATE TABLE profiles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
description TEXT
);
CREATE TABLE profile_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
profile_id INTEGER NOT NULL,
quality_name TEXT NOT NULL,
allowed BOOLEAN NOT NULL DEFAULT 1,
position INTEGER NOT NULL,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE,
UNIQUE(profile_id, quality_name)
);
+6
View File
@@ -0,0 +1,6 @@
{
"version": "0.1",
"dependencies": {
"base": "0.1"
}
}
+11
View File
@@ -0,0 +1,11 @@
INSERT INTO profiles (name, description) VALUES
('radarr', 'Radarr quality profile for movies'),
('sonarr', 'Sonarr quality profile for TV shows');
INSERT INTO profile_items (profile_id, quality_name, allowed, position) VALUES
(1, 'WEBDL-1080p', 1, 1),
(1, 'Bluray-1080p', 1, 2),
(1, 'WEBDL-720p', 1, 3),
(2, 'WEBDL-1080p', 1, 1),
(2, 'HDTV-1080p', 1, 2),
(2, 'WEBDL-720p', 1, 3);
+7
View File
@@ -0,0 +1,7 @@
UPDATE profile_items SET position = 1 WHERE profile_id = 1 AND quality_name = 'WEBDL-1080p';
UPDATE profile_items SET position = 2 WHERE profile_id = 1 AND quality_name = 'WEBDL-720p';
UPDATE profile_items SET position = 3 WHERE profile_id = 1 AND quality_name = 'Bluray-1080p';
UPDATE profile_items SET position = 1 WHERE profile_id = 2 AND quality_name = 'WEBDL-1080p';
UPDATE profile_items SET position = 2 WHERE profile_id = 2 AND quality_name = 'WEBDL-720p';
UPDATE profile_items SET position = 3 WHERE profile_id = 2 AND quality_name = 'HDTV-1080p';
+17
View File
@@ -0,0 +1,17 @@
{
"imports": {
"@std/path": "jsr:@std/path@0.224.0",
"@db/sqlite": "jsr:@db/sqlite@^0.12.0",
"hono": "jsr:@hono/hono@^4.9.12",
"@/utils/logger": "./src/utils/logger/logger.ts",
"@/utils/config": "./src/utils/config/config.ts",
"@/utils/cache": "./src/utils/cache/cache.ts"
},
"tasks": {
"dev": "deno run --allow-net --allow-read --allow-env --allow-ffi --watch src/main.ts"
},
"compilerOptions": {
"jsx": "precompile",
"jsxImportSource": "hono/jsx"
}
}
Generated
+169
View File
@@ -0,0 +1,169 @@
{
"version": "5",
"specifiers": {
"jsr:@db/sqlite@0.12": "0.12.0",
"jsr:@denosaurs/plug@1": "1.1.0",
"jsr:@hono/hono@^4.9.12": "4.9.12",
"jsr:@std/assert@0.217": "0.217.0",
"jsr:@std/assert@0.224": "0.224.0",
"jsr:@std/encoding@1": "1.0.10",
"jsr:@std/fmt@1": "1.0.8",
"jsr:@std/fs@1": "1.0.19",
"jsr:@std/internal@^1.0.10": "1.0.12",
"jsr:@std/internal@^1.0.9": "1.0.12",
"jsr:@std/path@*": "1.1.2",
"jsr:@std/path@0.217": "0.217.0",
"jsr:@std/path@0.224.0": "0.224.0",
"jsr:@std/path@1": "1.1.2",
"jsr:@std/path@^1.1.1": "1.1.2"
},
"jsr": {
"@db/sqlite@0.12.0": {
"integrity": "dd1ef7f621ad50fc1e073a1c3609c4470bd51edc0994139c5bf9851de7a6d85f",
"dependencies": [
"jsr:@denosaurs/plug",
"jsr:@std/path@0.217"
]
},
"@denosaurs/plug@1.1.0": {
"integrity": "eb2f0b7546c7bca2000d8b0282c54d50d91cf6d75cb26a80df25a6de8c4bc044",
"dependencies": [
"jsr:@std/encoding",
"jsr:@std/fmt",
"jsr:@std/fs",
"jsr:@std/path@1"
]
},
"@hono/hono@4.9.12": {
"integrity": "d4a0fe012c2f890bb514e539b37393ed352c173957038e44e2e45457b0a8256f"
},
"@std/assert@0.217.0": {
"integrity": "c98e279362ca6982d5285c3b89517b757c1e3477ee9f14eb2fdf80a45aaa9642"
},
"@std/assert@0.224.0": {
"integrity": "8643233ec7aec38a940a8264a6e3eed9bfa44e7a71cc6b3c8874213ff401967f"
},
"@std/encoding@1.0.10": {
"integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1"
},
"@std/fmt@1.0.8": {
"integrity": "71e1fc498787e4434d213647a6e43e794af4fd393ef8f52062246e06f7e372b7"
},
"@std/fs@1.0.19": {
"integrity": "051968c2b1eae4d2ea9f79a08a3845740ef6af10356aff43d3e2ef11ed09fb06",
"dependencies": [
"jsr:@std/internal@^1.0.9",
"jsr:@std/path@^1.1.1"
]
},
"@std/internal@1.0.12": {
"integrity": "972a634fd5bc34b242024402972cd5143eac68d8dffaca5eaa4dba30ce17b027"
},
"@std/path@0.217.0": {
"integrity": "1217cc25534bca9a2f672d7fe7c6f356e4027df400c0e85c0ef3e4343bc67d11",
"dependencies": [
"jsr:@std/assert@0.217"
]
},
"@std/path@0.224.0": {
"integrity": "55bca6361e5a6d158b9380e82d4981d82d338ec587de02951e2b7c3a24910ee6",
"dependencies": [
"jsr:@std/assert@0.224"
]
},
"@std/path@1.1.2": {
"integrity": "c0b13b97dfe06546d5e16bf3966b1cadf92e1cc83e56ba5476ad8b498d9e3038",
"dependencies": [
"jsr:@std/internal@^1.0.10"
]
}
},
"remote": {
"https://deno.land/std@0.224.0/assert/assert.ts": "09d30564c09de846855b7b071e62b5974b001bb72a4b797958fe0660e7849834",
"https://deno.land/std@0.224.0/assert/assertion_error.ts": "ba8752bd27ebc51f723702fac2f54d3e94447598f54264a6653d6413738a8917",
"https://deno.land/std@0.224.0/path/_common/assert_path.ts": "dbdd757a465b690b2cc72fc5fb7698c51507dec6bfafce4ca500c46b76ff7bd8",
"https://deno.land/std@0.224.0/path/_common/basename.ts": "569744855bc8445f3a56087fd2aed56bdad39da971a8d92b138c9913aecc5fa2",
"https://deno.land/std@0.224.0/path/_common/common.ts": "ef73c2860694775fe8ffcbcdd387f9f97c7a656febf0daa8c73b56f4d8a7bd4c",
"https://deno.land/std@0.224.0/path/_common/constants.ts": "dc5f8057159f4b48cd304eb3027e42f1148cf4df1fb4240774d3492b5d12ac0c",
"https://deno.land/std@0.224.0/path/_common/dirname.ts": "684df4aa71a04bbcc346c692c8485594fc8a90b9408dfbc26ff32cf3e0c98cc8",
"https://deno.land/std@0.224.0/path/_common/format.ts": "92500e91ea5de21c97f5fe91e178bae62af524b72d5fcd246d6d60ae4bcada8b",
"https://deno.land/std@0.224.0/path/_common/from_file_url.ts": "d672bdeebc11bf80e99bf266f886c70963107bdd31134c4e249eef51133ceccf",
"https://deno.land/std@0.224.0/path/_common/glob_to_reg_exp.ts": "6cac16d5c2dc23af7d66348a7ce430e5de4e70b0eede074bdbcf4903f4374d8d",
"https://deno.land/std@0.224.0/path/_common/normalize.ts": "684df4aa71a04bbcc346c692c8485594fc8a90b9408dfbc26ff32cf3e0c98cc8",
"https://deno.land/std@0.224.0/path/_common/normalize_string.ts": "33edef773c2a8e242761f731adeb2bd6d683e9c69e4e3d0092985bede74f4ac3",
"https://deno.land/std@0.224.0/path/_common/relative.ts": "faa2753d9b32320ed4ada0733261e3357c186e5705678d9dd08b97527deae607",
"https://deno.land/std@0.224.0/path/_common/strip_trailing_separators.ts": "7024a93447efcdcfeaa9339a98fa63ef9d53de363f1fbe9858970f1bba02655a",
"https://deno.land/std@0.224.0/path/_common/to_file_url.ts": "7f76adbc83ece1bba173e6e98a27c647712cab773d3f8cbe0398b74afc817883",
"https://deno.land/std@0.224.0/path/_interface.ts": "8dfeb930ca4a772c458a8c7bbe1e33216fe91c253411338ad80c5b6fa93ddba0",
"https://deno.land/std@0.224.0/path/_os.ts": "8fb9b90fb6b753bd8c77cfd8a33c2ff6c5f5bc185f50de8ca4ac6a05710b2c15",
"https://deno.land/std@0.224.0/path/basename.ts": "7ee495c2d1ee516ffff48fb9a93267ba928b5a3486b550be73071bc14f8cc63e",
"https://deno.land/std@0.224.0/path/common.ts": "03e52e22882402c986fe97ca3b5bb4263c2aa811c515ce84584b23bac4cc2643",
"https://deno.land/std@0.224.0/path/constants.ts": "0c206169ca104938ede9da48ac952de288f23343304a1c3cb6ec7625e7325f36",
"https://deno.land/std@0.224.0/path/dirname.ts": "85bd955bf31d62c9aafdd7ff561c4b5fb587d11a9a5a45e2b01aedffa4238a7c",
"https://deno.land/std@0.224.0/path/extname.ts": "593303db8ae8c865cbd9ceec6e55d4b9ac5410c1e276bfd3131916591b954441",
"https://deno.land/std@0.224.0/path/format.ts": "6ce1779b0980296cf2bc20d66436b12792102b831fd281ab9eb08fa8a3e6f6ac",
"https://deno.land/std@0.224.0/path/from_file_url.ts": "911833ae4fd10a1c84f6271f36151ab785955849117dc48c6e43b929504ee069",
"https://deno.land/std@0.224.0/path/glob_to_regexp.ts": "7f30f0a21439cadfdae1be1bf370880b415e676097fda584a63ce319053b5972",
"https://deno.land/std@0.224.0/path/is_absolute.ts": "4791afc8bfd0c87f0526eaa616b0d16e7b3ab6a65b62942e50eac68de4ef67d7",
"https://deno.land/std@0.224.0/path/is_glob.ts": "a65f6195d3058c3050ab905705891b412ff942a292bcbaa1a807a74439a14141",
"https://deno.land/std@0.224.0/path/join.ts": "ae2ec5ca44c7e84a235fd532e4a0116bfb1f2368b394db1c4fb75e3c0f26a33a",
"https://deno.land/std@0.224.0/path/join_globs.ts": "5b3bf248b93247194f94fa6947b612ab9d3abd571ca8386cf7789038545e54a0",
"https://deno.land/std@0.224.0/path/mod.ts": "f6bd79cb08be0e604201bc9de41ac9248582699d1b2ee0ab6bc9190d472cf9cd",
"https://deno.land/std@0.224.0/path/normalize.ts": "4155743ccceeed319b350c1e62e931600272fad8ad00c417b91df093867a8352",
"https://deno.land/std@0.224.0/path/normalize_glob.ts": "cc89a77a7d3b1d01053b9dcd59462b75482b11e9068ae6c754b5cf5d794b374f",
"https://deno.land/std@0.224.0/path/parse.ts": "77ad91dcb235a66c6f504df83087ce2a5471e67d79c402014f6e847389108d5a",
"https://deno.land/std@0.224.0/path/posix/_util.ts": "1e3937da30f080bfc99fe45d7ed23c47dd8585c5e473b2d771380d3a6937cf9d",
"https://deno.land/std@0.224.0/path/posix/basename.ts": "d2fa5fbbb1c5a3ab8b9326458a8d4ceac77580961b3739cd5bfd1d3541a3e5f0",
"https://deno.land/std@0.224.0/path/posix/common.ts": "26f60ccc8b2cac3e1613000c23ac5a7d392715d479e5be413473a37903a2b5d4",
"https://deno.land/std@0.224.0/path/posix/constants.ts": "93481efb98cdffa4c719c22a0182b994e5a6aed3047e1962f6c2c75b7592bef1",
"https://deno.land/std@0.224.0/path/posix/dirname.ts": "76cd348ffe92345711409f88d4d8561d8645353ac215c8e9c80140069bf42f00",
"https://deno.land/std@0.224.0/path/posix/extname.ts": "e398c1d9d1908d3756a7ed94199fcd169e79466dd88feffd2f47ce0abf9d61d2",
"https://deno.land/std@0.224.0/path/posix/format.ts": "185e9ee2091a42dd39e2a3b8e4925370ee8407572cee1ae52838aed96310c5c1",
"https://deno.land/std@0.224.0/path/posix/from_file_url.ts": "951aee3a2c46fd0ed488899d024c6352b59154c70552e90885ed0c2ab699bc40",
"https://deno.land/std@0.224.0/path/posix/glob_to_regexp.ts": "76f012fcdb22c04b633f536c0b9644d100861bea36e9da56a94b9c589a742e8f",
"https://deno.land/std@0.224.0/path/posix/is_absolute.ts": "cebe561ad0ae294f0ce0365a1879dcfca8abd872821519b4fcc8d8967f888ede",
"https://deno.land/std@0.224.0/path/posix/is_glob.ts": "8a8b08c08bf731acf2c1232218f1f45a11131bc01de81e5f803450a5914434b9",
"https://deno.land/std@0.224.0/path/posix/join.ts": "7fc2cb3716aa1b863e990baf30b101d768db479e70b7313b4866a088db016f63",
"https://deno.land/std@0.224.0/path/posix/join_globs.ts": "a9475b44645feddceb484ee0498e456f4add112e181cb94042cdc6d47d1cdd25",
"https://deno.land/std@0.224.0/path/posix/mod.ts": "2301fc1c54a28b349e20656f68a85f75befa0ee9b6cd75bfac3da5aca9c3f604",
"https://deno.land/std@0.224.0/path/posix/normalize.ts": "baeb49816a8299f90a0237d214cef46f00ba3e95c0d2ceb74205a6a584b58a91",
"https://deno.land/std@0.224.0/path/posix/normalize_glob.ts": "9c87a829b6c0f445d03b3ecadc14492e2864c3ebb966f4cea41e98326e4435c6",
"https://deno.land/std@0.224.0/path/posix/parse.ts": "09dfad0cae530f93627202f28c1befa78ea6e751f92f478ca2cc3b56be2cbb6a",
"https://deno.land/std@0.224.0/path/posix/relative.ts": "3907d6eda41f0ff723d336125a1ad4349112cd4d48f693859980314d5b9da31c",
"https://deno.land/std@0.224.0/path/posix/resolve.ts": "08b699cfeee10cb6857ccab38fa4b2ec703b0ea33e8e69964f29d02a2d5257cf",
"https://deno.land/std@0.224.0/path/posix/to_file_url.ts": "7aa752ba66a35049e0e4a4be5a0a31ac6b645257d2e031142abb1854de250aaf",
"https://deno.land/std@0.224.0/path/posix/to_namespaced_path.ts": "28b216b3c76f892a4dca9734ff1cc0045d135532bfd9c435ae4858bfa5a2ebf0",
"https://deno.land/std@0.224.0/path/relative.ts": "ab739d727180ed8727e34ed71d976912461d98e2b76de3d3de834c1066667add",
"https://deno.land/std@0.224.0/path/resolve.ts": "a6f977bdb4272e79d8d0ed4333e3d71367cc3926acf15ac271f1d059c8494d8d",
"https://deno.land/std@0.224.0/path/to_file_url.ts": "88f049b769bce411e2d2db5bd9e6fd9a185a5fbd6b9f5ad8f52bef517c4ece1b",
"https://deno.land/std@0.224.0/path/to_namespaced_path.ts": "b706a4103b104cfadc09600a5f838c2ba94dbcdb642344557122dda444526e40",
"https://deno.land/std@0.224.0/path/windows/_util.ts": "d5f47363e5293fced22c984550d5e70e98e266cc3f31769e1710511803d04808",
"https://deno.land/std@0.224.0/path/windows/basename.ts": "6bbc57bac9df2cec43288c8c5334919418d784243a00bc10de67d392ab36d660",
"https://deno.land/std@0.224.0/path/windows/common.ts": "26f60ccc8b2cac3e1613000c23ac5a7d392715d479e5be413473a37903a2b5d4",
"https://deno.land/std@0.224.0/path/windows/constants.ts": "5afaac0a1f67b68b0a380a4ef391bf59feb55856aa8c60dfc01bd3b6abb813f5",
"https://deno.land/std@0.224.0/path/windows/dirname.ts": "33e421be5a5558a1346a48e74c330b8e560be7424ed7684ea03c12c21b627bc9",
"https://deno.land/std@0.224.0/path/windows/extname.ts": "165a61b00d781257fda1e9606a48c78b06815385e7d703232548dbfc95346bef",
"https://deno.land/std@0.224.0/path/windows/format.ts": "bbb5ecf379305b472b1082cd2fdc010e44a0020030414974d6029be9ad52aeb6",
"https://deno.land/std@0.224.0/path/windows/from_file_url.ts": "ced2d587b6dff18f963f269d745c4a599cf82b0c4007356bd957cb4cb52efc01",
"https://deno.land/std@0.224.0/path/windows/glob_to_regexp.ts": "e45f1f89bf3fc36f94ab7b3b9d0026729829fabc486c77f414caebef3b7304f8",
"https://deno.land/std@0.224.0/path/windows/is_absolute.ts": "4a8f6853f8598cf91a835f41abed42112cebab09478b072e4beb00ec81f8ca8a",
"https://deno.land/std@0.224.0/path/windows/is_glob.ts": "8a8b08c08bf731acf2c1232218f1f45a11131bc01de81e5f803450a5914434b9",
"https://deno.land/std@0.224.0/path/windows/join.ts": "8d03530ab89195185103b7da9dfc6327af13eabdcd44c7c63e42e27808f50ecf",
"https://deno.land/std@0.224.0/path/windows/join_globs.ts": "a9475b44645feddceb484ee0498e456f4add112e181cb94042cdc6d47d1cdd25",
"https://deno.land/std@0.224.0/path/windows/mod.ts": "2301fc1c54a28b349e20656f68a85f75befa0ee9b6cd75bfac3da5aca9c3f604",
"https://deno.land/std@0.224.0/path/windows/normalize.ts": "78126170ab917f0ca355a9af9e65ad6bfa5be14d574c5fb09bb1920f52577780",
"https://deno.land/std@0.224.0/path/windows/normalize_glob.ts": "9c87a829b6c0f445d03b3ecadc14492e2864c3ebb966f4cea41e98326e4435c6",
"https://deno.land/std@0.224.0/path/windows/parse.ts": "08804327b0484d18ab4d6781742bf374976de662f8642e62a67e93346e759707",
"https://deno.land/std@0.224.0/path/windows/relative.ts": "3e1abc7977ee6cc0db2730d1f9cb38be87b0ce4806759d271a70e4997fc638d7",
"https://deno.land/std@0.224.0/path/windows/resolve.ts": "8dae1dadfed9d46ff46cc337c9525c0c7d959fb400a6308f34595c45bdca1972",
"https://deno.land/std@0.224.0/path/windows/to_file_url.ts": "40e560ee4854fe5a3d4d12976cef2f4e8914125c81b11f1108e127934ced502e",
"https://deno.land/std@0.224.0/path/windows/to_namespaced_path.ts": "4ffa4fb6fae321448d5fe810b3ca741d84df4d7897e61ee29be961a6aac89a4c"
},
"workspace": {
"dependencies": [
"jsr:@db/sqlite@0.12",
"jsr:@hono/hono@^4.9.12",
"jsr:@std/path@0.224.0"
]
}
}
+97
View File
@@ -0,0 +1,97 @@
import { Database } from "@db/sqlite";
import { join } from "@std/path";
import { logger } from "@/utils/logger";
import { config } from "@/utils/config";
interface Metadata {
version: string;
dependencies?: Record<string, string>;
}
/**
* Resolve dependencies for a database and return execution order
* @param dbName - The database name to resolve dependencies for
* @param schemaRoot - Root directory containing schema folders
* @returns Array of database names in execution order (dependencies first)
*/
async function resolveDependencies(
dbName: string,
dbRoot: string,
visited = new Set<string>()
): Promise<string[]> {
// Avoid circular dependencies
if (visited.has(dbName)) {
return [];
}
visited.add(dbName);
const metadataPath = join(dbRoot, dbName, 'metadata.json');
let metadata: Metadata;
try {
const content = await Deno.readTextFile(metadataPath);
metadata = JSON.parse(content);
} catch (error) {
// Base has no metadata, or metadata doesn't exist
if (dbName === 'base') {
return ['base'];
}
throw new Error(`Failed to read metadata for ${dbName}: ${error}`);
}
const order: string[] = [];
// Recursively resolve dependencies
if (metadata.dependencies) {
for (const depName of Object.keys(metadata.dependencies)) {
const depOrder = await resolveDependencies(depName, dbRoot, visited);
order.push(...depOrder);
}
}
// Add current database after its dependencies
order.push(dbName);
// Remove duplicates while preserving order
return Array.from(new Set(order));
}
/**
* Compile databases into an in-memory SQLite database
* @param databases - List of database names to compile
* @returns In-memory SQLite database instance
*/
export async function compile(databases: string[]): Promise<Database> {
const db = new Database(":memory:");
// Databases live in data/db/
const dbRoot = config.dbPath;
// Build complete execution order for all databases
const executionOrder: string[] = [];
for (const dbName of databases) {
const order = await resolveDependencies(dbName, dbRoot);
executionOrder.push(...order);
}
// Remove duplicates while preserving order
const uniqueOrder = Array.from(new Set(executionOrder));
logger.debug('Database execution order:', uniqueOrder);
// Execute operations.sql for each database in order
for (const dbName of uniqueOrder) {
const operationsPath = join(dbRoot, dbName, 'operations.sql');
try {
const sql = await Deno.readTextFile(operationsPath);
logger.debug(`Executing operations for: ${dbName}`);
db.exec(sql);
} catch (error) {
logger.error(`Failed to execute operations for ${dbName}:`, error);
throw error;
}
}
return db;
}
+62
View File
@@ -0,0 +1,62 @@
import { config } from "@/utils/config";
import { cache } from "@/utils/cache";
import { logger } from "@/utils/logger";
import { compile } from "./compile.ts";
import type { Database } from "@db/sqlite";
// Hardcoded list of databases to compile (we'd get this from config in a more complex app)
const DATABASES = ['dictionarry', 'banana'];
// In-memory SQLite database instances (one per database)
const databases = new Map<string, Database>();
/**
* Initialize the database system
* - Compile databases into memory
* - Watch for changes and recompile
*/
export async function initDb() {
logger.info('Initializing database system');
// Compile each database separately
for (const dbName of DATABASES) {
const db = await compile([dbName]);
databases.set(dbName, db);
logger.info(`Database '${dbName}' compiled successfully`);
}
// Watch the db directory for changes
cache.watch(config.dbPath, async (path, kind) => {
if (path.endsWith('.sql') || path.endsWith('.json')) {
logger.info(`Database file changed: ${path} (${kind})`);
logger.info('Recompiling databases...');
// Recompile all databases
for (const dbName of DATABASES) {
const db = await compile([dbName]);
databases.set(dbName, db);
}
logger.info('All databases recompiled successfully');
}
});
}
/**
* Get a specific database instance
* @param dbName - The name of the database to retrieve
*/
export function getDb(dbName: string): Database {
const db = databases.get(dbName);
if (!db) {
throw new Error(`Database '${dbName}' not found. Available databases: ${Array.from(databases.keys()).join(', ')}`);
}
return db;
}
/**
* Get all available database names
*/
export function getAvailableDatabases(): string[] {
return Array.from(databases.keys());
}
+30
View File
@@ -0,0 +1,30 @@
import { Hono } from 'hono';
import { getAvailableDatabases, getDb } from '../db.ts';
export const dbRoutes = new Hono();
dbRoutes.get('/databases', (c) => {
const databases = getAvailableDatabases();
return c.json({
databases,
});
});
dbRoutes.get('/databases/profiles/:db', (c) => {
const dbName = c.req.param('db');
try {
const db = getDb(dbName);
const profiles = db.prepare('SELECT * FROM profiles').all();
return c.json({
database: dbName,
profiles,
});
} catch (error) {
return c.json({
error: error instanceof Error ? error.message : 'Unknown error',
}, 404);
}
});
+28
View File
@@ -0,0 +1,28 @@
import { Hono, Context } from 'hono'
import { logger } from '@/utils/logger'
import { initDb } from './db/db.ts'
import { dbRoutes } from './db/routes/get.ts'
const app = new Hono()
// Logging middleware
app.use('*', async (c, next) => {
logger.info(`${c.req.method} ${c.req.path}`)
await next()
})
app.get('/', (c: Context) => {
return c.text('Hello Hono!')
})
app.route('/', dbRoutes)
// Initialize database before starting server
await initDb()
Deno.serve({
port: 8001,
onListen: ({ hostname, port }) => {
logger.info(`Server listening on http://${hostname}:${port}`)
}
}, app.fetch)
+109
View File
@@ -0,0 +1,109 @@
import { logger } from "@/utils/logger";
type WatchCallback = (path: string, kind: Deno.FsEvent["kind"]) => void | Promise<void>;
interface Watcher {
path: string;
callback: WatchCallback;
abortController: AbortController;
}
class Cache {
private watchers: Map<string, Watcher> = new Map();
/**
* Watch a folder and execute a callback when changes occur
* @param folderPath - The path to watch
* @param callback - Function to execute on file changes
* @returns A function to stop watching
*/
watch(folderPath: string, callback: WatchCallback): () => void {
// If already watching this path, stop the previous watcher
if (this.watchers.has(folderPath)) {
this.unwatch(folderPath);
}
const abortController = new AbortController();
this.watchers.set(folderPath, {
path: folderPath,
callback,
abortController,
});
logger.debug(`Watching folder: ${folderPath}`);
// Start watching in the background
this.startWatching(folderPath, callback, abortController.signal);
// Return unwatch function
return () => this.unwatch(folderPath);
}
private async startWatching(
folderPath: string,
callback: WatchCallback,
signal: AbortSignal
) {
try {
const watcher = Deno.watchFs(folderPath, { recursive: true });
const debounceMap = new Map<string, number>();
const DEBOUNCE_MS = 100;
for await (const event of watcher) {
if (signal.aborted) {
break;
}
// Execute callback for each changed path with debouncing
for (const path of event.paths) {
// Clear existing timeout for this path
const existingTimeout = debounceMap.get(path);
if (existingTimeout) {
clearTimeout(existingTimeout);
}
// Set new timeout
const timeoutId = setTimeout(async () => {
debounceMap.delete(path);
try {
await callback(path, event.kind);
} catch (error) {
logger.error(`Error in watch callback for ${path}:`, error);
}
}, DEBOUNCE_MS);
debounceMap.set(path, timeoutId);
}
}
} catch (error) {
if (!signal.aborted) {
logger.error(`Error watching ${folderPath}:`, error);
}
}
}
/**
* Stop watching a specific folder
*/
unwatch(folderPath: string): void {
const watcher = this.watchers.get(folderPath);
if (watcher) {
watcher.abortController.abort();
this.watchers.delete(folderPath);
logger.debug(`Stopped watching folder: ${folderPath}`);
}
}
/**
* Stop watching all folders
*/
unwatchAll(): void {
for (const [path] of this.watchers) {
this.unwatch(path);
}
}
}
// Export as singleton
export const cache = new Cache();
+14
View File
@@ -0,0 +1,14 @@
import { join } from "@std/path";
// Base path points to the data directory
const BASE_PATH = '/home/sam-chau/code/dictionarry/schema/data';
export const config = {
basePath: BASE_PATH,
// Database path
dbPath: join(BASE_PATH, 'db'),
// Logs path
logsPath: join(BASE_PATH, 'logs'),
} as const;
+51
View File
@@ -0,0 +1,51 @@
// ANSI color codes
const colors = {
reset: '\x1b[0m',
grey: '\x1b[90m',
red: '\x1b[31m',
yellow: '\x1b[33m',
green: '\x1b[32m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
}
type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
const levelColors: Record<LogLevel, string> = {
DEBUG: colors.cyan,
INFO: colors.green,
WARN: colors.yellow,
ERROR: colors.red,
}
function getTimestamp(): string {
const now = new Date()
return now.toISOString()
}
function formatLevel(level: LogLevel): string {
const color = levelColors[level]
const paddedLevel = level.padEnd(5, ' ')
return `${color}${paddedLevel}${colors.reset}`
}
function log(level: LogLevel, message: string, ...additional: unknown[]) {
const timestamp = `${colors.grey}${getTimestamp()}${colors.reset}`
const separator = `${colors.grey}|${colors.reset}`
const formattedLevel = formatLevel(level)
const additionalInfo = additional.length > 0
? ' ' + additional.map(item =>
typeof item === 'object' ? JSON.stringify(item) : String(item)
).join(' ')
: ''
console.log(`${timestamp} ${separator} ${formattedLevel} ${separator} ${message}${additionalInfo}`)
}
export const logger = {
debug: (message: string, ...additional: unknown[]) => log('DEBUG', message, ...additional),
info: (message: string, ...additional: unknown[]) => log('INFO', message, ...additional),
warn: (message: string, ...additional: unknown[]) => log('WARN', message, ...additional),
error: (message: string, ...additional: unknown[]) => log('ERROR', message, ...additional),
separator: () => console.log(`${colors.grey}${'─'.repeat(80)}${colors.reset}`),
}