Create database schema in NODE JS example

In this article, I will explain how to create database schema in Node js application. So as a database, I will use MongoDB, it is very compatible with Node JS because MongoDB stores data in JSON format. In this example, I will create a  schema of student’s detail for MongoDB

In action to create database schema, first create a folder models inside the application. This is the good practice to write all schemas in separate folder. Once you created models folder, create a student.js file.
Steps:
1)      Create a folder models in application.
2)      Create Student.js file inside the models folder.
3)      Copy and paste below code in Student.js file

const mongoose=require('mongoose');
const Schema= require('mongoose').Schema;

const studentSchema = mongoose.Schema({
    name:{
        type:String,
        required:true,
        unique:true
    },
    class:{
        type:String,
        required:true,
        unique:false
    },
    section:{
        type:String,
        required:true,
        unique:false
    },
    address:{
        type:String,
        required:true,
        unique:false
    },
    bloodgrp:{
        type:String,
        required:true,
        unique:false
    }
})

    const Student=module.exports=mongoose.model('Student', studentSchema);
4)      Now Student schema is ready. Now you need to understand how to use this schema to perform CURD operations.
5)      Open app.js and write below code. (to know more about app.js refer http://java.gunustories.com/2018/06/create-first-node-js-application.html
      http://java.gunustories.com/2018/06/generate-node-js-web-application.html articles.)

const mongoose = require('mongoose')

var stdRouter = require('./routes/student');

app.use('/student',stdRouter);

// connect to mongodb
const db = require('./config/database')
mongoose.connect('mongodb://localhost:27017/', {
  useMongoclient: true
})
  .then(() => {
    console.log('MongoDB connected Successfully')
  })
  .catch((err) => {
    console.log(err)})
6)      Create a route student.js inside the routes folder.
7)      Copy and paste below code inside student.js
var express = require('express');
var router = express.Router();
const mongoose = require('mongoose')

require('../models/Student')
const Student = mongoose.model('Student')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.render('appdetails')
});

router.get('/add', function(req, res, next) {
  const newStudent =new Student();
  newStudent.name='Manoj';
  newStudent.class = '10th';
  newStudent.section='A';
  newStudent.address='XYZ address';
  newStudent.bloodgrp='XYZ group';
  console.log(newStudent);
  newStudent.save()
  res.redirect('/')
});

module.exports = router;
8)      Now run your application using nodemon
9)      Click on http://localhost:3000/student/add to add application details.

In this example, I have hardcoded student’s information. You can read these vaule from GUI(i.e html) and replace from hardcoded values.
      
Thanks for reading this article. Please share this article if you found meaningful for you.

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example