@mongoosejs/studio 0.0.70 → 0.0.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,11 +11,18 @@ const CreateDocumentParams = new Archetype({
11
11
  data: {
12
12
  $type: Archetype.Any,
13
13
  $required: true
14
+ },
15
+ roles: {
16
+ $type: ['string'],
14
17
  }
15
18
  }).compile('CreateDocumentParams');
16
19
 
17
20
  module.exports = ({ db }) => async function CreateDocument(params) {
18
- const { model, data } = new CreateDocumentParams(params);
21
+ const { model, data, roles } = new CreateDocumentParams(params);
22
+
23
+ if (roles && roles.includes('readonly')) {
24
+ throw new Error('Not authorized');
25
+ }
19
26
 
20
27
  const Model = db.models[model];
21
28
  if (Model == null) {
@@ -10,13 +10,20 @@ const DeleteDocumentParams = new Archetype({
10
10
  documentId: {
11
11
  $type: 'string',
12
12
  $required: true
13
+ },
14
+ roles: {
15
+ $type: ['string'],
13
16
  }
14
17
  }).compile('DeleteDocumentParams');
15
18
 
16
19
  module.exports = ({ db }) => async function DeleteDocument(params) {
17
- const { model, documentId } = new DeleteDocumentParams(params);
20
+ const { model, documentId, roles } = new DeleteDocumentParams(params);
18
21
 
19
22
  const Model = db.models[model];
23
+
24
+ if (roles && roles.includes('readonly')) {
25
+ throw new Error('Not authorized');
26
+ }
20
27
  if (Model == null) {
21
28
  throw new Error(`Model ${model} not found`);
22
29
  }
@@ -24,7 +24,7 @@ const GetDocumentsParams = new Archetype({
24
24
  }
25
25
  }).compile('GetDocumentsParams');
26
26
 
27
- module.exports = ({ db }) => async function getDocuments(params, req, res) {
27
+ module.exports = ({ db }) => async function exportQueryResults(params, req, res) {
28
28
  params = new GetDocumentsParams(params);
29
29
  let { filter } = params;
30
30
  const { model, propertiesToInclude } = params;
@@ -57,10 +57,10 @@ module.exports = ({ db }) => async function getDocuments(params, req, res) {
57
57
  }));
58
58
  }
59
59
  const csv = stringify(rows);
60
-
60
+
61
61
  res.set({
62
62
  'Content-Type': 'text/csv',
63
63
  'Content-Disposition': `attachment; filename="${model.toLowerCase()}-export.csv"`,
64
64
  });
65
65
  return csv;
66
- };
66
+ };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const Archetype = require('archetype');
4
+
5
+ const GetDocumentsParams = new Archetype({
6
+ model: {
7
+ $type: 'string',
8
+ $required: true
9
+ },
10
+ }).compile('GetDocumentsParams');
11
+
12
+ module.exports = ({ db }) => async function getIndexes(params) {
13
+ params = new GetDocumentsParams(params);
14
+
15
+ const { model } = params;
16
+
17
+ const Model = db.models[model];
18
+ if (Model == null) {
19
+ throw new Error(`Model ${model} not found`);
20
+ }
21
+
22
+ const mongoDBIndexes = await Model.listIndexes();
23
+ const schemaIndexes = Model.schema.indexes().map(([fields, options]) => ({
24
+ key: fields,
25
+ name: Object.keys(fields).map(key => `${key}_${fields[key]}`).join("_")
26
+ }));
27
+ const diffIndexes = await Model.diffIndexes();
28
+ return {
29
+ mongoDBIndexes,
30
+ schemaIndexes,
31
+ diffIndexes
32
+ };
33
+ };
@@ -5,5 +5,6 @@ exports.deleteDocument = require('./deleteDocument');
5
5
  exports.exportQueryResults = require('./exportQueryResults');
6
6
  exports.getDocument = require('./getDocument');
7
7
  exports.getDocuments = require('./getDocuments');
8
+ exports.getIndexes = require('./getIndexes');
8
9
  exports.listModels = require('./listModels');
9
10
  exports.updateDocument = require('./updateDocument');
@@ -14,12 +14,18 @@ const UpdateDocumentsParams = new Archetype({
14
14
  update: {
15
15
  $type: Object,
16
16
  $required: true
17
+ },
18
+ roles: {
19
+ $type: ['string'],
17
20
  }
18
21
  }).compile('UpdateDocumentsParams');
19
22
 
20
23
  module.exports = ({ db }) => async function updateDocument(params) {
21
- const { model, _id, update } = new UpdateDocumentsParams(params);
24
+ const { model, _id, update, roles } = new UpdateDocumentsParams(params);
22
25
 
26
+ if (roles && roles.includes('readonly')) {
27
+ throw new Error('Not authorized');
28
+ }
23
29
  const Model = db.models[model];
24
30
  if (Model == null) {
25
31
  throw new Error(`Model ${model} not found`);
package/express.js CHANGED
@@ -63,6 +63,7 @@ module.exports = async function(apiUrl, conn, options) {
63
63
  if (!user || !roles) {
64
64
  throw new Error('Not authorized');
65
65
  }
66
+ req.params.roles = roles;
66
67
 
67
68
  next();
68
69
  })