@@ -17,19 +17,44 @@ class DataStore {
17
17
this . init ( )
18
18
}
19
19
20
- init ( ) {
21
- this . snippets = new Store ( {
22
- autoload : true ,
23
- filename : path . join ( this . _path , '/snippets.db' )
24
- } )
25
- this . tags = new Store ( {
26
- autoload : true ,
27
- filename : path . join ( this . _path , '/tags.db' )
20
+ async init ( ) {
21
+ this . createDB ( 'masscode' , false )
22
+ this . createDB ( 'snippets' )
23
+ this . createDB ( 'tags' )
24
+
25
+ this . masscode . loadDatabase ( err => {
26
+ if ( err ) throw err
27
+
28
+ const defaultFolder = {
29
+ list : [
30
+ {
31
+ id : shortid ( ) ,
32
+ name : 'Default' ,
33
+ open : false ,
34
+ defaultLanguage : 'text'
35
+ }
36
+ ] ,
37
+ _id : 'folders'
38
+ }
39
+ this . masscode . insert ( defaultFolder )
28
40
} )
29
- this . masscode = new Store ( {
30
- autoload : true ,
31
- filename : path . join ( this . _path , '/masscode.db' )
41
+
42
+ await this . createBackupDir ( )
43
+ this . autoBackup ( )
44
+ }
45
+
46
+ async createDB ( name , autoload = true ) {
47
+ this [ name ] = new Store ( {
48
+ autoload,
49
+ filename : path . join ( this . _path , `/${ name } .db` ) ,
50
+ onload : err => {
51
+ if ( err ) {
52
+ this . createDB ( name )
53
+ console . log ( `db ${ name } is restarted` )
54
+ }
55
+ }
32
56
} )
57
+ console . log ( `db ${ name } is created` )
33
58
}
34
59
35
60
updatePath ( ) {
@@ -72,6 +97,10 @@ class DataStore {
72
97
this . tags . persistence . compactDatafile ( )
73
98
}
74
99
100
+ async createBackupDir ( ) {
101
+ await fs . ensureDir ( this . _backupPath )
102
+ }
103
+
75
104
createBackupDirByDate ( date ) {
76
105
date = date || new Date ( )
77
106
const backupFolderDatePattern = 'yyyy-MM-dd_HH-mm-ss'
@@ -216,21 +245,4 @@ class DataStore {
216
245
}
217
246
}
218
247
219
- const db = new DataStore ( )
220
-
221
- const defaultFolder = {
222
- list : [
223
- {
224
- id : shortid ( ) ,
225
- name : 'Default' ,
226
- open : false ,
227
- defaultLanguage : 'text'
228
- }
229
- ] ,
230
- _id : 'folders'
231
- }
232
-
233
- db . masscode . insert ( defaultFolder )
234
- db . autoBackup ( )
235
-
236
- export default db
248
+ export default new DataStore ( )
0 commit comments