adminmate-express-mongoose 1.2.6 → 1.2.7

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/index.js CHANGED
@@ -15,10 +15,15 @@ const { customQuery } = require('./src/controllers/model-query');
15
15
 
16
16
  const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, charts, authorizedIps }) => {
17
17
  const api = {
18
+ // App config
19
+ getAppConfig: fnHelper.getAppConfig,
20
+
18
21
  // General
19
22
  getModelProperties: fnHelper.getModelProperties,
20
23
  getModelRealname: fnHelper.getModelRealname,
21
24
  getModelRelationships: fnHelper.getModelAssociations,
25
+ getModelPrimaryKeys: fnHelper.getModelPrimaryKeys,
26
+ getModelWhereClause: fnHelper.getModelWhereClause,
22
27
 
23
28
  // CRUD
24
29
  modelGetAll: getAll,
package/jest.config.js CHANGED
@@ -1,5 +1,4 @@
1
1
  module.exports = {
2
- testEnvironment: 'node',
3
2
  projects: [
4
3
  {
5
4
  displayName: 'mongodb',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminmate-express-mongoose",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "Adminmate Express/Mongoose connector",
5
5
  "author": "Marc Delalonde",
6
6
  "homepage": "http://adminmate.io",
@@ -16,15 +16,14 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "start": "node ./index",
19
- "test": "jest --runInBand",
20
- "reset-db": "node migration.js"
19
+ "test": "jest --runInBand"
21
20
  },
22
21
  "repository": {
23
22
  "type": "git",
24
23
  "url": "https://github.com/Adminmate/adminmate-express-mongoose.git"
25
24
  },
26
25
  "dependencies": {
27
- "adminmate-express-core": "^1.1.5",
26
+ "adminmate-express-core": "^1.1.6",
28
27
  "lodash": "^4.17.21",
29
28
  "moment": "^2.29.1",
30
29
  "mongoose": "^5.9.7",
@@ -12,12 +12,15 @@ module.exports = async (currentModel, data) => {
12
12
 
13
13
  const toSum = data.field && data.operation === 'sum' ? `$${data.field}` : 1;
14
14
 
15
+ // To set the max date
16
+ const toDate = data.to ? moment(data.to) : moment();
17
+
15
18
  let matchReq = {};
16
19
  let groupFormat = '';
17
20
 
18
21
  // Day timeframe
19
22
  if (data.timeframe === 'day') {
20
- const startOfCurrentDay = moment().startOf('day');
23
+ const startOfCurrentDay = toDate.startOf('day');
21
24
  matchReq = {
22
25
  '$gte': new Date(startOfCurrentDay.clone().subtract(30, 'day').startOf('day').format()),
23
26
  '$lt': new Date(startOfCurrentDay.format())
@@ -26,7 +29,7 @@ module.exports = async (currentModel, data) => {
26
29
  }
27
30
  // Week timeframe
28
31
  else if (data.timeframe === 'week') {
29
- const startOfCurrentWeek = moment().startOf('week');
32
+ const startOfCurrentWeek = toDate.startOf('week');
30
33
  matchReq = {
31
34
  '$gte': new Date(startOfCurrentWeek.clone().subtract(26, 'week').startOf('week').format()),
32
35
  '$lt': new Date(startOfCurrentWeek.format())
@@ -35,7 +38,7 @@ module.exports = async (currentModel, data) => {
35
38
  }
36
39
  // Month timeframe
37
40
  else if (data.timeframe === 'month') {
38
- const startOfCurrentMonth = moment().startOf('month');
41
+ const startOfCurrentMonth = toDate.startOf('month');
39
42
  matchReq = {
40
43
  '$gte': new Date(startOfCurrentMonth.clone().subtract(12, 'month').startOf('month').format()),
41
44
  '$lt': new Date(startOfCurrentMonth.format())
@@ -44,7 +47,7 @@ module.exports = async (currentModel, data) => {
44
47
  }
45
48
  // Year timeframe
46
49
  else if (data.timeframe === 'year') {
47
- const startOfCurrentYear = moment().startOf('year');
50
+ const startOfCurrentYear = toDate.startOf('year');
48
51
  matchReq = {
49
52
  '$gte': new Date(startOfCurrentYear.clone().subtract(8, 'year').startOf('year').format()),
50
53
  '$lt': new Date(startOfCurrentYear.format())
@@ -83,7 +86,7 @@ module.exports = async (currentModel, data) => {
83
86
  // Day timeframe
84
87
  if (data.timeframe === 'day') {
85
88
  for (let i = 1; i <= 30; i++) {
86
- const currentDate = moment().subtract(i, 'day').startOf('day');
89
+ const currentDate = toDate.clone().subtract(i, 'day').startOf('day');
87
90
  const countForTheTimeframe = _.find(repartitionData, { key: currentDate.format('YYYY-MM-DD') });
88
91
  formattedData.push({
89
92
  key: currentDate.format('DD/MM'),
@@ -94,7 +97,7 @@ module.exports = async (currentModel, data) => {
94
97
  // Week timeframe
95
98
  else if (data.timeframe === 'week') {
96
99
  for (let i = 1; i <= 26; i++) {
97
- const currentWeek = moment().subtract(i, 'week').startOf('week');
100
+ const currentWeek = toDate.clone().subtract(i, 'week').startOf('week');
98
101
  const countForTheTimeframe = _.find(repartitionData, { key: currentWeek.format('WW') });
99
102
  formattedData.push({
100
103
  key: currentWeek.startOf('week').format('DD/MM'),
@@ -105,7 +108,7 @@ module.exports = async (currentModel, data) => {
105
108
  // Month timeframe
106
109
  else if (data.timeframe === 'month') {
107
110
  for (let i = 1; i <= 12; i++) {
108
- const currentMonth = moment().subtract(i, 'month').startOf('month');
111
+ const currentMonth = toDate.clone().subtract(i, 'month').startOf('month');
109
112
  const countForTheTimeframe = _.find(repartitionData, { key: currentMonth.format('MM') });
110
113
  formattedData.push({
111
114
  key: currentMonth.startOf('month').format('MMM'),
@@ -116,7 +119,7 @@ module.exports = async (currentModel, data) => {
116
119
  // Year timeframe
117
120
  else if (data.timeframe === 'year') {
118
121
  for (let i = 1; i <= 8; i++) {
119
- const currentYear = moment().subtract(i, 'year').startOf('year');
122
+ const currentYear = toDate.clone().subtract(i, 'year').startOf('year');
120
123
  const countForTheTimeframe = _.find(repartitionData, { key: currentYear.format('YYYY') });
121
124
  formattedData.push({
122
125
  key: currentYear.startOf('year').format('YYYY'),
@@ -3,6 +3,8 @@ const { serializeError } = require('serialize-error');
3
3
  const _ = require('lodash');
4
4
  const moment = require('moment');
5
5
 
6
+ const pjson = require('../../package.json');
7
+
6
8
  const getModelProperties = model => {
7
9
  let modelFields = [];
8
10
  const modelProps = model.schema.paths;
@@ -354,6 +356,14 @@ module.exports.constructSearch = (search, fieldsToSearchIn, fieldsToPopulate = [
354
356
  return params;
355
357
  };
356
358
 
359
+ module.exports.getModelWhereClause = (model, idsArray) => {
360
+ return { _id: idsArray };
361
+ };
362
+
363
+ module.exports.getModelPrimaryKeys = model => {
364
+ return ['_id'];
365
+ };
366
+
357
367
  module.exports.getModelAssociations = model => {
358
368
  // Get current model mongoose realname
359
369
  const currentModelRealName = getModelRealname(model);
@@ -462,4 +472,11 @@ module.exports.getCleanOrderStructure = orderConfig => {
462
472
  order[oc[0]] = oc[1];
463
473
  });
464
474
  return order;
475
+ };
476
+
477
+ module.exports.getAppConfig = () => {
478
+ return {
479
+ package: pjson.name,
480
+ version: pjson.version
481
+ };
465
482
  };
package/database.js DELETED
@@ -1,16 +0,0 @@
1
- const mongoose = require('mongoose');
2
-
3
- const User = require('./models/user');
4
- const Car = require('./models/car');
5
- const Blocked = require('./models/blocked');
6
-
7
- const connectDb = () => {
8
- return mongoose.connect('mongodb://localhost:27017/node-express-mongodb-server', {
9
- useNewUrlParser: true,
10
- useUnifiedTopology: true
11
- });
12
- };
13
-
14
- const models = { User, Car, Blocked };
15
-
16
- module.exports = { models, connectDb };
package/migration.js DELETED
@@ -1,234 +0,0 @@
1
- const { models, connectDb } = require('./database');
2
-
3
- connectDb().then(async () => {
4
- console.log('MongoDB connected');
5
-
6
- // Remove all entries
7
- await Promise.all([
8
- models.User.deleteMany({}),
9
- models.Car.deleteMany({}),
10
- models.Blocked.deleteMany({})
11
- ]);
12
-
13
- // Create users
14
- await models.User.insertMany([
15
- {
16
- _id: '5cd5308e695db945d3cc81a1',
17
- firstname: 'John',
18
- lastname: 'Doe',
19
- birthdate: '2021-04-02T00:00:00.000Z',
20
- rating: 4,
21
- createdAt: '2021-04-02T00:00:00.000Z',
22
- updatedAt: '2021-04-02T00:00:00.000Z'
23
- },
24
- {
25
- _id: '5cd5308e695db945d3cc81a2',
26
- firstname: 'Maria',
27
- lastname: 'Doe',
28
- birthdate: '2021-04-02T00:00:00.000Z',
29
- rating: 5,
30
- createdAt: '2021-04-02T00:00:00.000Z',
31
- updatedAt: '2021-04-02T00:00:00.000Z'
32
- }
33
- ]);
34
-
35
- // Create cars
36
- await models.Car.insertMany([
37
- {
38
- _id: '5cd5308e695db945d3cc81b1',
39
- name: 'Porsche 356',
40
- manufacturer: 'Porsche',
41
- year: 1948,
42
- user_id: '5cd5308e695db945d3cc81a1',
43
- createdAt: "2021-04-02T00:00:00.000Z",
44
- updatedAt: "2021-04-02T00:00:00.000Z"
45
- },
46
- {
47
- _id: '5cd5308e695db945d3cc81b2',
48
- name: 'Porsche 550',
49
- manufacturer: 'Porsche',
50
- year: 1953,
51
- user_id: '5cd5308e695db945d3cc81a2',
52
- createdAt: "2021-04-02T00:00:00.000Z",
53
- updatedAt: "2021-04-02T00:00:00.000Z"
54
- },
55
- {
56
- _id: '5cd5308e695db945d3cc81b3',
57
- name: 'Porsche 911',
58
- manufacturer: 'Porsche',
59
- year: 1963,
60
- user_id: '5cd5308e695db945d3cc81a1',
61
- createdAt: "2021-04-02T00:00:00.000Z",
62
- updatedAt: "2021-04-02T00:00:00.000Z"
63
- },
64
- {
65
- _id: '5cd5308e695db945d3cc81b4',
66
- name: 'Porsche 904',
67
- manufacturer: 'Porsche',
68
- year: 1964,
69
- user_id: '5cd5308e695db945d3cc81a2',
70
- createdAt: "2021-04-02T00:00:00.000Z",
71
- updatedAt: "2021-04-02T00:00:00.000Z"
72
- },
73
- {
74
- _id: '5cd5308e695db945d3cc81b5',
75
- name: 'Porsche 906',
76
- manufacturer: 'Porsche',
77
- year: 1966,
78
- user_id: '5cd5308e695db945d3cc81a1',
79
- createdAt: "2021-04-02T00:00:00.000Z",
80
- updatedAt: "2021-04-02T00:00:00.000Z"
81
- },
82
- {
83
- _id: '5cd5308e695db945d3cc81b6',
84
- name: 'Porsche 908',
85
- manufacturer: 'Porsche',
86
- year: 1968,
87
- user_id: '5cd5308e695db945d3cc81a2',
88
- createdAt: "2021-04-02T00:00:00.000Z",
89
- updatedAt: "2021-04-02T00:00:00.000Z"
90
- },
91
- {
92
- _id: '5cd5308e695db945d3cc81b7',
93
- name: 'Porsche 914',
94
- manufacturer: 'Porsche',
95
- year: 1969,
96
- user_id: '5cd5308e695db945d3cc81a1',
97
- createdAt: "2021-04-02T00:00:00.000Z",
98
- updatedAt: "2021-04-02T00:00:00.000Z"
99
- },
100
- {
101
- _id: '5cd5308e695db945d3cc81b8',
102
- name: 'Porsche 917',
103
- manufacturer: 'Porsche',
104
- year: 1969,
105
- user_id: '5cd5308e695db945d3cc81a2',
106
- createdAt: "2021-04-02T00:00:00.000Z",
107
- updatedAt: "2021-04-02T00:00:00.000Z"
108
- },
109
- {
110
- _id: '5cd5308e695db945d3cc81b9',
111
- name: 'Porsche 924',
112
- manufacturer: 'Porsche',
113
- year: 1976,
114
- user_id: '5cd5308e695db945d3cc81a2',
115
- createdAt: "2021-04-02T00:00:00.000Z",
116
- updatedAt: "2021-04-02T00:00:00.000Z"
117
- },
118
- {
119
- _id: '5cd5308e695db945d3cc81c1',
120
- name: 'Porsche Macan',
121
- manufacturer: 'Porsche',
122
- year: 2014,
123
- user_id: '5cd5308e695db945d3cc81a2',
124
- createdAt: "2021-04-02T00:00:00.000Z",
125
- updatedAt: "2021-04-02T00:00:00.000Z"
126
- },
127
- {
128
- _id: '5cd5308e695db945d3cc81c2',
129
- name: 'Ferrari 212 Inter',
130
- manufacturer: 'Ferrari',
131
- year: 1951,
132
- user_id: '5cd5308e695db945d3cc81a1',
133
- createdAt: "2021-04-02T00:00:00.000Z",
134
- updatedAt: "2021-04-02T00:00:00.000Z"
135
- },
136
- {
137
- _id: '5cd5308e695db945d3cc81c3',
138
- name: 'Ferrari 250 MM',
139
- manufacturer: 'Ferrari',
140
- year: 1952,
141
- user_id: '5cd5308e695db945d3cc81a2',
142
- createdAt: "2021-04-02T00:00:00.000Z",
143
- updatedAt: "2021-04-02T00:00:00.000Z"
144
- },
145
- {
146
- _id: '5cd5308e695db945d3cc81c4',
147
- name: 'Ferrari 250 GT Coupé',
148
- manufacturer: 'Ferrari',
149
- year: 1954,
150
- user_id: '5cd5308e695db945d3cc81a1',
151
- createdAt: "2021-04-02T00:00:00.000Z",
152
- updatedAt: "2021-04-02T00:00:00.000Z"
153
- },
154
- {
155
- _id: '5cd5308e695db945d3cc81c5',
156
- name: 'Ferrari 250 GT Berlinetta SWB',
157
- manufacturer: 'Ferrari',
158
- year: 1959,
159
- user_id: '5cd5308e695db945d3cc81a2',
160
- createdAt: "2021-04-02T00:00:00.000Z",
161
- updatedAt: "2021-04-02T00:00:00.000Z"
162
- },
163
- {
164
- _id: '5cd5308e695db945d3cc81c6',
165
- name: 'Ferrari 250 GT 2+2',
166
- manufacturer: 'Ferrari',
167
- year: 1960,
168
- user_id: '5cd5308e695db945d3cc81a1',
169
- createdAt: "2021-04-02T00:00:00.000Z",
170
- updatedAt: "2021-04-02T00:00:00.000Z"
171
- },
172
- {
173
- _id: '5cd5308e695db945d3cc81c7',
174
- name: 'Ferrari 250 GTO',
175
- manufacturer: 'Ferrari',
176
- year: 1962,
177
- user_id: '5cd5308e695db945d3cc81a2',
178
- createdAt: "2021-04-02T00:00:00.000Z",
179
- updatedAt: "2021-04-02T00:00:00.000Z"
180
- },
181
- {
182
- _id: '5cd5308e695db945d3cc81c8',
183
- name: 'Ferrari 250 GT Lusso',
184
- manufacturer: 'Ferrari',
185
- year: 1962,
186
- user_id: '5cd5308e695db945d3cc81a1',
187
- createdAt: "2021-04-02T00:00:00.000Z",
188
- updatedAt: "2021-04-02T00:00:00.000Z"
189
- },
190
- {
191
- _id: '5cd5308e695db945d3cc81c9',
192
- name: 'Ferrari 488',
193
- manufacturer: 'Ferrari',
194
- year: 1969,
195
- user_id: '5cd5308e695db945d3cc81a2',
196
- createdAt: "2021-04-02T00:00:00.000Z",
197
- updatedAt: "2021-04-02T00:00:00.000Z"
198
- },
199
- {
200
- _id: '5cd5308e695db945d3cc81d1',
201
- name: 'Ferrari 275',
202
- manufacturer: 'Ferrari',
203
- year: 1964,
204
- user_id: '5cd5308e695db945d3cc81a2',
205
- createdAt: "2021-04-02T00:00:00.000Z",
206
- updatedAt: "2021-04-02T00:00:00.000Z"
207
- },
208
- {
209
- _id: '5cd5308e695db945d3cc81d2',
210
- name: 'Ferrari 250 LM',
211
- manufacturer: 'Ferrari',
212
- year: 1964,
213
- user_id: '5cd5308e695db945d3cc81a2',
214
- createdAt: "2021-04-02T00:00:00.000Z",
215
- updatedAt: "2021-04-02T00:00:00.000Z"
216
- }
217
- ]);
218
-
219
- // Create Blocked
220
- await models.Blocked.insertMany([
221
- {
222
- _id: '5cd5308e695db945d3cc81c1',
223
- blocked_id: '5cd5308e695db945d3cc81a1',
224
- blocked_model: 'User'
225
- },
226
- {
227
- _id: '5cd5308e695db945d3cc81c2',
228
- blocked_id: '5cd5308e695db945d3cc81b1',
229
- blocked_model: 'Car'
230
- }
231
- ]);
232
-
233
- console.log('Done!');
234
- });
package/models/blocked.js DELETED
@@ -1,17 +0,0 @@
1
- const mongoose = require('mongoose');
2
- const Schema = mongoose.Schema;
3
-
4
- const BlockedSchema = new Schema({
5
- blocked_id: {
6
- type: Schema.Types.ObjectId,
7
- required: true,
8
- refPath: 'blocked_model'
9
- },
10
- blocked_model: {
11
- type: String,
12
- required: true,
13
- enum: ['User', 'Car']
14
- }
15
- });
16
-
17
- module.exports = mongoose.model('Blocked', BlockedSchema, 'blocked');
package/models/car.js DELETED
@@ -1,26 +0,0 @@
1
- const mongoose = require('mongoose');
2
- const Schema = mongoose.Schema;
3
-
4
- const CarSchema = new Schema({
5
- user_id: {
6
- type: Schema.Types.ObjectId,
7
- ref: 'User'
8
- },
9
- name: {
10
- type: String
11
- },
12
- manufacturer: {
13
- type: String
14
- },
15
- year: {
16
- type: Number
17
- },
18
- createdAt: {
19
- type: Date
20
- },
21
- updatedAt: {
22
- type: Date
23
- }
24
- });
25
-
26
- module.exports = mongoose.model('Car', CarSchema, 'cars');
package/models/user.js DELETED
@@ -1,25 +0,0 @@
1
- const mongoose = require('mongoose');
2
- const Schema = mongoose.Schema;
3
-
4
- const UserSchema = new Schema({
5
- firstname: {
6
- type: String
7
- },
8
- lastname: {
9
- type: String
10
- },
11
- birthdate: {
12
- type: Date
13
- },
14
- rating: {
15
- type: Number
16
- },
17
- createdAt: {
18
- type: Date
19
- },
20
- updatedAt: {
21
- type: Date
22
- }
23
- });
24
-
25
- module.exports = mongoose.model('User', UserSchema, 'users');