AppReports/lib/db/index.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-08-19 12:33:09 +02:00
/*
* 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 */
2020-07-29 20:56:03 +02:00
'use strict';
const knex = require ('knex');
2020-08-23 13:08:04 +02:00
const fs = require ('fs');
2020-07-29 20:56:03 +02:00
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');
2020-08-23 13:08:04 +02:00
2020-07-29 20:56:03 +02:00
db = knex ({
client: 'sqlite',
connection: { filename: 'data/db.sqlite' },
2020-07-29 20:56:03 +02:00
migrations: { directory: 'migrations' },
seeds: { directory: 'seeds' },
useNullAsDefault: true
});
await db.migrate.latest ();
if (use_fake_seed)
await db.seed.run ({ specific: 'fake.js' });
2020-08-22 10:43:35 +02:00
else
await db.seed.run ({ specific: 'prod.js' });
2020-07-29 20:56:03 +02:00
}
2020-08-16 11:48:06 +02:00
function get_db () {
return db;
2020-07-29 20:56:03 +02:00
}
2020-08-16 11:48:06 +02:00
const log = require ('./log') (get_db);
const app = require ('./app') (get_db);
module.exports = { init, log, app };