MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / createDb

Function createDb

packages/baseai/src/utils/memory/db/lib.ts:51–79  ·  view source on GitHub ↗
(memoryName: string)

Source from the content-addressed store, hash-verified

49// Create a new database
50// basePath. The path to
51export async function createDb(memoryName: string): Promise<Low<Schema>> {
52 const baseDir = path.join(process.cwd(), '.baseai', 'db');
53 const dbFilePath = path.join(baseDir, `${memoryName}.json`);
54
55 // Ensure the directory exists
56 await fs.mkdir(baseDir, { recursive: true });
57
58 // Check if the file exists, if not, create it with default data
59 try {
60 await fs.access(dbFilePath);
61 } catch (error) {
62 // File doesn't exist, create it with default data
63 await fs.writeFile(dbFilePath, JSON.stringify(defaultData, null, 2));
64 }
65
66 const adapter = new JSONFile<Schema>(dbFilePath);
67 const db = new Low(adapter, defaultData);
68
69 // Read the existing data or initialize with default
70 await db.read();
71
72 // If the file was empty or invalid JSON, write the default data
73 if (db.data === null) {
74 db.data = defaultData;
75 await db.write();
76 }
77
78 return db;
79}
80
81// Read an existing database
82export async function readDb(dbPath: string): Promise<Low<Schema>> {

Callers 2

createMemoryFunction · 0.90
loadDbFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected