snowballtools-base/packages/backend/src/database.ts

37 lines
856 B
TypeScript
Raw Normal View History

import { DataSource } from 'typeorm';
import path from 'path';
import debug from 'debug';
import { User } from './entity/User';
import { DatabaseConfig } from './config';
const log = debug('snowball:database');
export class Database {
private dataSource: DataSource;
constructor ({ dbPath }: DatabaseConfig) {
this.dataSource = new DataSource({
type: 'better-sqlite3',
database: dbPath,
entities: [path.join(__dirname, '/entity/*')],
synchronize: true,
logging: false
});
}
async init () : Promise<void> {
await this.dataSource.initialize();
log('database initialized');
}
async getUser (userId: number) : Promise<User | null> {
const userRepository = this.dataSource.getRepository(User);
const user = await userRepository.findOneBy({
id: userId
});
return user;
}
}