Create databases using JSON files
git clone this repository and run the following command in the root directory:
pip install .
Download the jar file from the releases page and add it to your project or build the jar file from the source code.
mvn clean package
You can also use the makefile to build the jar file:
make build-jar
The general usage of the library is the same for both Python and Java. The only difference is the syntax.
from JSONdb import JSONdb
db = JSONdb("database.json")
This creates a JSONdb object with the specified file. If the file does not exist, it will be created on the first write operation.
By default, the database is saved after every write operation. You can disable this by setting the autosave parameter to False:
db = JSONdb("database.json", autosave=False)
You can manually save the database with the following command:
db.saveDatabase()
Autoload loads the database file before each operation. This is useful if you are working with multiple instances of the database file.
db = JSONdb("database.json", autoload=True)
Tracelogs are used to log the operations performed on the database. This is useful for debugging purposes.
db = JSONdb("database.json", traceLogs=True)
If the database is empty then you can initialize a table with the following command:
db.init("NAME", "DESCRIPTION"]
This outputs the following JSON structure:
{
"meta": {
"name": "NAME",
"description": "DESCRIPTION",
"columns": {}
},
"data": {}
}
In this demo lets use a more concrete example:
db.init("Products", "A demo database for products")
{
"meta": {
"name": "Products",
"description": "A demo database for products",
"columns": {}
},
"data": {}
}
You can add columns to a table with the following command:
db.createColumn("NAME", "DATATYPE", "DESCRIPTION")
For example:
db.createColumn("name", "STRING", "The name of the product")
This outputs the following JSON structure:
{
"meta": {
"name": "Products",
"description": "A demo database for products",
"columns": {
"0": {
"name": "product",
"type": "STRING",
"description": "The name of the product"
}
}
},
"data": {
"0": []
}
}
Note how JSONdb has automatically assigned an ID to the column. This is because the column ID is used to reference the data in the table.
In order for JSONdb to work properly, you must use the following data types:
- STRING
- INTEGER
- FLOAT
- BOOLEAN
You can add data to a table with the following command:
db.insertItems(0, ["Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon"])
This outputs the following JSON structure:
{
"meta": {
"name": "Products",
"description": "A demo database for products",
"columns": {
"0": {
"name": "product",
"type": "STRING",
"description": "The name of the product"
}
}
},
"data": {
"0": ["Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon"]
}
}
#### Fetching a column
You can fetch a column with the following command:
db.get(0)
This outputs the following JSON structure:
["Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon"]
You can fetch a single item with the following command:
db.getItem(0, 2)
This outputs the following JSON structure:
"Orange"
You can fetch the metadata of a table with the following command:
db.meta()
This outputs the following JSON structure:
{
"name": "Products",
"description": "A demo database for products",
"columns": {
"0": {
"name": "product",
"type": "STRING",
"description": "The name of the product"
}
}
}
You can get all the data in a table with the following command:
db.data()
This outputs the following JSON structure:
{
"0": ["Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon"]
}
You can delete a column with the following command:
db.clear(0)
This will remove all the data in the column.
You can delete a item with the following command:
db.delete(0, 2)
This will remove the item at the specified index. In this case, it will remove "Orange".
This project is licenced under the GNU General Public License v3.0 - see the LICENCE file for details.