Skip to content

Commit 53196bb

Browse files
authored
Merge pull request #10 from iolufemi/feature/APIModel
feature(API Model):Added API model feature
2 parents 68c02d9 + 0d8957b commit 53196bb

File tree

20 files changed

+703
-140
lines changed

20 files changed

+703
-140
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
- mongodb
55
- redis-server
66
env:
7-
- MONGOLAB_URL=mongodb://127.0.0.1/snipe LOG_MONGOLAB_URL=mongodb://127.0.0.1/snipelogs RATE_LIMIT=10 REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no ENFORCE_USER_ID_APP_ID_DEVELOPER_ID=yes
7+
- MONGOLAB_URL=mongodb://127.0.0.1/snipe LOG_MONGOLAB_URL=mongodb://127.0.0.1/snipelogs REDIS_URL=redis://127.0.0.1/1 SECURE_MODE=true NO_CACHE=no
88
addons:
99
apt:
1010
sources:
@@ -19,6 +19,9 @@ before_script:
1919
- npm install -g gulp mocha gulp-cli codecov istanbul
2020
install:
2121
- npm install
22+
- gulp service -n api
23+
- gulp service -n test -b http://localhost:8080 -e apis
24+
- npm start >> femi.log . &
2225
script:
2326
- >-
2427
istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ Then generate your first API endpoint
5151
$ gulp service --name yourFirstEndpoint // This command will create a CRUD endpoint for yourFirstEndpoint.
5252
```
5353

54+
With the `gulp service` command, you have the option of using either Mongo DB for your database model or using an API generated by this Express Generator as a database model. To use an API as a database you can pass the `baseurl` and the `endpoint` for the API to the `gulp service `. See an example below
55+
56+
```
57+
$ gulp service --name yourEndpointWithAPIAsDB --baseurl http://localhost:8080 --endpoint users
58+
```
59+
60+
> Note: You can use -n instead of --name, -b instead of --baseurl, -e instead of --endpoint
61+
5462
Try out your new endpoint.
5563

5664
Start the app

TODO.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
| app.js | 62 | Write a complete Documentation for this project
55
| config/index.js | 12 | Test for production and development senarios
66
| models/index.js | 14 | Automatically generate tests with the schema structure
7-
| routes/index.js | 314 | Test API versioning
8-
| routes/index.js | 315 | Test rate limiting
9-
| routes/index.js | 316 | Test complete route Loader test
10-
| routes/index.js | 317 | Test _sanitizeRequestUrl middleware function
11-
| routes/index.js | 318 | Test _allRequestData middleware function for default value scenario
12-
| routes/index.js | 319 | Test _enforceUserIdAndAppId middle function for when req.body is an array
13-
| routes/index.js | 320 | Make Log requests testable and write unit tests for it
14-
| routes/index.js | 321 | Develop the route loader into a separate node module to be publish on npm
15-
| routes/index.js | 322 | Develop all services onto separate node module to be publish on npm
7+
| routes/index.js | 294 | Test API versioning
8+
| routes/index.js | 295 | Test rate limiting
9+
| routes/index.js | 296 | Test complete route Loader test
10+
| routes/index.js | 297 | Test _sanitizeRequestUrl middleware function
11+
| routes/index.js | 298 | Test _allRequestData middleware function for default value scenario
12+
| routes/index.js | 299 | Test _enforceUserIdAndAppId middle function for when req.body is an array
13+
| routes/index.js | 300 | Make Log requests testable and write unit tests for it
14+
| routes/index.js | 301 | Develop the route loader into a separate node module to be publish on npm
15+
| routes/index.js | 302 | Develop all services onto separate node module to be publish on npm
1616
| services/logger/index.js | 36 | Test Error Handler
1717
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
1818
| services/queue/clock.js | 12 | Use the cron package here https://www.npmjs.com/package/cron for timer

conf.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

config/development.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2323
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2424
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
25-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no',
26+
apiDBKey: process.env.API_DB_Key || 'MDg4NWM1NTA0ZTZlNTQ5MjAzNzA1ODBlOWVkNzI3MzdlNmYxZTcyMjVkOTA3N2JjYTBhZjA0YmM0N2U4NDZkNi8vLy8vLzQ1MDY='
2627
};

config/production.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2323
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2424
queueUIPort: process.env.QUEUE_UI_PORT || 3000,
25-
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no'
25+
enforceUserIdAppIdDeveloperId: process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID || 'no',
26+
apiDBKey: process.env.API_DB_Key || 'MDg4NWM1NTA0ZTZlNTQ5MjAzNzA1ODBlOWVkNzI3MzdlNmYxZTcyMjVkOTA3N2JjYTBhZjA0YmM0N2U4NDZkNi8vLy8vLzQ1MDY='
2627
};

gulpfile.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ gulp.task('test', function() {
4545
process.env.SECURE_MODE = true;
4646
process.env.NO_CACHE = 'no';
4747
process.env.NODE_ENV = 'test';
48-
process.env.ENFORCE_USER_ID_APP_ID_DEVELOPER_ID = 'yes';
4948
gulp.src('./test', {read: false})
5049
// `gulp-mocha` needs filepaths so you can't have any plugins before it
5150
.pipe(mocha({
@@ -55,10 +54,23 @@ gulp.task('test', function() {
5554
);
5655

5756
// Remember to pass argument '--name TheServiceName' or '-n TheServiceName' to the service creation command
57+
// If you want to use an API as a database model, pass the base url and the endpoint. '--baseurl http://google.com' or '--b http://google.com'
58+
// '--endpoint users' or '--e users'
5859
// Note that the name must be singular
5960
gulp.task('service', function(){
6061
var args = argv(process.argv.slice(2));
6162
var name;
63+
var baseurl;
64+
var endpoint;
65+
baseurl = args.baseurl;
66+
endpoint = args.endpoint;
67+
if(!baseurl){
68+
baseurl = args.b;
69+
}
70+
71+
if(!endpoint){
72+
endpoint = args.e;
73+
}
6274
name = args.name;
6375
if(!name){
6476
name = args.n;
@@ -106,20 +118,37 @@ gulp.task('service', function(){
106118
});
107119

108120
// Create the Model
109-
fs.readFile('./template/model.tmpl', function(err, data){
110-
if (err){
111-
throw err;
112-
}
113-
var tpl = _.template(data);
114-
var result = tpl({service: nameCapitalise, object: nameLowerCase});
121+
if(baseurl && endpoint){
122+
fs.readFile('./template/model_api.tmpl', function(err, data){
123+
if (err){
124+
throw err;
125+
}
126+
var tpl = _.template(data);
127+
var result = tpl({baseurl: baseurl, endpoint: endpoint});
115128

116-
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
117-
if (err){
129+
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
130+
if (err){
131+
throw err;
132+
}
133+
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
134+
});
135+
});
136+
}else{
137+
fs.readFile('./template/model.tmpl', function(err, data){
138+
if (err){
118139
throw err;
119140
}
120-
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
141+
var tpl = _.template(data);
142+
var result = tpl({service: nameCapitalise, object: nameLowerCase});
143+
144+
fs.writeFile('./models/'+nameCapitalisePlural+'.js', result, function(err){
145+
if (err){
146+
throw err;
147+
}
148+
console.log('Model created at ./models/'+nameCapitalisePlural+'.js');
149+
});
121150
});
122-
});
151+
}
123152

124153
// Create the Model Unit Test
125154
fs.readFile('./template/model_test.tmpl', function(err, data){

models/RequestLogs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Schema.pre('update', function(next) {
179179
queue.create('searchIndex', ourDoc)
180180
.save();
181181
}
182-
182+
ourDoc.updatedAt = new Date(Date.now()).toISOString();
183183
next();
184184
});
185185

models/Trash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Schema.pre('update', function(next) {
148148
queue.create('searchIndex', ourDoc)
149149
.save();
150150
}
151-
151+
ourDoc.updatedAt = new Date(Date.now()).toISOString();
152152
next();
153153
});
154154

routes/index.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ router._enforceUserIdAndAppId = function(req,res,next){
108108
var appId = req.param('appId');
109109
var developer = req.param('developer');
110110
// The routes after this line will require userid, appid and developer.
111-
if(config.enforceUserIdAppIdDeveloperId === 'yes'){
111+
if(config.enforceUserIdAppIdDeveloperId === 'yes' || req.test){
112112
// Make userId compolsory in every request
113113
if(!userId){
114114
return res.badRequest(false,'No userId parameter was passed in the payload of this request. Please pass a userId.');
@@ -140,27 +140,7 @@ router._enforceUserIdAndAppId = function(req,res,next){
140140
}
141141
}
142142
}else{
143-
req.userId = userId;
144-
req.appId = appId;
145-
req.developer = developer;
146-
if(req.body){
147-
if(req.body && req.body.length){
148-
req.body = _.map(req.body,function(value){
149-
value.client = appId;
150-
value.owner = userId;
151-
value.createdBy = userId;
152-
value.developer = developer;
153-
return value;
154-
});
155-
next();
156-
}else{
157-
req.body.client = appId;
158-
req.body.owner = userId;
159-
req.body.createdBy = userId;
160-
req.body.developer = developer;
161-
next();
162-
}
163-
}
143+
next();
164144
}
165145
};
166146

0 commit comments

Comments
 (0)