From ac41a4668f600123262bc3655ceeb10196952c11 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 22 Aug 2022 20:59:22 +0300 Subject: [PATCH] typeorm test --- src/datasource.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/datasource.ts diff --git a/src/datasource.ts b/src/datasource.ts new file mode 100644 index 0000000..d6cf2f2 --- /dev/null +++ b/src/datasource.ts @@ -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 });