54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
/*
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
* This file is part of appreports which is released under GPL-3.0-or-later.
|
|
* See file 'LICENSE' for full license details.
|
|
* Created by Timo Hocker <timo@scode.ovh>, August 2020
|
|
*/
|
|
|
|
/* eslint-disable no-sync */
|
|
|
|
'use strict';
|
|
|
|
const knex = require ('knex');
|
|
const fs = require ('fs');
|
|
const job = require ('./job');
|
|
|
|
let db = null;
|
|
|
|
async function init (use_fake_seed) {
|
|
if (!fs.existsSync ('data'))
|
|
fs.mkdirSync ('data');
|
|
|
|
if (use_fake_seed && fs.existsSync ('data/db.sqlite')) {
|
|
fs.unlinkSync ('data/db.sqlite');
|
|
fs.copyFileSync ('data/test.sqlite', 'data/db.sqlite');
|
|
}
|
|
|
|
db = knex ({
|
|
client: 'sqlite',
|
|
connection: { filename: 'data/db.sqlite' },
|
|
migrations: { directory: 'migrations' },
|
|
seeds: { directory: 'seeds' },
|
|
useNullAsDefault: true
|
|
});
|
|
|
|
await db.migrate.latest ();
|
|
|
|
await db.seed.run ({ specific: 'prod.js' });
|
|
|
|
// await db.seed.run ({ specific: 'fake.js' });
|
|
await job (db);
|
|
setInterval (() => {
|
|
job (db);
|
|
}, 3600e3);
|
|
}
|
|
|
|
function get_db () {
|
|
return db;
|
|
}
|
|
|
|
const log = require ('./log') (get_db);
|
|
const app = require ('./app') (get_db);
|
|
|
|
module.exports = { init, log, app };
|