Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require('./db'); | ||
const Person = require('./models/person'); | ||
|
||
const test = async function(){ | ||
const data = { | ||
name: "Jane Doe", | ||
age: 24, | ||
gender: "Female" | ||
} | ||
await Person.addPerson(data); | ||
const p = await Person.listPersons(); | ||
console.log(p); | ||
} | ||
|
||
test(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoURI = "mongodb://localhost:27017/ciphertrick"; //connecting to ciphertrick | ||
const options = { | ||
keepAlive: true, | ||
keepAliveInitialDelay: 300000, | ||
useNewUrlParser: true | ||
}; | ||
|
||
mongoose.connect(mongoURI, options); | ||
|
||
mongoose.connection.on('connected', ()=>{ | ||
console.log('Mongoose default connection open to ' + mongoURI); | ||
}); | ||
|
||
// If the connection throws an error | ||
mongoose.connection.on('error', (err)=>{ | ||
console.log('handle mongo errored connections: ' + err); | ||
}); | ||
|
||
// When the connection is disconnected | ||
mongoose.connection.on('disconnected', ()=>{ | ||
console.log('Mongoose default connection disconnected'); | ||
}); | ||
|
||
process.on('SIGINT', ()=>{ | ||
mongoose.connection.close(()=>{ | ||
console.log('App terminated, closing mongo connections'); | ||
process.exit(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const mongoose = require("mongoose"); | ||
var Schema = new mongoose.Schema({ | ||
name: {type:String, required:true}, | ||
age: {type: Number, required: true}, | ||
gender: {type: String, enum: ['Male', 'Female', 'Other']} | ||
}, { timestamps: true}); | ||
|
||
Schema.statics.addPerson = async function(person){ | ||
var Person = new this(person); | ||
var result = await Person.save(person); | ||
return result; | ||
} | ||
|
||
Schema.statics.listPersons = async function(){ | ||
return await this.find(); | ||
} | ||
|
||
module.exports = mongoose.model('person', Schema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "mongodb-tutorial", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"mongoose": "^5.4.6" | ||
} | ||
} |