Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()