This module helps you to work with GH_DB. https://github.com/GreenHouseControllers/GH-database
npm install ghc-db --save
npm install ghc-db
Add this string everywhere where you want to use db methods.
const db = require('ghc-db');
Do it only once in te main file of the project.
db.connect(url, token);
exemple
db.connect('http://localhost:7202', 't111');
default url is "http://localhost:7202"
default token is "t111"
You can change port and token in db ./config/config.json
exemple of config file
{
"port": 7202,
"dbToken": "t111"
}
All requests mast be inside async function or callback.
- readFile - getElement file and return its contents.
- createFile - create file and return message or error.
- removeFile - remove file and return message or error.
- createDir - create directory and return message or error.
- removeDir - remove directory and return message or error.
- writeFile - write data to file and return message or error.
- rename - rename file or directory and return message or error.
- getDirContent - get content of directory (the same as ls in terminal).
- readJson - getElement and parse json file and return its contents.
- writeJson - write json file and return message.
- getElement - return element from array in json file.
- pushElement - push element to the end of array in json file.
- deleteElement - delete element from json array.
main
- createCollection - create collection and return message.
- removeCollection - remove collection and return message.
CRUD
- create - add object and return message.
- read - read collection and return its content.
- update - delete object and add new object and return message.
- delete - delete object and return message.
more methods
- get - get object by key and value.
- renameCollection - rename collection.
- upload - upload your file to the storage.
- download - download file from the storage.
- remove - remove file from the storage.
- register - register user that can work with db without token, but you need token for register.
- login - authorizes the user without token and returns you token.
- getErrorLog - returns full error log list.
- readFile read file and return its contents.
const data = {
path: "./exemple_path",
name: "newFile.json"
}
let answer = await db.readFile(data);
- createFile create file and return message or error.
const data = {
path: "./exemple_path",
name: "newFile.json"
}
let answer = await db.createFile(data);
- removeFile remove file and return message or error.
const data = {
path: "./exemple_path",
name: "newFile.json"
}
let answer = await db.removeFile(data);
- createDir create folder and return message or error.
const data = {
path: "./exemple_path",
name: "newDir"
}
let answer = await db.createDir(data);
- removeDir remove folder and return message or error.
const data = {
path: "./exemple_path",
name: "newDir"
}
let answer = await db.removeDir(data);
- writeFile write data to file and return message or error.
const data = {
path: "./exemple_path",
name: "newDir",
data: data
}
let answer = await db.writeFile(data);
- readJson read json file and return its contents.
const data = {
path: "./exemple_path",
name: "newDir"
}
let answer = await db.readJson(data);
- writeJson write object, or array to json file and return message or error.
const data = {
path: "./exemple_path",
name: "newDir",
data: data
}
let answer = await db.writeJson(data);
- getElement return element from array in json file. (here data is name or number of the element)
const data = {
path: "./exemple_path",
name: "newDir",
data: "name_of_element"
}
let answer = await db.getElement(data);
- pushElement push element to the end of array in json file
const data = {
path: "./exemple_path",
name: "newDir",
data: data
}
let answer = await db.pushElement(data);
- deleteElement delete element in the array.
const data = {
path: "./exemple_path",
name: "newDir",
data: data
}
let answer = await db.deleteElement(data);
- create add a new element to the collection.
const data = {
name: "newDir",
data: data
}
let answer = await db.create(data);
- read read collection.
const data = {
name: "newDir"
}
let answer = await db.read(data);
- update change one element to other
const data = {
name: "newDir",
key: "key",
data: "value of object",
newData: data
}
let answer = await db.update(data);
- delete delete one element in the collection
const data = {
name: "newDir",
key: "key",
data: "value of object"
}
let answer = await db.delete(data);
- createCollection - create collection.
const data = {
"name": "newCollection",
"path": "./exemple_path/newCollection.json",
"fileName": "newCollection.json"
}
let answer = await db.createCollection(data);
- removeCollection - remove collection.
const data = {
"name": "newCollection"
}
let answer = await db.removeCollection(data);
- get - get one element from the collection (if you have collection with the same objects that you fined db will return just the first)
const data = {
"name": "newCollection",
"key": "name",
"data": "jack"
}
let answer = await db.get(data);
- renameCollection - rename collection
const data = {
"name": "newCollection",
"newName": "otherCollection"
}
let answer = await db.renameCollection(data);
- upload - uploads files to the storage.
const fs = require('fs');
const FormData = require('form-data');
const upload = async () => {
let data = new FormData();
data.append('filedata', fs.createReadStream('/some/path/file.txt'));
let answer = await db.upload(data);
}
upload();
- download - downloads files from the storage.
const data = {
"name": "image.jpg"
}
let answer = await db.download(data);
- remove - remove file from the storage.
const data = {
"name": "image.jpg"
}
let answer = await db.remove(data);
- login - login user.
const data = {
"username": "joseph",
"password": "123a",
"token": "t111"
}
let answer = await db.register(data);
- login - login user.
const data = {
"username": "joseph",
"password": "123a"
}
let answer = await db.login(data);
- getErrorLog - returns all error logs
let answer = await db.getErrorLog();
file has been wrote
{
"message": "Can not read file",
"err": {
"errno": -2,
"code": "ENOENT",
"syscall": "open",
"path": "/home/DB/file_system/example/example.txt"
}
}
To find info about errors, look at the file sistem documentation
[
{
"time": "10-29-2020, 12:57:01 pm",
"log": {
"message": "Can not remove collection",
"err": {}
}
},
{
"time": "11-06-2020, 12:43:39 pm",
"log": {
"message": "Can not remove file",
"error": {
"code": "ERR_INVALID_ARG_TYPE"
}
}
},
{
"time": "11-06-2020, 12:44:10 pm",
"log": {
"message": "Can not remove directory",
"err": {
"errno": -2,
"syscall": "rmdir",
"code": "ENOENT",
"path": "/home/glab/GH-database/data/file_system/example"
}
}
},
{
"time": "11-06-2020, 12:44:26 pm",
"log": {
"message": "Can not get collection path",
"err": "invalid collection name"
}
}
]
const db = require('ghc-db'); //add ghc-db
const express = require('express');
const app = express();
const PORT = 3000;
// add bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//connect with db
db.connect(7202);
// work with file
app.get('/json', async (req, res) => {
try {
//create data
const data = {
path: "./exemple_path",
name: "newFile.json"
}
//request to db
let answer = await db.createFile(data);
res.status(200).send(answer);
}catch(err){
res.status(400).send(err);
}
});
// work with json
app.get('/json', async (req, res) => {
try {
//create data
const data = {
path: "./exemple_path",
name: "newFile.json"
}
//request to db
let answer = await db.readJson(data);
res.status(200).send(answer);
}catch(err){
res.status(400).send(err);
}
});
// work with crud
app.get('/crud/create', async (req, res) => {
try {
//create data
const data = {
"method": "create",
"name": "newCollection",
"data": {
"name": "jack",
"age": 15
}
}
//request to db
let answer = await db.create(data);
res.status(200).send(answer);
}catch(err){
res.status(400).send(err);
}
});
app.post('/getImage', async function(req, res){
try{
let data = {
name: req.body.name
}
let answer = await db.download(data);
res.status(200).send(answer);
} catch (err) {
res.status(400).send(err);
}
});
app.listen(PORT, function() {
console.log(`DB started on port ${PORT}`);
});