Created
April 18, 2018 08:57
-
-
Save kigiri/bf4d8807c84c450a9e540516b3d16052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DB_PATH = process.env.DB_PATH || './db.json' | |
const SAVE_INTERVAL = process.env.SAVE_INTERVAL || 60*1000 | |
const db = require(DB_PATH) | |
const fs = require('fs') | |
const saveDB = () => { | |
const start = Date.now() | |
try { | |
fs.writeFile(DB_PATH, JSON.stringify(db), 'utf8', err => { | |
if (err) { | |
console.error('Unable to save database', err) | |
process.exit(1) | |
} | |
console.log('DB Saved in', ((Date.now() - start) / 1000).toFixed(2), ' seconds') | |
}) | |
} catch (err) { | |
console.error('Unable to save database', err) | |
process.exit(1) | |
} | |
} | |
setInterval(saveDB, SAVE_INTERVAL) | |
process.on('exit', saveDB) | |
module.exports = db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment