Code Monkey home page Code Monkey logo

express-mysql's Introduction

express-mysql

Code for understanding for creating REST API in Node.js with Express and MySQL.

The article/tutorial can be be read here: https://time2hack.com/2019/09/creating-rest-api-in-node-js-with-express-and-mysql/

You cna run this collection in Postman with following button:

Run in Postman


Or Download and import in Postman from here: Download


CREATE DATABASE twitter_clone;

use twitter_clone;

CREATE TABLE users(
  id int NOT NULL AUTO_INCREMENT,
  username varchar(15) NOT NULL,
  password varchar(32) NOT NULL,
  followers int DEFAULT 0,
  following int DEFAULT 0,
  tweets int DEFAULT 0,
  PRIMARY KEY (id)
);

CREATE TABLE following(
  id int NOT NULL AUTO_INCREMENT,
  user1_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  user2_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  PRIMARY KEY (id)
);

CREATE TABLE tweets(
  id int NOT NULL AUTO_INCREMENT,
  username varchar(15) NOT NULL,
  user_id int REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  tweet varchar(140) NOT NULL,
  timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

INSERT INTO USERS(username, password) VALUE('pankaj', MD5('pankaj'));

INSERT INTO USERS(username, password) VALUE('tim', MD5('tim'));

INSERT INTO USERS(username, password) VALUE('jim', MD5('jim'));

INSERT INTO TWEETS(username, user_id, tweet) VALUE('pankaj', 1, 'Hello World Again!');

INSERT INTO FOLLOWING(user1_id, user2_id) VALUE(1, 2);
INSERT INTO FOLLOWING(user1_id, user2_id) VALUE(1, 3);
INSERT INTO FOLLOWING(user1_id, user2_id) VALUE(3, 1);

SELECT * FROM tweets;

# Followers

SELECT
USER_INFO.*, username as user1_username
FROM (SELECT 
user1_id, user2_id, username as user2_username
FROM FOLLOWING LEFT JOIN USERS ON user2_id = users.id
WHERE user1_id = 1) as USER_INFO
LEFT JOIN USERS ON user1_id = users.id ;

# Following

SELECT
USER_INFO.*, username as user1_username
FROM (SELECT 
user1_id, user2_id, username as user2_username
FROM FOLLOWING LEFT JOIN USERS ON user2_id = users.id
WHERE user2_id = 1) as USER_INFO
LEFT JOIN USERS ON user1_id = users.id ;

express-mysql's People

Contributors

pankajpatel avatar

Stargazers

Caio A. Torquato avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.