Skip to content

Commit 213330b

Browse files
committed
Add support for enum of collections
1 parent 44e1829 commit 213330b

9 files changed

Lines changed: 153 additions & 21 deletions

File tree

OpenFlow/src/DatabaseConnection.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ export class DatabaseConnection {
3131
this._dbname = dbname;
3232
this.mongodburl = mongodburl;
3333
}
34+
static toArray(iterator): Promise<any[]> {
35+
return new Promise((resolve, reject) => {
36+
iterator.toArray((err, res) => {
37+
if (err) {
38+
reject(err);
39+
} else {
40+
resolve(res);
41+
}
42+
});
43+
});
44+
}
3445
/**
3546
* Connect to MongoDB
3647
* @returns Promise<void>
@@ -45,7 +56,17 @@ export class DatabaseConnection {
4556
});
4657
this.db = this.cli.db(this._dbname);
4758
}
48-
59+
async ListCollections(jwt: string): Promise<any[]> {
60+
var result = await DatabaseConnection.toArray(this.db.listCollections());
61+
Crypt.verityToken(jwt);
62+
return result;
63+
}
64+
async DropCollection(collectionname: string, jwt: string): Promise<void> {
65+
var user: TokenUser = Crypt.verityToken(jwt);
66+
if (!user.hasrolename("admins")) throw new Error("Access denied");
67+
if (["workflow", "entities", "config", "audit", "jslog", "openrpa", "nodered", "openrpa_instances", "forms", "workflow_instances", "users"].indexOf(collectionname) > -1) throw new Error("Access denied");
68+
await this.db.dropCollection(collectionname);
69+
}
4970

5071
/**
5172
* Send a query to the database.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export class DropCollectionMessage implements IReplyMessage {
2+
public error: string;
3+
public jwt: string;
4+
public collectionname: string;
5+
6+
static assign(o: any): DropCollectionMessage {
7+
if (typeof o === "string" || o instanceof String) {
8+
return Object.assign(new DropCollectionMessage(), JSON.parse(o.toString()));
9+
}
10+
return Object.assign(new DropCollectionMessage(), o);
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class ListCollectionsMessage implements IReplyMessage {
2+
public error: string;
3+
public jwt: string;
4+
public includehist: boolean;
5+
6+
public result: any[];
7+
static assign(o: any): ListCollectionsMessage {
8+
if (typeof o === "string" || o instanceof String) {
9+
return Object.assign(new ListCollectionsMessage(), JSON.parse(o.toString()));
10+
}
11+
return Object.assign(new ListCollectionsMessage(), o);
12+
}
13+
}

OpenFlow/src/Messages/Message.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import { SaveFileMessage } from "./SaveFileMessage";
3434
import { Readable, Stream } from "stream";
3535
import { GridFSBucket, ObjectID, Db } from "mongodb";
3636
import { GetFileMessage } from "./GetFileMessage";
37+
import { ListCollectionsMessage } from "./ListCollectionsMessage";
38+
import { DropCollectionMessage } from "./DropCollectionMessage";
3739
const safeObjectID = (s: string | number | ObjectID) => ObjectID.isValid(s) ? new ObjectID(s) : null;
3840
export class Message {
3941
public id: string;
@@ -89,6 +91,12 @@ export class Message {
8991
break;
9092
case "pong":
9193
break;
94+
case "listcollections":
95+
this.ListCollections(cli);
96+
break;
97+
case "dropcollection":
98+
this.DropCollection(cli);
99+
break;
92100
case "query":
93101
this.Query(cli);
94102
break;
@@ -242,14 +250,62 @@ export class Message {
242250
}
243251
private UnknownCommand(cli: WebSocketClient): void {
244252
this.Reply("error");
245-
this.data = "Unknown command";
253+
this.data = "Unknown command " + this.command;
246254
cli._logger.error(this.data);
247255
this.Send(cli);
248256
}
249257
private Ping(cli: WebSocketClient): void {
250258
this.Reply("pong");
251259
this.Send(cli);
252260
}
261+
private async ListCollections(cli: WebSocketClient): Promise<void> {
262+
this.Reply();
263+
var msg: ListCollectionsMessage
264+
try {
265+
msg = ListCollectionsMessage.assign(this.data);
266+
if (Util.IsNullEmpty(msg.jwt)) { msg.jwt = cli.jwt; }
267+
msg.result = await Config.db.ListCollections(msg.jwt);
268+
if (msg.includehist !== true) {
269+
msg.result = msg.result.filter(x => !x.name.endsWith("_hist"));
270+
}
271+
msg.result = msg.result.filter(x => x.name != "fs.chunks");
272+
msg.result = msg.result.filter(x => x.name != "fs.files");
273+
} catch (error) {
274+
cli._logger.error(error);
275+
if (Util.IsNullUndefinded(msg)) { (msg as any) = {}; }
276+
msg.error = error.toString();
277+
cli._logger.error(error);
278+
}
279+
try {
280+
this.data = JSON.stringify(msg);
281+
} catch (error) {
282+
this.data = "";
283+
cli._logger.error(error);
284+
}
285+
this.Send(cli);
286+
}
287+
private async DropCollection(cli: WebSocketClient): Promise<void> {
288+
this.Reply();
289+
var msg: DropCollectionMessage
290+
try {
291+
msg = DropCollectionMessage.assign(this.data);
292+
if (Util.IsNullEmpty(msg.jwt)) { msg.jwt = cli.jwt; }
293+
await Config.db.DropCollection(msg.collectionname, msg.jwt);
294+
} catch (error) {
295+
cli._logger.error(error);
296+
if (Util.IsNullUndefinded(msg)) { (msg as any) = {}; }
297+
msg.error = error.toString();
298+
cli._logger.error(error);
299+
}
300+
try {
301+
this.data = JSON.stringify(msg);
302+
} catch (error) {
303+
this.data = "";
304+
cli._logger.error(error);
305+
}
306+
this.Send(cli);
307+
}
308+
253309
private async Query(cli: WebSocketClient): Promise<void> {
254310
this.Reply();
255311
var msg: QueryMessage<Base>

OpenFlow/src/public/CommonControllers.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,19 @@ module openflow {
185185
this.$rootScope.$broadcast("signin", q);
186186
return q;
187187
}
188-
async Query(collection: string, query: any, projection: any = null, orderby: any = { _created: -1 }, top: number = 500, skip: number = 0): Promise<any[]> {
188+
async ListCollections(): Promise<any[]> {
189+
var q: ListCollectionsMessage = new ListCollectionsMessage();
190+
var msg: Message = new Message(); msg.command = "listcollections"; msg.data = JSON.stringify(q);
191+
q = await this.WebSocketClient.Send<ListCollectionsMessage>(msg);
192+
return q.result;
193+
}
194+
async DropCollection(collectionname: string): Promise<void> {
195+
var q: DropCollectionMessage = new DropCollectionMessage();
196+
q.collectionname = collectionname;
197+
var msg: Message = new Message(); msg.command = "dropcollection"; msg.data = JSON.stringify(q);
198+
q = await this.WebSocketClient.Send<DropCollectionMessage>(msg);
199+
}
200+
async Query(collection: string, query: any, projection: any = null, orderby: any = { _created: -1 }, top: number = 100, skip: number = 0): Promise<any[]> {
189201
var q: QueryMessage = new QueryMessage();
190202
q.collectionname = collection; q.query = query;
191203
q.query = JSON.stringify(query, (key, value) => {

OpenFlow/src/public/Controllers.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ module openflow {
369369
this.collection = "workflow_instances"
370370
this.basequery = { state: { $ne: "completed" }, form: { $exists: true } };
371371
WebSocketClient.onSignedin((_user: TokenUser) => {
372-
console.log(_user);
373372
this.loadData();
374373
});
375374
}
@@ -1022,6 +1021,7 @@ module openflow {
10221021
}
10231022
}
10241023
export class EntitiesCtrl extends entitiesCtrl<openflow.Base> {
1024+
public collections: any;
10251025
constructor(
10261026
public $scope: ng.IScope,
10271027
public $location: ng.ILocationService,
@@ -1036,10 +1036,23 @@ module openflow {
10361036
this.basequery = {};
10371037
this.collection = $routeParams.collection;
10381038
this.baseprojection = { _type: 1, type: 1, name: 1, _created: 1, _createdby: 1, _modified: 1 };
1039-
WebSocketClient.onSignedin((user: TokenUser) => {
1039+
WebSocketClient.onSignedin(async (user: TokenUser) => {
1040+
this.collections = await api.ListCollections();
10401041
this.loadData();
10411042
});
10421043
}
1044+
SelectCollection() {
1045+
// this.$location.path("/Entities/" + this.collection);
1046+
//this.$location.hash("#/Entities/" + this.collection);
1047+
// if (!this.$scope.$$phase) { this.$scope.$apply(); }
1048+
this.loadData();
1049+
}
1050+
async DropCollection() {
1051+
await this.api.DropCollection(this.collection);
1052+
this.collections = await this.api.ListCollections();
1053+
this.collection = "entities";
1054+
this.loadData();
1055+
}
10431056
async DeleteOne(model: any): Promise<any> {
10441057
this.loading = true;
10451058
await this.api.Delete(this.collection, model);
@@ -1049,6 +1062,7 @@ module openflow {
10491062
}
10501063
async DeleteMany(): Promise<void> {
10511064
this.loading = true;
1065+
if (!this.$scope.$$phase) { this.$scope.$apply(); }
10521066
var Promises: Promise<DeleteOneMessage>[] = [];
10531067
var q: DeleteOneMessage = new DeleteOneMessage();
10541068
this.models.forEach(model => {
@@ -1062,7 +1076,8 @@ module openflow {
10621076
values.forEach((x: DeleteOneMessage) => ids.push(x._id));
10631077
this.models = this.models.filter(function (m: any): boolean { return ids.indexOf(m._id) === -1; });
10641078
this.loading = false;
1065-
if (!this.$scope.$$phase) { this.$scope.$apply(); }
1079+
this.loadData();
1080+
// if (!this.$scope.$$phase) { this.$scope.$apply(); }
10661081
}
10671082
}
10681083

OpenFlow/src/public/Entities.html

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,11 @@ <h1 translate lib="web">entities</h1>
1616
<div class="col-md-6">
1717
<div className="btn-group-justified">
1818
<div class="btn-group" role="group">
19-
<a ng-href="#/Entities/entities" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
20-
lib="web">entities</a>
21-
<a ng-href="#/Entities/config" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
22-
lib="web">config</a>
23-
<a ng-href="#/Entities/nodered" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
24-
lib="web">nodered</a>
25-
<a ng-href="#/Entities/openrpa" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
26-
lib="web">openrpa</a>
27-
<a ng-href="#/Entities/users" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
28-
lib="web">users</a>
29-
<a ng-href="#/Entities/audit" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
30-
lib="web">audit</a>
31-
<a ng-href="#/Entities/workflow_instances" class="btn btn-info" ng-disabled="ctrl.loading==true" translate
32-
lib="web">wfinst</a>
19+
<select ng-model="ctrl.collection" ng-options="item.name as item.name for item in ctrl.collections"
20+
ng-change="ctrl.SelectCollection()">
21+
</select>
22+
<a href ng-click="ctrl.DropCollection()" class="btn btn-secondary" translate lib="web"
23+
ng-show="ctrl.WebSocketClient.user.username=='az'"" ng-disabled=" ctrl.loading==true">Drop</a>
3324
<a href ng-click="ctrl.DeleteMany()" class="btn btn-secondary" translate lib="web"
3425
ng-show="ctrl.WebSocketClient.user.username=='az'"" ng-disabled=" ctrl.loading==true">empty</a>
3526
</div>

OpenFlow/src/public/Message.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ module openflow {
6060
return Object.assign(new SigninMessage(), o);
6161
}
6262
}
63+
export class ListCollectionsMessage {
64+
public error: string;
65+
public jwt: string;
66+
public result: any;
67+
}
68+
export class DropCollectionMessage {
69+
public error: string;
70+
public jwt: string;
71+
public collectionname: string;
72+
}
6373
export class QueryMessage {
6474
public error: string;
6575

@@ -308,7 +318,9 @@ module openflow {
308318
break;
309319
}
310320
} catch (error) {
321+
console.log(this);
311322
console.error(error);
323+
312324
}
313325
}
314326
public async Send(cli: WebSocketClient): Promise<void> {

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.318
1+
0.0.319

0 commit comments

Comments
 (0)