Search
 
SCRIPT & CODE EXAMPLE
 

SQL

express api ith mysql data

const sql = require("./db.js");

// constructor
const Tutorial = function(tutorial) {
  this.title = tutorial.title;
  this.description = tutorial.description;
  this.published = tutorial.published;
};

Tutorial.create = (newTutorial, result) => {
  sql.query("INSERT INTO tutorials SET ?", newTutorial, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    console.log("created tutorial: ", { id: res.insertId, ...newTutorial });
    result(null, { id: res.insertId, ...newTutorial });
  });
};

Tutorial.findById = (id, result) => {
  sql.query(`SELECT * FROM tutorials WHERE id = ${id}`, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    if (res.length) {
      console.log("found tutorial: ", res[0]);
      result(null, res[0]);
      return;
    }

    // not found Tutorial with the id
    result({ kind: "not_found" }, null);
  });
};

Tutorial.getAll = (title, result) => {
  let query = "SELECT * FROM tutorials";

  if (title) {
    query += ` WHERE title LIKE '%${title}%'`;
  }

  sql.query(query, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    console.log("tutorials: ", res);
    result(null, res);
  });
};

Tutorial.getAllPublished = result => {
  sql.query("SELECT * FROM tutorials WHERE published=true", (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    console.log("tutorials: ", res);
    result(null, res);
  });
};

Tutorial.updateById = (id, tutorial, result) => {
  sql.query(
    "UPDATE tutorials SET title = ?, description = ?, published = ? WHERE id = ?",
    [tutorial.title, tutorial.description, tutorial.published, id],
    (err, res) => {
      if (err) {
        console.log("error: ", err);
        result(null, err);
        return;
      }

      if (res.affectedRows == 0) {
        // not found Tutorial with the id
        result({ kind: "not_found" }, null);
        return;
      }

      console.log("updated tutorial: ", { id: id, ...tutorial });
      result(null, { id: id, ...tutorial });
    }
  );
};

Tutorial.remove = (id, result) => {
  sql.query("DELETE FROM tutorials WHERE id = ?", id, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    if (res.affectedRows == 0) {
      // not found Tutorial with the id
      result({ kind: "not_found" }, null);
      return;
    }

    console.log("deleted tutorial with id: ", id);
    result(null, res);
  });
};

Tutorial.removeAll = result => {
  sql.query("DELETE FROM tutorials", (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(null, err);
      return;
    }

    console.log(`deleted ${res.affectedRows} tutorials`);
    result(null, res);
  });
};

module.exports = Tutorial;
Comment

PREVIOUS NEXT
Code Example
Sql :: convert db timestamp to date 
Sql :: sql multiple into 
Sql :: oracle sqlp update amount / quantity 
Sql :: get who is hired in specific month in sql 
Sql :: oracle sql developer script output limit 
Sql :: What are the advantages of MySQL when compared with Oracle? 
Sql :: Is there a way to use read_sql_query and the query has WHERE column = Variable? 
Sql :: mysql Puede ser solamente un campo automatico y este debe ser definido como una clave 
Sql :: oracle database connection visual studio 2019 
Sql :: get all jobs if salary more than 5500 less than 10000 sql 
Sql :: SQL IN Operator With Duplicate Values 
Sql :: create user oracle hash by value 
Sql :: postgres row expiration 
Sql :: how to put value in parameters in mysqldataadapter 
Sql :: sql gather statistics to increase performance 
Sql :: insert data mysql with cmd 
Sql :: ceil upto 2 decimal place mysql 
Sql :: phpmyadmin mysql conflict 
Sql :: low level operator in dbms 
Sql :: how to change null display in psql 
Sql :: copy row from db to db mysql 
Sql :: oracle pl/sql check if file exists 
Sql :: how to check table in postgresql from terminal 
Sql :: how to change oracle sid name in 19c database 
Sql :: Laravel SQLSTATE[HY093] with array query 
Sql :: oracle query archive mode 
Sql :: USING THE NOT CONDITION IN SQL 
Sql :: Run batch file from SQL 
Sql :: sql oop example 
Sql :: sql include rows with 0 values 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =