File size: 1,360 Bytes
597ee18 | 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 | import fs from 'fs'
import loki from 'lokijs'
const init = () => {
const userHome = process.env.APPDATA || process.env.HOME
const projectDbFileName = '.stringerdb'
const dbFile = `${userHome}/${projectDbFileName}`
console.log('Initializing the database in ', dbFile)
if (!fs.existsSync(dbFile)) {
console.log('Database did NOT exist, creating it now')
fs.writeFile(dbFile, '', (err) => {
if (err) {
console.error('Error creating the empty database file', err)
}
})
while (true) {
if (fs.existsSync(dbFile)) {
console.log('Database file found at', dbFile)
break
}
console.log('Waiting for the blank db file to be created...')
}
}
let db = new loki(dbFile, {
autoload: true,
autoloadCallback: onDbAutoload,
autosave: true,
autosaveInterval: 125,
verbose: process.env.ENV === 'development'
})
function onDbAutoload() {
console.log('Database loaded!')
initProjectsCollection(db)
}
return db
}
const initProjectsCollection = (db) => {
let projectsCollection = db.getCollection('projects')
if (!projectsCollection) {
console.log('[projects] collection did not exist, creating it now')
projectsCollection = db.addCollection('projects')
}
return projectsCollection
}
export function initDatabase() {
return init()
}
|