feat: add ability to paginate results, add ability to retrieve certain fields, fix docker-compose output, update docs
This commit is contained in:
@@ -12,14 +12,10 @@ const edit = require('./edit');
|
||||
const remove = require('./remove');
|
||||
|
||||
//basic route management (all query possibilities)
|
||||
router.get('/', cors(), query(false, false));
|
||||
router.get('/:id(\\d+)', cors(), query(false, false));
|
||||
router.get('/archive', cors(), query(true, false));
|
||||
router.get('/archive/:id(\\d+)', cors(), query(true, false));
|
||||
router.get('/metadata', cors(), query(false, true));
|
||||
router.get('/metadata/:id(\\d+)', cors(), query(false, true));
|
||||
router.get('/archive/metadata', cors(), query(true, true));
|
||||
router.get('/archive/metadata/:id(\\d+)', cors(), query(true, true));
|
||||
router.get('/:id(\\d+)?', cors(), query(false, false));
|
||||
router.get('/archive/:id(\\d+)?', cors(), query(true, false));
|
||||
router.get('/metadata/:id(\\d+)?', cors(), query(false, true));
|
||||
router.get('/archive/metadata/:id(\\d+)?', cors(), query(true, true));
|
||||
|
||||
//use middleware to authenticate the rest of the routes
|
||||
router.use(cors({
|
||||
|
||||
+41
-14
@@ -1,18 +1,45 @@
|
||||
const { Op } = require('sequelize');
|
||||
const { articles } = require('../database/models');
|
||||
|
||||
//the query function that can be reused
|
||||
const query = (ascending, metadataOnly) => async (req, res) => {
|
||||
if (process.env.QUERY_LIMIT) {
|
||||
process.env.PAGE_SIZE = process.env.QUERY_LIMIT;
|
||||
console.warn('The use of QUERY_LIMIT is deprecated. Please use PAGE_SIZE instead.');
|
||||
}
|
||||
|
||||
if (req.query.limit) {
|
||||
req.query.page_size = req.query.limit;
|
||||
console.warn('The use of the limit parameter is deprecated. Please use page_size instead.');
|
||||
}
|
||||
|
||||
const PAGE_SIZE = parseInt(req.query.page_size) || parseInt(process.env.PAGE_SIZE) || 999;
|
||||
const PAGE = parseInt(req.query.page) || 1;
|
||||
const ARTICLE_ID = req.params.id ? parseInt(req.params.id) : undefined;
|
||||
const FIELDS = req.query.fields ? req.query.fields.split(',') : undefined;
|
||||
|
||||
const attributes = [
|
||||
'index',
|
||||
'author',
|
||||
'createdAt',
|
||||
'edits',
|
||||
'title',
|
||||
'updatedAt',
|
||||
].concat(metadataOnly ? [] : [
|
||||
'body',
|
||||
'rendered'
|
||||
]);
|
||||
|
||||
//filter out attributes that aren't requested
|
||||
const attributesToFetch = FIELDS ? attributes.filter((attr) => {
|
||||
return FIELDS.includes(attr) || attr === 'index';
|
||||
}) : attributes;
|
||||
|
||||
//specific search (id is defined)
|
||||
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
|
||||
if (typeof(ARTICLE_ID) === 'number' && !isNaN(ARTICLE_ID)) {
|
||||
const result = await articles.findOne({
|
||||
attributes: [
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!metadataOnly ? ['body', 'rendered'] : [])
|
||||
],
|
||||
attributes: attributesToFetch,
|
||||
where: {
|
||||
index: {
|
||||
[Op.eq]: ascending ? parseInt(req.params.id) : (await articles.max('index')) - parseInt(req.params.id) + 1
|
||||
}
|
||||
index: ascending ? ARTICLE_ID : (await articles.max('index') - ARTICLE_ID) + 1,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,16 +50,16 @@ const query = (ascending, metadataOnly) => async (req, res) => {
|
||||
//default search
|
||||
else {
|
||||
const result = await articles.findAndCountAll({
|
||||
attributes: [
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!metadataOnly ? ['body', 'rendered'] : [])
|
||||
],
|
||||
attributes: attributesToFetch,
|
||||
limit: PAGE_SIZE,
|
||||
offset: Math.max((PAGE - 1) * PAGE_SIZE, 0),
|
||||
order: [
|
||||
['index', ascending ? 'ASC' : 'DESC']
|
||||
],
|
||||
limit: parseInt(req.query.limit) || parseInt(process.env.QUERY_LIMIT) || 999
|
||||
]
|
||||
});
|
||||
|
||||
return res.status(200).json(result.rows || result);
|
||||
//result is empty array if failed to find
|
||||
return res.status(200).json(result.rows || result || []);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user