Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

crud in node

# Config DB and query function 

const mysql = require('mysql');
const mysqlConnection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'QAZwsx@123',
    database: 'blog',
    multipleStatements: true
});

mysqlConnection.connect((err) => {
    if (!err) {
        console.log('DB Connection Established Successfully');
        return true;
    } else {
        console.log('Connection Failed!' + JSON.stringify(err, undefined, 2));
        return false;
    }
});

class DB {
    constructor() {
        this.connection = mysqlConnection;
    }
    // All categories
    getCategories() {
        return new Promise((resolve, reject) => {
            this.connection.query('SELECT * FROM categories', (err, rows) => {
                if (!err)
                    resolve(rows);
                else
                    reject(err);
            });
        });
    }
    // Get category by id
    getCategoryById(id) {
        return new Promise((resolve, reject) => {
            this.connection.query('SELECT * FROM categories WHERE id = ?', [id], (err, rows) => {
                if (!err)
                    resolve(rows);
                else
                    reject(err);
            });
        });
    }
    // Add category
    addCategory(category) {
        return new Promise((resolve, reject) => {
            this.connection.query('INSERT INTO categories SET ?', [category], (err, rows) => {
                if (!err)
                    resolve(this.getCategories());
                else
                    reject(err);
            });
        });
    }
    // Update category
    updateCategory(id, category) {
        return new Promise((resolve, reject) => {
            this.connection.query('UPDATE categories SET ? WHERE id = ?', [category, id], (err, rows) => {
                if (!err)
                    resolve(this.getCategories());
                else
                    reject(err);
            });
        });
    }
    // Delete category
    deleteCategory(id) {
        return new Promise((resolve, reject) => {
            this.connection.query('DELETE FROM categories WHERE id = ?', [id], (err, rows) => {
                if (!err)
                    resolve(this.getCategories());
                else
                    reject(err);
            });
        });
    }
}
module.exports = DB;
Comment

node js crud operation

npm install --save express body-parser mongoose
Comment

crud in node

# For frontend follow the below link
https://www.tutsmake.com/first-crud-node-express-js-mysql-example/
Comment

crud in node

# Crud Operations
const db = require('./DB');
var dbInstance = new db();
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Listening on port ${port}..`));
//Configuring express server
app.use(bodyparser.json());
// Running express server
app.get('/', function (req, res) {
    res.send('Hello World!')
});
// Get all categories
app.get('/categories', function (req, res) {
    let categories = dbInstance.getCategories();
    categories.then((result) => {
        res.send(result);
    }).catch((err) => {
        res.send(err);
    });
});
// Get category by id
app.patch('/categories/:id', function (req, res) {
    let id = req.params.id;
    let category = dbInstance.getCategoryById(id);
    category.then((result) => {
        res.send(result);
    }).catch((err) => {
        res.send(err);
    });
});
// Add category
app.post('/categories', function (req, res) {
    let category = req.body;
    let addCategory = dbInstance.addCategory(category);
    addCategory.then((result) => {
        res.send(result);
    }).catch((err) => {
        res.send(err);
    });
});
// Update category
app.put('/categories/:id', function (req, res) {
    let id = req.params.id;
    let category = req.body;
    let updateCategory = dbInstance.updateCategory(id, category);
    updateCategory.then((result) => {
        res.send(result);
    }).catch((err) => {
        res.send(err);
    });
});
// Delete category
app.delete('/categories/:id', function (req, res) {
    let id = req.params.id;
    let deleteCategory = dbInstance.deleteCategory(id);
    deleteCategory.then((result) => {
        res.send(result);
    }).catch((err) => {
        res.send(err);
    });
});

Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript close calendar after select 
Javascript :: cy visit cypress 
Javascript :: React ES6 Arrow Functions 
Javascript :: shorthand if in javascript with return 
Javascript :: javascript rest parameters vs spread operator 
Javascript :: jsx else if statement 
Javascript :: Is there an “exists” function for jQuery 
Javascript :: column to comma separated string in mongodb 
Javascript :: jest test thunk 
Javascript :: install svelte router 
Javascript :: ajaxstart not working in chrome 
Javascript :: reverse array in javascript 
Javascript :: js sleep function 
Javascript :: js get target foreach 
Javascript :: JQuery UI Saving Sortable List 
Javascript :: inject html via template tags js 
Javascript :: jquery-3.5.0.min.js 
Javascript :: chart js react 
Javascript :: image loading in Compose display image 
Javascript :: using dot prototype with constructor in javascript 
Javascript :: js .length 
Javascript :: reac native play sound 
Javascript :: deleting an instance in sequelize 
Javascript :: how contvert array to string like implode in jquery 
Javascript :: sending api request in axios with files 
Javascript :: yup.array not working 
Javascript :: javascript get string byte size 
Javascript :: react native updating options with setoptions 
Javascript :: adobe target triggerview 
Javascript :: custom css mui 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =