forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheClearCommand.ts
66 lines (55 loc) · 2.15 KB
/
CacheClearCommand.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {createConnection} from "../index";
import {ConnectionOptionsReader} from "../connection/ConnectionOptionsReader";
import {Connection} from "../connection/Connection";
import * as yargs from "yargs";
import chalk from "chalk";
/**
* Clear cache command.
*/
export class CacheClearCommand implements yargs.CommandModule {
command = "cache:clear";
describe = "Clears all data stored in query runner cache.";
builder(args: yargs.Argv) {
return args
.option("connection", {
alias: "c",
default: "default",
describe: "Name of the connection on which run a query."
})
.option("config", {
alias: "f",
default: "ormconfig",
describe: "Name of the file with connection configuration."
});
}
async handler(args: yargs.Arguments) {
let connection: Connection|undefined = undefined;
try {
const connectionOptionsReader = new ConnectionOptionsReader({
root: process.cwd(),
configName: args.config as any
});
const connectionOptions = await connectionOptionsReader.get(args.connection as any);
Object.assign(connectionOptions, {
subscribers: [],
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: ["schema"]
});
connection = await createConnection(connectionOptions);
if (!connection.queryResultCache) {
console.log(chalk.black.bgRed("Cache is not enabled. To use cache enable it in connection configuration."));
return;
}
await connection.queryResultCache.clear();
console.log(chalk.green("Cache was successfully cleared"));
if (connection) await connection.close();
} catch (err) {
if (connection) await (connection as Connection).close();
console.log(chalk.black.bgRed("Error during cache clear:"));
console.error(err);
process.exit(1);
}
}
}