Skip to content

Commit 3b43134

Browse files
a'
1 parent dec1ed2 commit 3b43134

File tree

5 files changed

+226
-11
lines changed

5 files changed

+226
-11
lines changed

Readme.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@ This is the seed application for node js
33
## Available Scripts
44

55
In the project directory, you can run:
6-
> npm install
7-
> npm start
8-
> localhost:5000
6+
>> npm install
7+
>> npm start
8+
>> localhost:5000
9+
10+
## Available Test Script
11+
12+
### `npm test`
13+
Using Mocha and chai for testing
14+
Add test cases in test folder
15+
16+
## Database connector
17+
Added mongodb file with all
18+
>> GET
19+
>> POST
20+
>> PUT
21+
>> DELETE
22+
Operation just call mongodb file where ever required
923

package-lock.json

Lines changed: 65 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "@aakashdeveloper/create-node-app",
3-
"version": "1.0.4",
3+
"version": "1.0.9",
44
"description": "The Seed will help you build node app with es6 very quick",
55
"main": "index.js",
66
"scripts": {
7-
"start":"node start.js"
7+
"start": "node start.js",
8+
"dev": "nodemon start.js",
9+
"test": "mocha --timeout 10000"
810
},
911
"repository": {
1012
"type": "git",
11-
"url": "git+https://github.com/Aakashdeveloper/March_React_Mrng.git"
13+
"url": "git+https://github.com/Aakashdeveloper/create-node-app"
1214
},
1315
"keywords": [
1416
"Node",
@@ -19,14 +21,15 @@
1921
"author": "aakashdeveloper",
2022
"license": "ISC",
2123
"bugs": {
22-
"url": "https://github.com/Aakashdeveloper/March_React_Mrng/issues"
24+
"url": "https://github.com/Aakashdeveloper/create-node-app/issues"
2325
},
24-
"homepage": "https://github.com/Aakashdeveloper/March_React_Mrng#readme",
26+
"homepage": "https://github.com/Aakashdeveloper/create-node-app#readme",
2527
"dependencies": {
2628
"@babel/core": "^7.4.3",
2729
"@babel/preset-env": "^7.4.3",
2830
"@babel/register": "^7.4.0",
2931
"babel-polyfill": "^6.26.0",
30-
"express": "^4.16.4"
32+
"express": "^4.16.4",
33+
"mongodb": "^3.2.3"
3134
}
3235
}

src/datatbase/mongoconnect.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import mongodb from 'mongodb'
2+
const MongoClient = mongodb.MongoClient;
3+
const url = "mongodb://localhost:27017";
4+
5+
const maincall = (myObj) => {
6+
MongoClient.connect(url,(err,db)=> {
7+
if(err) throw err;
8+
var dbo = db.db('testdata')
9+
dbo.collection('first').insertOne(myObj,(err) => {
10+
if(err) throw err;
11+
console.log('data inserted')
12+
db.close();
13+
})
14+
})
15+
}
16+
17+
// Post Call
18+
maincall.prototype.postData = (colName,myObj) => {
19+
MongoClient.connect(url,(err,db)=> {
20+
if(err) throw err;
21+
var dbo = db.db('testdata')
22+
dbo.collection(colName).insertOne(myObj,(err) => {
23+
if(err) throw err;
24+
console.log('data inserted')
25+
db.close();
26+
})
27+
})
28+
29+
return outres;
30+
}
31+
32+
// PUt Call
33+
maincall.prototype.putData = (colName,query,myObj) => {
34+
MongoClient.connect(url,(err,db)=> {
35+
if(err) throw err;
36+
var dbo = db.db('testdata')
37+
dbo.collection(colName).update(query,myObj,(err) => {
38+
if(err) throw err;
39+
console.log('data updated')
40+
db.close();
41+
})
42+
})
43+
44+
return outres;
45+
}
46+
47+
// Delete Call
48+
maincall.prototype.deleteData = (colName,query) => {
49+
MongoClient.connect(url,(err,db)=> {
50+
if(err) throw err;
51+
var dbo = db.db('testdata')
52+
dbo.collection(colName).deleteOne(query,(err) => {
53+
if(err) throw err;
54+
console.log('data deleted')
55+
db.close();
56+
})
57+
})
58+
59+
return outres;
60+
}
61+
62+
// Get Call
63+
let outres;
64+
maincall.prototype.getData = (colName) => {
65+
MongoClient.connect(url,(err,db)=> {
66+
if(err) throw err;
67+
var dbo = db.db('testdata')
68+
dbo.collection(colName).find({}).toArray(
69+
(err,results) => {
70+
if(err) throw err
71+
outres = results
72+
})
73+
})
74+
75+
return outres;
76+
}
77+
78+
maincall.prototype.getDataByName = (colName,query) => {
79+
MongoClient.connect(url,(err,db)=> {
80+
if(err) throw err;
81+
var dbo = db.db('testdata')
82+
dbo.collection(colName).find({query}).toArray(
83+
(err,results) => {
84+
if(err) throw err
85+
outres = results
86+
})
87+
})
88+
89+
return outres;
90+
}
91+
92+
module.exports= maincall;

test/common.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let chai = require('chai');
2+
let chaiHttp = require('chai-http');
3+
let expect = chai.expect;
4+
chai.use(chaiHttp);
5+
6+
describe('Testing my get Api', () => {
7+
it('should be return status 200 for /', function(done){
8+
chai
9+
.request('http://localhost:7600')
10+
.get('/')
11+
.then(function(res){
12+
expect(res).to.have.status(200);
13+
done();
14+
})
15+
.catch(function(err){
16+
throw(err);
17+
});
18+
});
19+
it('should be return status 200 for about', function(done){
20+
chai
21+
.request('http://localhost:7600')
22+
.get('/about')
23+
.then(function(res){
24+
expect(res).to.have.status(200);
25+
done();
26+
})
27+
.catch(function(err){
28+
throw(err);
29+
});
30+
});
31+
it('should be return status 201 for about', function(done){
32+
chai
33+
.request('http://localhost:7600')
34+
.get('/abouts')
35+
.then(function(res){
36+
expect(res).to.have.status(404);
37+
done();
38+
})
39+
.catch(function(err){
40+
throw(err);
41+
});
42+
});
43+
})

0 commit comments

Comments
 (0)