@wocker/mongodb-plugin 1.0.0 → 1.0.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,7 +2,7 @@ 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): Promise<void>;
5
+ create(name?: string, image?: string, imageVersion?: string): Promise<void>;
6
6
  destroy(name: string, yes?: boolean, force?: boolean): Promise<void>;
7
7
  use(name: string): Promise<void>;
8
8
  start(name?: string, restart?: boolean): Promise<void>;
@@ -28,9 +28,9 @@ let MongodbController = class MongodbController {
28
28
  constructor(mongodbService) {
29
29
  this.mongodbService = mongodbService;
30
30
  }
31
- create(name) {
31
+ create(name, image, imageVersion) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
- yield this.mongodbService.create(name);
33
+ yield this.mongodbService.create(name, image, imageVersion);
34
34
  });
35
35
  }
36
36
  destroy(name, yes, force) {
@@ -42,7 +42,7 @@ let MongodbController = class MongodbController {
42
42
  }
43
43
  use(name) {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
- yield this.mongodbService.use(name);
45
+ this.mongodbService.use(name);
46
46
  });
47
47
  }
48
48
  start(name, restart) {
@@ -64,29 +64,52 @@ let MongodbController = class MongodbController {
64
64
  }
65
65
  getNames() {
66
66
  return __awaiter(this, void 0, void 0, function* () {
67
- return [];
67
+ return this.mongodbService.config.databases.items.map((database) => {
68
+ return database.name;
69
+ });
68
70
  });
69
71
  }
70
72
  };
71
73
  exports.MongodbController = MongodbController;
72
74
  __decorate([
73
75
  (0, core_1.Command)("mongodb:create [name]"),
76
+ (0, core_1.Description)("Creates a MongoDB service with configurable credentials, host, and storage options."),
74
77
  __param(0, (0, core_1.Param)("name")),
78
+ __param(1, (0, core_1.Option)("image", {
79
+ type: "string",
80
+ alias: "i",
81
+ description: "The image name to start the service with",
82
+ })),
83
+ __param(2, (0, core_1.Option)("image-version", {
84
+ type: "string",
85
+ alias: "I",
86
+ description: "The image version to start the service with"
87
+ })),
75
88
  __metadata("design:type", Function),
76
- __metadata("design:paramtypes", [String]),
89
+ __metadata("design:paramtypes", [String, String, String]),
77
90
  __metadata("design:returntype", Promise)
78
91
  ], MongodbController.prototype, "create", null);
79
92
  __decorate([
80
93
  (0, core_1.Command)("mongodb:destroy <name>"),
94
+ (0, core_1.Description)("Destroys a specified MongodbDB service instance with an option to force deletion."),
81
95
  __param(0, (0, core_1.Param)("name")),
82
- __param(1, (0, core_1.Option)("yes", { alias: "y" })),
83
- __param(2, (0, core_1.Option)("force", { alias: "f" })),
96
+ __param(1, (0, core_1.Option)("yes", {
97
+ type: "boolean",
98
+ alias: "y",
99
+ description: "Skip confirmation"
100
+ })),
101
+ __param(2, (0, core_1.Option)("force", {
102
+ type: "boolean",
103
+ alias: "f",
104
+ description: "Force deletion"
105
+ })),
84
106
  __metadata("design:type", Function),
85
107
  __metadata("design:paramtypes", [String, Boolean, Boolean]),
86
108
  __metadata("design:returntype", Promise)
87
109
  ], MongodbController.prototype, "destroy", null);
88
110
  __decorate([
89
111
  (0, core_1.Command)("mongodb:use <name>"),
112
+ (0, core_1.Description)("Sets a specified MongoDB service as the default."),
90
113
  __param(0, (0, core_1.Param)("name")),
91
114
  __metadata("design:type", Function),
92
115
  __metadata("design:paramtypes", [String]),
@@ -94,14 +117,20 @@ __decorate([
94
117
  ], MongodbController.prototype, "use", null);
95
118
  __decorate([
96
119
  (0, core_1.Command)("mongodb:start [name]"),
120
+ (0, core_1.Description)("Starts a specified MongoDB service and optionally restarts it if already running."),
97
121
  __param(0, (0, core_1.Param)("name")),
98
- __param(1, (0, core_1.Option)("restart", { alias: "r" })),
122
+ __param(1, (0, core_1.Option)("restart", {
123
+ type: "boolean",
124
+ alias: "r",
125
+ description: "Restart the service if already running"
126
+ })),
99
127
  __metadata("design:type", Function),
100
128
  __metadata("design:paramtypes", [String, Boolean]),
101
129
  __metadata("design:returntype", Promise)
102
130
  ], MongodbController.prototype, "start", null);
103
131
  __decorate([
104
132
  (0, core_1.Command)("mongodb:stop [name]"),
133
+ (0, core_1.Description)("Stops a specified MongoDB service instance."),
105
134
  __param(0, (0, core_1.Param)("name")),
106
135
  __metadata("design:type", Function),
107
136
  __metadata("design:paramtypes", [String]),
@@ -109,6 +138,7 @@ __decorate([
109
138
  ], MongodbController.prototype, "stop", null);
110
139
  __decorate([
111
140
  (0, core_1.Command)("mongodb:ls"),
141
+ (0, core_1.Description)("Lists all available MongoDB services."),
112
142
  __metadata("design:type", Function),
113
143
  __metadata("design:paramtypes", []),
114
144
  __metadata("design:returntype", Promise)
@@ -8,8 +8,12 @@ export declare abstract class Config {
8
8
  default?: string;
9
9
  databases: ConfigCollection<Database, DatabaseProps>;
10
10
  constructor(props: ConfigProps);
11
- getDatabase(name?: string): Database;
11
+ setDatabase(database: Database): void;
12
+ hasDatabase(name: string): boolean;
13
+ getDefault(): Database;
14
+ getDatabaseOrDefault(name?: string): Database;
15
+ getDatabase(name: string): Database;
12
16
  removeDatabase(name: string): void;
13
- abstract save(): Promise<void>;
17
+ abstract save(): void;
14
18
  toJSON(): ConfigProps;
15
19
  }
@@ -9,27 +9,39 @@ class Config {
9
9
  this.default = defaultDatabase;
10
10
  this.databases = new core_1.ConfigCollection(Database_1.Database, databases);
11
11
  }
12
- getDatabase(name) {
12
+ setDatabase(database) {
13
+ this.databases.setConfig(database);
14
+ }
15
+ hasDatabase(name) {
16
+ return !!this.databases.getConfig(name);
17
+ }
18
+ getDefault() {
19
+ if (!this.default) {
20
+ throw new Error("Default database is not defined");
21
+ }
22
+ const database = this.databases.getConfig(this.default);
23
+ if (!database) {
24
+ throw new Error(`Default database "${this.default}" not found`);
25
+ }
26
+ return database;
27
+ }
28
+ getDatabaseOrDefault(name) {
13
29
  if (!name) {
14
- if (!this.default) {
15
- throw new Error("Default database is not defined");
16
- }
17
- const database = this.databases.getConfig(this.default);
18
- if (!database) {
19
- throw new Error(`Default database ${this.default} not found`);
20
- }
21
- return database;
30
+ return this.getDefault();
22
31
  }
32
+ return this.getDatabase(name);
33
+ }
34
+ getDatabase(name) {
23
35
  const database = this.databases.getConfig(name);
24
36
  if (!database) {
25
- throw new Error(`Database ${name} not found`);
37
+ throw new Error(`Database "${name}" not found`);
26
38
  }
27
39
  return database;
28
40
  }
29
41
  removeDatabase(name) {
30
42
  const database = this.databases.getConfig(name);
31
43
  if (!database) {
32
- throw new Error(`Storage ${name} not found`);
44
+ throw new Error(`Database "${name}" not found`);
33
45
  }
34
46
  this.databases.removeConfig(name);
35
47
  }
@@ -1,17 +1,22 @@
1
1
  import { Config, ConfigProperties } from "@wocker/core";
2
2
  export type DatabaseProps = ConfigProperties & {
3
+ imageName?: string;
4
+ imageVersion?: string;
3
5
  username: string;
4
6
  password: string;
5
7
  configStorage?: string;
6
8
  storage?: string;
7
9
  };
8
10
  export declare class Database extends Config<DatabaseProps> {
11
+ imageName?: string;
12
+ imageVersion?: string;
9
13
  username: string;
10
14
  password: string;
11
15
  configStorage: string;
12
16
  storage: string;
13
17
  constructor(props: DatabaseProps);
14
18
  get containerName(): string;
19
+ get image(): string;
15
20
  get defaultStorage(): string;
16
21
  get defaultConfigStorage(): string;
17
22
  }
@@ -5,7 +5,9 @@ const core_1 = require("@wocker/core");
5
5
  class Database extends core_1.Config {
6
6
  constructor(props) {
7
7
  super(props);
8
- const { username, password, configStorage, storage } = props;
8
+ const { imageName, imageVersion, username, password, configStorage, storage } = props;
9
+ this.imageName = imageName;
10
+ this.imageVersion = imageVersion;
9
11
  this.username = username;
10
12
  this.password = password;
11
13
  this.configStorage = configStorage || this.defaultConfigStorage;
@@ -14,6 +16,10 @@ class Database extends core_1.Config {
14
16
  get containerName() {
15
17
  return `mongodb-${this.name}.ws`;
16
18
  }
19
+ get image() {
20
+ const imageName = this.imageName || "mongo", imageVersion = this.imageVersion || "latest";
21
+ return `${imageName}:${imageVersion}`;
22
+ }
17
23
  get defaultStorage() {
18
24
  return `wocker-mongodb-${this.name}`;
19
25
  }
@@ -5,13 +5,14 @@ export declare class MongodbService {
5
5
  protected readonly pluginConfigService: PluginConfigService;
6
6
  protected readonly dockerService: DockerService;
7
7
  protected readonly proxyService: ProxyService;
8
+ protected _config?: Config;
8
9
  adminContainerName: string;
9
10
  constructor(appConfigService: AppConfigService, pluginConfigService: PluginConfigService, dockerService: DockerService, proxyService: ProxyService);
10
- protected _config?: Config;
11
11
  get config(): Config;
12
- create(name?: string, username?: string, password?: string): Promise<void>;
12
+ create(name?: string, imageName?: string, imageVersion?: string, username?: string, password?: string): Promise<void>;
13
+ upgrade(name?: string, imageName?: string, imageVersion?: string): Promise<void>;
13
14
  destroy(name: string, yes?: boolean, force?: boolean): Promise<void>;
14
- use(name: string): Promise<void>;
15
+ use(name: string): void;
15
16
  start(name?: string, restart?: boolean): Promise<void>;
16
17
  admin(): Promise<void>;
17
18
  stop(name?: string): Promise<void>;
@@ -37,40 +37,27 @@ let MongodbService = class MongodbService {
37
37
  }
38
38
  get config() {
39
39
  if (!this._config) {
40
- const data = this.pluginConfigService.fs.exists("config.json")
41
- ? this.pluginConfigService.fs.readJSON("config.json")
42
- : {
43
- default: "default",
44
- databases: [
45
- {
46
- name: "default",
47
- username: "root",
48
- password: "toor",
49
- configStorage: "wocker-mongoconfig-default",
50
- storage: "wocker-mongodb-default"
51
- }
52
- ]
53
- };
54
40
  const fs = this.pluginConfigService.fs;
41
+ const data = fs.exists("config.json")
42
+ ? fs.readJSON("config.json")
43
+ : {};
55
44
  this._config = new class extends Config_1.Config {
56
45
  save() {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- if (!fs.exists()) {
59
- fs.mkdir("", {
60
- recursive: true
61
- });
62
- }
63
- yield fs.writeJSON("config.json", this.toJSON());
64
- });
46
+ if (!fs.exists()) {
47
+ fs.mkdir("", {
48
+ recursive: true
49
+ });
50
+ }
51
+ fs.writeJSON("config.json", this.toJSON());
65
52
  }
66
53
  }(data);
67
54
  }
68
55
  return this._config;
69
56
  }
70
- create(name_1) {
71
- return __awaiter(this, arguments, void 0, function* (name, username = "", password = "") {
72
- if (name && this.config.databases.getConfig(name)) {
73
- throw new Error(`${name}`);
57
+ create(name_1, imageName_1, imageVersion_1) {
58
+ return __awaiter(this, arguments, void 0, function* (name, imageName, imageVersion, username = "", password = "") {
59
+ if (name && this.config.hasDatabase(name)) {
60
+ throw new Error(`Database name "${name}" is already taken`);
74
61
  }
75
62
  if (!name) {
76
63
  name = (yield (0, utils_1.promptText)({
@@ -80,7 +67,7 @@ let MongodbService = class MongodbService {
80
67
  if (!name) {
81
68
  return "Name is required";
82
69
  }
83
- if (this.config.databases.getConfig(name)) {
70
+ if (this.config.hasDatabase(name)) {
84
71
  return `Database name "${name}" is already taken`;
85
72
  }
86
73
  return true;
@@ -111,16 +98,36 @@ let MongodbService = class MongodbService {
111
98
  }
112
99
  const database = new Database_1.Database({
113
100
  name,
101
+ imageName,
102
+ imageVersion,
114
103
  username,
115
104
  password
116
105
  });
117
- this.config.databases.setConfig(database);
118
- yield this.config.save();
106
+ this.config.setDatabase(database);
107
+ this.config.save();
108
+ });
109
+ }
110
+ upgrade(name, imageName, imageVersion) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const service = this.config.getDatabaseOrDefault(name);
113
+ let changed = false;
114
+ if (imageName) {
115
+ service.imageName = imageName;
116
+ changed = true;
117
+ }
118
+ if (imageVersion) {
119
+ service.imageVersion = imageVersion;
120
+ changed = true;
121
+ }
122
+ if (changed) {
123
+ this.config.setDatabase(service);
124
+ this.config.save();
125
+ }
119
126
  });
120
127
  }
121
128
  destroy(name, yes, force) {
122
129
  return __awaiter(this, void 0, void 0, function* () {
123
- if (!this.appConfigService.isVersionGTE || !this.appConfigService.isVersionGTE("1.0.19")) {
130
+ if (!this.pluginConfigService.isVersionGTE("1.0.19")) {
124
131
  throw new Error("Please update @wocker/ws");
125
132
  }
126
133
  const database = this.config.getDatabase(name);
@@ -143,31 +150,33 @@ let MongodbService = class MongodbService {
143
150
  yield this.dockerService.rmVolume(database.storage);
144
151
  }
145
152
  this.config.removeDatabase(database.name);
146
- yield this.config.save();
153
+ this.config.save();
147
154
  });
148
155
  }
149
156
  use(name) {
150
- return __awaiter(this, void 0, void 0, function* () {
151
- const database = this.config.getDatabase(name);
152
- this.config.default = database.name;
153
- yield this.config.save();
154
- });
157
+ const database = this.config.getDatabase(name);
158
+ this.config.default = database.name;
159
+ this.config.save();
155
160
  }
156
161
  start(name, restart) {
157
162
  return __awaiter(this, void 0, void 0, function* () {
158
- if (!this.appConfigService.isVersionGTE || !this.appConfigService.isVersionGTE("1.0.19")) {
163
+ if (!this.pluginConfigService.isVersionGTE("1.0.19")) {
159
164
  throw new Error("Please update @wocker/ws");
160
165
  }
161
- const database = this.config.getDatabase(name);
162
- if (restart) {
163
- yield this.dockerService.removeContainer(database.containerName);
166
+ if (!name || !this.config.default) {
167
+ yield this.create();
164
168
  }
169
+ const database = this.config.getDatabaseOrDefault(name);
165
170
  let container = yield this.dockerService.getContainer(database.containerName);
171
+ if (restart && container) {
172
+ yield this.dockerService.removeContainer(database.containerName);
173
+ container = null;
174
+ }
166
175
  if (!container) {
167
176
  container = yield this.dockerService.createContainer({
168
177
  name: database.containerName,
169
178
  restart: "always",
170
- image: "mongo:latest",
179
+ image: database.image,
171
180
  env: {
172
181
  MONGO_INITDB_ROOT_USERNAME: database.username,
173
182
  MONGO_INITDB_ROOT_PASSWORD: database.password,
@@ -239,7 +248,7 @@ let MongodbService = class MongodbService {
239
248
  }
240
249
  stop(name) {
241
250
  return __awaiter(this, void 0, void 0, function* () {
242
- const database = this.config.getDatabase(name);
251
+ const database = this.config.getDatabaseOrDefault(name);
243
252
  console.info(`Stopping ${database.name}...`);
244
253
  yield this.dockerService.removeContainer(database.containerName);
245
254
  });
@@ -251,6 +260,7 @@ let MongodbService = class MongodbService {
251
260
  "Name",
252
261
  "Username",
253
262
  "Host",
263
+ "Image",
254
264
  "Storages"
255
265
  ]
256
266
  });
@@ -259,6 +269,7 @@ let MongodbService = class MongodbService {
259
269
  database.name + (database.name === this.config.default ? " (default)" : ""),
260
270
  database.username,
261
271
  database.containerName,
272
+ database.image,
262
273
  `${database.configStorage}\n${database.storage}`
263
274
  ]);
264
275
  }
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@wocker/mongodb-plugin",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "author": "Kris Papercut <krispcut@gmail.com>",
5
5
  "description": "Mongodb plugin for wocker",
6
6
  "license": "MIT",
7
- "main": "lib/index.js",
8
- "types": "lib/index.d.ts",
7
+ "main": "./lib/index.js",
8
+ "types": "./lib/index.d.ts",
9
9
  "keywords": [
10
10
  "wocker",
11
11
  "plugin",
12
12
  "mongodb"
13
13
  ],
14
- "homepage": "https://kearisp.github.io/wocker/plugins/mongodb",
14
+ "homepage": "https://kearisp.github.io/wocker/docs/plugins/mongodb",
15
15
  "repository": {
16
16
  "type": "git",
17
17
  "url": "git+https://github.com/kearisp/wocker-mongodb-plugin.git"
@@ -26,12 +26,12 @@
26
26
  "test": "echo \"Error: no test specified\" && exit 1"
27
27
  },
28
28
  "dependencies": {
29
- "@wocker/core": "^1.0.20",
30
- "@wocker/utils": "^1.0.8",
29
+ "@wocker/core": "^1.0.21",
30
+ "@wocker/utils": "^1.0.9",
31
31
  "cli-table3": "^0.6.5"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/node": ">=20.17.6",
34
+ "@types/node": "^22.7.8",
35
35
  "typescript": "^5.6.3"
36
36
  }
37
37
  }