@@ -34,6 +34,8 @@ import { SaveFileMessage } from "./SaveFileMessage";
3434import { Readable , Stream } from "stream" ;
3535import { GridFSBucket , ObjectID , Db } from "mongodb" ;
3636import { GetFileMessage } from "./GetFileMessage" ;
37+ import { ListCollectionsMessage } from "./ListCollectionsMessage" ;
38+ import { DropCollectionMessage } from "./DropCollectionMessage" ;
3739const safeObjectID = ( s : string | number | ObjectID ) => ObjectID . isValid ( s ) ? new ObjectID ( s ) : null ;
3840export 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 >
0 commit comments