@@ -28,10 +28,12 @@ class Database {
2828 /**
2929 * The SQLite3 database.
3030 */
31- Object . defineProperty ( this , "_database" , {
32- value : options . database || new SQLite ( name , options ) ,
33- writable : false ,
34- enumerable : true
31+ Object . defineProperties ( this , {
32+ _database : {
33+ value : options . database || new SQLite ( name , options ) ,
34+ writable : true ,
35+ enumerable : false
36+ } ,
3537 } ) ;
3638
3739 this . prepareTable ( ) ;
@@ -261,9 +263,9 @@ class Database {
261263 try { data = JSON . parse ( data ) } catch { }
262264
263265 if ( target && typeof data === "object" ) {
264- data = lodash . unset ( data , target ) ;
266+ const r = lodash . unset ( data , target ) ;
267+ if ( ! r ) return false ;
265268 data = JSON . stringify ( data ) ;
266-
267269 this . database . prepare ( `UPDATE ${ table } SET json = (?) WHERE ID = (?)` ) . run ( data , id ) ;
268270
269271 return true ;
@@ -554,6 +556,49 @@ class Database {
554556 tables : Object . values ( data ) . map ( m => m . name )
555557 } ;
556558 }
559+
560+ /**
561+ * Exports this db
562+ * @param {object } options Export options
563+ */
564+ export ( options = { stringify : false , format : false , tableName : null , allTable : false } ) {
565+ let data = {
566+ data : options . allTable ? this . allTableArray ( ) : this . all ( { table : options . tableName || this . tableName } ) ,
567+ mod : "quick.db" ,
568+ generatedTimestamp : new Date ( ) . getTime ( )
569+ } ;
570+
571+ if ( options . stringify ) data = JSON . stringify ( data , null , options . format ? ( typeof options . format === "number" ? options . format : "\t" ) : null ) ;
572+
573+ return data ;
574+ }
575+
576+ use ( database ) {
577+ if ( ! database ) throw new Error ( "Database was not provided!" ) ;
578+ if ( database . prototype instanceof Database || database instanceof Database ) this . _database = database . _database ;
579+ if ( database instanceof SQLite || database . prototype instanceof SQLite || database instanceof this . _database || database . prototype instanceof this . _database ) this . _database = database ;
580+
581+ throw new Error ( "Invalid database" ) ;
582+ }
583+
584+ /**
585+ * Returns all table(s) as array.
586+ */
587+ allTableArray ( ) {
588+ const { tables } = this . tables ( ) ;
589+ let arr = [ ] ;
590+
591+ tables . forEach ( ( table , index ) => {
592+ arr . push ( {
593+ id : index ,
594+ table : table ,
595+ data : this . all ( { table : table } )
596+ } )
597+ } )
598+
599+ return arr ;
600+ }
601+
557602}
558603
559604module . exports = Database ;
0 commit comments