memory service schema added
This commit is contained in:
23
packages/memory-service/src/db/index.js
Normal file
23
packages/memory-service/src/db/index.js
Normal file
@@ -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
|
||||||
|
};
|
||||||
63
packages/memory-service/src/db/schema.js
Normal file
63
packages/memory-service/src/db/schema.js
Normal file
@@ -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;
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
require ('dotenv').config();
|
require ('dotenv').config();
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const {getEnv} = require('@nexusai/shared');
|
const {getEnv} = require('@nexusai/shared');
|
||||||
|
const { getDB } = require('./db');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
const PORT = getEnv('PORT', '3002'); // Default to 3002 if PORT is not set
|
const PORT = getEnv('PORT', '3002'); // Default to 3002 if PORT is not set
|
||||||
|
|
||||||
|
//initialize database on startup
|
||||||
|
const db = getDB();
|
||||||
|
|
||||||
// Health check endpoint
|
// Health check endpoint
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
res.json({ service: 'Memory Service', status: 'healthy' });
|
res.json({ service: 'Memory Service', status: 'healthy' });
|
||||||
|
|||||||
Reference in New Issue
Block a user