refactor: small tweaks and fixes

This commit is contained in:
Carlos Valente
2024-03-06 22:42:20 +01:00
committed by GitHub
parent 8196ea584d
commit d22cc48565
61 changed files with 401 additions and 198 deletions
+2 -1
View File
@@ -97,7 +97,8 @@ const lastLoadedProject = isTest ? 'db.json' : getLastLoadedProject();
// path to public db
export const resolveDbDirectory = join(testDbStartDirectory, isTest ? `../${config.database.testdb}` : config.projects);
export const resolveDbPath = join(resolveDbDirectory, lastLoadedProject ? lastLoadedProject : config.database.filename);
export const resolveDbName = lastLoadedProject ? lastLoadedProject : config.database.filename;
export const resolveDbPath = join(resolveDbDirectory, resolveDbName);
export const pathToStartDb = isTest
? join(srcDirectory, '..', config.database.testdb, config.database.filename)
+23 -12
View File
@@ -8,7 +8,7 @@ import { join } from 'path';
import { ensureDirectory } from '../utils/fileManagement.js';
import { dbModel } from '../models/dataModel.js';
import { pathToStartDb, resolveDbDirectory, resolveDbPath } from './index.js';
import { pathToStartDb, resolveDbDirectory, resolveDbName } from './index.js';
import { parseProjectFile } from '../services/project-service/projectFileUtils.js';
import { parseJson } from '../utils/parser.js';
@@ -16,25 +16,25 @@ import { parseJson } from '../utils/parser.js';
* @description ensures directories exist and populates database
* @return {string} - path to db file
*/
const populateDb = (): string => {
// if everything goes well, the DB in disk is the one loaded
let dbInDisk = resolveDbPath;
ensureDirectory(resolveDbDirectory);
const populateDb = (directory: string, filename: string): string => {
ensureDirectory(directory);
let dbPath = join(directory, filename);
// if everything goes well, the DB in disk is the one loaded
// if dbInDisk doesn't exist we want to use startup db
if (!existsSync(dbInDisk)) {
if (!existsSync(dbPath)) {
try {
const dbDirectory = resolveDbDirectory;
const newFileDirectory = join(dbDirectory, pathToStartDb.split('/').pop());
copyFileSync(pathToStartDb, newFileDirectory);
dbInDisk = newFileDirectory;
dbPath = newFileDirectory;
} catch (_) {
/* we do not handle this */
}
}
return dbInDisk;
return dbPath;
};
/**
@@ -55,10 +55,9 @@ const parseDatabase = async (fileToRead: string, adapterToUse: Low<DatabaseModel
/**
* @description loads ontime db
* @return {Promise<{data: (*), db: Low<unknown>}>}
*/
async function loadDb() {
const dbInDisk = populateDb();
async function loadDb(directory: string, filename: string) {
const dbInDisk = populateDb(directory, filename);
const adapter = new JSONFile<DatabaseModel>(dbInDisk);
const db = new Low(adapter, dbModel);
@@ -72,12 +71,24 @@ async function loadDb() {
export let db = {} as Low<DatabaseModel>;
export let data = {} as DatabaseModel;
export const dbLoadingProcess = loadDb();
export const dbLoadingProcess = loadDb(resolveDbDirectory, resolveDbName);
/**
* Initialises database at known location
*/
const init = async () => {
const dbProvider = await dbLoadingProcess;
db = dbProvider.db;
data = dbProvider.data;
};
/**
* Allows to switch the database to a new file
*/
export const switchDb = async (newFileName: string) => {
const { db: newDb, data: newData } = await loadDb(resolveDbDirectory, newFileName);
db = newDb;
data = newData;
};
init();