32 lines
909 B
TypeScript
32 lines
909 B
TypeScript
import { DataSource } from "typeorm";
|
|
import { Photo } from "./entity/photo";
|
|
|
|
const dataSource = new DataSource({
|
|
type: "better-sqlite3",
|
|
database: "db.sql",
|
|
entities: [Photo],
|
|
migrations: ["./migrations"],
|
|
})
|
|
|
|
export default class PhotoDataSource {
|
|
private static _dataSource: DataSource | null = null;
|
|
|
|
static get dataSource(): Promise<DataSource> {
|
|
if (PhotoDataSource._dataSource === null) {
|
|
return PhotoDataSource.initDataSource();
|
|
} else {
|
|
return Promise.resolve(PhotoDataSource._dataSource);
|
|
}
|
|
}
|
|
|
|
static async initDataSource(): Promise<DataSource> {
|
|
if (!PhotoDataSource._dataSource || !PhotoDataSource._dataSource.isInitialized) {
|
|
const ds = await dataSource.initialize();
|
|
console.log('Photo data source initialized')
|
|
PhotoDataSource._dataSource = ds;
|
|
}
|
|
return PhotoDataSource._dataSource;
|
|
}
|
|
}
|
|
|
|
PhotoDataSource.initDataSource(); |