Skip to content

Commit

Permalink
fc
Browse files Browse the repository at this point in the history
  • Loading branch information
rahil471 committed Feb 6, 2019
1 parent 34dc93e commit 2a07fc5
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app.js
@@ -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();
30 changes: 30 additions & 0 deletions db.js
@@ -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);
});
});
18 changes: 18 additions & 0 deletions models/person.js
@@ -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);
168 changes: 168 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit 2a07fc5

Please sign in to comment.