2019-06-23 03:35:23 -07:00
|
|
|
import {
|
2019-08-03 05:06:11 -07:00
|
|
|
GenericHelpers,
|
2019-06-23 03:35:23 -07:00
|
|
|
IDatabaseCollections,
|
|
|
|
DatabaseType,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
import {
|
|
|
|
UserSettings,
|
2019-09-19 05:14:37 -07:00
|
|
|
} from 'n8n-core';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ConnectionOptions,
|
|
|
|
createConnection,
|
|
|
|
getRepository,
|
2020-04-27 03:46:09 -07:00
|
|
|
Connection,
|
2019-09-19 05:14:37 -07:00
|
|
|
} from 'typeorm';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
MongoDb,
|
2019-07-22 11:29:06 -07:00
|
|
|
PostgresDb,
|
2019-06-23 03:35:23 -07:00
|
|
|
SQLite,
|
2020-02-10 08:09:06 -08:00
|
|
|
MySQLDb,
|
2019-06-24 01:30:46 -07:00
|
|
|
} from './databases';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
export let collections: IDatabaseCollections = {
|
|
|
|
Credentials: null,
|
|
|
|
Execution: null,
|
|
|
|
Workflow: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
2020-04-27 03:46:09 -07:00
|
|
|
export async function init(synchronize?: boolean): Promise<boolean> {
|
2019-08-03 05:06:11 -07:00
|
|
|
const dbType = await GenericHelpers.getConfigValue('database.type') as DatabaseType;
|
2019-06-23 03:35:23 -07:00
|
|
|
const n8nFolder = UserSettings.getUserN8nFolderPath();
|
|
|
|
|
|
|
|
let entities;
|
|
|
|
let connectionOptions: ConnectionOptions;
|
2020-04-27 03:46:09 -07:00
|
|
|
let connection;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-08-04 11:59:10 -07:00
|
|
|
let dbNotExistError: string | undefined;
|
2020-02-10 08:09:06 -08:00
|
|
|
switch (dbType) {
|
|
|
|
case 'mongodb':
|
|
|
|
entities = MongoDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'mongodb',
|
2020-03-21 10:32:26 -07:00
|
|
|
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
2020-02-10 08:09:06 -08:00
|
|
|
url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string,
|
|
|
|
useNewUrlParser: true,
|
2020-04-27 03:46:09 -07:00
|
|
|
migrations: ['./databases/mongodb/migrations/*.js'],
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'postgresdb':
|
|
|
|
dbNotExistError = 'does not exist';
|
|
|
|
entities = PostgresDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'postgres',
|
2020-03-21 10:32:26 -07:00
|
|
|
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
2020-02-10 08:09:06 -08:00
|
|
|
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,
|
2020-03-17 22:04:53 -07:00
|
|
|
schema: await GenericHelpers.getConfigValue('database.postgresdb.schema') as string,
|
2020-04-27 03:46:09 -07:00
|
|
|
migrations: ['./databases/postgresdb/migrations/*.js']
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
2020-04-14 10:54:11 -07:00
|
|
|
case 'mariadb':
|
2020-02-10 08:09:06 -08:00
|
|
|
case 'mysqldb':
|
|
|
|
dbNotExistError = 'does not exist';
|
|
|
|
entities = MySQLDb;
|
|
|
|
connectionOptions = {
|
2020-04-14 10:54:11 -07:00
|
|
|
type: dbType === 'mysqldb' ? 'mysql' : 'mariadb',
|
2020-02-10 08:09:06 -08:00
|
|
|
database: await GenericHelpers.getConfigValue('database.mysqldb.database') as string,
|
2020-03-21 10:32:26 -07:00
|
|
|
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
2020-02-10 08:09:06 -08:00
|
|
|
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,
|
2020-03-21 10:32:26 -07:00
|
|
|
username: await GenericHelpers.getConfigValue('database.mysqldb.user') as string,
|
2020-04-27 03:46:09 -07:00
|
|
|
migrations: ['./databases/mysqldb/migrations/*.js']
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'sqlite':
|
|
|
|
dbNotExistError = 'no such table:';
|
|
|
|
entities = SQLite;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'sqlite',
|
|
|
|
database: path.join(n8nFolder, 'database.sqlite'),
|
2020-03-21 10:32:26 -07:00
|
|
|
entityPrefix: await GenericHelpers.getConfigValue('database.tablePrefix') as string,
|
2020-04-27 03:46:09 -07:00
|
|
|
migrations: ['./databases/sqlite/migrations/*.js'],
|
2020-02-10 08:09:06 -08:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`The database "${dbType}" is currently not supported!`);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(connectionOptions, {
|
|
|
|
entities: Object.values(entities),
|
2020-04-22 07:03:50 -07:00
|
|
|
synchronize: false,//synchronize === true || process.env['NODE_ENV'] !== 'production',
|
2020-04-27 03:46:09 -07:00
|
|
|
logging: true,
|
|
|
|
migrationsRun: true
|
2019-06-23 03:35:23 -07:00
|
|
|
});
|
|
|
|
|
2020-04-27 03:46:09 -07:00
|
|
|
for(let i = 0; i < 1000; i++){
|
|
|
|
console.log(connectionOptions);
|
2019-08-04 11:59:10 -07:00
|
|
|
}
|
|
|
|
|
2020-04-27 03:46:09 -07:00
|
|
|
try{
|
|
|
|
connection = await createConnection(connectionOptions);
|
|
|
|
|
|
|
|
await connection.runMigrations({
|
|
|
|
transaction: "none"
|
|
|
|
});
|
|
|
|
}catch(e){
|
|
|
|
throw new Error("Couldn't connect to db / migrate stuff.")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return connection.isConnected;
|
|
|
|
};
|