u
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import chalk from 'chalk';
|
||||
import sql from 'mssql';
|
||||
import LibDashStore from 'libdashstore';
|
||||
import env from '../config/env.js';
|
||||
@@ -6,6 +7,61 @@ let pool;
|
||||
let store;
|
||||
let connecting;
|
||||
|
||||
const STORE_NAMESPACES = [
|
||||
'companyuser',
|
||||
'company',
|
||||
'category',
|
||||
'article',
|
||||
'lang',
|
||||
'tax',
|
||||
'google_auth',
|
||||
];
|
||||
|
||||
/**
|
||||
* Wraps LibDashStore namespace methods so each call logs its duration.
|
||||
* @param {InstanceType<typeof LibDashStore>} rawStore
|
||||
* @returns {InstanceType<typeof LibDashStore>}
|
||||
*/
|
||||
function wrapStoreWithTiming(rawStore) {
|
||||
for (const namespace of STORE_NAMESPACES) {
|
||||
const api = rawStore[namespace];
|
||||
if (!api || typeof api !== 'object') continue;
|
||||
|
||||
for (const [method, fn] of Object.entries(api)) {
|
||||
if (typeof fn !== 'function') continue;
|
||||
|
||||
api[method] = async (...args) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
return await fn(...args);
|
||||
} finally {
|
||||
const duration = Date.now() - start;
|
||||
console.log(
|
||||
`${chalk.magenta('libstore')} ${namespace}.${method} ${chalk.gray(`(${duration}ms)`)}`
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
for (const method of ['purge']) {
|
||||
const fn = rawStore[method];
|
||||
if (typeof fn !== 'function') continue;
|
||||
|
||||
rawStore[method] = async (...args) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
return await fn(...args);
|
||||
} finally {
|
||||
const duration = Date.now() - start;
|
||||
console.log(`${chalk.magenta('libstore')} ${method} ${chalk.gray(`(${duration}ms)`)}`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return rawStore;
|
||||
}
|
||||
|
||||
function getMssqlConfig() {
|
||||
const { server, database, user, password, port, encrypt, trustServerCertificate } = env.mssql;
|
||||
|
||||
@@ -43,7 +99,7 @@ export async function getStore() {
|
||||
if (!connecting) {
|
||||
connecting = (async () => {
|
||||
pool = await new sql.ConnectionPool(getMssqlConfig()).connect();
|
||||
store = await new LibDashStore(pool);
|
||||
store = wrapStoreWithTiming(await new LibDashStore(pool));
|
||||
return store;
|
||||
})().finally(() => {
|
||||
connecting = undefined;
|
||||
|
||||
Reference in New Issue
Block a user