From 5d51aa989501966c993024c151e0e7c915bbaa70 Mon Sep 17 00:00:00 2001 From: Storme-bit Date: Sat, 4 Apr 2026 05:49:57 -0700 Subject: [PATCH] memory service schema added --- packages/memory-service/src/db/index.js | 23 +++++++++ packages/memory-service/src/db/schema.js | 63 ++++++++++++++++++++++++ packages/memory-service/src/index.js | 4 ++ 3 files changed, 90 insertions(+) create mode 100644 packages/memory-service/src/db/index.js create mode 100644 packages/memory-service/src/db/schema.js diff --git a/packages/memory-service/src/db/index.js b/packages/memory-service/src/db/index.js new file mode 100644 index 0000000..04f7a53 --- /dev/null +++ b/packages/memory-service/src/db/index.js @@ -0,0 +1,23 @@ +const Database = require('better-sqlite3'); +const schema = require('./schema'); +const {getEnv } = require('@nexusai/shared'); + +let db; // Declare db variable in a scope accessible to all functions + +function getDB() { + if (!db) { + const path = getEnv('SQLITE_PATH', './data/nexusai.db'); + db = new Database(path); + + db.pragma('journal_mode = WAL'); + db.pragma('foreign_keys = ON'); + + db.exec(schema); + console.log(`Connected to SQLite database at ${path}`); + } + return db; +} + +module.exports = { + getDB +}; \ No newline at end of file diff --git a/packages/memory-service/src/db/schema.js b/packages/memory-service/src/db/schema.js new file mode 100644 index 0000000..bd8e526 --- /dev/null +++ b/packages/memory-service/src/db/schema.js @@ -0,0 +1,63 @@ +const schema = ` + CREATE TABLE IF NOT EXISTS sessions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + external_id TEXT UNIQUE NOT NULL, + created_at INTEGER NOT NULL DEFAULT (unixepoch()), + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + metadata TEXT + ); + + CREATE TABLE IF NOT EXISTS episodes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + session_id INTEGER NOT NULL REFERENCES sessions(id) ON DELETE CASCADE, + user_message TEXT NOT NULL, + ai_response TEXT NOT NULL, + created_at INTEGER NOT NULL DEFAULT (unixepoch()), + token_count INTEGER, + metadata TEXT + ); + + CREATE TABLE IF NOT EXISTS entities ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + type TEXT NOT NULL, + notes TEXT, + created_at INTEGER NOT NULL DEFAULT (unixepoch()), + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + metadata TEXT, + UNIQUE(name, type) + ); + + CREATE TABLE IF NOT EXISTS relationships ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + from_id INTEGER NOT NULL REFERENCES entities(id) ON DELETE CASCADE, + to_id INTEGER NOT NULL REFERENCES entities(id) ON DELETE CASCADE, + label TEXT NOT NULL, + created_at INTEGER NOT NULL DEFAULT (unixepoch()), + metadata TEXT, + UNIQUE(from_id, to_id, label) + ); + + CREATE TABLE IF NOT EXISTS summaries ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + session_id INTEGER REFERENCES sessions(id) ON DELETE CASCADE, + content TEXT NOT NULL, + episode_range TEXT, + created_at INTEGER NOT NULL DEFAULT (unixepoch()), + metadata TEXT + ); + + CREATE INDEX IF NOT EXISTS idx_episodes_session + ON episodes(session_id); + CREATE INDEX IF NOT EXISTS idx_episodes_created + ON episodes(created_at); + CREATE INDEX IF NOT EXISTS idx_entities_type + ON entities(type); + CREATE INDEX IF NOT EXISTS idx_summaries_session + ON summaries(session_id); + + CREATE VIRTUAL TABLE IF NOT EXISTS episodes_fts + USING fts5(user_message, ai_response, content=episodes, content_rowid=id); +`; + +module.exports = schema; \ No newline at end of file diff --git a/packages/memory-service/src/index.js b/packages/memory-service/src/index.js index 8f599b5..aec0faf 100644 --- a/packages/memory-service/src/index.js +++ b/packages/memory-service/src/index.js @@ -1,12 +1,16 @@ require ('dotenv').config(); const express = require('express'); const {getEnv} = require('@nexusai/shared'); +const { getDB } = require('./db'); const app = express(); app.use(express.json()); const PORT = getEnv('PORT', '3002'); // Default to 3002 if PORT is not set +//initialize database on startup +const db = getDB(); + // Health check endpoint app.get('/health', (req, res) => { res.json({ service: 'Memory Service', status: 'healthy' });