PysonDB - A JSON based lightweight Database for Python.

PysonDB - A JSON based lightweight Database for Python.

·

6 min read

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

Join Discord server here

Github Repo

Give it a Star if you like this project.


Install

pip install pysondb

Create a database

  • You can create a database using CLI.
    pysondb create database_name
    
  • Or in the python file.
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({})
>> 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([{},{}....])
>> 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

{"data":[{"name":"pysondb","type":"DB"},{"name":"py_cli","type":"CLI"},{"name":"py_cli2","type":"CLI"}]}

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

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

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

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

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

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

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

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

pysondb create [name]

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

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

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

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 |
+-----------+------+-------+------------+

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.