backend/source/database.ts

119 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2024-05-20 21:56:48 +02:00
/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend
Copyright (C) 2024 Christian Fraß
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
*/
2024-04-22 23:23:42 +02:00
namespace _espe.database
{
/**
*/
const _compatible_revisions : Array<string> = [
2025-03-31 18:39:50 +00:00
"r6",
2024-04-22 23:23:42 +02:00
];
/**
*/
export function get_implementation(
) : lib_plankton.database.type_database
{
switch (_espe.conf.get().database.kind) {
case "sqlite": {
2024-05-27 17:32:42 +02:00
type type_parameters = {
path : string;
};
const parameters : type_parameters = (_espe.conf.get().database.data as type_parameters);
2024-04-22 23:23:42 +02:00
return lib_plankton.database.sqlite_database(
{
2024-05-27 17:32:42 +02:00
"path": parameters.path,
2024-04-22 23:23:42 +02:00
}
);
break;
}
2024-06-03 11:03:21 +02:00
case "postgresql": {
type type_parameters = {
host : string;
port ?: int;
username : string;
password : string;
schema : string;
};
const parameters : type_parameters = (_espe.conf.get().database.data as type_parameters);
return lib_plankton.database.postgresql_database(
{
"host": parameters.host,
"port": parameters.port,
"username": parameters.username,
"password": parameters.password,
"schema": parameters.schema,
}
);
}
2024-04-22 23:23:42 +02:00
default: {
throw (new Error("database implementation not available: " + _espe.conf.get().database.kind));
}
}
}
/**
*/
function get_revision(
) : Promise<(null | string)>
{
return (
get_implementation().query_select(
{
"source": "_meta",
"fields": ["revision"],
}
)
.then<(null | string)>(
(rows) => Promise.resolve<(null | string)>(rows[0]["revision"])
)
.catch<(null | string)>(
() => Promise.resolve<(null | string)>(null)
)
);
}
/**
*/
export async function check(
) : Promise<void>
{
const revision : (null | string) = await get_revision();
lib_plankton.log.info(
"database_check",
{
"revision_found": revision,
"revisions_compatible": _compatible_revisions,
}
);
if (revision === null) {
return Promise.reject<void>(new Error("database appearently missing"));
}
else {
if (! _compatible_revisions.includes(revision)) {
return Promise.reject<void>(new Error("database revision incompatible; found: " + revision + "; required: " + String(_compatible_revisions)));
}
else {
return Promise.resolve<void>(undefined);
}
}
}
}