typeorm test

This commit is contained in:
Evert Prants 2022-08-22 20:59:22 +03:00
parent 473e30e7fe
commit ac41a4668f
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

20
src/datasource.ts Normal file
View File

@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { join } from 'path';
import * as dotenv from 'dotenv';
import { readFileSync } from 'fs';
import * as toml from 'toml';
import { DataSource } from 'typeorm';
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 default new DataSource({ ...config.database });