Skip to content

Commit 69d1f57

Browse files
refactor: create and init db #72 #83 (#90)
1 parent 8ad79aa commit 69d1f57

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

src/renderer/datastore.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,44 @@ class DataStore {
1717
this.init()
1818
}
1919

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)
2840
})
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+
}
3256
})
57+
console.log(`db ${name} is created`)
3358
}
3459

3560
updatePath () {
@@ -72,6 +97,10 @@ class DataStore {
7297
this.tags.persistence.compactDatafile()
7398
}
7499

100+
async createBackupDir () {
101+
await fs.ensureDir(this._backupPath)
102+
}
103+
75104
createBackupDirByDate (date) {
76105
date = date || new Date()
77106
const backupFolderDatePattern = 'yyyy-MM-dd_HH-mm-ss'
@@ -216,21 +245,4 @@ class DataStore {
216245
}
217246
}
218247

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

Comments
 (0)