Skip to content
This repository has been archived by the owner on Aug 12, 2021. It is now read-only.

Commit

Permalink
Fix performance issues with list queries (#18)
Browse files Browse the repository at this point in the history
* Enable CORS

* Add health check endpoint for load balancers

* Optimize list query
  • Loading branch information
cl9200 authored Dec 9, 2020
1 parent 86bb813 commit 7b034b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 5 additions & 3 deletions server/src/db/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}));
};
};

0 comments on commit 7b034b1

Please sign in to comment.