-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionOptionsProvider.ts
49 lines (46 loc) · 1.73 KB
/
connectionOptionsProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { injectable, multiInject } from 'inversify';
import * as path from 'path';
import { memoize } from '../../utils/memoize/memoize';
import { IConnectionSettings } from '../interfaces/IConnectionSettings';
import { IEntity, IEntityConstructor } from '../interfaces/IEntity';
import { ConnectionOptions } from 'typeorm/connection/ConnectionOptions';
import { IConnectionOptionsProvider } from '../interfaces/IConnectionOptionsProvider';
const DB_FILE = `cache.db`;
const MIGRATIONS_DIR = path.join(__dirname, `..`, `migrations`);
const MIGRATIONS_DIR_RELATIVE = MIGRATIONS_DIR.slice(process.cwd().length + 1);
const MIGRATIONS_FILES = path.join(MIGRATIONS_DIR, `**`, `!(*.d).[jt]s`);
@injectable()
export class ConnectionOptionsProvider extends IConnectionOptionsProvider {
constructor(
private readonly settings: IConnectionSettings,
@multiInject(IEntity) private readonly entities: IEntityConstructor[]
) {
super();
}
@memoize()
public getConnectionOptions(): ConnectionOptions {
const connectionOptions: ConnectionOptions = {
name: this.settings.migrationGenerationConfig ? `default` : this.settings.databaseFilePath,
type: `sqlite`,
database: path.join(this.settings.databaseFilePath, DB_FILE),
synchronize: false,
migrations: [MIGRATIONS_FILES],
logging: false,
dropSchema: false,
entities: this.entities,
cli: {
migrationsDir: MIGRATIONS_DIR_RELATIVE,
},
};
Object.defineProperty(connectionOptions, `migrationsRun`, {
get: (): boolean => {
return true;
},
set: (): void => {
// Do nothing as we want migration generation to build incremental queries
},
enumerable: true,
});
return connectionOptions;
}
}