(memoryName: string)
| 49 | // Create a new database |
| 50 | // basePath. The path to |
| 51 | export 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 |
| 82 | export async function readDb(dbPath: string): Promise<Low<Schema>> { |
no outgoing calls
no test coverage detected