This commit is contained in:
seb
2026-07-31 05:54:13 +02:00
parent 6db7d4d75c
commit db8b192094
32 changed files with 100 additions and 2048 deletions

66
src/db/store.js Normal file
View File

@@ -0,0 +1,66 @@
import sql from 'mssql';
import LibDashStore from 'libdashstore';
import env from '../config/env.js';
let pool;
let store;
let connecting;
function getMssqlConfig() {
const { server, database, user, password, port, encrypt, trustServerCertificate } = env.mssql;
if (!server || !database || !user || !password) {
throw new Error('MSSQL is not configured (MSSQL_SERVER, MSSQL_DATABASE, MSSQL_USER, MSSQL_PASSWORD required)');
}
return {
server,
database,
user,
password,
port,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30_000,
},
options: {
encrypt,
trustServerCertificate,
},
};
}
/**
* Returns the initialized LibDashStore singleton.
* @returns {Promise<InstanceType<typeof LibDashStore>>}
*/
export async function getStore() {
if (store) {
return store;
}
if (!connecting) {
connecting = (async () => {
pool = await new sql.ConnectionPool(getMssqlConfig()).connect();
store = await new LibDashStore(pool);
return store;
})().finally(() => {
connecting = undefined;
});
}
return connecting;
}
export async function ensureConnection() {
await getStore();
}
export async function closeStore() {
if (pool) {
await pool.close();
pool = undefined;
store = undefined;
}
}