initial commit
This commit is contained in:
33
server/getDatabase.js
Normal file
33
server/getDatabase.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import knex from 'knex';
|
||||
import config from "../config.js";
|
||||
|
||||
let database = null;
|
||||
let lastFailed = null;
|
||||
|
||||
/**
|
||||
* @return {Knex}
|
||||
*/
|
||||
export default function getDatabase() {
|
||||
if (database) {
|
||||
return database;
|
||||
}
|
||||
|
||||
if (lastFailed && (new Date().getTime() - lastFailed.getTime() < 60000)) {
|
||||
// attempt to reconnect every 1 minute, not more frequently
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('connecting db');
|
||||
database = knex({
|
||||
...config.database,
|
||||
});
|
||||
lastFailed = null;
|
||||
return database;
|
||||
} catch (e) {
|
||||
console.error("Cannot connect to database". e);
|
||||
database = null;
|
||||
lastFailed = new Date();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user