yikart/AiToEarn · error · Error
Database is not initialized
Error message
Database is not initialized
What it means
exportDatabase serializes the local SQLite database to a SQL file for backup. It checks AppDataSource.isInitialized first and throws a plain Error if the TypeORM DataSource has not been initialized, since no query runner or tables can be accessed before initialization.
Source
Thrown at project/aitoearn-electron/electron/db/index.ts:93
// await AppDataSource.runMigrations(); // 上面已经有自动迁移
return true;
} catch (error) {
logger.error('Error during database initialization:', error);
return false;
}
}
return true;
}
/**
* 导出数据库到SQL文件
* @param filePath 导出文件路径
*/
export async function exportDatabase(filePath: string): Promise<void> {
try {
if (!AppDataSource.isInitialized) {
logger.error('Database is not initialized');
throw new Error('Database is not initialized');
}
const queryRunner = AppDataSource.createQueryRunner();
await queryRunner.connect();
// 获取所有表的数据
const tables = AppDataSource.entityMetadatas.map(
(entity) => entity.tableName,
);
let sqlContent = '';
for (const table of tables) {
const records = await queryRunner.query(`SELECT * FROM ${table}`);
if (records.length > 0) {
sqlContent += `-- Table: ${table}\n`;
for (const record of records) {
const columns = Object.keys(record).join(', ');
const values = Object.values(record)View on GitHub (pinned to d3aa8bea5b)
Solutions
- Await database initialization (AppDataSource.initialize()) before calling exportDatabase/createBackup
- Check startup logs for the underlying initialization failure and fix it (corrupt/locked DB file)
- Gate the backup UI/action on an 'db ready' flag
- Retry the backup after the app reports the database as ready
Example fix
// before await createBackup(); // after if (!AppDataSource.isInitialized) await AppDataSource.initialize(); await createBackup();
Defensive patterns
Strategy: retry
Validate before calling
import { AppDataSource } from './db';
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
await createBackup(path); Type guard
function isDbReady(ds: { isInitialized: boolean }): boolean {
return ds.isInitialized === true;
} Try / catch
try {
await createBackup(path);
} catch (e) {
if (e.message === 'Database is not initialized') {
await waitForDbReady();
await createBackup(path);
} else throw e;
} Prevention
- Await DB initialization before exposing backup actions in the UI
- Gate backup features on an app 'dbReady' event/flag
- Monitor init failures (corrupt/locked DB file) and surface them to the user
- Serialize startup tasks so backups never race initialization
When it happens
Trigger: createBackup is invoked before the app finished database initialization (e.g. during startup races), or after initialization failed (bad DB file, migration error) so isInitialized stayed false.
Common situations: User triggers backup immediately on app launch before DB init completes; database file locked/corrupt causing init failure; init awaited nowhere before the backup routine runs.
Related errors
AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31).
Data as JSON: /api/errors/e8580c40ce28d14a.
Report an issue: GitHub.