@modular-rest/server 1.2.4 → 1.3.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-rest/server",
3
- "version": "1.2.4",
3
+ "version": "1.3.1",
4
4
  "description": "a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -22,9 +22,11 @@ let defaultServiceRoot = __dirname + '/services';
22
22
  * @param {number} options.port server port
23
23
  * @param {boolean} options.dontListen server will not being run if it was true and just return koa app object.
24
24
  *
25
+ *
25
26
  * @param {string} options.mongo mongodb options.
26
27
  * @param {string} options.mongo.dbPrefix a prefix for your database name.
27
28
  * @param {string} options.mongo.mongoBaseAddress the address of your mongo server without any database specification on it.
29
+ * @param {string} options.mongo.addressMap specific address for each database
28
30
  *
29
31
  * @param {object} options.keypair RSA keypair for authentication module
30
32
  * @param {string} options.keypair.private
@@ -43,6 +45,7 @@ async function createRest(options) {
43
45
  port: 3000,
44
46
  dontListen: false,
45
47
  mongo: {
48
+ atlas: false,
46
49
  mongoBaseAddress: 'mongodb://localhost:27017',
47
50
  dbPrefix: 'mrest_',
48
51
  },
@@ -1,6 +1,9 @@
1
1
  let name = 'dataProvider';
2
- var colog = require('colog');
3
- let { AccessTypes, AccessDefinition } = require('../../class/security');
2
+ const colog = require('colog');
3
+ let {
4
+ AccessTypes,
5
+ AccessDefinition
6
+ } = require('../../class/security');
4
7
 
5
8
  const Mongoose = require('mongoose');
6
9
  Mongoose.set('useCreateIndex', true);
@@ -23,10 +26,20 @@ let TypeCasters = require('./typeCasters');
23
26
  function connectToDatabaseByCollectionDefinitionList(dbName, collectionDefinitionList = [], mongoOption) {
24
27
 
25
28
  return new Promise((done, reject) => {
26
- // create db connection
27
- let connectionString = mongoOption.mongoBaseAddress + `/${mongoOption.dbPrefix + dbName}`;
28
- let connection = Mongoose.createConnection(connectionString, { useUnifiedTopology: true, useNewUrlParser: true, });
29
- // store connection
29
+ // Create db connection
30
+ //
31
+ const fullDbName = (mongoOption.dbPrefix || '') + dbName
32
+ const connectionString = mongoOption.mongoBaseAddress + '/' + fullDbName;
33
+
34
+ colog.info(`- Connecting to database ${connectionString}`)
35
+
36
+ let connection = Mongoose.createConnection(connectionString, {
37
+ ...mongoOption,
38
+ useUnifiedTopology: true,
39
+ useNewUrlParser: true,
40
+ });
41
+
42
+ // Store connection
30
43
  connections[dbName] = connection;
31
44
 
32
45
  // add db models from schemas
@@ -62,7 +75,7 @@ function connectToDatabaseByCollectionDefinitionList(dbName, collectionDefinitio
62
75
  })
63
76
 
64
77
  connection.on('connected', () => {
65
- colog.success(`- ${mongoOption.dbPrefix + dbName} database has been connected`)
78
+ colog.success(`- ${fullDbName} database has been connected`)
66
79
  done()
67
80
  });
68
81
  })
@@ -76,7 +89,10 @@ function connectToDatabaseByCollectionDefinitionList(dbName, collectionDefinitio
76
89
  * @param {string} option.mongoOption.dbPrefix
77
90
  * @param {string} option.mongoOption.mongoBaseAddress
78
91
  */
79
- async function addCollectionDefinitionByList({ list, mongoOption }) {
92
+ async function addCollectionDefinitionByList({
93
+ list,
94
+ mongoOption
95
+ }) {
80
96
  let clusteredByDBName = {};
81
97
 
82
98
  // cluster list by their database name.
@@ -118,14 +134,10 @@ function _getPermissionList(db, collection, operationType) {
118
134
  permissionDefinition.permissionList.forEach(permission => {
119
135
  if (permission.onlyOwnData == true) {
120
136
  permissionList.push(permission);
121
- }
122
-
123
- else if (operationType == AccessTypes.read &&
137
+ } else if (operationType == AccessTypes.read &&
124
138
  permission.read == true) {
125
139
  permissionList.push(permission);
126
- }
127
-
128
- else if (operationType == AccessTypes.write &&
140
+ } else if (operationType == AccessTypes.write &&
129
141
  permission.write == true) {
130
142
  permissionList.push(permission);
131
143
  }
@@ -151,15 +163,11 @@ function checkAccess(db, collection, operationType, queryOrDoc, user) {
151
163
  } catch (error) {
152
164
  key = false;
153
165
  }
154
- }
155
-
156
- else if (operationType == AccessTypes.read) {
166
+ } else if (operationType == AccessTypes.read) {
157
167
  if (permission.read &&
158
168
  user.permission[permissionType] == true)
159
169
  key = true;
160
- }
161
-
162
- else if (operationType == AccessTypes.write) {
170
+ } else if (operationType == AccessTypes.write) {
163
171
 
164
172
  if (permission.write &&
165
173
  user.permission[permissionType] == true)
@@ -213,11 +221,13 @@ function performAdditionalOptionsToQueryObject(queryObj, options) {
213
221
 
214
222
 
215
223
  module.exports = {
216
- name, addCollectionDefinitionByList, getCollection,
217
- checkAccess, getAsID,
224
+ name,
225
+ addCollectionDefinitionByList,
226
+ getCollection,
227
+ checkAccess,
228
+ getAsID,
218
229
  performPopulateToQueryObject,
219
230
  performAdditionalOptionsToQueryObject,
220
231
  triggers,
221
232
  TypeCasters,
222
- }
223
-
233
+ }