Skip to content

Commit

Permalink
Switch database from PouchDB to NeDB (also persist Slack tokens)
Browse files Browse the repository at this point in the history
This was because PounchDB required the database code to be run in the render threads, which was annoying. NeDB allows running from the main thread or the render threads. Most of the time, I want it to run from the main thread.

Resolves #52

Also, whilst testing NeDB - we're also storing the Slack authtokens. Note that at this stage, they're being stored in plain text. Need to look into the best way of storing them. Issue #16 related to Slack integration.
  • Loading branch information
dracan committed Feb 4, 2018
1 parent 16ed4cd commit 179f460
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 1,100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ src/dist/
tools/
tomatoad.*.nupkg
!Chocolatey/tools/
src/db/
31 changes: 23 additions & 8 deletions src/database/database.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-adapter-idb'))
var Datastore = require('nedb')

var db = new PouchDB('tomatoad_test', { adapter: 'idb' })
const dbSlack = new Datastore({ filename: __dirname + '/../db/slack.json', autoload: true });

dbSlack.persistence.compactDatafile()

module.exports = {
saveNewPomodoro: function (callback) {
/*
var pomodoro = {
_id: new Date().toISOString(),
completed: false
}
db.put(pomodoro, function callback(err, result) {
if (!err) {
console.log('Successfully saved a pomodoro!');
}
})
db.insert(pomodoro, function (err, newDoc) { // Callback is optional
// newDoc is the newly inserted document, including its _id
// newDoc has no key called notToBeSaved since its value was undefined
});
*/
},

saveSlackAuthTokens(authTokens) {
dbSlack.update({
_id: 'slackAuthTokens'
}, { _id: 'slackAuthTokens', data: authTokens }, { upsert: true }, function (err, numReplaced, upsert) {
});
},

loadSlackAuthTokens(callback) {
dbSlack.find({ _id: 'slackAuthTokens' }, function (err, docs) {
callback(docs.length == 0 ? {} : docs[0].data)
});
}
}
11 changes: 0 additions & 11 deletions src/database/dbcontext.html

This file was deleted.

8 changes: 0 additions & 8 deletions src/database/dbcontext.js

This file was deleted.

28 changes: 2 additions & 26 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let breakLengthSeconds = 300 // 5 minutes
let trayIcon
let overlayWindow
let aboutWindow
let dbContextWindow

function createSystemTrayIcon() {
trayIcon = new Tray(path.join(__dirname, 'images', 'tomato.ico'))
Expand Down Expand Up @@ -129,7 +128,7 @@ function createSlackWindow() {

slackWindow.webContents.on('did-get-response-details', function (event, status, newURL, originalURL, httpResponseCode, requestMethod, referrer, headers, resourceType) {
if(newURL.indexOf("https://tomatoadauth.azurewebsites.net/api/slackauth") !== -1) {
slack.setAuthCode(headers['x-auth-code']);
slack.setAuthCode(headers['x-team-name'][0], headers['x-auth-code'][0]);
}
});

Expand All @@ -142,37 +141,14 @@ function createSlackWindow() {
})
}

// This creates a dummy window, so we can use IndexedDB in the render process (unfortunately doesn't work in the main process)
function createDatabaseContextWindow() {
dbContextWindow = new BrowserWindow({ width: 0, height: 0, frame: false })

dbContextWindow.hide();
dbContextWindow.setSkipTaskbar(true);

// For dev
//dbContextWindow.maximize()
//dbContextWindow.webContents.openDevTools()

dbContextWindow.loadURL(url.format({
pathname: path.join(__dirname, 'database', 'dbcontext.html'),
protocol: 'file:',
slashes: true,
}))

dbContextWindow.on('closed', function () {
dbContextWindow = null
})
}

app.on('ready', function() {
createDatabaseContextWindow();
slack.loadAuthTokens()
createSystemTrayIcon();
createOverlayWindow();
})

broadcastEvent = function(eventName, arg) {
overlayWindow && overlayWindow.webContents.send(eventName, arg)
dbContextWindow && dbContextWindow.webContents.send(eventName, arg)
}

ipcMain.on('pomodoro-start', (evt) => {
Expand Down
Loading

0 comments on commit 179f460

Please sign in to comment.