n8n/packages/cli/src/Db.ts

74 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import {
IDatabaseCollections,
DatabaseType,
} from './';
import {
UserSettings,
} from "n8n-core";
import {
ConnectionOptions,
createConnection,
getRepository,
} from "typeorm";
import * as config from './../config';
2019-06-23 03:35:23 -07:00
import {
MongoDb,
SQLite,
} 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',
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;
}