Skip to content

Commit

Permalink
feat(build): add preferred sources support
Browse files Browse the repository at this point in the history
  • Loading branch information
sarabveer committed Jun 25, 2023
1 parent 5ff290e commit ba0dc8e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
28 changes: 24 additions & 4 deletions lib/build-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const importTranslationSources = async trx => {
* Imports lines, shabads, and translations.
* @param {Object} trx The transaction to use when executing any queries.
*/
const importLines = async trx => {
const importLines = async ( trx, sourcesFallback ) => {
console.log( '\nImporting tables lines, shabads, translations'.subheader )

// Using camelCase since reading from database, not JSON
Expand Down Expand Up @@ -243,6 +243,11 @@ const importLines = async trx => {

if ( !existsSync( compositionDir ) ) { return [] }

const preferredSources = sourcesFallback ? sourcesFallback[ compositionName ] : null
if ( sourcesFallback && preferredSources == null ) {
throw new ReferenceError( `No preferred source found for ${compositionName}` )
}

// Load and concatenate all the shabad files for the composition
const paths = readdirSync( compositionDir ).map( path => `${JSON_PATH}/${compositionName}/${path}` )

Expand Down Expand Up @@ -328,10 +333,24 @@ const importLines = async trx => {
lineData.push( omit( line, 'gurmukhi' ) )
translationData.push( ...translations )

// Only insert preferred sources if defined
let gurmukhiObj = gurmukhi
if ( preferredSources ) {
gurmukhiObj = preferredSources
.reduce( ( gurmukhiLine, source ) => {
if ( gurmukhiLine ) {
return gurmukhiLine
}
if ( source in gurmukhi ) {
return { [ source ]: gurmukhi[ source ] }
}
return null
}, null )
}

// Add each sources's gurmukhi + transliterations
Object.entries( gurmukhi ).forEach( ( [ sourceName, gurmukhi ] ) => {
Object.entries( gurmukhiObj ).forEach( ( [ sourceName, gurmukhi ] ) => {
const sourceId = sourceNames.findIndex( nameEnglish => sourceName === nameEnglish ) + 1

const getFirstLetters = text => ( {
first_letters: [
toUnicode,
Expand Down Expand Up @@ -464,6 +483,7 @@ const importBanis = async trx => {

const main = async ( {
type = '?',
sourcesFallback = null,
beforeInitialise = () => {},
onInitialise = () => {},
afterBuild = () => {},
Expand All @@ -478,7 +498,7 @@ const main = async ( {
await importSimpleTables( knex )
await importCompositions( knex )
await importTranslationSources( knex )
await importLines( knex )
await importLines( knex, sourcesFallback )
await importBanis( knex )

await afterBuild( knex )
Expand Down
38 changes: 25 additions & 13 deletions lib/build-gutka.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ const { filename } = require( '..' )
const fs = require( 'fs' )

const OUTPUT_PATH = './build'
const backupFile = `${OUTPUT_PATH}/database.bak.sqlite`
const outputFile = `${OUTPUT_PATH}/gutka-db.sqlite`

// Gurmukhi sources with fallback ordering
const GURMUKHI_SOURCES = {
Ardaas: [ 'SGPC' ],
'Ganj Nama Bhai Nand Lal Ji': [ 'Master Jaswant Singh', 'Dr. Ganda Singh' ],
'Ghazals Bhai Nand Lal Ji': [ 'Dr. Ganda Singh' ],
'Jot Bigas Bhai Nand Lal Ji': [ 'Dr. Ganda Singh' ],
'Kabit Savaiye Bhai Gurdas Ji': [ 'Seva Singh' ],
'Sarabloh Granth': [ 'Amrit Keertan' ],
'Sri Dasam Granth': [ 'GurbaniNow', 'SGPC', 'Piara Singh Padam', 'Budha Dal Mehron', 'Dr. Rattan Singh Jaggi' ],
'Sri Guru Granth Sahib Ji': [ 'GurbaniNow', 'Bhai Sahib Randhir Singh', 'SGPC' ],
'Vaaran Bhai Gurdas Ji': [ 'Amrit Keertan', 'SGPC' ],
'Zindagi Nama Bhai Nand Lal Ji': [ 'Dr. Ganda Singh' ],
Rehitname: [],
Uggardanti: [],
}

/**
* Sets up SQLite pragma settings.
*/
Expand Down Expand Up @@ -34,16 +49,12 @@ const beforeInitialise = async () => {
const afterBuild = async knex => {
console.log( '\nBuilding gutka database...'.subheader )

// Backup built DB
if ( fs.existsSync( filename ) ) {
fs.copyFileSync( filename, backupFile )
}

// Drop/remove un-needed data
await knex.schema.alterTable( 'line_content', table => {
table.dropColumn( 'vishraam_first_letters' )
table.dropColumn( 'first_letters' )
table.dropForeign( 'source_id' )
table.dropColumn( 'source_id' )
table.primary( 'line_id' )
} )
await knex.schema.alterTable( 'lines', table => {
table.dropColumn( 'pronunciation_information' )
Expand Down Expand Up @@ -74,11 +85,12 @@ const afterBuild = async knex => {
if ( fs.existsSync( filename ) ) {
fs.renameSync( filename, outputFile )
}

// Restore original db
if ( fs.existsSync( backupFile ) ) {
fs.renameSync( backupFile, filename )
}
}

build( { onInitialise, beforeInitialise, afterBuild, type: 'SQLite' } )
build( {
onInitialise,
beforeInitialise,
afterBuild,
sourcesFallback: GURMUKHI_SOURCES,
type: 'SQLite',
} )

0 comments on commit ba0dc8e

Please sign in to comment.