From 7b034b1628d91d33b5b232299e16d6273de85629 Mon Sep 17 00:00:00 2001 From: Cha Minkyoo Date: Wed, 9 Dec 2020 22:41:22 +0900 Subject: [PATCH] Fix performance issues with list queries (#18) * Enable CORS * Add health check endpoint for load balancers * Optimize list query --- server/src/app.js | 10 ++++++---- server/src/db/query.js | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/server/src/app.js b/server/src/app.js index 6f08f06..a2f9d30 100644 --- a/server/src/app.js +++ b/server/src/app.js @@ -9,9 +9,6 @@ import morgan from 'morgan'; import route from './route'; import logger, { stream } from './logger'; - -const ENV = process.env.NODE_ENV; - const sendError = (err, req, res) => { logger.error(err.stack); const code = err.status || 500; @@ -52,13 +49,18 @@ export default () => { const app = express(); app.use(expressRequestId()); - if (ENV === 'development') app.use(cors()); + app.use(cors()); app.use(compression()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(sseMiddleware); app.use(morgan('tiny', { stream })); + // HealthCheck Endpoint + app.get('/', async (_req, res) => { + res.send('OK'); + }); + app.use('/api', route); app.use('/', express.static('build')); app.use('*', (req, res) => { diff --git a/server/src/db/query.js b/server/src/db/query.js index cd1d20e..0aeb49d 100644 --- a/server/src/db/query.js +++ b/server/src/db/query.js @@ -64,11 +64,13 @@ export const listQuery = ({ return params; }; -export const listQueryWithCount = (model, ...args) => { +export const listQueryWithCount = async (model, ...args) => { const params = listQuery(...args); const { offset } = params; - return model.findAndCountAll(params).then(({ count, rows }) => ({ + const count = await model.max('id'); + const rows = await model.findAll(params); + return { data: rows, pagination: { count: rows.length, offset, total: count }, - })); + }; };