mem100x
Version:
⚡ The FASTEST MCP memory server ever built - 66k+ entities/sec with intelligent context detection
109 lines (105 loc) • 3.87 kB
JavaScript
"use strict";
/**
* Database schema definitions for Mem100x
* Single source of truth for all database table definitions and indexes
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FTS_REBUILD = exports.FTS_TRIGGERS = exports.FTS_SCHEMA = exports.INDEXES = exports.TABLES = exports.PRAGMAS = void 0;
exports.getCompleteSchema = getCompleteSchema;
exports.getPragmas = getPragmas;
const config_js_1 = require("./config.js");
exports.PRAGMAS = `
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = -${config_js_1.config.database.cacheSizeMb * 256};
PRAGMA temp_store = MEMORY;
PRAGMA mmap_size = ${config_js_1.config.database.mmapSizeMb * 1024 * 1024};
PRAGMA foreign_keys = ON;
`;
exports.TABLES = `
-- Entities table with full-text search support
CREATE TABLE IF NOT EXISTS entities (
name TEXT PRIMARY KEY COLLATE NOCASE,
entity_type TEXT NOT NULL,
observations TEXT NOT NULL,
created_at REAL DEFAULT (julianday('now')),
updated_at REAL DEFAULT (julianday('now'))
);
-- Relations table with efficient indexing
CREATE TABLE IF NOT EXISTS relations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
from_entity TEXT NOT NULL COLLATE NOCASE,
to_entity TEXT NOT NULL COLLATE NOCASE,
relation_type TEXT NOT NULL,
created_at REAL DEFAULT (julianday('now')),
UNIQUE(from_entity, to_entity, relation_type),
FOREIGN KEY (from_entity) REFERENCES entities(name) ON DELETE CASCADE,
FOREIGN KEY (to_entity) REFERENCES entities(name) ON DELETE CASCADE
);
`;
exports.INDEXES = `
-- Entity indexes for fast lookups
CREATE INDEX IF NOT EXISTS idx_entity_type ON entities(entity_type);
CREATE INDEX IF NOT EXISTS idx_entity_updated ON entities(updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_observations ON entities(observations);
-- Relation indexes for efficient queries
CREATE INDEX IF NOT EXISTS idx_from_entity ON relations(from_entity);
CREATE INDEX IF NOT EXISTS idx_to_entity ON relations(to_entity);
CREATE INDEX IF NOT EXISTS idx_relation_type ON relations(relation_type);
CREATE INDEX IF NOT EXISTS idx_relation_composite ON relations(from_entity, to_entity);
`;
exports.FTS_SCHEMA = `
-- Create FTS5 virtual table for full-text search
CREATE VIRTUAL TABLE IF NOT EXISTS entities_fts USING fts5(
name,
entity_type,
observations,
tokenize='unicode61'
);
`;
exports.FTS_TRIGGERS = `
-- Triggers to keep FTS table in sync with entities table
CREATE TRIGGER IF NOT EXISTS entities_fts_insert AFTER INSERT ON entities
BEGIN
INSERT INTO entities_fts(name, entity_type, observations)
VALUES (new.name, new.entity_type, new.observations);
END;
CREATE TRIGGER IF NOT EXISTS entities_fts_update AFTER UPDATE ON entities
BEGIN
DELETE FROM entities_fts WHERE name = old.name;
INSERT INTO entities_fts(name, entity_type, observations)
VALUES (new.name, new.entity_type, new.observations);
END;
CREATE TRIGGER IF NOT EXISTS entities_fts_delete AFTER DELETE ON entities
BEGIN
DELETE FROM entities_fts WHERE name = old.name;
END;
`;
exports.FTS_REBUILD = `
-- Rebuild FTS index from existing data
INSERT INTO entities_fts(name, entity_type, observations)
SELECT name, entity_type, observations FROM entities;
`;
/**
* Get complete schema creation SQL
*/
function getCompleteSchema() {
return [
exports.TABLES,
exports.INDEXES,
exports.FTS_SCHEMA,
exports.FTS_TRIGGERS,
exports.FTS_REBUILD
].join('\n');
}
/**
* Get pragmas as individual statements for execution
*/
function getPragmas() {
return exports.PRAGMAS
.split('\n')
.map(line => line.trim())
.filter(line => line.startsWith('PRAGMA'))
.map(line => line.replace(/;$/, ''));
}
//# sourceMappingURL=database-schema.js.map