Add runner ci (test)
This commit is contained in:
@@ -6,12 +6,27 @@ import {
|
||||
ServerEntity,
|
||||
JoinRequestEntity
|
||||
} from '../entities';
|
||||
import { serverMigrations } from '../migrations';
|
||||
import { findExistingPath, resolveRuntimePath } from '../runtime-paths';
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), 'data');
|
||||
const DATA_DIR = resolveRuntimePath('data');
|
||||
const DB_FILE = path.join(DATA_DIR, 'metoyou.sqlite');
|
||||
|
||||
let applicationDataSource: DataSource | undefined;
|
||||
|
||||
function resolveSqlJsConfig(): { locateFile: (file: string) => string } {
|
||||
return {
|
||||
locateFile: (file) => {
|
||||
const bundledBinaryPath = path.join(__dirname, '..', '..', 'node_modules', 'sql.js', 'dist', file);
|
||||
|
||||
return findExistingPath(
|
||||
resolveRuntimePath(file),
|
||||
bundledBinaryPath
|
||||
) ?? bundledBinaryPath;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function getDataSource(): DataSource {
|
||||
if (!applicationDataSource?.isInitialized) {
|
||||
throw new Error('DataSource not initialised');
|
||||
@@ -29,22 +44,34 @@ export async function initDatabase(): Promise<void> {
|
||||
if (fs.existsSync(DB_FILE))
|
||||
database = fs.readFileSync(DB_FILE);
|
||||
|
||||
applicationDataSource = new DataSource({
|
||||
type: 'sqljs',
|
||||
database,
|
||||
entities: [
|
||||
AuthUserEntity,
|
||||
ServerEntity,
|
||||
JoinRequestEntity
|
||||
],
|
||||
migrations: [path.join(__dirname, '..', 'migrations', '*.js'), path.join(__dirname, '..', 'migrations', '*.ts')],
|
||||
synchronize: false,
|
||||
logging: false,
|
||||
autoSave: true,
|
||||
location: DB_FILE
|
||||
});
|
||||
try {
|
||||
applicationDataSource = new DataSource({
|
||||
type: 'sqljs',
|
||||
database,
|
||||
entities: [
|
||||
AuthUserEntity,
|
||||
ServerEntity,
|
||||
JoinRequestEntity
|
||||
],
|
||||
migrations: serverMigrations,
|
||||
synchronize: false,
|
||||
logging: false,
|
||||
autoSave: true,
|
||||
location: DB_FILE,
|
||||
sqlJsConfig: resolveSqlJsConfig()
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[DB] Failed to configure the sql.js data source', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
await applicationDataSource.initialize();
|
||||
} catch (error) {
|
||||
console.error('[DB] Failed to initialise the sql.js data source', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
await applicationDataSource.initialize();
|
||||
console.log('[DB] Connection initialised at:', DB_FILE);
|
||||
|
||||
await applicationDataSource.runMigrations();
|
||||
|
||||
Reference in New Issue
Block a user