This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-auth-server/src/datasource.ts

34 lines
996 B
TypeScript

import { join } from 'path';
import { readFileSync } from 'fs';
import { DataSource } from 'typeorm';
import * as toml from 'toml';
import * as dotenv from 'dotenv';
dotenv.config();
const CONFIG_ENV = process.env.NODE_ENV === 'production' ? 'prod' : 'dev';
const CONFIG_FILENAME = process.env.CONFIG || `config.${CONFIG_ENV}.toml`;
const CONFIG_PATH = join(process.cwd(), CONFIG_FILENAME);
// toml.parse returns an object that doesn't have the correct prototype,
// thus this JSON workaround is used.
const config = JSON.parse(
JSON.stringify(toml.parse(readFileSync(CONFIG_PATH, { encoding: 'utf-8' }))),
);
if (config.database.entities?.length) {
config.database.entities = config.database.entities.map((path: string) =>
join(process.cwd(), path),
);
}
if (config.database.migrations?.length) {
config.database.migrations = config.database.migrations.map((path: string) =>
join(process.cwd(), path),
);
}
export const AppDataSource = new DataSource(config.database);