MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / DatabaseConnection

Class DatabaseConnection

src/db/index.ts:94–1005  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92 * Database connection wrapper with lifecycle management
93 */
94export class DatabaseConnection {
95 private db: SqliteDatabase;
96 private dbPath: string;
97 private backend: SqliteBackend;
98 /**
99 * `dev:ino` of the DB file at the moment we opened it (or null when the
100 * platform/filesystem reports no usable inode). Lets us notice when the file
101 * we hold open has been unlinked and REPLACED by a new file at the same path
102 * — a git worktree removed and re-added, or `.codegraph/` deleted and
103 * re-`init`ed under a long-lived server — at which point our fd reads a now
104 * dead inode forever (#925). See `isReplacedOnDisk`.
105 */
106 private openedInode: string | null;
107
108 /**
109 * Whether FTS5 is available in this Node.js build. When false, search
110 * falls back to LIKE + fuzzy matching (#1532).
111 */
112 readonly fts5Available: boolean;
113
114 private constructor(db: SqliteDatabase, dbPath: string, backend: SqliteBackend, fts5Available: boolean, readonly readOnly = false) {
115 this.db = db;
116 this.dbPath = dbPath;
117 this.backend = backend;
118 this.fts5Available = fts5Available;
119 this.openedInode = statInode(dbPath);
120 }
121
122 /**
123 * Initialize a new database at the given path
124 */
125 static initialize(dbPath: string): DatabaseConnection {
126 // Ensure parent directory exists
127 const dir = path.dirname(dbPath);
128 if (!fs.existsSync(dir)) {
129 fs.mkdirSync(dir, { recursive: true });
130 }
131
132 const { db, backend } = createDatabase(dbPath);
133 return withOpenFailures(db, dbPath, () => DatabaseConnection.initializeOn(db, backend, dbPath));
134 }
135
136 /** The rest of {@link initialize}: configure the connection and create the schema. */
137 private static initializeOn(db: SqliteDatabase, backend: SqliteBackend, dbPath: string): DatabaseConnection {
138 configureConnection(db);
139
140 // Run schema initialization, splitting FTS5 from the rest so
141 // codegraph still works when Node.js was built without FTS5 (#1532).
142 const schemaPath = path.join(__dirname, 'schema.sql');
143 const schema = fs.readFileSync(schemaPath, 'utf-8');
144
145 const FTS5_MARKER = '-- Full-text search index on node names, docstrings, and signatures';
146 const ftsIdx = schema.indexOf(FTS5_MARKER);
147 let fts5Available = true;
148
149 if (ftsIdx >= 0) {
150 const preFts = schema.slice(0, ftsIdx);
151 // FTS ends after the update trigger; required tables and indexes follow

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected