Skip to content

Commit

Permalink
Create model with mongoose and insert three test records
Browse files Browse the repository at this point in the history
Setup app for heroku.
  • Loading branch information
rudolfgrauberger committed Oct 17, 2018
1 parent 0b3926b commit 3431175
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 5 deletions.
55 changes: 55 additions & 0 deletions db/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const mongoose = require('mongoose');
const student = require('../models/student');

mongoose.Promise = global.Promise;

const saveStudent = (student) => {
student.save().then(() => {
console.log(`Saved student ${student}`);
}, (e) => {
console.log(`Unable to save ${student}`);
});
};

const insertTestData = () => {
const rudolf = new student.Student({
Vorname: 'Rudolf',
Nachname: 'Grauberger',
Matrikelnummer: 00000001,
Studiengang: 'INF',
Semester: 5,
EMail: '[email protected]'
});

const max = new student.Student({
Vorname: 'Max',
Nachname: 'Mustermann',
Matrikelnummer: 00000002,
Studiengang: 'MI',
Semester: 6,
EMail: '[email protected]'
});

const peter = new student.Student({
Vorname: 'Peter',
Nachname: 'Mustermann',
Matrikelnummer: 00000003,
Studiengang: 'WI',
Semester: 3,
EMail: '[email protected]'
});

saveStudent(rudolf);
saveStudent(max);
saveStudent(peter);
}

const initDatabase = (connectionString) => {
mongoose.connect(connectionString);

insertTestData();
};

module.exports = {
initDatabase
};
30 changes: 30 additions & 0 deletions models/student.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const mongoose = require('mongoose');

// Definiert den Aufbau des Objekts
const StudentSchema = new mongoose.Schema({
Vorname: {
type: String
},
Nachname: {
type: String
},
Matrikelnummer: {
type: Number
},
Studiengang: {
type: String
},
Semester: {
type: Number
},
EMail: {
type: String
}
});

// Das muss ausgeführt werden, damit ein Constructor für Student zur Verfügung steht
const Student = mongoose.model('Student', StudentSchema);

module.exports = {
Student
};
180 changes: 176 additions & 4 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"bugs": {
"url": "https://github.com/rudolfgrauberger/qq2-eignung-nodejs-fuer-microservice/issues"
},
"homepage": "https://github.com/rudolfgrauberger/qq2-eignung-nodejs-fuer-microservice#readme"
"homepage": "https://github.com/rudolfgrauberger/qq2-eignung-nodejs-fuer-microservice#readme",
"dependencies": {
"mongoose": "^5.3.4"
}
}
6 changes: 6 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const http = require('http');

const db = require('./db/database');

const connectionString = process.env.MONGODB_URI || 'mongodb://localhost:27017/QQ2Project';

db.initDatabase(connectionString);

// Nutzt den Port aus der Umgebungsvariable (für Heroku benötigt)
// alternativ/lokal den Port 3000.
const port = process.env.PORT || 3000;
Expand Down

0 comments on commit 3431175

Please sign in to comment.