├── LICENSE ├── README.md ├── SQLite ├── create_db.js ├── package.json └── sqlite_server.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 anandpatel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SARAL API on Terminal with SQLite-Database 2 | ### In this project I have write Saral-like-API by use of SQLite database. I have create saraldb database in this database create two table courses and exercise. this project we can test on postman also use express module in this project. 3 | 4 | #### Installing the SQLite3 Driver for Node.js 5 | Now that I have created our project, I did install the node-sqlite3 package from npm using the following command: 6 | 7 | sudo apt-get update 8 | npm install sqlite3 --save 9 | 10 | Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. 11 | 12 | #### To install express, run the following command on your terminal in Linux. 13 | 14 | sudo apt-get install npm 15 | npm install express --save 16 | 17 | To run your code, first open your server port using node file_name.js in your working directory in terminal. And, then you can apply the basic CRUD operations with database. 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /SQLite: -------------------------------------------------------------------------------- 1 | Here I have write important of queries of SQLite, we can easily create, insert and delete table by use of these are queries. 2 | 3 | 1) How to create table? 4 | 5 | Here I Created table name = Anand 6 | 7 | CREATE TABLE Anand(id integer, name text, description text); 8 | 9 | -- You can check if you have make or not by using cmd .table if this get successful then you can use 10 | 11 | INSERT INTO Anand (id, name, description) VALUES (1,'name', 'yes'); 12 | or 13 | INSERT INTO Anand values(1,'name','yes'); 14 | 15 | You can SELECT * from Anand; 16 | 17 | .mode column 18 | .headers on 19 | .width 5 7 9; 20 | .save 21 | .quit 22 | 23 | 2) How to change table name? 24 | 25 | ALTER TABLE Anand rename to patel; 26 | 27 | 3) How to do update table? 28 | 29 | update Anand set name = "navgurukul" where id = 1; 30 | 31 | 4) How to do Delete table? 32 | 33 | Delete from Anand where id = 1; 34 | 35 | 5) How to do DELETE database; 36 | 37 | drop database database name; 38 | 39 | -------------------------------------------------------------------------------- /create_db.js: -------------------------------------------------------------------------------- 1 | 2 | var sqlite = require("sqlite3"); 3 | var db = new sqlite.Database("saraldb",(err)=>{ 4 | if (err){ 5 | console.log(err); 6 | }else{ 7 | db.run("create table if not exists courses (id integer Primary key autoincrement, name text, description text)"); 8 | console.log("database make successfully"); 9 | 10 | db.run("create table if not exists exercise (id integer Primary key autoincrement, name text, description text, courses_id integer)"); 11 | console.log("successfully"); 12 | } 13 | }) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sqlite-crud", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "keywords": [], 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "mysql": "^2.17.1", 13 | "sqlite3": "^4.0.6" 14 | }, 15 | "devDependencies": {}, 16 | "description": "" 17 | } 18 | -------------------------------------------------------------------------------- /sqlite_server.js: -------------------------------------------------------------------------------- 1 | var express = require("express"); 2 | var sqlite = require("sqlite3"); 3 | var app = express(); 4 | var create_db = require("./create_db"); 5 | 6 | app.use(express.json()); 7 | 8 | // list of allcourses 9 | app.get("/allcourses", (req, res)=>{ 10 | let db = new sqlite.Database('saraldb',(err)=>{ 11 | if (err){ 12 | console.log(err); 13 | }else{ 14 | db.all("select * from courses", (err,data)=>{ 15 | if (err){ 16 | console.log(err); 17 | }else{ 18 | console.log("success") 19 | return res.send(data); 20 | } 21 | }) 22 | } 23 | }) 24 | }) 25 | 26 | // create new course 27 | app.post('/allcourses',(req,res)=>{ 28 | 29 | var name = req.body.name; 30 | var description = req.body.description; 31 | // var id = req.body.id; 32 | let db = new sqlite.Database('saraldb',(err)=>{ 33 | if (err){ 34 | console.log(err); 35 | }else{ 36 | db.run('insert into courses (name,description) values(" '+name+' "," '+description+'")'); 37 | db.close(); 38 | return res.send('successfully') 39 | } 40 | }) 41 | }) 42 | 43 | // get course details by id 44 | app.get('/allcourses/:id', (req, res)=>{ 45 | let id = req.params.id; 46 | let db = new sqlite.Database('saraldb', (err)=>{ 47 | if (err){ 48 | console.log(err); 49 | }else{ 50 | db.all('select * from courses where id="'+id+'";', (err, data)=>{ 51 | if (err){ 52 | console.log(err); 53 | }else{ 54 | return res.send(data); 55 | console.log("database successfull"); 56 | } 57 | }) 58 | } 59 | }) 60 | }) 61 | 62 | // edit a course by id 63 | app.put("/allcourses/:id", (req, res)=>{ 64 | var name = req.body.name; 65 | var description = req.body.description; 66 | var id = req.params.id 67 | let db = new sqlite.Database('saraldb', (err)=>{ 68 | if (err){ 69 | console.log({"ErrMsg": "check your json file"}); 70 | }else{ 71 | db.run('update exercise set name ="'+name+'",description="'+description+'" where id ="'+id+'"'); 72 | db.close(); 73 | return res.send('Successful'); 74 | } 75 | }) 76 | }) 77 | 78 | // get exercises of a course 79 | app.get("/allcourses/:id/exercise", (req, res)=>{ 80 | let id = req.params.id 81 | // console.log(id) 82 | let db = new sqlite.Database('saraldb', (err)=>{ 83 | if (err){ 84 | console.log({"ErrMsg": "pls check your json file"}) 85 | }else{ 86 | db.all('select * from exercise', (err, data)=>{ 87 | if (err){ 88 | console.log("Error"); 89 | }else{ 90 | var list_allcourses = []; 91 | for (var i of data){ 92 | // console.log(i); 93 | if (i.courses_id == id){ 94 | list_allcourses.push(i); 95 | } 96 | } 97 | return res.send(list_allcourses); 98 | console.log("successful") 99 | } 100 | }); 101 | } 102 | }) 103 | }) 104 | 105 | // create exercise of course 106 | app.post('/allcourses/:id/exercise', (req, res)=>{ 107 | var name = req.body.name; 108 | var description = req.body.description; 109 | var id = req.params.id; 110 | let db = new sqlite.Database('saraldb', (err)=>{ 111 | if (err){ 112 | console.log("err"); 113 | }else{ 114 | db.run('insert into exercise (name, description, courses_id) values ("'+name+' "," '+description+' "," '+id+' ")'); 115 | db.close(); 116 | return res.send("successfull") 117 | } 118 | }) 119 | }) 120 | 121 | // get exercise by Id 122 | app.get('/allcourses/:id/exercise/:Id', (req, res)=>{ 123 | let Id = req.params.Id; 124 | console.log(Id); 125 | // console.log(id); 126 | // let courses_id = req.body.courses_id; 127 | let db = new sqlite.Database('saraldb', (err)=>{ 128 | if (err){ 129 | console.log("err"); 130 | }else{ 131 | db.all('select * from exercise where id="'+Id+'";', (err, data)=>{ 132 | if (err){ 133 | console.log("err"); 134 | }else{ 135 | // console.log(data); 136 | return res.send(data); 137 | console.log("database successfull"); 138 | } 139 | }) 140 | } 141 | }) 142 | }) 143 | 144 | // Delete method by id for allcourses 145 | app.delete("/allcourses/:id", (req, res) =>{ 146 | var delete_id = req.params.id; 147 | console.log(delete_id); 148 | let db = new sqlite.Database("saraldb", (err) =>{ 149 | if (err){ 150 | console.log(err); 151 | }else{ 152 | db.all("DELETE from courses where id = '"+delete_id+"' ",(err)=>{ 153 | if (err){ 154 | console.log(err); 155 | }else{ 156 | console.log("Delete successfully......."); 157 | res.send("delete"); 158 | } 159 | }) 160 | } 161 | }) 162 | }) 163 | 164 | // get method for exercise 165 | app.get("/exercise", (req, res) =>{ 166 | let db = new sqlite.Database('saraldb',(err) =>{ 167 | if (err){ 168 | console.log(err); 169 | }else{ 170 | db.all("SELECT * from exercise",(err, data) =>{ 171 | if (err){ 172 | console.log(err); 173 | }else{ 174 | console.log("get method successfully done....."); 175 | res.send(data); 176 | } 177 | }); 178 | } 179 | }) 180 | }) 181 | 182 | // get method for exercise by id 183 | app.get("/exercise/:id",(req, res) =>{ 184 | let exercise_id = req.params.id; 185 | let db = new sqlite.Database('saraldb',(err) =>{ 186 | if (err){ 187 | console.log(err); 188 | }else{ 189 | db.all("SELECT * FROM exercise where id = '"+exercise_id+"' ",(err, data) =>{ 190 | if (err){ 191 | console.log(err); 192 | }else{ 193 | console.log("exercise get successfully by id...!"); 194 | res.send(data); 195 | } 196 | }) 197 | } 198 | }) 199 | }) 200 | 201 | // delete method by id for exercise 202 | app.delete("/exercise/:id", (req, res) =>{ 203 | let delete_id = req.params.id; 204 | let db = new sqlite.Database('saraldb', (err) =>{ 205 | if (err){ 206 | console.log(err); 207 | }else{ 208 | db.all("DELETE from exercise where id = '"+delete_id+"' ",(err) =>{ 209 | if (err){ 210 | console.log(err); 211 | }else{ 212 | console.log("done"); 213 | res.send("delete method done successfully....."); 214 | } 215 | }); 216 | 217 | } 218 | }) 219 | }) 220 | 221 | var server = app.listen(3031, function(){ 222 | var host = server.address().address; 223 | var port = server.address().port; 224 | console.log(host, port); 225 | }); 226 | --------------------------------------------------------------------------------