Refacor electron app and add migrations
This commit is contained in:
43
electron/data-source.ts
Normal file
43
electron/data-source.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* TypeORM DataSource used by the CLI migration tools (generate / run / revert).
|
||||
*
|
||||
* At **runtime** the DataSource is constructed inside `main.ts` and
|
||||
* pointed at the user's Electron `userData` directory instead.
|
||||
*
|
||||
* For CLI use, the database is stored at the project root (`metoyou.sqlite`)
|
||||
* so TypeORM can diff entity metadata against the current schema when
|
||||
* generating new migrations.
|
||||
*/
|
||||
import 'reflect-metadata';
|
||||
import { DataSource } from 'typeorm';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { settings } from './settings';
|
||||
import {
|
||||
MessageEntity,
|
||||
UserEntity,
|
||||
RoomEntity,
|
||||
ReactionEntity,
|
||||
BanEntity,
|
||||
AttachmentEntity,
|
||||
MetaEntity
|
||||
} from './entities';
|
||||
|
||||
const projectRootDatabaseFilePath = path.join(__dirname, '..', '..', settings.databaseName);
|
||||
|
||||
let databaseFileBuffer: Uint8Array | undefined;
|
||||
|
||||
if (fs.existsSync(projectRootDatabaseFilePath)) {
|
||||
databaseFileBuffer = fs.readFileSync(projectRootDatabaseFilePath);
|
||||
}
|
||||
|
||||
export const AppDataSource = new DataSource({
|
||||
type: 'sqljs',
|
||||
database: databaseFileBuffer,
|
||||
entities: [MessageEntity, UserEntity, RoomEntity, ReactionEntity, BanEntity, AttachmentEntity, MetaEntity],
|
||||
migrations: [path.join(__dirname, 'migrations', '*.{ts,js}')],
|
||||
synchronize: false,
|
||||
logging: false,
|
||||
autoSave: true,
|
||||
location: projectRootDatabaseFilePath
|
||||
});
|
||||
Reference in New Issue
Block a user