mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-23 05:00:23 +03:00
5b126c60bc
DATABASE_SSL_KEY_BASE64 takes priority over DATABASE_SSL env var If neither are provided, no ssl value will be used. This allows for the usage of PGSSLMODE
87 lines
3.2 KiB
TypeScript
87 lines
3.2 KiB
TypeScript
import 'reflect-metadata'
|
|
import path from 'path'
|
|
import { DataSource } from 'typeorm'
|
|
import { getUserHome } from './utils'
|
|
import { entities } from './database/entities'
|
|
import { sqliteMigrations } from './database/migrations/sqlite'
|
|
import { mysqlMigrations } from './database/migrations/mysql'
|
|
import { postgresMigrations } from './database/migrations/postgres'
|
|
|
|
let appDataSource: DataSource
|
|
|
|
export const init = async (): Promise<void> => {
|
|
let homePath
|
|
switch (process.env.DATABASE_TYPE) {
|
|
case 'sqlite':
|
|
homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise')
|
|
appDataSource = new DataSource({
|
|
type: 'sqlite',
|
|
database: path.resolve(homePath, 'database.sqlite'),
|
|
synchronize: false,
|
|
migrationsRun: false,
|
|
entities: Object.values(entities),
|
|
migrations: sqliteMigrations
|
|
})
|
|
break
|
|
case 'mysql':
|
|
appDataSource = new DataSource({
|
|
type: 'mysql',
|
|
host: process.env.DATABASE_HOST,
|
|
port: parseInt(process.env.DATABASE_PORT || '3306'),
|
|
username: process.env.DATABASE_USER,
|
|
password: process.env.DATABASE_PASSWORD,
|
|
database: process.env.DATABASE_NAME,
|
|
charset: 'utf8mb4',
|
|
synchronize: false,
|
|
migrationsRun: false,
|
|
entities: Object.values(entities),
|
|
migrations: mysqlMigrations
|
|
})
|
|
break
|
|
case 'postgres':
|
|
appDataSource = new DataSource({
|
|
type: 'postgres',
|
|
host: process.env.DATABASE_HOST,
|
|
port: parseInt(process.env.DATABASE_PORT || '5432'),
|
|
username: process.env.DATABASE_USER,
|
|
password: process.env.DATABASE_PASSWORD,
|
|
database: process.env.DATABASE_NAME,
|
|
...(process.env.DATABASE_SSL_KEY_BASE64
|
|
? {
|
|
ssl: {
|
|
rejectUnauthorized: false,
|
|
cert: Buffer.from(process.env.DATABASE_SSL_KEY_BASE64, 'base64')
|
|
}
|
|
}
|
|
: process.env.DATABASE_SSL === 'true'
|
|
? {
|
|
ssl: true
|
|
}
|
|
: {}),
|
|
synchronize: false,
|
|
migrationsRun: false,
|
|
entities: Object.values(entities),
|
|
migrations: postgresMigrations
|
|
})
|
|
break
|
|
default:
|
|
homePath = process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise')
|
|
appDataSource = new DataSource({
|
|
type: 'sqlite',
|
|
database: path.resolve(homePath, 'database.sqlite'),
|
|
synchronize: false,
|
|
migrationsRun: false,
|
|
entities: Object.values(entities),
|
|
migrations: sqliteMigrations
|
|
})
|
|
break
|
|
}
|
|
}
|
|
|
|
export function getDataSource(): DataSource {
|
|
if (appDataSource === undefined) {
|
|
init()
|
|
}
|
|
return appDataSource
|
|
}
|