icynet-auth-server/src/datasource.ts

22 lines
680 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' }))),
);
export const AppDataSource = new DataSource(config.database);