Skip to content

Commit

Permalink
Fix 73 consolidate search (#313)
Browse files Browse the repository at this point in the history
* Promisify all search methods and perform all searches inside local banidb module

* Update viewer.js to use local banidb module
ES6-ify functions for scope consistency

* fix #73
  • Loading branch information
navdeepsinghkhalsa authored Sep 13, 2018
1 parent cd15b84 commit b3fe328
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 145 deletions.
File renamed without changes.
124 changes: 70 additions & 54 deletions www/js/search-database.js → www/js/banidb/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const Realm = require('realm');

const realmDB = require('./realm-db');
const CONSTS = require('./constants');

module.exports = {
search(searchQuery, searchType, searchSource) {
const query = (searchQuery, searchType, searchSource) => (
new Promise((resolve, reject) => {
let dbQuery = '';
let searchCol = '';
let condition = '';
Expand All @@ -13,6 +12,7 @@ module.exports = {
// default source for ang search to GURU_GRANTH_SAHIB
let angSearchSourceId = CONSTS.SOURCE_TYPES.GURU_GRANTH_SAHIB;
const order = [];
let howManyRows = 20;
switch (searchType) {
case CONSTS.SEARCH_TYPES.FIRST_LETTERS: // First letter start
case CONSTS.SEARCH_TYPES.FIRST_LETTERS_ANYWHERE: { // First letter anywhere
Expand Down Expand Up @@ -55,6 +55,7 @@ module.exports = {
}
case CONSTS.SEARCH_TYPES.ANG: // Ang
searchCol = 'PageNo';
howManyRows = 1000;
dbQuery = parseInt(saniQuery, 10);
condition = `${searchCol} = ${dbQuery}`;

Expand All @@ -75,33 +76,38 @@ module.exports = {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered(condition);
global.core.search.printResults(rows.slice(0, 20));
});
},
resolve(rows.slice(0, howManyRows));
})
.catch(reject);
})
);

loadShabad(ShabadID, LineID) {
const loadShabad = ShabadID => (
new Promise((resolve, reject) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID);
if (rows.length > 0) {
global.core.search.printShabad(rows, ShabadID, LineID || rows[0].ID);
resolve(rows);
}
});
},
})
.catch(reject);
})
);

getAng(ShabadID) {
return new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const row = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID)[0];
const { PageNo, SourceID } = row;
resolve({
PageNo,
SourceID,
});
const getAng = ShabadID => (
new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const row = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID)[0];
const { PageNo, SourceID } = row;
resolve({
PageNo,
SourceID,
});
});
},
});
})
);

/**
* Retrieve all lines from a page
Expand All @@ -115,19 +121,19 @@ module.exports = {
* loadAng(1);
* // => [{ Gurmukhi: 'jo gurisK guru syvdy sy puMn prwxI ]', ID: 31057 },...]
*/
loadAng(PageNo, SourceID = 'G') {
return new Promise((resolve, reject) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered('PageNo = $0 AND Source.SourceID = $1', PageNo, SourceID);
if (rows.length > 0) {
resolve(rows);
} else {
reject();
}
});
});
},
const loadAng = (PageNo, SourceID = 'G') => (
new Promise((resolve, reject) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered('PageNo = $0 AND Source.SourceID = $1', PageNo, SourceID);
if (rows.length > 0) {
resolve(rows);
} else {
reject();
}
});
})
);

/**
* Retrieve Shabad for Verse
Expand All @@ -140,15 +146,15 @@ module.exports = {
* getShabad(1);
* // => 1
*/
getShabad(VerseID) {
return new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const shabad = realm.objects('Verse').filtered('ID = $0', VerseID)[0];
resolve(shabad.Shabads[0].ShabadID);
});
});
},
const getShabad = VerseID => (
new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const shabad = realm.objects('Verse').filtered('ID = $0', VerseID)[0];
resolve(shabad.Shabads[0].ShabadID);
});
})
);

/**
* Retrieve a random Shabad from a source
Expand All @@ -161,14 +167,24 @@ module.exports = {
* randomShabad();
* // => 13
*/
randomShabad(SourceID = 'G') {
return new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered('Source.SourceID = $0', SourceID);
const row = rows[Math.floor(Math.random() * rows.length)];
resolve(row.Shabads[0].ShabadID);
});
});
},
const randomShabad = (SourceID = 'G') => (
new Promise((resolve) => {
Realm.open(realmDB.realmVerseSchema)
.then((realm) => {
const rows = realm.objects('Verse').filtered('Source.SourceID = $0', SourceID);
const row = rows[Math.floor(Math.random() * rows.length)];
resolve(row.Shabads[0].ShabadID);
});
})
);

// Re-export CONSTS for use in other areas
module.exports = {
CONSTS,
query,
loadShabad,
getAng,
loadAng,
getShabad,
randomShabad,
};
File renamed without changes.
3 changes: 0 additions & 3 deletions www/js/desktop_scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const path = require('path');
const request = require('request');
const progress = require('request-progress');

const search = require('./search-database');

const { remote } = electron;
const ipc = electron.ipcRenderer;
const userDataPath = remote.app.getPath('userData');
Expand Down Expand Up @@ -60,7 +58,6 @@ function checkForNotifcations() {

module.exports = {
ipc,
search,
store,

init() {
Expand Down
8 changes: 6 additions & 2 deletions www/js/menu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { randomShabad } = require('./banidb');

const h = require('hyperscript');
const settings = require('./settings');
const getJSON = require('get-json');
Expand Down Expand Up @@ -133,7 +135,7 @@ const randomShabadButton = h(
'a.random-shabad-button',
{
onclick: () => {
global.platform.search.randomShabad()
randomShabad()
.then(goToShabadPage);
} },
h('i.fa.fa-random.list-icon'),
Expand Down Expand Up @@ -163,7 +165,9 @@ const hukamnamaButton = h(
{
onclick: () => {
getJSON('https://api.banidb.com/hukamnama/today', (error, response) => {
goToShabadPage(response.shabadinfo.id);
if (!error) {
goToShabadPage(response.shabadinfo.id);
}
});
},
},
Expand Down
28 changes: 17 additions & 11 deletions www/js/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable arrow-parens */
const { CONSTS, query, getAng, loadAng, getShabad, loadShabad } = require('./banidb');

// Gurmukhi keyboard layout file
const keyboardLayout = require('./keyboard.json');
Expand All @@ -9,8 +10,6 @@ const h = require('hyperscript');

const { store } = require('electron').remote.require('./app');

const CONSTS = require('./constants.js');

// the non-character keys that will register as a keypress when searching
const allowedKeys = [
8, // Backspace
Expand Down Expand Up @@ -472,7 +471,8 @@ module.exports = {
searchQuery = this.$search.value;
}
if (searchQuery.length >= 1) {
global.platform.search.search(searchQuery, searchType, this.searchSource);
query(searchQuery, searchType, this.searchSource)
.then(rows => this.printResults(rows));
} else {
this.$results.innerHTML = '';
}
Expand Down Expand Up @@ -570,21 +570,20 @@ module.exports = {
$shabadList.innerHTML = '';
currentShabad.splice(0, currentShabad.length);
if (apv) {
global.platform.search
.getAng(ShabadID)
getAng(ShabadID)
.then(ang => {
currentMeta = ang;
return global.platform.search.loadAng(ang.PageNo, ang.SourceID);
return loadAng(ang.PageNo, ang.SourceID);
})
.then(rows => this.printShabad(rows, ShabadID, LineID));
} else {
global.platform.search.loadShabad(ShabadID, LineID);
loadShabad(ShabadID, LineID)
.then(rows => this.printShabad(rows, ShabadID, LineID));
}
},

loadAng(PageNo, SourceID) {
global.platform.search
.loadAng(PageNo, SourceID)
loadAng(PageNo, SourceID)
.then(rows => this.printShabad(rows));
},

Expand All @@ -597,8 +596,15 @@ module.exports = {
const PreviousVerseID = FirstLine === 1 ? FirstLine : FirstLine - 1;
const NextVerseID = LastLine === 60403 ? LastLine : LastLine + 1;
const adjacentVerseID = Forward ? NextVerseID : PreviousVerseID;
global.platform.search.getShabad(adjacentVerseID)
.then(global.platform.search.loadShabad);
let adjacentShabadID;
getShabad(adjacentVerseID)
.then(ShabadID => {
adjacentShabadID = ShabadID;
return loadShabad(ShabadID);
})
.then((rows) => {
this.printShabad(rows, adjacentShabadID);
});
},

printShabad(rows, ShabadID, LineID) {
Expand Down
Loading

0 comments on commit b3fe328

Please sign in to comment.