Skip to content

Commit

Permalink
refactor: create and init db #72 #83 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov authored Apr 4, 2020
1 parent 8ad79aa commit 69d1f57
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions src/renderer/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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()

0 comments on commit 69d1f57

Please sign in to comment.