DEV Community

Cover image for PysonDB - A JSON based lightweight Database for Python.
Fredy Somy
Fredy Somy

Posted on • Updated on

PysonDB - A JSON based lightweight Database for Python.

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
>> 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"}]
Enter fullscreen mode Exit fullscreen mode
  • See its simple..

Join Discord server here

Github Repo

Give it a Star if you like this project.


Install

pip install pysondb
Enter fullscreen mode Exit fullscreen mode

Create a database

  • You can create a database using CLI.
pysondb create database_name
Enter fullscreen mode Exit fullscreen mode
  • Or in the python file.
from pysondb import db

a=db.getDb("db.json')
Enter fullscreen mode Exit fullscreen mode
  • 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({})
>> 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"

Enter fullscreen mode Exit fullscreen mode
  • addMany([{},{}....])
>> 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.
Enter fullscreen mode Exit fullscreen mode

Get Data

  • returns only one data by default.

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

path.json

{"data":[{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]}
Enter fullscreen mode Exit fullscreen mode

get(n)

>> 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"}]

Enter fullscreen mode Exit fullscreen mode

getAll()

  • Returns all data in the database
>> 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"}]

Enter fullscreen mode Exit fullscreen mode

getBy(query)

  • getBy(query) query must be a JSON data.
  • getBy({"type":"CLI"})
>> 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"}]

Enter fullscreen mode Exit fullscreen mode

Update Data

JSON file:file.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}]}
Enter fullscreen mode Exit fullscreen mode

updateById(ID,what_to_update)

  • ID=Integer
>> 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}]

Enter fullscreen mode Exit fullscreen mode

update(query,what_to_update)

  • query,what_to_update are both JSON data.
>> 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}]


Enter fullscreen mode Exit fullscreen mode

Delete Data

deleteById(ID)

  • file.json is same as the above
>> 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}]
Enter fullscreen mode Exit fullscreen mode

CLI operations

Use

pysondb convert --c [csv file destination] --d [json file to write]

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


Enter fullscreen mode Exit fullscreen mode

pysondb create [name]

C:\Users\Admin\Desktop\pysondb>pysondb create filedb.json
Succesfull created filedb.json in the directory.
Enter fullscreen mode Exit fullscreen mode

pysondb delete [name]

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
Enter fullscreen mode Exit fullscreen mode

pysondb display [name]

File: filedb.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}]}
Enter fullscreen mode Exit fullscreen mode

Code.

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 |
+-----------+------+-------+------------+
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (3)

Collapse
 
artydev profile image
artydev

Thank you very much, I will use it in my next project :-)

Collapse
 
jrhamdi profile image
Hamdi Jr

This is great!!. Keep up at the good work

Collapse
 
fredysomy profile image
Fredy Somy

Thanks a lot man..