mongo.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { SCOPE } from '@growi/core/dist/interfaces';
  2. import { accessTokenParser } from '~/server/middlewares/access-token-parser';
  3. import loggerFactory from '~/utils/logger';
  4. const logger = loggerFactory('growi:routes:apiv3:mongo'); // eslint-disable-line no-unused-vars
  5. const express = require('express');
  6. const mongoose = require('mongoose');
  7. const router = express.Router();
  8. /** @param {import('~/server/crowi').default} crowi Crowi instance */
  9. module.exports = (crowi) => {
  10. const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
  11. const adminRequired = require('../../middlewares/admin-required')(crowi);
  12. /**
  13. * @swagger
  14. *
  15. * /mongo/collections:
  16. * get:
  17. * tags: [MongoDB]
  18. * summary: /mongo/collections
  19. * description: get mongodb collections names
  20. * responses:
  21. * 200:
  22. * description: a list of collections in mongoDB
  23. * content:
  24. * application/json:
  25. * schema:
  26. * properties:
  27. * ok:
  28. * type: boolean
  29. * description: whether the request is succeeded
  30. * collections:
  31. * type: array
  32. * items:
  33. * type: string
  34. */
  35. router.get('/collections', accessTokenParser([SCOPE.READ.ADMIN.EXPORT_DATA]), loginRequiredStrictly, adminRequired, async(req, res) => {
  36. const listCollectionsResult = await mongoose.connection.db.listCollections().toArray();
  37. const collections = listCollectionsResult.map(collectionObj => collectionObj.name);
  38. // TODO: use res.apiv3
  39. return res.json({
  40. ok: true,
  41. collections,
  42. });
  43. });
  44. return router;
  45. };