-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdatabase-management.console.ts
More file actions
156 lines (147 loc) · 4.45 KB
/
database-management.console.ts
File metadata and controls
156 lines (147 loc) · 4.45 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { ConsoleWriterService } from '@infra/console/console-writer/console-writer.service';
import { Command, Console } from 'nestjs-console';
import { DatabaseManagementUc } from '../uc/database-management.uc';
interface Options {
collection?: string;
override?: boolean;
onlyfactories?: boolean;
}
interface MigrationOptions {
up?: boolean;
down?: boolean;
from?: string;
to?: string;
only?: string;
pending?: boolean;
create?: boolean;
}
@Console({ command: 'database', description: 'database setup console' })
export class DatabaseManagementConsole {
constructor(private consoleWriter: ConsoleWriterService, private databaseManagementUc: DatabaseManagementUc) {}
@Command({
command: 'seed',
options: [
{
flags: '-c, --collection <collection>',
required: false,
description: 'filter for a single <collection>',
},
{
flags: '-o, --onlyfactories',
required: false,
description: 'seed from factories only',
},
],
description: 'reset database collections with seed data from filesystem',
})
public async seedCollections(options: Options): Promise<string[]> {
const filter = options?.collection ? [options.collection] : undefined;
const collections = options.onlyfactories
? await this.databaseManagementUc.seedDatabaseCollectionsFromFactories(filter)
: await this.databaseManagementUc.seedDatabaseCollectionsFromFileSystem(filter);
const report = JSON.stringify(collections);
this.consoleWriter.info(report);
return collections;
}
@Command({
command: 'export',
options: [
{
flags: '-c, --collection <collection>',
required: false,
description: 'filter for a single <collection>',
},
{
flags: '-o, --override',
required: false,
description: 'optional export collections to setup folder and override existing files',
},
],
description: 'export database collections to filesystem',
})
public async exportCollections(options: Options): Promise<string[]> {
const filter = options?.collection ? [options.collection] : undefined;
const toSeedFolder = options?.override ? true : undefined;
const collections = await this.databaseManagementUc.exportCollectionsToFileSystem(filter, toSeedFolder);
const report = JSON.stringify(collections);
this.consoleWriter.info(report);
return collections;
}
@Command({
command: 'sync-indexes',
options: [],
description: 'sync indexes from nest and mikroorm',
})
public async syncIndexes(): Promise<string> {
await this.databaseManagementUc.syncIndexes();
const report = 'sync of indexes is completed';
this.consoleWriter.info(report);
return report;
}
@Command({
command: 'migration',
options: [
{
flags: '--up',
required: false,
description: 'execute migration up',
},
{
flags: '--down',
required: false,
description: 'rollback migration (down)',
},
{
flags: '-f, --from',
required: false,
description: 'run migration up/down from specified name',
},
{
flags: '-t, --to',
required: false,
description: 'run migration up/down to specified name',
},
{
flags: '-o, --only',
required: false,
description: 'run a single migration',
},
{
flags: '--pending',
required: false,
description: 'list pending migrations',
},
{
flags: '--create',
required: false,
description: 'create a new migration',
},
],
description: 'Execute MikroOrm migration up/down',
})
public async migration(migrationOptions: MigrationOptions): Promise<string> {
let report = 'no migration option was given';
if (!migrationOptions.up && !migrationOptions.down && !migrationOptions.pending && !migrationOptions.create) {
this.consoleWriter.error(report);
return report;
}
if (migrationOptions.up) {
await this.databaseManagementUc.migrationUp(migrationOptions.from, migrationOptions.to, migrationOptions.only);
report = 'migration up is completed';
}
if (migrationOptions.down) {
await this.databaseManagementUc.migrationDown(migrationOptions.from, migrationOptions.to, migrationOptions.only);
report = 'migration down is completed';
}
if (migrationOptions.pending) {
const pendingMigrations = await this.databaseManagementUc.migrationPending();
report = `Pending: ${JSON.stringify(pendingMigrations.map((migration) => migration.name))}`;
}
if (migrationOptions.create) {
await this.databaseManagementUc.migrationCreate();
report = 'migration created';
}
this.consoleWriter.info(report);
return report;
}
}