AppReports/lib/db/index.js
2020-08-23 13:08:04 +02:00

44 lines
1.0 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
*/
'use strict';
const knex = require ('knex');
const fs = require ('fs');
let db = null;
async function init (use_fake_seed) {
// eslint-disable-next-line no-sync
if (use_fake_seed && fs.existsSync ('db.sqlite'))
// eslint-disable-next-line no-sync
fs.unlinkSync ('db.sqlite');
db = knex ({
client: 'sqlite',
connection: { filename: 'db.sqlite' },
migrations: { directory: 'migrations' },
seeds: { directory: 'seeds' },
useNullAsDefault: true
});
await db.migrate.latest ();
if (use_fake_seed)
await db.seed.run ({ specific: 'fake.js' });
else
await db.seed.run ({ specific: 'prod.js' });
}
function get_db () {
return db;
}
const log = require ('./log') (get_db);
const app = require ('./app') (get_db);
module.exports = { init, log, app };