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,
|
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,
|
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';
|
|
|
|
|
2019-08-04 11:59:10 -07:00
|
|
|
export async function init(synchronize?: boolean): Promise<IDatabaseCollections> {
|
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;
|
|
|
|
|
2019-08-04 11:59:10 -07:00
|
|
|
let dbNotExistError: string | undefined;
|
2019-06-23 03:35:23 -07:00
|
|
|
if (dbType === 'mongodb') {
|
|
|
|
entities = MongoDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'mongodb',
|
2019-08-03 05:06:11 -07:00
|
|
|
url: await GenericHelpers.getConfigValue('database.mongodb.connectionUrl') as string,
|
2019-06-23 03:35:23 -07:00
|
|
|
useNewUrlParser: true,
|
|
|
|
};
|
2019-07-22 11:29:06 -07:00
|
|
|
} else if (dbType === 'postgresdb') {
|
2019-08-04 11:59:10 -07:00
|
|
|
dbNotExistError = 'does not exist';
|
2019-07-22 11:29:06 -07:00
|
|
|
entities = PostgresDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'postgres',
|
2019-08-03 05:06:11 -07: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,
|
2019-07-22 11:29:06 -07:00
|
|
|
};
|
2019-06-23 03:35:23 -07:00
|
|
|
} else if (dbType === 'sqlite') {
|
2019-08-04 11:59:10 -07:00
|
|
|
dbNotExistError = 'no such table:';
|
2019-06-23 03:35:23 -07:00
|
|
|
entities = SQLite;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'sqlite',
|
|
|
|
database: path.join(n8nFolder, 'database.sqlite'),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new Error(`The database "${dbType}" is currently not supported!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(connectionOptions, {
|
|
|
|
entities: Object.values(entities),
|
2019-08-04 11:59:10 -07:00
|
|
|
synchronize: synchronize === true || process.env['NODE_ENV'] !== 'production',
|
2019-06-23 03:35:23 -07:00
|
|
|
logging: false
|
|
|
|
});
|
|
|
|
|
2019-08-04 11:59:10 -07:00
|
|
|
const connection = await createConnection(connectionOptions);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-06-24 01:28:18 -07:00
|
|
|
// TODO: Fix that properly
|
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
collections.Credentials = getRepository(entities.CredentialsEntity);
|
2019-06-24 01:28:18 -07:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
collections.Execution = getRepository(entities.ExecutionEntity);
|
2019-06-24 01:28:18 -07:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
collections.Workflow = getRepository(entities.WorkflowEntity);
|
|
|
|
|
2019-08-04 11:59:10 -07:00
|
|
|
// Make sure that database did already get initialized
|
|
|
|
try {
|
|
|
|
// Try a simple query, if it fails it is normally a sign that
|
|
|
|
// database did not get initialized
|
|
|
|
await collections.Workflow!.findOne({ id: 1 });
|
|
|
|
} catch (error) {
|
|
|
|
// If query errors and the problem is that the database does not exist
|
|
|
|
// run the init again with "synchronize: true"
|
|
|
|
if (dbNotExistError !== undefined && error.message.includes(dbNotExistError)) {
|
|
|
|
// Disconnect before we try to connect again
|
|
|
|
if (connection.isConnected) {
|
|
|
|
await connection.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return init(true);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
return collections;
|
|
|
|
}
|