forked from shabados/viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
95 lines (81 loc) · 2.99 KB
/
Copy pathdb.js
File metadata and controls
95 lines (81 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { readJSON, remove, move } from 'fs-extra'
import { manifest, extract } from 'pacote'
import importFresh from 'import-fresh'
import { Lines, Sources, knex } from '@shabados/database'
import { dependencies } from './package.json'
const databasePackage = `@shabados/database@${dependencies[ '@shabados/database' ]}`
// Check every 5 minutes for updates
const UPDATE_CHECK_INTERVAL = 5 * 60 * 1000
const UPDATE_TMP_FOLDER = 'temp'
/**
* Gets all the DB sources.
*/
export const getSources = () => Sources.query()
/**
* Get all the lines on a page for a source.
* @param {number} sourceId The ID of the source to use.
* @param {number} page The page in the source to retrieve all lines from.
*/
export const getLinesOnPage = ( sourceId, page ) => Lines
.query()
.join( 'shabads', 'shabads.id', 'lines.shabad_id' )
.where( 'source_page', page )
.andWhere( 'source_id', sourceId )
.orderBy( 'order_id' )
/**
* Determines whether the database is the latest version, according to semver.
* @async
* @returns {boolean} Whether or not the latest database is installed.
*/
export const isLatestDatabase = async () => {
// Read package.json database semver and database package file
const [ remotePackage, localPackage ] = await Promise.all( [
manifest( databasePackage ),
readJSON( 'node_modules/@shabados/database/package.json', 'utf-8' ),
] )
console.log( 'Local Database Version:', localPackage.version )
console.log( 'Remote Database Version:', remotePackage.version )
return localPackage.version === remotePackage.version
}
/**
* Downloads the latest version of the database, according to semver.
* Hot-reloads the data only.
* ! Code will not be hot-reloaded, and code updates require a restart.
* @async
*/
export const updateDatabase = async () => {
// Download and extract the database package from npm
console.log( `Downloading database update to ${UPDATE_TMP_FOLDER}` )
await remove( UPDATE_TMP_FOLDER )
await extract( databasePackage, UPDATE_TMP_FOLDER )
console.log( 'Hot-patching database module' )
// Disconnect the Shabad OS database connection
await knex.destroy()
// Move across the updated npm database module
await move( UPDATE_TMP_FOLDER, 'node_modules/@shabados/database', { overwrite: true } )
// Reimport the database
//! Relies on knex being reinitialised globally
importFresh( '@shabados/database' )
}
/**
* Checks for database updates, according to semver, and updates if there are.
* @async
*/
export const checkUpdates = async () => {
console.log( `Checking for database updates satisfying ${databasePackage}` )
// Exit if there aren't any updates
if ( await isLatestDatabase() ) {
console.log( 'No database updates available' )
return
}
await updateDatabase()
console.log( 'Database successfully updated' )
}
/**
* Provides a recursive update checking function.
* Checks for updates at constant interval.
*/
export const updateLoop = async () => {
await checkUpdates()
setTimeout( updateLoop, UPDATE_CHECK_INTERVAL )
}