PysonDB - A JSON based lightweight Database for Python.
Tags: python, database, datascience, showdev
Published: 2020-12-01 18:26:04

## Features

* __Lightweight__ JSON based database.
* Supports __CRUD__ commands.
* No Database drivers required.
* __Unique ID__ assigned for each JSON document added.
* Strict about __Schema__ of data added. 
* __Inbuilt CLI__ to delete,display,create JSON database.
* __Convert CSV__ files to JSON file to use in pysonDB

```python
>> from pysondb import db
>> a=db.getDb("path/to/json.json")
>> a.addMany([{"name":"pysondb","type":"DB"},{"name":"pysondb-cli","type":"CLI"}])
>> a.getAll()
>> [{"name":"pysondb","type":"DB"},{"name":"pysondb-cli","type":"CLI"}]
```
* See its simple..

#### Join Discord server [here](https://discord.gg/SZyk2dCgwg)
#### [Github Repo](https://github.com/fredysomy/pysonDB)
#### Give it a Star if you like this project.

***

## Install

```python
pip install pysondb
```
## Create a database

* You can create a database using CLI.
```bash
pysondb create database_name
```
* Or in the python file.

```python
from pysondb import db

a=db.getDb("db.json')
```

* The above piece of code will create a database with ``` {data:[]}``` in it.
* Even if the json file exists there is no problem.

## __Add data__.

* There are two methods to add data.
* add({})

```python
>> from pysondb import db
>> a=db.getDb("pathtojson.json")
>> a.add({"name":"pysondb","type":"DB"})
>> # returns 1929323232 which is a ID assigned to the above data.
>> a.add({"namme":"pyson","type":"DB"})
>> # The data wont be added as the key "name" is mispelled as "namme"

```

* addMany([{},{}....])

```python
>> from pysondb import db
>> a=db.getDb("pathtojson.json")
>> a.addMany([{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"}])
>> # Both data is added in the database.
>> a.addMany([{"namme":"pyson","type":"DB"},{"name":"py_cli2","type":"CLI"}])

>> # The first data wont be added as the key "name" is mispelled as "namme"
>> # But the second data will be added.
```
## __Get Data__

* returns only one data by default.

* get(3) => retruns 3 json data. 

path.json

```json
{"data":[{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]}
```
<h3><code>get(n)</code></h3>
```python
>> from pysondb import db
>> a=db.getDb("path.json")
>> a.get()
>> [{"name":"pysondb","type":"DB"}]
>> a.get(2)
>> [{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"}]

```
<h3><code>getAll()</code></h3>

* Returns all data in the database

```python
>> from pysondb import db
>> a=db.getDb("path.json")
>> a.getAll()
>> [{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]

```
<h3><code>getBy(query)</code></h3>

* getBy(query)  query must be a JSON data.
* getBy({"type":"CLI"})

```python
>> from pysondb import db
>> a=db.getDb("path.json")
>> a.getBy({"type":"CLI"})
>> [{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]
>> a.getBy({"name":"py_cli"})
>> [{"name":"py_cli","type":"CLI"}]

```
## __Update Data__

>JSON file:file.json

```json
{"data": [{"name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802},
{"name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690},
{"name": "Tiny", "type": "DB", "score": "9", "id": 6991190264},
{"name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202}]}
```

***

<h3><code>updateById(ID,what_to_update)</code></h3>

* ID=Integer
```python
>> from pysondb import db
>> a=db.getDb("file.json")
>> a.updateById("9416036202",{"name":"Qwerty12"})

>> #In the file.json the data is updated.
>> #We can verify it below.
>> a.getBy({"name":"Qwerty12"})
>> [{"name": "Qwerty12", "type": "DB", "score": "5", "id": 9416036202}]

```
<h3><code>update(query,what_to_update)</code></h3>

* query,what_to_update are both JSON data.

```python
>> from pysondb import db
>> a=db.getDb("file.json")
>> a.update({"name":"Pyson-CLI"},{"name":"PysonCLI"})

>> # In the file.json the data is updated for all data where name=Pyson-CLI
>> # We can verify it below.
>> a.getBy({"name":"PysonCLI"})
>> [{"name": "PysonCLI", "type": "CLI", "score": "10", "id": 2242313690}]


```
## __Delete Data__

<h3><code>deleteById(ID)</code></h3>

* file.json is same as the above

```python
>> from pysondb import db
>> a=db.getDb("file.json")
>> a.deleteById("6991190264")
>> # The JSON data with ID "6991190264" is deleted.Lets verify.
>> a.getAll()
>> [{"name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802},
{"name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690},
{"name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202}]
```

## __CLI operations__
<h3 id="convert">Use</h3>

<h4><code> pysondb convert --c [csv file destination] --d [json file to write]</code></h4>

```bash
C:\Users\Admin\Desktop\pysondb>pysondb convert --c file.csv --d file.json
Reading data from file.csv
Writing data into file.json
Conversion Succesfull


```

<h4><code> pysondb create [name]</code></h4>

```bash
C:\Users\Admin\Desktop\pysondb>pysondb create filedb.json
Succesfull created filedb.json in the directory.
```

<h4><code> pysondb delete [name]</code></h4>

```bash
C:\Users\Admin\Desktop\pysondb>pysondb delete file2.json
Do you want to remove the json file..(y/n)y
File deleted.

C:\Users\Admin\Desktop\pysondb>pysondb delete filedb.json
Do you want to remove the json file..(y/n)n
Action terminated
```
<h4><code> pysondb display [name]</code></h4>

File: filedb.json
```json
{"data": [{"name": "PysonDB", "type": "DB", "score": "10", "id": 5161221802}, {"name": "Pyson-CLI", "type": "CLI", "score": "10", "id": 2242313690}, {"name": "TinyDb", "type": "DB", "score": "9", "id": 6991190264}, {"name": "QWERTY", "type": "DB", "score": "5", "id": 9416036202}]}
```
Code.
```bash
C:\Users\Admin\Desktop\pysondb>pysondb display filedb.json
+-----------+------+-------+------------+
|   name    | type | score |     id     |
+-----------+------+-------+------------+
|  PysonDB  |  DB  |  10   | 5161221802 |
+-----------+------+-------+------------+
| Pyson-CLI | CLI  |  10   | 2242313690 |
+-----------+------+-------+------------+
|   Tiny    |  DB  |   9   | 6991190264 |
+-----------+------+-------+------------+
|  QWERTY   |  DB  |   5   | 9416036202 |
+-----------+------+-------+------------+
```

## What makes pysonDB different

* CLI support to create,delete and display database.
* Unique Id automatically assigned for each JSON data added.
* Schema regularity is checked.

## What pysonDB can't do.

* Cannot store images,videos etc.