فهرست منبع

apiv3 mongo/collections

mizozobu 6 سال پیش
والد
کامیت
acfabf981b

+ 8 - 2
src/client/js/components/Admin/Export/ExportPage.jsx

@@ -14,6 +14,7 @@ class ExportPage extends React.Component {
     super(props);
     super(props);
 
 
     this.state = {
     this.state = {
+      collections: [],
       zipFileStats: [],
       zipFileStats: [],
     };
     };
 
 
@@ -23,9 +24,13 @@ class ExportPage extends React.Component {
 
 
   async componentDidMount() {
   async componentDidMount() {
     // TODO: use apiv3.get
     // TODO: use apiv3.get
-    const { zipFileStats } = await this.props.appContainer.apiGet('/v3/export/status', {});
-    this.setState({ zipFileStats });
+    const [{ collections }, { zipFileStats }] = await Promise.all([
+      this.props.appContainer.apiGet('/v3/mongo/collections', {}),
+      this.props.appContainer.apiGet('/v3/export/status', {}),
+    ]);
     // TODO toastSuccess, toastError
     // TODO toastSuccess, toastError
+
+    this.setState({ collections, zipFileStats });
   }
   }
 
 
   onZipFileStatAdd(newStat) {
   onZipFileStatAdd(newStat) {
@@ -53,6 +58,7 @@ class ExportPage extends React.Component {
       <Fragment>
       <Fragment>
         <h2>{t('export_management.export_as_zip')}</h2>
         <h2>{t('export_management.export_as_zip')}</h2>
         <ExportZipForm
         <ExportZipForm
+          collections={this.state.collections}
           zipFileStats={this.state.zipFileStats}
           zipFileStats={this.state.zipFileStats}
           onZipFileStatAdd={this.onZipFileStatAdd}
           onZipFileStatAdd={this.onZipFileStatAdd}
         />
         />

+ 4 - 4
src/client/js/components/Admin/Export/ExportZipForm.jsx

@@ -15,8 +15,6 @@ class ExportZipForm extends React.Component {
       collections: new Set(),
       collections: new Set(),
     };
     };
 
 
-    this.collections = ['pages', 'revisions'];
-
     this.toggleCheckbox = this.toggleCheckbox.bind(this);
     this.toggleCheckbox = this.toggleCheckbox.bind(this);
     this.checkAll = this.checkAll.bind(this);
     this.checkAll = this.checkAll.bind(this);
     this.uncheckAll = this.uncheckAll.bind(this);
     this.uncheckAll = this.uncheckAll.bind(this);
@@ -42,7 +40,7 @@ class ExportZipForm extends React.Component {
   }
   }
 
 
   checkAll() {
   checkAll() {
-    this.setState({ collections: new Set(this.collections) });
+    this.setState({ collections: new Set(this.props.collections) });
   }
   }
 
 
   uncheckAll() {
   uncheckAll() {
@@ -78,7 +76,7 @@ class ExportZipForm extends React.Component {
           </div>
           </div>
         </div>
         </div>
         <div className="row checkbox checkbox-info my-5">
         <div className="row checkbox checkbox-info my-5">
-          {this.collections.map((collectionName) => {
+          {this.props.collections.map((collectionName) => {
           return (
           return (
             <div className="col-md-6 my-1" key={collectionName}>
             <div className="col-md-6 my-1" key={collectionName}>
               <input
               <input
@@ -111,6 +109,8 @@ class ExportZipForm extends React.Component {
 ExportZipForm.propTypes = {
 ExportZipForm.propTypes = {
   t: PropTypes.func.isRequired, // i18next
   t: PropTypes.func.isRequired, // i18next
   appContainer: PropTypes.instanceOf(AppContainer).isRequired,
   appContainer: PropTypes.instanceOf(AppContainer).isRequired,
+
+  collections: PropTypes.arrayOf(PropTypes.string).isRequired,
   zipFileStats: PropTypes.arrayOf(PropTypes.object).isRequired,
   zipFileStats: PropTypes.arrayOf(PropTypes.object).isRequired,
   onZipFileStatAdd: PropTypes.func.isRequired,
   onZipFileStatAdd: PropTypes.func.isRequired,
 };
 };

+ 1 - 1
src/server/routes/apiv3/export.js

@@ -22,7 +22,7 @@ module.exports = (crowi) => {
    *  /export/status:
    *  /export/status:
    *    get:
    *    get:
    *      tags: [Export]
    *      tags: [Export]
-   *      description: get mongodb collections names and zip files for them
+   *      description: get properties of zip files for export
    *      produces:
    *      produces:
    *        - application/json
    *        - application/json
    *      responses:
    *      responses:

+ 2 - 0
src/server/routes/apiv3/index.js

@@ -9,6 +9,8 @@ const router = express.Router();
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   router.use('/healthcheck', require('./healthcheck')(crowi));
   router.use('/healthcheck', require('./healthcheck')(crowi));
 
 
+  router.use('/mongo', require('./mongo')(crowi));
+
   router.use('/export', require('./export')(crowi));
   router.use('/export', require('./export')(crowi));
 
 
   router.use('/import', require('./import')(crowi));
   router.use('/import', require('./import')(crowi));

+ 48 - 0
src/server/routes/apiv3/mongo.js

@@ -0,0 +1,48 @@
+const loggerFactory = require('@alias/logger');
+
+const logger = loggerFactory('growi:routes:apiv3:mongo'); // eslint-disable-line no-unused-vars
+
+const express = require('express');
+
+const router = express.Router();
+
+/**
+ * @swagger
+ *  tags:
+ *    name: Mongo
+ */
+
+module.exports = (crowi) => {
+  /**
+   * @swagger
+   *
+   *  /mongo/collections:
+   *    get:
+   *      tags: [Mongo]
+   *      description: get mongodb collections names
+   *      produces:
+   *        - application/json
+   *      responses:
+   *        200:
+   *          description: list of collections in mongoDB
+   *          content:
+   *            application/json:
+   *              schema:
+   *                properties:
+   *                  collections:
+   *                    type: array
+   *                    items:
+   *                      type: string
+   */
+  router.get('/collections', async(req, res) => {
+    const collections = Object.values(crowi.models).map(model => model.collection.collectionName);
+
+    // TODO: use res.apiv3
+    return res.json({
+      ok: true,
+      collections: [...new Set(collections)], // remove duplicates
+    });
+  });
+
+  return router;
+};