diff --git a/src/renderer/datastore.js b/src/renderer/datastore.js index 0d805d1..359a086 100644 --- a/src/renderer/datastore.js +++ b/src/renderer/datastore.js @@ -17,19 +17,44 @@ class DataStore { this.init() } - init () { - this.snippets = new Store({ - autoload: true, - filename: path.join(this._path, '/snippets.db') - }) - this.tags = new Store({ - autoload: true, - filename: path.join(this._path, '/tags.db') + async init () { + this.createDB('masscode', false) + this.createDB('snippets') + this.createDB('tags') + + this.masscode.loadDatabase(err => { + if (err) throw err + + const defaultFolder = { + list: [ + { + id: shortid(), + name: 'Default', + open: false, + defaultLanguage: 'text' + } + ], + _id: 'folders' + } + this.masscode.insert(defaultFolder) }) - this.masscode = new Store({ - autoload: true, - filename: path.join(this._path, '/masscode.db') + + await this.createBackupDir() + this.autoBackup() + } + + async createDB (name, autoload = true) { + this[name] = new Store({ + autoload, + filename: path.join(this._path, `/${name}.db`), + onload: err => { + if (err) { + this.createDB(name) + console.log(`db ${name} is restarted`) + } + } }) + console.log(`db ${name} is created`) } updatePath () { @@ -72,6 +97,10 @@ class DataStore { this.tags.persistence.compactDatafile() } + async createBackupDir () { + await fs.ensureDir(this._backupPath) + } + createBackupDirByDate (date) { date = date || new Date() const backupFolderDatePattern = 'yyyy-MM-dd_HH-mm-ss' @@ -216,21 +245,4 @@ class DataStore { } } -const db = new DataStore() - -const defaultFolder = { - list: [ - { - id: shortid(), - name: 'Default', - open: false, - defaultLanguage: 'text' - } - ], - _id: 'folders' -} - -db.masscode.insert(defaultFolder) -db.autoBackup() - -export default db +export default new DataStore()