├── _config.yml ├── .dockerignore ├── .gitignore ├── src ├── routes │ ├── index.ts │ ├── table.routes.ts │ └── query.routes.ts ├── controllers │ ├── index.ts │ ├── table.controller.ts │ └── query.controller.ts ├── config.ts ├── models │ ├── index.ts │ └── table.model.ts └── index.ts ├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── tslint.json ├── Dockerfile ├── libs └── js │ ├── package.json │ ├── README.md │ └── stor.js ├── package.json ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── tsconfig.json /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | build -------------------------------------------------------------------------------- /src/routes/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./query.routes"; 2 | export * from "./table.routes"; -------------------------------------------------------------------------------- /src/controllers/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./table.controller"; 2 | export * from "./query.controller"; -------------------------------------------------------------------------------- /src/routes/table.routes.ts: -------------------------------------------------------------------------------- 1 | import { Router, IRoute } from "express"; 2 | import { tablePostFunc, tableByNameGetFunc } from "../controllers"; 3 | 4 | const router: Router = Router(); 5 | 6 | const table: IRoute = router.route("/"); 7 | table.post(tablePostFunc); 8 | 9 | const tableByName: IRoute = router.route("/:name/"); 10 | tableByName.get(tableByNameGetFunc); 11 | 12 | export const TableRoutes: Router = router; -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Describe the bug** 8 | A clear and concise description of what the bug is. 9 | 10 | **To Reproduce** 11 | Steps to reproduce the behavior. 12 | 13 | **Expected behavior** 14 | A clear and concise description of what you expected to happen. 15 | 16 | **Screenshots** 17 | If applicable, add screenshots to help explain your problem. 18 | -------------------------------------------------------------------------------- /src/config.ts: -------------------------------------------------------------------------------- 1 | export const MongoUri: string = process.env.STOR_MONGO_URI || "mongodb://localhost:27017/icy"; 2 | export const PortNumber: number = Number(process.env.STOR_PORT) || 8080; 3 | export const AuthToken: string = process.env.STOR_PASSWORD || "password"; 4 | export const Cors: { enabled: boolean, whitelist?: string[] } = { enabled: Number(process.env.STOR_CORS_ENABLED) == 0 ? false : true, whitelist: process.env.STOR_CORS_WHITELIST ? process.env.STOR_CORS_WHITELIST.split(",") : [] }; -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | ], 5 | "jsRules": { 6 | "max-line-length": { 7 | "options": [180] 8 | } 9 | }, 10 | "rules": { 11 | "semicolon": [true, "always"], 12 | "quotemark": [true, "double"], 13 | "object-literal-sort-keys": false, 14 | "eofline": false, 15 | "ordered-imports": false, 16 | "no-irregular-whitespace": false, 17 | "no-console": [true, "error"] 18 | }, 19 | "rulesDirectory": [] 20 | } 21 | -------------------------------------------------------------------------------- /src/models/index.ts: -------------------------------------------------------------------------------- 1 | import { connect } from "mongoose"; 2 | import { MongoError } from "mongodb"; 3 | 4 | import { MongoUri } from "../config"; 5 | 6 | /** 7 | * @function connection - Connect to mongoDB 8 | */ 9 | const connection: VoidFunction = () => { 10 | connect(MongoUri, { useNewUrlParser: true }, (err: MongoError) => { 11 | if (err) { 12 | console.log(err.message); 13 | } else { 14 | console.log("Connected to mongodb"); 15 | } 16 | }); 17 | }; 18 | 19 | export const Connect: VoidFunction = connection; 20 | 21 | export * from "./table.model"; -------------------------------------------------------------------------------- /src/routes/query.routes.ts: -------------------------------------------------------------------------------- 1 | import { Router, IRoute } from "express"; 2 | import { selectAllFunc, whereGetFunc, wherePutFunc, whereDeleteFunc, createFunc } from "../controllers"; 3 | 4 | const router: Router = Router(); 5 | 6 | const create: IRoute = router.route("/:name/create/"); 7 | create.post(createFunc); 8 | 9 | const selectAll: IRoute = router.route("/:name/all/"); 10 | selectAll.get(selectAllFunc); 11 | 12 | const where: IRoute = router.route("/:name/where/:obj/is/:is/"); 13 | where.get(whereGetFunc); 14 | where.put(wherePutFunc); 15 | where.delete(whereDeleteFunc); 16 | 17 | export const QueryRoutes: Router = router; -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:8 2 | 3 | # Create app directory 4 | WORKDIR /usr/src/app 5 | 6 | # Environment variables 7 | ENV STOR_MONGO_URI mongodb://localhost:27017/icy 8 | ENV STOR_PORT 8080 9 | ENV STOR_PASSWORD password 10 | ENV STOR_CORS_ENABLED 0 11 | 12 | # Install app dependencies 13 | # A wildcard is used to ensure both package.json AND package-lock.json are copied 14 | # where available (npm@5+) 15 | COPY package*.json ./ 16 | 17 | RUN npm install 18 | # If you are building your code for production 19 | # RUN npm install --only=production 20 | 21 | # Bundle app source 22 | COPY . . 23 | 24 | EXPOSE 8080 25 | CMD [ "npm", "start" ] 26 | 27 | -------------------------------------------------------------------------------- /src/models/table.model.ts: -------------------------------------------------------------------------------- 1 | import { Schema, Model, model } from "mongoose"; 2 | 3 | const TableSchema: Schema = new Schema({ 4 | name: { 5 | type: String, 6 | required: true, 7 | }, 8 | content: { 9 | type: String, 10 | required: true, 11 | }, 12 | password: { 13 | type: String, 14 | required: false, 15 | }, 16 | }); 17 | 18 | const TableModel: Model = model("table", TableSchema); 19 | 20 | export const Table: Model = TableModel; 21 | 22 | export interface ITable { 23 | name: string; 24 | content: string; 25 | password: string; 26 | save: Function; 27 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /libs/js/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stor-js", 3 | "version": "1.0.0", 4 | "description": "The STOR official wrapper", 5 | "main": "stor.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/dimensi0n/stor.git" 12 | }, 13 | "keywords": [ 14 | "cloud", 15 | "database", 16 | "json", 17 | "mongodb", 18 | "api" 19 | ], 20 | "author": "Erwan ROUSSEL", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/dimensi0n/stor/issues" 24 | }, 25 | "homepage": "https://github.com/dimensi0n/stor#readme" 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "icy", 3 | "version": "1.0.0", 4 | "description": "The icy package", 5 | "main": "./build/index.js", 6 | "scripts": { 7 | "dev": "concurrently \"tsc --watch\" \"nodemon build/index.js\"", 8 | "start": "node ./build/index.js" 9 | }, 10 | "author": "Erwan ROUSSEL", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "@types/node": "^10.12.12", 14 | "concurrently": "^4.0.1", 15 | "jest": "^24.5.0", 16 | "nodemon": "^1.18.4", 17 | "tslint": "^5.11.0", 18 | "typescript": "^2.9.2" 19 | }, 20 | "dependencies": { 21 | "@types/cors": "^2.8.4", 22 | "@types/express": "^4.16.0", 23 | "@types/mongoose": "^5.2.0", 24 | "@types/mongoose-auto-increment": "^5.0.30", 25 | "body-parser": "^1.18.3", 26 | "cors": "^2.8.4", 27 | "express": "^4.16.3", 28 | "mongoose": "^5.7.7", 29 | "mongoose-auto-increment": "^5.0.1", 30 | "node-fetch": "^2.2.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import bodyParser from "body-parser"; 3 | import cors from "cors"; 4 | 5 | import { QueryRoutes, TableRoutes } from "./routes"; 6 | import { Connect } from "./models"; 7 | 8 | import { PortNumber, Cors } from "./config"; 9 | 10 | Connect(); 11 | 12 | const app: express.Application = express(); 13 | 14 | app.use(bodyParser.json()); 15 | 16 | if(Cors.enabled) { 17 | app.use(cors()); 18 | } else if (Cors.whitelist) { 19 | const whitelist: string[] = Cors.whitelist || []; 20 | const corsOptions = { 21 | origin: (origin: string, callback: Function) => { 22 | if (whitelist.indexOf(origin) !== -1) { 23 | callback(null, true); 24 | } else { 25 | callback(new Error("Not allowed by CORS")); 26 | } 27 | } 28 | }; 29 | } 30 | 31 | app.use("/table/", TableRoutes); 32 | app.use("/query/", QueryRoutes); 33 | 34 | app.listen(PortNumber, () => console.log(PortNumber)); -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Welcome to the CONTRIBUTING.md 2 | 3 | * [How to](#how-to) 4 | * [Code style](#code-style) 5 | 6 | If you want to contribute just follow the next steps : 7 | 8 | ## How to 9 | 10 | ### Fork & Clone the project 11 | 12 | ```bash 13 | git clone https://github.com/dimensi0n/stor.git 14 | ``` 15 | 16 | ### Install dependencies 17 | 18 | ```bash 19 | npm i 20 | ``` 21 | 22 | ### Make your modifications 23 | 24 | ```bash 25 | touch file.js 26 | ``` 27 | 28 | ### Commit your modifications 29 | 30 | ```bash 31 | commit -m "ADD or UPDATE or FIX " 32 | ``` 33 | 34 | ### Create a pull request 35 | 36 |
37 | 38 | ## Code style 39 | 40 | ### Semicolon 41 | 42 | 🚫 Wrong : 43 | 44 | ```js 45 | console.log() 46 | ``` 47 | 48 | ✅ Correct : 49 | 50 | ```js 51 | console.log(); 52 | ``` 53 | 54 | ### Double quotes 55 | 56 | 🚫 Wrong : 57 | 58 | ```js 59 | console.log('hello'); 60 | ``` 61 | 62 | ✅ Correct : 63 | 64 | ```js 65 | console.log("hello"); 66 | ``` 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Erwan ROUSSEL 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 |

2 | 3 |

4 | 5 |

6 | 7 | 8 |

9 | 10 | **STOR** provides a JSON database with the power of HTTP requests 11 | 12 | ## 1. Installation 13 | 14 | *You need to install NodeJS first* 15 | 16 | Just clone the repository : 17 | 18 | git clone https://www.github.com/dimensi0n/stor.git 19 | 20 | And run : 21 | 22 | npm install 23 | 24 | ## 2. Configuration 25 | 26 | Stor uses Environment variable : 27 | 28 | STOR_MONGO_URI is the link to your MongoDB database 29 | STOR_PORT is the port number you want the database to run on 30 | STOR_PASSWORD is the token you will write for each request on the request header 31 | STOR_CORS is the Cors config object : 1 if cors is enabled, by default is true; 0 if cors is disabled 32 | STOR_CORS_WHITELIST (optionnal, the domain you want to be validate) Example: STOR_CORS_WHITELIST=www.mydomain.com,www.myotherdomain.com 33 | 34 | Once you finished to complete this fields just transpile it : 35 | 36 | npx tsc 37 | 38 | ## 3. Run it 39 | 40 | If you want to run it just for testing you can launch it with this command : 41 | 42 | npm start 43 | 44 | ## 4. Use it 45 | 46 | Install the official js library : 47 | 48 | npm install stor-js 49 | 50 | Connect to your Stor database and select your table : 51 | ```js 52 | const stor = require("stor-js"); 53 | 54 | const Stor = new stor.Stor("link to your Stor database", "STOR_PASSWORD"); 55 | 56 | let users = Stor.Table("users"); 57 | ``` 58 | 59 | Then init your Stor database : 60 | ```js 61 | users.Init([]) 62 | .then(res => res.text()) 63 | .then(body => console.log(body)) 64 | ``` 65 | ### Select All : 66 | ```js 67 | users.SelectAll() 68 | .then(res => res.json()) 69 | .then(body => console.log(body.content)) 70 | ``` 71 | ### Create : 72 | ```js 73 | users.Create({name:'pierre'}) 74 | .then(res => res.text()) 75 | .then(body => console.log(body)) 76 | ``` 77 | ### Get where : 78 | ```js 79 | users.Get('name', 'pierre') 80 | .then(res => res.text()) 81 | .then(body => console.log(body)) 82 | ``` 83 | *Get user when name is 'pierre'* 84 | 85 | ### Update : 86 | ```js 87 | users.Put('name', 'pierre', 'jean') 88 | .then(res => res.text()) 89 | .then(body => console.log(body)) 90 | ``` 91 | *Update user when name is 'pierre' to 'jean'* 92 | 93 | ### Delete : 94 | ```js 95 | users.Delete('name', 'jean') 96 | .then(res => res.text()) 97 | .then(body => console.log(body)) 98 | ``` 99 | *Delete user when name is 'jean'* 100 | -------------------------------------------------------------------------------- /libs/js/README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 |

6 | 7 | 8 |

9 | 10 | **STOR** provides a JSON database with the power of HTTP requests 11 | 12 | ## 1. Installation 13 | 14 | *You need to install NodeJS first* 15 | 16 | Just clone the repository : 17 | 18 | git clone https://www.github.com/dimensi0n/stor.git 19 | 20 | And run : 21 | 22 | npm install 23 | 24 | ## 2. Configuration 25 | 26 | Stor uses Environment variable : 27 | 28 | STOR_MONGO_URI is the link to your MongoDB database 29 | STOR_PORT is the port number you want the database to run on 30 | STOR_PASSWORD is the token you will write for each request on the request header 31 | STOR_CORS is the Cors config object : 1 if cors is enabled, by default is true; 0 if cors is disabled 32 | STOR_CORS_WHITELIST (optionnal, the domain you want to be validate) Example: STOR_CORS_WHITELIST=www.mydomain.com,www.myotherdomain.com 33 | 34 | Once you finished to complete this fields just transpile it : 35 | 36 | npx tsc 37 | 38 | ## 3. Run it 39 | 40 | If you want to run it just for testing you can launch it with this command : 41 | 42 | npm start 43 | 44 | ## 4. Use it 45 | 46 | Install the official js library : 47 | 48 | npm install stor-js 49 | 50 | Connect to your Stor database and select your table : 51 | ```js 52 | const stor = require("stor-js"); 53 | 54 | const Stor = new stor.Stor("link to your Stor database", "STOR_PASSWORD"); 55 | 56 | let users = Stor.Table("users"); 57 | ``` 58 | 59 | Then init your Stor database : 60 | ```js 61 | users.Init([]) 62 | .then(res => res.text()) 63 | .then(body => console.log(body)) 64 | ``` 65 | ### Select All : 66 | ```js 67 | users.SelectAll() 68 | .then(res => res.json()) 69 | .then(body => console.log(body.content)) 70 | ``` 71 | ### Create : 72 | ```js 73 | users.Create({name:'pierre'}) 74 | .then(res => res.text()) 75 | .then(body => console.log(body)) 76 | ``` 77 | ### Get where : 78 | ```js 79 | users.Get('name', 'pierre') 80 | .then(res => res.text()) 81 | .then(body => console.log(body)) 82 | ``` 83 | *Get user when name is 'pierre'* 84 | 85 | ### Update : 86 | ```js 87 | users.Put('name', 'pierre', 'jean') 88 | .then(res => res.text()) 89 | .then(body => console.log(body)) 90 | ``` 91 | *Update user when name is 'pierre' to 'jean'* 92 | 93 | ### Delete : 94 | ```js 95 | users.Delete('name', 'jean') 96 | .then(res => res.text()) 97 | .then(body => console.log(body)) 98 | ``` 99 | *Delete user when name is 'jean'* 100 | -------------------------------------------------------------------------------- /src/controllers/table.controller.ts: -------------------------------------------------------------------------------- 1 | import { IRoute, Router, Handler } from "express"; 2 | import { Table, ITable } from "../models"; 3 | import { AuthToken } from "../config"; 4 | import * as crypto from "crypto"; 5 | import { Request, Response, NextFunction } from "express-serve-static-core"; 6 | 7 | const router: Router = Router(); 8 | 9 | /** 10 | * @function tablePost - Create table handler 11 | * @param req 12 | * @param res 13 | * @param next 14 | */ 15 | const tablePost: Handler = (req: Request, res: Response, next: NextFunction) => { 16 | if (req.get("Authorization") == AuthToken ) { 17 | if (req.body.name && req.body.content) { 18 | Table.find({name: req.body.name}, (err: any, raw: any[]) => { 19 | if (raw[0]) { 20 | res.send("Database already exists"); 21 | } else { 22 | if (req.body.password) { 23 | console.log("ping"); 24 | const database: ITable = new Table(); 25 | database.name = req.body.name; 26 | database.content = JSON.stringify(req.body.content); 27 | database.password = crypto.createHash("sha256").update(req.body.password).digest("base64"); 28 | database.save(); 29 | res.send(database.name + " created with " + database.content); 30 | } else { 31 | const database: ITable = new Table(); 32 | database.name = req.body.name; 33 | database.content = JSON.stringify(req.body.content); 34 | database.save(); 35 | res.send(database.name + " created with " + database.content); 36 | } 37 | } 38 | }); 39 | } else { 40 | res.send("Not enough parameters (name, content, ?password"); 41 | } 42 | } else { 43 | res.send("Wrong Authorization token"); 44 | } 45 | }; 46 | 47 | /** 48 | * @function tableByNameGet - Get table by name handler 49 | * @param req 50 | * @param res 51 | * @param next 52 | */ 53 | const tableByNameGet: Handler = (req: Request, res: Response, next: NextFunction) => { 54 | if (req.get("Authorization") == AuthToken ) { 55 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 56 | if (raw[0]) { 57 | if (raw[0].password != null) { 58 | if (req.get("Password")) { 59 | const password: string = req.get("Password") || ""; 60 | if (raw[0].password == crypto.createHash("sha256").update(password).digest("base64")) { 61 | res.send(raw[0]); 62 | } 63 | } else { 64 | res.send("You have to give a password in the header (\"Password:\")"); 65 | } 66 | } else { 67 | res.send(raw[0]); 68 | } 69 | } else { 70 | res.send("Database doesn't exits"); 71 | } 72 | }); 73 | } else { 74 | res.send("Wrong Authorization token"); 75 | } 76 | }; 77 | 78 | export const tablePostFunc: Handler = tablePost; 79 | export const tableByNameGetFunc: Handler = tableByNameGet; -------------------------------------------------------------------------------- /libs/js/stor.js: -------------------------------------------------------------------------------- 1 | const fetch = require("node-fetch"); 2 | 3 | /** 4 | * @class Stor - The Stor DB config 5 | * @param { string } - Link to your Stor DB 6 | * @param { string } - Your token 7 | */ 8 | class Stor { 9 | constructor(link, token) { 10 | this.link = link; 11 | this.token = token; 12 | } 13 | Table(name) { 14 | return new _Table(this.link, this.token, name); 15 | } 16 | } 17 | 18 | class _Table { 19 | constructor(link, token, name) { 20 | this.link = link; 21 | this.token = token; 22 | this.name = name; 23 | } 24 | 25 | /** 26 | * @function Init - Init your Table with an object 27 | * @param { Object } content 28 | */ 29 | async Init(content) { 30 | return await fetch(`${this.link}/table/`, { 31 | method: 'POST', 32 | body: JSON.stringify({ 33 | name: this.name, 34 | content: content 35 | }), 36 | headers: { 37 | 'Authorization': this.token, 38 | 'Content-Type': 'application/json' 39 | } 40 | }); 41 | } 42 | 43 | /** 44 | * @function SelectAll - Select all your table 45 | */ 46 | async SelectAll() { 47 | return await fetch(`${this.link}/query/${this.name}/all`, { 48 | method: 'GET', 49 | headers: { 50 | 'Authorization': this.token 51 | } 52 | }); 53 | } 54 | 55 | /** 56 | * @function Get - Get something where champ ... is ... 57 | * @param { string } champ - The champ 58 | * @param { string } is - The champ value 59 | */ 60 | async Get(champ, is) { 61 | return await fetch(`${this.link}/query/${this.name}/where/${champ}/is/${is}/`, { 62 | method: 'GET', 63 | headers: { 64 | 'Authorization': this.token 65 | } 66 | }); 67 | } 68 | 69 | /** 70 | * @function Put - Put something 71 | * @param { string } champ - The champ 72 | * @param { string } is - The champ value 73 | * @param { string } content - The new content 74 | */ 75 | async Put(champ, is, content) { 76 | return await fetch(`${this.link}/query/${this.name}/where/${champ}/is/${is}/`, { 77 | method: 'PUT', 78 | headers: { 79 | 'Authorization': this.token, 80 | 'Content-Type': 'application/json' 81 | }, 82 | body: JSON.stringify({ 83 | content: content 84 | }) 85 | }); 86 | } 87 | 88 | /** 89 | * @function Delete - Delete something where champ ... is ... 90 | * @param { string } champ - The champ 91 | * @param { string } is - The champ value 92 | */ 93 | async Delete(champ, is) { 94 | return await fetch(`${this.link}/query/${this.name}/where/${champ}/is/${is}/`, { 95 | method: 'DELETE', 96 | headers: { 97 | 'Authorization': this.token, 98 | } 99 | }); 100 | } 101 | 102 | /** 103 | * @function Create - Create an object in your table 104 | * @param { Object } content 105 | */ 106 | async Create(content) { 107 | return await fetch(`${this.link}/query/${this.name}/create`, { 108 | method: 'POST', 109 | headers: { 110 | 'Authorization': this.token, 111 | 'Content-Type': 'application/json' 112 | }, 113 | body: JSON.stringify({ 114 | content: content 115 | }) 116 | }); 117 | } 118 | } 119 | 120 | exports.Stor = Stor; 121 | exports.Table = Table; -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Basic Options */ 4 | "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ 5 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 6 | // "lib": [], /* Specify library files to be included in the compilation. */ 7 | // "allowJs": true, /* Allow javascript files to be compiled. */ 8 | // "checkJs": true, /* Report errors in .js files. */ 9 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 10 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 11 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 12 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 13 | // "outFile": "./", /* Concatenate and emit output to single file. */ 14 | "outDir": "./build", /* Redirect output structure to the directory. */ 15 | "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 16 | // "composite": true, /* Enable project compilation */ 17 | "removeComments": true, /* Do not emit comments to output. */ 18 | // "noEmit": true, /* Do not emit outputs. */ 19 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 20 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 21 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 22 | 23 | /* Strict Type-Checking Options */ 24 | "strict": true, /* Enable all strict type-checking options. */ 25 | "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 26 | // "strictNullChecks": true, /* Enable strict null checks. */ 27 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 28 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 29 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 30 | "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 31 | 32 | /* Additional Checks */ 33 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 34 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 35 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 36 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 37 | 38 | /* Module Resolution Options */ 39 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 40 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 41 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 42 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 43 | // "typeRoots": [], /* List of folders to include type definitions from. */ 44 | // "types": [], /* Type declaration files to be included in compilation. */ 45 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 46 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 47 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 48 | 49 | /* Source Map Options */ 50 | // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 51 | // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ 52 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 53 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 54 | 55 | /* Experimental Options */ 56 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 57 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 58 | } 59 | } -------------------------------------------------------------------------------- /src/controllers/query.controller.ts: -------------------------------------------------------------------------------- 1 | import { Router, IRoute, Handler } from "express"; 2 | import { Table } from "../models"; 3 | import { AuthToken } from "../config"; 4 | import * as crypto from "crypto"; 5 | import { NextFunction } from "connect"; 6 | import { Request, Response } from "express-serve-static-core"; 7 | import { Error } from "mongoose"; 8 | 9 | const router: Router = Router(); 10 | 11 | /** 12 | * @function selectAll - Select all handler 13 | * @param req 14 | * @param res 15 | * @param next 16 | */ 17 | const selectAll: Handler = (req: Request, res: Response, next: NextFunction) => { 18 | if (req.get("Authorization") == AuthToken ) { 19 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 20 | if (raw[0]) { 21 | if (raw[0].password != null) { 22 | if (req.get("Password")) { 23 | const password: string = req.get("Password") || ""; 24 | if (raw[0].password == crypto.createHash("sha256").update(password).digest("base64")) { 25 | res.send(raw[0]); 26 | } 27 | } else { 28 | res.send("You have to give a password in the header (\"Password:\")"); 29 | } 30 | } else { 31 | res.send(raw[0]); 32 | } 33 | } else { 34 | res.send("Database doesn't exits"); 35 | } 36 | }); 37 | } else { 38 | res.send("Wrong Authorization token"); 39 | } 40 | }; 41 | 42 | /** 43 | * @function whereGet - Get where handler 44 | * @param req 45 | * @param res 46 | * @param next 47 | */ 48 | const whereGet: Handler = (req: Request, res: Response, next: NextFunction) => { 49 | if (req.get("Authorization") == AuthToken ) { 50 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 51 | if (raw[0]) { 52 | let state: number = 0; 53 | const content: any[] = JSON.parse(raw[0].content); 54 | const elements: any[] = []; 55 | content.forEach((element: any) => { 56 | if (element[req.params.obj] == req.params.is) { 57 | elements.push(element); 58 | state = 1; 59 | } 60 | }); 61 | res.send(elements); 62 | if (state != 1) { 63 | res.send("No results found"); 64 | } 65 | } else { 66 | res.send("Database doesn't exist"); 67 | } 68 | }); 69 | } else { 70 | res.send("Wrong Authorization token"); 71 | } 72 | }; 73 | 74 | /** 75 | * @function wherePut - Put where handler 76 | * @param req 77 | * @param res 78 | * @param next 79 | */ 80 | const wherePut: Handler = (req: Request, res: Response, next: NextFunction) => { 81 | if (req.get("Authorization") == AuthToken) { 82 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 83 | if (raw[0]) { 84 | let state: number = 0; 85 | const content: any[] = JSON.parse(raw[0].content); 86 | content.forEach((element: any) => { 87 | if (element[req.params.obj] == req.params.is) { 88 | element[req.params.obj] = req.body.content; 89 | Table.update({name: req.params.name}, {content: JSON.stringify(content)}, (err: Error, raw: any) => { 90 | if (err) { throw err; } 91 | res.send(raw); 92 | }); 93 | state = 1; 94 | } 95 | }); 96 | if (state != 1) { 97 | res.send("No results found"); 98 | } 99 | } else { 100 | res.send("Database doesn't exist"); 101 | } 102 | }); 103 | } else { 104 | res.send("Wrong Authorization Token"); 105 | } 106 | }; 107 | 108 | /** 109 | * @function whereDelete - Delete where Handler 110 | * @param req 111 | * @param res 112 | * @param next 113 | */ 114 | const whereDelete: Handler = (req: Request, res: Response, next: NextFunction) => { 115 | if (req.get("Authorization") == AuthToken) { 116 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 117 | if (raw[0]) { 118 | let state: number = 0; 119 | const content: any[] = JSON.parse(raw[0].content); 120 | content.forEach((element: any) => { 121 | if (element[req.params.obj] == req.params.is) { 122 | content.splice(element); 123 | Table.update({name: req.params.name}, {content: JSON.stringify(content)}, (err: Error, raw: any) => { 124 | if (err) { throw err; } 125 | res.send(raw); 126 | }); 127 | state = 1; 128 | } 129 | }); 130 | if (state != 1) { 131 | res.send("No results found"); 132 | } 133 | } else { 134 | res.send("Database doesn't exist"); 135 | } 136 | }); 137 | } else { 138 | res.send("Wrong Authorization Token"); 139 | } 140 | }; 141 | 142 | /** 143 | * @function create - 'Create' handler 144 | * @param req 145 | * @param res 146 | * @param next 147 | */ 148 | const create: Handler = (req: Request, res: Response, next: NextFunction) => { 149 | if (req.get("Authorization") == AuthToken) { 150 | Table.find({name: req.params.name}, (err: any, raw: any[]) => { 151 | if (raw[0]) { 152 | const content: any[] = JSON.parse(raw[0].content); 153 | content.push(req.body.content); 154 | Table.update({name: req.params.name}, {content: JSON.stringify(content)}, (err: Error, raw2: any) => { 155 | if (err) { throw err; } 156 | res.send(raw2); 157 | }); 158 | } else { 159 | res.send("Database doesn't exist"); 160 | } 161 | }); 162 | } else { 163 | res.send("Wrong Authorization Token"); 164 | } 165 | }; 166 | 167 | export const selectAllFunc: Handler = selectAll; 168 | export const whereGetFunc: Handler = whereGet; 169 | export const wherePutFunc: Handler = wherePut; 170 | export const whereDeleteFunc: Handler = whereDelete; 171 | export const createFunc: Handler = create; 172 | --------------------------------------------------------------------------------