Created
February 18, 2020 10:46
-
-
Save soulgalore/87dde2a729baa7afe131392a4c807e9c 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
'use strict'; | |
const Database = require('better-sqlite3'); | |
// keep the name in package.json so its easy just to chanfe | |
const DATABASE_NAME= require('./package').database_name; | |
function getQ(domains) { | |
const q = []; | |
for(let domain of domains){ | |
q.push('?') | |
} | |
return q.join(','); | |
} | |
function getDatabase() { | |
return new Database(`./data/${DATABASE_NAME}`, { readonly: true, fileMustExist: true }); | |
} | |
module.exports = { | |
get(domain) { | |
const db = getDatabase(); | |
try { | |
const stmt = db.prepare('SELECT * FROM green_presenting WHERE url = ?'); | |
return stmt.get(domain); | |
} finally { | |
if (db) { | |
db.close(); | |
} | |
} | |
}, | |
getDomains(domains) { | |
const db = getDatabase(); | |
try { | |
const stmt = db.prepare(`SELECT * FROM green_presenting WHERE url in (${getQ(domains)})`); | |
return stmt.all(domains); | |
} finally { | |
if (db) { | |
db.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment