backend/source/repositories/name_index.ts

108 lines
2.4 KiB
TypeScript

/*
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/>.
*/
namespace _espe.repository.name_index
{
/**
*/
var _chest : (
null
|
lib_plankton.storage.type_chest<
Array<any>,
Record<string, any>,
lib_plankton.database.type_description_create_table,
lib_plankton.storage.sql_table_common.type_sql_table_common_search_term,
Record<string, any>
>
) = null;
/**
*/
function get_chest(
) : lib_plankton.storage.type_chest<
Array<any>,
Record<string, any>,
lib_plankton.database.type_description_create_table,
lib_plankton.storage.sql_table_common.type_sql_table_common_search_term,
Record<string, any>
>
{
if (_chest === null) {
_chest = lib_plankton.storage.sql_table_common.chest(
{
"database_implementation": _espe.helpers.database_implementation(),
"table_name": "name_indices",
"key_names": ["name_image"],
}
);
}
else {
// do nothing
}
return _chest;
}
/**
*/
async function get_name_image(
name : string
) : Promise<string>
{
return (
(! _espe.conf.get().settings.name_index.veil)
? name
: await lib_plankton.sha256.get(
lib_plankton.json.encode(name),
(_espe.conf.get().settings.name_index.salt ?? undefined)
)
);
}
/**
*/
export async function read(
name : string
) : Promise<int>
{
const name_image : string = await get_name_image(name);
let row : Record<string, any>;
try {
row = await get_chest().read([name_image]);
return row["index"];
}
catch (error) {
return 0;
}
}
/**
*/
export async function write(
name : string,
index : int
) : Promise<void>
{
const name_image : string = await get_name_image(name);
await get_chest().write([name_image], {"index": index});
}
}