@wocker/mongodb-plugin 1.0.14-beta.0 → 1.0.14-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,14 +2,20 @@ import { MongodbService } from "../services/MongodbService";
2
2
  export declare class MongodbController {
3
3
  protected readonly mongodbService: MongodbService;
4
4
  constructor(mongodbService: MongodbService);
5
- create(name?: string, username?: string, password?: string, imageName?: string, imageVersion?: string, containerPort?: number): Promise<void>;
6
- upgrade(name?: string, imageName?: string, imageVersion?: string, volume?: string, configVolume?: string, containerPort?: number, enableAdmin?: boolean, disableAdmin?: boolean): Promise<void>;
5
+ mongosh(name?: string, database?: string): Promise<void>;
6
+ init(adminHostname?: string): Promise<void>;
7
+ create(name?: string, username?: string, password?: string, image?: string, containerPort?: number): Promise<void>;
8
+ upgrade(name?: string, image?: string, volume?: string, configVolume?: string, containerPort?: number, enableAdmin?: boolean, disableAdmin?: boolean): Promise<void>;
7
9
  destroy(name: string, yes?: boolean, force?: boolean): Promise<void>;
8
- use(name: string): Promise<void>;
10
+ use(name?: string): Promise<string | undefined>;
9
11
  start(name?: string, restart?: boolean): Promise<void>;
10
12
  stop(name?: string): Promise<void>;
11
13
  list(): Promise<string>;
12
14
  backup(service?: string, database?: string, file?: string, del?: boolean): Promise<void>;
13
15
  restore(service?: string, database?: string, file?: string): Promise<void>;
14
16
  getNames(): Promise<string[]>;
17
+ getDatabases(name?: string): Promise<string[]>;
18
+ getDumpDatabases(name?: string): Promise<string[]>;
19
+ getBackupFilenames(name?: string, database?: string): Promise<string[]>;
20
+ getRestoreFilenames(name?: string, database?: string): Promise<string[]>;
15
21
  }
@@ -28,24 +28,32 @@ let MongodbController = class MongodbController {
28
28
  constructor(mongodbService) {
29
29
  this.mongodbService = mongodbService;
30
30
  }
31
- create(name, username, password, imageName, imageVersion, containerPort) {
31
+ mongosh(name, database) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ yield this.mongodbService.mongosh(name, database);
34
+ });
35
+ }
36
+ init(adminHostname) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ yield this.mongodbService.init(adminHostname);
39
+ });
40
+ }
41
+ create(name, username, password, image, containerPort) {
32
42
  return __awaiter(this, void 0, void 0, function* () {
33
43
  yield this.mongodbService.create({
34
44
  name,
35
45
  username,
36
46
  password,
37
- imageName,
38
- imageVersion,
47
+ image,
39
48
  containerPort
40
49
  });
41
50
  });
42
51
  }
43
- upgrade(name, imageName, imageVersion, volume, configVolume, containerPort, enableAdmin, disableAdmin) {
52
+ upgrade(name, image, volume, configVolume, containerPort, enableAdmin, disableAdmin) {
44
53
  return __awaiter(this, void 0, void 0, function* () {
45
54
  yield this.mongodbService.upgrade({
46
55
  name,
47
- imageName,
48
- imageVersion,
56
+ image,
49
57
  volume,
50
58
  configVolume,
51
59
  containerPort
@@ -69,6 +77,10 @@ let MongodbController = class MongodbController {
69
77
  }
70
78
  use(name) {
71
79
  return __awaiter(this, void 0, void 0, function* () {
80
+ if (!name) {
81
+ const database = this.mongodbService.config.getDefault();
82
+ return database.name;
83
+ }
72
84
  this.mongodbService.use(name);
73
85
  });
74
86
  }
@@ -110,8 +122,83 @@ let MongodbController = class MongodbController {
110
122
  });
111
123
  });
112
124
  }
125
+ getDatabases(name) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ try {
128
+ const service = this.mongodbService.config.getDatabaseOrDefault(name);
129
+ return yield this.mongodbService.getDatabases(service);
130
+ }
131
+ catch (err) {
132
+ return [];
133
+ }
134
+ });
135
+ }
136
+ getDumpDatabases(name) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ try {
139
+ const service = this.mongodbService.config.getDatabaseOrDefault(name);
140
+ return yield this.mongodbService.getDumpDatabases(service);
141
+ }
142
+ catch (err) {
143
+ return [];
144
+ }
145
+ });
146
+ }
147
+ getBackupFilenames(name, database) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ if (!database) {
150
+ return [];
151
+ }
152
+ try {
153
+ const service = this.mongodbService.config.getDatabaseOrDefault(name);
154
+ return yield this.mongodbService.getDumpFiles(service, database);
155
+ }
156
+ catch (err) {
157
+ return [];
158
+ }
159
+ });
160
+ }
161
+ getRestoreFilenames(name, database) {
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ if (!database) {
164
+ return [];
165
+ }
166
+ try {
167
+ const service = this.mongodbService.config.getDatabaseOrDefault(name);
168
+ return yield this.mongodbService.getDumpFiles(service, database);
169
+ }
170
+ catch (err) {
171
+ return [];
172
+ }
173
+ });
174
+ }
113
175
  };
114
176
  exports.MongodbController = MongodbController;
177
+ __decorate([
178
+ (0, core_1.Command)("mongodb [name]"),
179
+ (0, core_1.Description)("Opens an interactive mongosh session for a specified MongoDB service."),
180
+ __param(0, (0, core_1.Param)("name")),
181
+ __param(1, (0, core_1.Option)("database", {
182
+ type: "string",
183
+ alias: "d",
184
+ description: "<name> Specify the database to connect to"
185
+ })),
186
+ __metadata("design:type", Function),
187
+ __metadata("design:paramtypes", [String, String]),
188
+ __metadata("design:returntype", Promise)
189
+ ], MongodbController.prototype, "mongosh", null);
190
+ __decorate([
191
+ (0, core_1.Command)("mongodb:init"),
192
+ (0, core_1.Description)("Initializes the MongoDB configuration."),
193
+ __param(0, (0, core_1.Option)("admin-hostname", {
194
+ type: "string",
195
+ alias: "A",
196
+ description: "Specifies the mongo-express hostname."
197
+ })),
198
+ __metadata("design:type", Function),
199
+ __metadata("design:paramtypes", [String]),
200
+ __metadata("design:returntype", Promise)
201
+ ], MongodbController.prototype, "init", null);
115
202
  __decorate([
116
203
  (0, core_1.Command)("mongodb:create [name]"),
117
204
  (0, core_1.Description)("Creates a MongoDB service with configurable credentials, host, and storage options."),
@@ -129,17 +216,12 @@ __decorate([
129
216
  __param(3, (0, core_1.Option)("image", {
130
217
  type: "string",
131
218
  alias: "i",
132
- description: "The image name to start the service with",
133
- })),
134
- __param(4, (0, core_1.Option)("image-version", {
135
- type: "string",
136
- alias: "I",
137
- description: "The image version to start the service with"
219
+ description: "The image to start the service with, e.g. \"mongo:6\""
138
220
  })),
139
- __param(5, (0, core_1.Option)("container-port")),
140
- __param(5, (0, core_1.Description)("Port on which the database container will be accessible on the host")),
221
+ __param(4, (0, core_1.Option)("container-port")),
222
+ __param(4, (0, core_1.Description)("Port on which the database container will be accessible on the host")),
141
223
  __metadata("design:type", Function),
142
- __metadata("design:paramtypes", [String, String, String, String, String, Number]),
224
+ __metadata("design:paramtypes", [String, String, String, String, Number]),
143
225
  __metadata("design:returntype", Promise)
144
226
  ], MongodbController.prototype, "create", null);
145
227
  __decorate([
@@ -149,29 +231,24 @@ __decorate([
149
231
  __param(1, (0, core_1.Option)("image", {
150
232
  type: "string",
151
233
  alias: "i",
152
- description: "The image name to start the service with"
234
+ description: "The image to start the service with, e.g. \"mongo:6\""
153
235
  })),
154
- __param(2, (0, core_1.Option)("image-version", {
155
- type: "string",
156
- alias: "I",
157
- description: "The image version to start the service with"
158
- })),
159
- __param(3, (0, core_1.Option)("volume", {
236
+ __param(2, (0, core_1.Option)("volume", {
160
237
  type: "string",
161
238
  alias: "v",
162
239
  description: "The volume name to start the service with"
163
240
  })),
164
- __param(4, (0, core_1.Option)("config-volume", {
241
+ __param(3, (0, core_1.Option)("config-volume", {
165
242
  type: "string",
166
243
  alias: "V",
167
244
  description: "The config volume name to start the service with"
168
245
  })),
169
- __param(5, (0, core_1.Option)("container-port")),
170
- __param(5, (0, core_1.Description)("Port on which the database container will be accessible on the host")),
171
- __param(6, (0, core_1.Option)("enable-admin")),
172
- __param(7, (0, core_1.Option)("disable-admin")),
246
+ __param(4, (0, core_1.Option)("container-port")),
247
+ __param(4, (0, core_1.Description)("Port on which the database container will be accessible on the host")),
248
+ __param(5, (0, core_1.Option)("enable-admin")),
249
+ __param(6, (0, core_1.Option)("disable-admin")),
173
250
  __metadata("design:type", Function),
174
- __metadata("design:paramtypes", [String, String, String, String, String, Number, Boolean, Boolean]),
251
+ __metadata("design:paramtypes", [String, String, String, String, Number, Boolean, Boolean]),
175
252
  __metadata("design:returntype", Promise)
176
253
  ], MongodbController.prototype, "upgrade", null);
177
254
  __decorate([
@@ -193,8 +270,8 @@ __decorate([
193
270
  __metadata("design:returntype", Promise)
194
271
  ], MongodbController.prototype, "destroy", null);
195
272
  __decorate([
196
- (0, core_1.Command)("mongodb:use <name>"),
197
- (0, core_1.Description)("Sets a specified MongoDB service as the default."),
273
+ (0, core_1.Command)("mongodb:use [name]"),
274
+ (0, core_1.Description)("Sets a specified MongoDB service as the default, or prints the current default when no name is given."),
198
275
  __param(0, (0, core_1.Param)("name")),
199
276
  __metadata("design:type", Function),
200
277
  __metadata("design:paramtypes", [String]),
@@ -253,7 +330,8 @@ __decorate([
253
330
  __metadata("design:returntype", Promise)
254
331
  ], MongodbController.prototype, "restore", null);
255
332
  __decorate([
256
- (0, core_1.Completion)("name", "mongodb:use <name>"),
333
+ (0, core_1.Completion)("name", "mongodb [name]"),
334
+ (0, core_1.Completion)("name", "mongodb:use [name]"),
257
335
  (0, core_1.Completion)("name", "mongodb:start [name]"),
258
336
  (0, core_1.Completion)("name", "mongodb:stop [name]"),
259
337
  (0, core_1.Completion)("name", "mongodb:restore [name]"),
@@ -264,6 +342,37 @@ __decorate([
264
342
  __metadata("design:paramtypes", []),
265
343
  __metadata("design:returntype", Promise)
266
344
  ], MongodbController.prototype, "getNames", null);
345
+ __decorate([
346
+ (0, core_1.Completion)("database", "mongodb:backup [name]"),
347
+ (0, core_1.Completion)("database", "mongodb [name]"),
348
+ __param(0, (0, core_1.Param)("name")),
349
+ __metadata("design:type", Function),
350
+ __metadata("design:paramtypes", [String]),
351
+ __metadata("design:returntype", Promise)
352
+ ], MongodbController.prototype, "getDatabases", null);
353
+ __decorate([
354
+ (0, core_1.Completion)("database", "mongodb:restore [name]"),
355
+ __param(0, (0, core_1.Param)("name")),
356
+ __metadata("design:type", Function),
357
+ __metadata("design:paramtypes", [String]),
358
+ __metadata("design:returntype", Promise)
359
+ ], MongodbController.prototype, "getDumpDatabases", null);
360
+ __decorate([
361
+ (0, core_1.Completion)("filename", "mongodb:backup [name]"),
362
+ __param(0, (0, core_1.Param)("name")),
363
+ __param(1, (0, core_1.Option)("database")),
364
+ __metadata("design:type", Function),
365
+ __metadata("design:paramtypes", [String, String]),
366
+ __metadata("design:returntype", Promise)
367
+ ], MongodbController.prototype, "getBackupFilenames", null);
368
+ __decorate([
369
+ (0, core_1.Completion)("file-name", "mongodb:restore [name]"),
370
+ __param(0, (0, core_1.Param)("name")),
371
+ __param(1, (0, core_1.Option)("database")),
372
+ __metadata("design:type", Function),
373
+ __metadata("design:paramtypes", [String, String]),
374
+ __metadata("design:returntype", Promise)
375
+ ], MongodbController.prototype, "getRestoreFilenames", null);
267
376
  exports.MongodbController = MongodbController = __decorate([
268
377
  (0, core_1.Controller)(),
269
378
  (0, core_1.Description)("MongoDB commands"),
@@ -1,6 +1,9 @@
1
1
  export type DatabaseProps = {
2
2
  name: string;
3
+ image?: string;
4
+ /** @deprecated use "image" */
3
5
  imageName?: string;
6
+ /** @deprecated use "image" */
4
7
  imageVersion?: string;
5
8
  username: string;
6
9
  password: string;
@@ -12,16 +15,16 @@ export type DatabaseProps = {
12
15
  };
13
16
  export declare class Database {
14
17
  name: string;
15
- imageName?: string;
16
- imageVersion?: string;
17
18
  username: string;
18
19
  password: string;
19
20
  containerPort?: number;
21
+ protected _image?: string;
20
22
  protected _configVolume?: string;
21
23
  protected _volume?: string;
22
24
  constructor(props: DatabaseProps);
23
25
  get containerName(): string;
24
26
  get image(): string;
27
+ set image(image: string | undefined);
25
28
  get volume(): string;
26
29
  set volume(volume: string);
27
30
  get configVolume(): string;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Database = void 0;
4
+ const utils_1 = require("@wocker/utils");
4
5
  class Database {
5
6
  constructor(props) {
6
- const { name, imageName, imageVersion, username, password, configStorage, configVolume, storage, volume, containerPort } = props;
7
+ const { name, image, imageName, imageVersion, username, password, configStorage, configVolume, storage, volume, containerPort } = props;
7
8
  this.name = name;
8
- this.imageName = imageName;
9
- this.imageVersion = imageVersion;
9
+ this._image = image || (imageName ? new utils_1.Image(imageName, imageVersion).toString() : undefined);
10
10
  this.username = username;
11
11
  this.password = password;
12
12
  this._configVolume = configStorage || configVolume;
@@ -17,11 +17,20 @@ class Database {
17
17
  return `mongodb-${this.name}.ws`;
18
18
  }
19
19
  get image() {
20
- const imageName = this.imageName || "mongo", imageVersion = this.imageVersion;
21
- if (!imageVersion) {
22
- return imageName;
20
+ if (!this._image) {
21
+ return "mongo:latest";
23
22
  }
24
- return `${imageName}:${imageVersion}`;
23
+ return this._image;
24
+ }
25
+ set image(image) {
26
+ if (!image) {
27
+ delete this._image;
28
+ return;
29
+ }
30
+ if (!utils_1.Image.isValid(image)) {
31
+ throw new Error(`Invalid image ${image}`);
32
+ }
33
+ this._image = image;
25
34
  }
26
35
  get volume() {
27
36
  if (!this._volume) {
@@ -50,8 +59,7 @@ class Database {
50
59
  toObject() {
51
60
  return {
52
61
  name: this.name,
53
- imageName: this.imageName,
54
- imageVersion: this.imageVersion,
62
+ image: this._image,
55
63
  username: this.username,
56
64
  password: this.password,
57
65
  volume: this._volume,
@@ -2,16 +2,17 @@ import { PluginConfig } from "@wocker/core";
2
2
  import { Database, DatabaseProps } from "./Database";
3
3
  export type AdminConfig = {
4
4
  enabled: boolean;
5
+ hostname: string;
5
6
  };
6
7
  export type ConfigProps = {
7
8
  default?: string;
8
- databases?: DatabaseProps[];
9
9
  admin?: AdminConfig;
10
+ databases?: DatabaseProps[];
10
11
  };
11
12
  export declare class MongodbPluginConfig extends PluginConfig {
12
13
  default?: string;
13
- databases: Database[];
14
14
  admin: AdminConfig;
15
+ databases: Database[];
15
16
  constructor(data: ConfigProps);
16
17
  setDatabase(database: Database): void;
17
18
  hasDatabase(name: string): boolean;
@@ -6,12 +6,13 @@ const Database_1 = require("./Database");
6
6
  class MongodbPluginConfig extends core_1.PluginConfig {
7
7
  constructor(data) {
8
8
  super(data);
9
- const { default: defaultDatabase, databases = [], admin: { enabled: enabledAdmin = true } = {} } = data;
9
+ const { default: defaultDatabase, admin: { enabled: enabledAdmin = true, hostname: adminHostname = "dbadmin-mongodb.workspace" } = {}, databases = [] } = data;
10
10
  this.default = defaultDatabase;
11
- this.databases = databases.map(database => new Database_1.Database(database));
12
11
  this.admin = {
13
- enabled: enabledAdmin
12
+ enabled: enabledAdmin,
13
+ hostname: adminHostname
14
14
  };
15
+ this.databases = databases.map(database => new Database_1.Database(database));
15
16
  }
16
17
  setDatabase(database) {
17
18
  let exists = false;
@@ -64,10 +65,10 @@ class MongodbPluginConfig extends core_1.PluginConfig {
64
65
  toObject() {
65
66
  return {
66
67
  default: this.default,
68
+ admin: this.admin,
67
69
  databases: this.databases.length > 0
68
70
  ? this.databases.map((database) => database.toObject())
69
- : [],
70
- admin: this.admin
71
+ : []
71
72
  };
72
73
  }
73
74
  }
@@ -6,10 +6,11 @@ export declare class MongodbService {
6
6
  protected readonly dockerService: DockerService;
7
7
  protected readonly proxyService: ProxyService;
8
8
  protected _config?: MongodbPluginConfig;
9
- adminContainerName: string;
10
9
  constructor(pluginConfigService: PluginConfigService, dockerService: DockerService, proxyService: ProxyService);
11
10
  get config(): MongodbPluginConfig;
12
11
  get fs(): FileSystem;
12
+ mongosh(name?: string, database?: string): Promise<void>;
13
+ init(adminHostname?: string): Promise<void>;
13
14
  create(props?: Partial<DatabaseProps>): Promise<void>;
14
15
  upgrade(props: Partial<DatabaseProps>): Promise<void>;
15
16
  destroy(name: string, yes?: boolean, force?: boolean): Promise<void>;
@@ -17,6 +18,8 @@ export declare class MongodbService {
17
18
  start(name?: string, restart?: boolean): Promise<void>;
18
19
  admin(): Promise<void>;
19
20
  stop(name?: string): Promise<void>;
21
+ getDumpDatabases(service: Database): Promise<string[]>;
22
+ getDumpFiles(service: Database, database: string): Promise<string[]>;
20
23
  backup(name?: string, database?: string): Promise<void>;
21
24
  deleteBackup(name?: string, database?: string, filename?: string, confirm?: boolean): Promise<void>;
22
25
  restore(name?: string, database?: string, filename?: string): Promise<void>;
@@ -23,6 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.MongodbService = void 0;
25
25
  const core_1 = require("@wocker/core");
26
+ const prompts_1 = require("@wocker/prompts");
26
27
  const utils_1 = require("@wocker/utils");
27
28
  const format_1 = require("date-fns/format");
28
29
  const cli_table3_1 = __importDefault(require("cli-table3"));
@@ -33,7 +34,6 @@ let MongodbService = class MongodbService {
33
34
  this.pluginConfigService = pluginConfigService;
34
35
  this.dockerService = dockerService;
35
36
  this.proxyService = proxyService;
36
- this.adminContainerName = "dbadmin-mongodb.workspace";
37
37
  }
38
38
  get config() {
39
39
  if (!this._config) {
@@ -44,6 +44,44 @@ let MongodbService = class MongodbService {
44
44
  get fs() {
45
45
  return this.pluginConfigService.fs;
46
46
  }
47
+ mongosh(name, database) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const service = this.config.getDatabaseOrDefault(name);
50
+ const container = yield this.dockerService.getContainer(service.containerName);
51
+ if (!container) {
52
+ throw new Error(`Service "${service.name}" is not started`);
53
+ }
54
+ if (!database && process.stdin.isTTY) {
55
+ database = yield (0, prompts_1.promptSelect)({
56
+ message: "Database:",
57
+ options: yield this.getDatabases(service)
58
+ });
59
+ }
60
+ yield this.dockerService.exec(service.containerName, {
61
+ cmd: [
62
+ "mongosh",
63
+ "--username", service.username,
64
+ "--password", service.password,
65
+ ...(database ? [database] : [])
66
+ ],
67
+ tty: true
68
+ });
69
+ });
70
+ }
71
+ init(adminHostname) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const config = this.config;
74
+ if (!adminHostname) {
75
+ adminHostname = (yield (0, prompts_1.promptInput)({
76
+ message: "Admin hostname",
77
+ required: true,
78
+ default: config.admin.hostname
79
+ }));
80
+ }
81
+ config.admin.hostname = adminHostname;
82
+ config.save();
83
+ });
84
+ }
47
85
  create() {
48
86
  return __awaiter(this, arguments, void 0, function* (props = {}) {
49
87
  if (props.name && this.config.hasDatabase(props.name)) {
@@ -51,7 +89,7 @@ let MongodbService = class MongodbService {
51
89
  delete props.name;
52
90
  }
53
91
  if (!props.name) {
54
- props.name = (yield (0, utils_1.promptInput)({
92
+ props.name = (yield (0, prompts_1.promptInput)({
55
93
  message: "Mongodb name",
56
94
  type: "text",
57
95
  validate: (name) => {
@@ -66,19 +104,19 @@ let MongodbService = class MongodbService {
66
104
  }));
67
105
  }
68
106
  if (!props.username) {
69
- props.username = yield (0, utils_1.promptInput)({
107
+ props.username = yield (0, prompts_1.promptInput)({
70
108
  message: "Username:",
71
109
  type: "text",
72
110
  required: true
73
111
  });
74
112
  }
75
113
  if (!props.password) {
76
- props.password = (yield (0, utils_1.promptInput)({
114
+ props.password = (yield (0, prompts_1.promptInput)({
77
115
  message: "Password:",
78
116
  type: "password",
79
117
  required: true
80
118
  }));
81
- const confirmPassword = yield (0, utils_1.promptInput)({
119
+ const confirmPassword = yield (0, prompts_1.promptInput)({
82
120
  message: "Confirm password:",
83
121
  type: "password",
84
122
  required: true
@@ -88,12 +126,12 @@ let MongodbService = class MongodbService {
88
126
  }
89
127
  }
90
128
  if (!props.containerPort) {
91
- const needPort = yield (0, utils_1.promptConfirm)({
129
+ const needPort = yield (0, prompts_1.promptConfirm)({
92
130
  message: "Do you need to expose container port?",
93
131
  default: false
94
132
  });
95
133
  if (needPort) {
96
- props.containerPort = yield (0, utils_1.promptInput)({
134
+ props.containerPort = yield (0, prompts_1.promptInput)({
97
135
  required: true,
98
136
  message: "Container port:",
99
137
  type: "number",
@@ -104,8 +142,7 @@ let MongodbService = class MongodbService {
104
142
  }
105
143
  const database = new Database_1.Database({
106
144
  name: props.name,
107
- imageName: props.imageName,
108
- imageVersion: props.imageVersion,
145
+ image: props.image,
109
146
  username: props.username,
110
147
  password: props.password,
111
148
  containerPort: props.containerPort
@@ -118,12 +155,8 @@ let MongodbService = class MongodbService {
118
155
  return __awaiter(this, void 0, void 0, function* () {
119
156
  const service = this.config.getDatabaseOrDefault(props.name);
120
157
  let changed = false;
121
- if (props.imageName) {
122
- service.imageName = props.imageName;
123
- changed = true;
124
- }
125
- if (props.imageVersion) {
126
- service.imageVersion = props.imageVersion;
158
+ if (props.image) {
159
+ service.image = props.image;
127
160
  changed = true;
128
161
  }
129
162
  if (props.volume) {
@@ -154,7 +187,7 @@ let MongodbService = class MongodbService {
154
187
  throw new Error(`Can't delete default database.`);
155
188
  }
156
189
  if (!yes) {
157
- const confirm = yield (0, utils_1.promptConfirm)({
190
+ const confirm = yield (0, prompts_1.promptConfirm)({
158
191
  message: `Are you sure you want to delete the "${database.name}" database? This action cannot be undone and all data will be lost.`,
159
192
  default: false
160
193
  });
@@ -226,6 +259,7 @@ let MongodbService = class MongodbService {
226
259
  }
227
260
  admin() {
228
261
  return __awaiter(this, void 0, void 0, function* () {
262
+ const hostname = this.config.admin.hostname;
229
263
  const connections = [];
230
264
  for (const database of this.config.databases) {
231
265
  try {
@@ -242,22 +276,22 @@ let MongodbService = class MongodbService {
242
276
  }
243
277
  catch (ignore) { }
244
278
  }
245
- yield this.dockerService.removeContainer(this.adminContainerName);
279
+ yield this.dockerService.removeContainer(hostname);
246
280
  if (!this.config.admin.enabled || connections.length === 0) {
247
281
  return;
248
282
  }
249
- let container = yield this.dockerService.getContainer(this.adminContainerName);
283
+ let container = yield this.dockerService.getContainer(hostname);
250
284
  if (!container) {
251
285
  console.info("Mongodb Admin starting...");
252
286
  yield this.dockerService.pullImage("mongo-express:latest");
253
287
  container = yield this.dockerService.createContainer({
254
- name: this.adminContainerName,
288
+ name: hostname,
255
289
  image: "mongo-express:latest",
256
290
  restart: "always",
257
291
  env: {
258
- VIRTUAL_HOST: this.adminContainerName,
292
+ VIRTUAL_HOST: hostname,
259
293
  VIRTUAL_PORT: "80",
260
- VCAP_APP_HOST: this.adminContainerName,
294
+ VCAP_APP_HOST: hostname,
261
295
  PORT: "80",
262
296
  ME_CONFIG_BASICAUTH: "false",
263
297
  ME_CONFIG_BASICAUTH_USERNAME: "",
@@ -281,11 +315,27 @@ let MongodbService = class MongodbService {
281
315
  yield this.dockerService.removeContainer(database.containerName);
282
316
  });
283
317
  }
318
+ getDumpDatabases(service) {
319
+ return __awaiter(this, void 0, void 0, function* () {
320
+ if (!this.fs.exists(`dump/${service.name}`)) {
321
+ return [];
322
+ }
323
+ return this.fs.readdir(`dump/${service.name}`);
324
+ });
325
+ }
326
+ getDumpFiles(service, database) {
327
+ return __awaiter(this, void 0, void 0, function* () {
328
+ if (!this.fs.exists(`dump/${service.name}/${database}`)) {
329
+ return [];
330
+ }
331
+ return this.fs.readdir(`dump/${service.name}/${database}`);
332
+ });
333
+ }
284
334
  backup(name, database) {
285
335
  return __awaiter(this, void 0, void 0, function* () {
286
336
  const service = this.config.getDatabaseOrDefault(name);
287
337
  if (!database) {
288
- database = yield (0, utils_1.promptSelect)({
338
+ database = yield (0, prompts_1.promptSelect)({
289
339
  message: "Database",
290
340
  required: true,
291
341
  options: yield this.getDatabases(service)
@@ -302,8 +352,8 @@ let MongodbService = class MongodbService {
302
352
  "mongodump",
303
353
  "--authenticationDatabase", "admin",
304
354
  "--host", `${service.containerName}:27017`,
305
- "--username", "root",
306
- "--password", "toor",
355
+ "--username", service.username,
356
+ "--password", service.password,
307
357
  "--db", database,
308
358
  "--archive",
309
359
  "--gzip"
@@ -329,29 +379,29 @@ let MongodbService = class MongodbService {
329
379
  return __awaiter(this, void 0, void 0, function* () {
330
380
  const service = this.config.getDatabaseOrDefault(name);
331
381
  if (!database) {
332
- const databases = this.fs.readdir(`dumps/${service.name}`);
382
+ const databases = yield this.getDumpDatabases(service);
333
383
  if (databases.length === 0) {
334
384
  throw new Error(`No backups were found for the "${service.name}" service`);
335
385
  }
336
- database = yield (0, utils_1.promptSelect)({
386
+ database = yield (0, prompts_1.promptSelect)({
337
387
  message: "Database",
338
388
  required: true,
339
389
  options: databases
340
390
  });
341
391
  }
342
392
  if (!filename) {
343
- const files = this.fs.readdir(`dumps/${service.name}/${database}`);
393
+ const files = yield this.getDumpFiles(service, database);
344
394
  if (files.length === 0) {
345
395
  throw new Error(`No backup files found for the "${database}" database`);
346
396
  }
347
- filename = yield (0, utils_1.promptSelect)({
397
+ filename = yield (0, prompts_1.promptSelect)({
348
398
  message: "File",
349
399
  required: true,
350
400
  options: files
351
401
  });
352
402
  }
353
403
  if (!confirm) {
354
- confirm = yield (0, utils_1.promptConfirm)({
404
+ confirm = yield (0, prompts_1.promptConfirm)({
355
405
  message: "Are you sure you want to delete?",
356
406
  default: false
357
407
  });
@@ -359,7 +409,7 @@ let MongodbService = class MongodbService {
359
409
  if (!confirm) {
360
410
  throw new Error("Canceled");
361
411
  }
362
- this.fs.rm(`dumps/${service.name}/${database}/${filename}`);
412
+ this.fs.rm(`dump/${service.name}/${database}/${filename}`);
363
413
  console.info(`File "${filename}" deleted`);
364
414
  const otherFiles = this.fs.readdir(`dump/${service.name}/${database}`);
365
415
  if (otherFiles.length === 0) {
@@ -374,28 +424,28 @@ let MongodbService = class MongodbService {
374
424
  return __awaiter(this, void 0, void 0, function* () {
375
425
  const service = this.config.getDatabaseOrDefault(name);
376
426
  if (!database) {
377
- const databases = this.fs.readdir(`dumps/${service.name}`);
427
+ const databases = yield this.getDumpDatabases(service);
378
428
  if (databases.length === 0) {
379
429
  throw new Error(`No backups were found for the "${service.name}" service`);
380
430
  }
381
- database = yield (0, utils_1.promptSelect)({
431
+ database = yield (0, prompts_1.promptSelect)({
382
432
  message: "Database",
383
433
  required: true,
384
434
  options: databases
385
435
  });
386
436
  }
387
437
  if (!filename) {
388
- const files = this.fs.readdir(`dumps/${service.name}/${database}`);
438
+ const files = yield this.getDumpFiles(service, database);
389
439
  if (files.length === 0) {
390
440
  throw new Error(`No backup files found for the "${database}" database`);
391
441
  }
392
- filename = yield (0, utils_1.promptSelect)({
442
+ filename = yield (0, prompts_1.promptSelect)({
393
443
  message: "File",
394
444
  required: true,
395
445
  options: files
396
446
  });
397
447
  }
398
- const file = this.fs.createReadStream(`dumps/${service.name}/${database}/${filename}`);
448
+ const file = this.fs.createReadStream(`dump/${service.name}/${database}/${filename}`);
399
449
  const stream = yield this.dockerService.exec(service.containerName, [
400
450
  "mongorestore",
401
451
  "--authenticationDatabase", "admin",
@@ -463,16 +513,31 @@ let MongodbService = class MongodbService {
463
513
  "Username",
464
514
  "Host",
465
515
  "Image",
466
- "Storages"
516
+ "Storages",
517
+ "Status",
518
+ "IP"
467
519
  ]
468
520
  });
469
521
  for (const database of this.config.databases) {
522
+ let status = "stopped";
523
+ let ip = "-";
524
+ try {
525
+ const container = yield this.dockerService.getContainer(database.containerName);
526
+ if (container) {
527
+ const { State: { Running }, NetworkSettings: { Networks: { workspace: { IPAddress } } } } = yield container.inspect();
528
+ status = Running ? "running" : "stopped";
529
+ ip = IPAddress || "-";
530
+ }
531
+ }
532
+ catch (ignore) { }
470
533
  table.push([
471
534
  database.name + (database.name === this.config.default ? " (default)" : ""),
472
535
  database.username,
473
536
  database.containerName + (database.containerPort ? `:${database.containerPort}` : ""),
474
537
  database.image,
475
- `${database.configVolume}\n${database.volume}`
538
+ `${database.configVolume}\n${database.volume}`,
539
+ status,
540
+ ip
476
541
  ]);
477
542
  }
478
543
  return table.toString();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wocker/mongodb-plugin",
3
3
  "type": "commonjs",
4
- "version": "1.0.14-beta.0",
4
+ "version": "1.0.14-beta.1",
5
5
  "author": "Kris Papercut <krispcut@gmail.com>",
6
6
  "description": "Mongodb plugin for wocker",
7
7
  "license": "MIT",
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@wocker/core": "^1.1.0",
31
+ "@wocker/prompts": "^1.0.0",
31
32
  "@wocker/utils": "^2.0.3",
32
33
  "cli-table3": "^0.6.5",
33
34
  "date-fns": "^4.1.0"