actual-server/account-db.js

29 lines
691 B
JavaScript
Raw Normal View History

2022-04-29 14:48:58 +00:00
let fs = require('fs');
2022-03-31 17:19:08 +00:00
let { join } = require('path');
let { openDatabase } = require('./db');
2022-04-29 14:48:58 +00:00
let config = require('./load-config');
2022-03-31 17:19:08 +00:00
let accountDb = null;
function getAccountDb() {
if (accountDb == null) {
2022-04-29 15:10:20 +00:00
if (!fs.existsSync(config.serverFiles)) {
console.log('MAKING SERVER DIR');
fs.mkdirSync(config.serverFiles);
}
2022-04-29 14:48:58 +00:00
let dbPath = join(config.serverFiles, 'account.sqlite');
let needsInit = !fs.existsSync(dbPath);
accountDb = openDatabase(dbPath);
if (needsInit) {
let initSql = fs.readFileSync(join(__dirname, 'sql/account.sql'), 'utf8');
accountDb.exec(initSql);
}
2022-03-31 17:19:08 +00:00
}
return accountDb;
}
module.exports = { getAccountDb };