2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IDatabaseCollections,
|
|
|
|
DatabaseType,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
import {
|
|
|
|
UserSettings,
|
|
|
|
} from "n8n-core";
|
|
|
|
|
|
|
|
import {
|
|
|
|
ConnectionOptions,
|
|
|
|
createConnection,
|
|
|
|
getRepository,
|
|
|
|
} from "typeorm";
|
|
|
|
|
2019-07-21 10:47:41 -07:00
|
|
|
import * as config from './../config';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
MongoDb,
|
|
|
|
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';
|
|
|
|
|
|
|
|
export async function init(): Promise<IDatabaseCollections> {
|
|
|
|
const dbType = config.get('database.type') as DatabaseType;
|
|
|
|
const n8nFolder = UserSettings.getUserN8nFolderPath();
|
|
|
|
|
|
|
|
let entities;
|
|
|
|
let connectionOptions: ConnectionOptions;
|
|
|
|
|
|
|
|
if (dbType === 'mongodb') {
|
|
|
|
entities = MongoDb;
|
|
|
|
connectionOptions = {
|
|
|
|
type: 'mongodb',
|
2019-07-21 10:47:41 -07:00
|
|
|
url: config.get('database.mongodb.connectionUrl') as string,
|
2019-06-23 03:35:23 -07:00
|
|
|
useNewUrlParser: true,
|
|
|
|
};
|
|
|
|
} else if (dbType === 'sqlite') {
|
|
|
|
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),
|
|
|
|
synchronize: true,
|
|
|
|
logging: false
|
|
|
|
});
|
|
|
|
|
|
|
|
await createConnection(connectionOptions);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return collections;
|
|
|
|
}
|