import { UserSettings } from 'n8n-core' ; import { ConnectionOptions , createConnection , getRepository } from 'typeorm' ; import { TlsOptions } from 'tls' ; import * as path from 'path' ; import { DatabaseType , GenericHelpers , IDatabaseCollections } from '.' ; import * as config from '../config' ; import { entities } from './databases/entities' ; import { postgresMigrations } from './databases/postgresdb/migrations' ; import { mysqlMigrations } from './databases/mysqldb/migrations' ; import { sqliteMigrations } from './databases/sqlite/migrations' ; export const collections = { Credentials : null , Execution : null , Workflow : null , Webhook : null , Tag : null , } ; export async function init ( ) { const dbType = ( await GenericHelpers . getConfigValue ( 'database.type' ) ) as DatabaseType ; const n8nFolder = UserSettings . getUserN8nFolderPath ( ) ; let connectionOptions ; const entityPrefix = config . get ( 'database.tablePrefix' ) ; switch ( dbType ) { case 'postgresdb' : const sslCa = ( await GenericHelpers . getConfigValue ( 'database.postgresdb.ssl.ca' ) ) as string ; const sslCert = ( await GenericHelpers . getConfigValue ( 'database.postgresdb.ssl.cert' ) ) as string ; const sslKey = ( await GenericHelpers . getConfigValue ( 'database.postgresdb.ssl.key' ) ) as string ; const sslRejectUnauthorized = ( await GenericHelpers . getConfigValue ( 'database.postgresdb.ssl.rejectUnauthorized' ) ) as boolean ; let ssl ; if ( sslCa !== '' || sslCert !== '' || sslKey !== '' || ! sslRejectUnauthorized ) { ssl = { ca : sslCa || undefined , cert : sslCert || undefined , key : sslKey || undefined , rejectUnauthorized : sslRejectUnauthorized , } ; } connectionOptions = { type : 'postgres' , entityPrefix , database : ( await GenericHelpers . getConfigValue ( 'database.postgresdb.database' ) ) as string , host : ( await GenericHelpers . getConfigValue ( 'database.postgresdb.host' ) ) as string , password : ( await GenericHelpers . getConfigValue ( 'database.postgresdb.password' ) ) as string , port : ( await GenericHelpers . getConfigValue ( 'database.postgresdb.port' ) ) as number , username : ( await GenericHelpers . getConfigValue ( 'database.postgresdb.user' ) ) as string , schema : config . get ( 'database.postgresdb.schema' ) , migrations : postgresMigrations , migrationsRun : true , migrationsTableName : `${ entityPrefix }migrations` , ssl , } ; break ; case 'mariadb' : case 'mysqldb' : connectionOptions = { type : dbType === 'mysqldb' ? 'mysql' : 'mariadb' , database : ( await GenericHelpers . getConfigValue ( 'database.mysqldb.database' ) ) as string , entityPrefix , host : ( await GenericHelpers . getConfigValue ( 'database.mysqldb.host' ) ) as string , password : ( await GenericHelpers . getConfigValue ( 'database.mysqldb.password' ) ) as string , port : ( await GenericHelpers . getConfigValue ( 'database.mysqldb.port' ) ) as number , username : ( await GenericHelpers . getConfigValue ( 'database.mysqldb.user' ) ) as string , migrations : mysqlMigrations , migrationsRun : true , migrationsTableName : `${ entityPrefix }migrations` , timezone : 'Z' , } ; break ; case 'sqlite' : connectionOptions = { type : 'sqlite' , database : path . join ( n8nFolder , 'database.sqlite' ) , entityPrefix , migrations : sqliteMigrations , migrationsRun : false , migrationsTableName : `${ entityPrefix }migrations` , } ; break ; default : throw new Error ( `The database "${ dbType }" is currently not supported!` ) ; } Object . assign ( connectionOptions , { entities : Object . values ( entities ) , synchronize : false , logging : false , } ) ; let connection = await createConnection ( connectionOptions ) ; if ( dbType === 'sqlite' ) { let migrations = [ ] ; try { migrations = await connection . query ( `SELECT id FROM ${ entityPrefix }migrations where name = "MakeStoppedAtNullable1607431743769"` ) ; } catch ( error ) { } await connection . runMigrations ( { transaction : 'none' , } ) ; if ( migrations . length === 0 ) { await connection . close ( ) ; connection = await createConnection ( connectionOptions ) ; } } collections . Credentials = getRepository ( entities . CredentialsEntity ) ; collections . Execution = getRepository ( entities . ExecutionEntity ) ; collections . Workflow = getRepository ( entities . WorkflowEntity ) ; collections . Webhook = getRepository ( entities . WebhookEntity ) ; collections . Tag = getRepository ( entities . TagEntity ) ; return collections ; }