Datagoose
Datagoose is an easy to use JSON based database for python.
With Datagoose:
- Best performance. Datagoose is a lightweight database.
- Methods that makes your job easier.
- Regex supports.
- You must enable
USE_REGEX
option.
- Safe to use.
- Auto or manual save, for who wants better performance.
- Easy to use database. Created for everyone.
- Rich options. includes hash keys, database path, regexp and more.
- Auto backup
- Can be dumped, also can load a data from JSON file.
<Datagoose>.load()
<Datagoose>.dump()
Update (1.6.0)
- Added
PIN
option for encryption.
- If you used
1.5.0
encryption, use PIN 2
or dump your database and make a new one.
- Added
.replace
, .replace_one
methods. - Added 3 new event.
- Added
ENCRYPTED
option for auto-backup. - Removed
.save_with_indent
method.
Download
You can download with pip install -U datagoose
(PyPi Page) or, you can use with source code.
Note: Current stable version is 1.6.1
. You can download stable version with pip install datagoose==1.6.1
(PyPi).
Quick Documentation
Before Start
You should know these things before using datagoose;
- Datagoose keeps data in memory, not in a file.
- You can save with
<Datagoose>.save()
for remember the database next time. - Also now you can enable
AUTO_SAVE
option for auto-saving.
- Datagoose is not a professional database.
Performance
Test Result (Auto-Save Enabled):
- 100 Data with insert many:
- 1,000 Data with insert many:
- 10,000 Data with insert many:
- 100,000 Data with insert many:
- 1,000,000 Data with insert many:
Methods
Quick Start
[] - Optinal
[argument_name:argument_type:argument_default_value] - Optinal Argument Name | Optional Argument Type | Optional Argument Default Value
{} - Normal
{argument_name:argument_type} - Argument Name | Argument Type
* - Args
from datagoose import Datagoose
database = Datagoose("example")
Options
database = Datagoose("example", {
"AUTO_SAVE": True,
"USE_REGEX": True,
"ENCRYPTED": True,
"PIN": 69 * 420,
"HASHING": [
"PASSWORD"
]
})
full_database = database.read()
print(database.info)
print(database.length)
print(database.uptime)
database.save()
database.clear()
Inserting Data
database.insert_one({
"name": "John",
"lastname": "Doe",
"age": 25,
"city": "New York",
"country": "USA",
"job": "Doctor"
})
database.insert_many({
"user": 1,
"nickname": "normal_guy_100"
}, {
"user": 2,
"nickname": "user_555"
})
Note: Datagoose adds unique hash ("_id") to every document that inserted. Make sure do not add same "_id" to documents.
database.insert_one({ "_id": 1, "name": "an_user" })
database.insert_one({ "_id": 1, "name": "another_user" })
Finding Data
found = database.find({
"age": 25
})
for result in found:
print(result)
for i in database.query(lambda data: 'POINT' in data and data['POINT'] >= 1337):
print(i)
result = database.find_one({
"user_id": 295818619
})
print(result["nickname"])
Updating Data
database.update({
"age": 25
}, {
"age": 26
})
database.update_one({
"user_id": 2486718956
}, {
"banned": True,
"ban_time": "30d"
})
Note: When you add new key and value to new_data dictionary, it will insert into data.
database.insert_one({ "_id": 1, "name": "an_user" })
database.update_one({
"_id": 1
}, {
"total_kill": 16
})
{
"_id": 1,
"name": "an_user",
"total_kill": 16
}
Replace Data
database.replace({
"age": 25
}, {
"thing": True,
"age": 26
})
database.replace_one({
"user_id": 2486718956
}, {
"banned": True,
"ban_time": "30d",
"user_id": 2486718956
})
Note: The difference between update and replace, replace changes the entire data with new one. update only updates the key that given.
Deleting Data
database.delete({
"total_hours": 1
})
database.delete_one({
"user_id": 6811
})
Count & Exists
database.count({
"name": "John",
"age": 40
})
result = database.exists({
"name": "a_guy_11"
})
if result:
database.delete_one({ "name": "a_guy_11" })
Note: "has" is an alias for exists.
result = database.has({
"name": "a_guy_11"
})
if result:
database.delete_one({ "name": "a_guy_11" })
result = database.exists({
"name": "a_guy_11"
})
if result:
database.delete_one({ "name": "a_guy_11" })
Using Regex
for i in db.find({"ANSWER": r"yes|y"}):
print(i)
Load & Dump
database.dump("./dump.json")
database.load("./old_database.json", overwrite=False)
database.load("./old_database.json")
Note: for .load(), the JSON will loaded must have 'database' key and value must be a list. Also in list, values must be dict.
Note: "export" is an aliases for .dump().
database.export("./dump.json", indent=4)
database.dump("./dump.json", indent=4)
Copying Data
database.copy({
"total_hours": 1
})
database.copy_one({
"user_id": 6811
})
Collecting Garbage Data
getgc = database.collect_garbage()
for garbage in getgc:
print("Found Garbage Data: ", garbage)
database.clear_garbage()
Sorting Database
point_list = database.sort_for_key("point", reverse=True)
winner = point_list[0]
print(f"Congrats, {winner['name']}. You won the game!")
finished = database.find_and_sort({"finished": True}, "time", reverse=True)
winner = finished[0]
print(f"Congrats, {winner['name']}. You won the game!")
Note: .sort_for_key() not changes the database, just returns sorted version of database.
Auto-Backup
database.start_backup({
"TIME": 60,
"PATH": "database/backups",
"ENCRYPTED": True
})
database.stop_backup()
if not database.is_backup_open:
print("Backup Disabled.")
Using Events
database.on("before_insert", lambda v: print("Starting to insert data: {0}".format(v)))
def before_insert_function(value):
print("Starting to insert value: {0}".format(value))
database.on("before_insert", before_insert_function)
Event List
All events and quick descriptions;
{
"before_insert": lambda value: None,
"should_insert": lambda value: True,
"after_insert": lambda value: None,
"before_update": lambda now, changes: None,
"should_update": lambda now, changes: True,
"after_update": lambda now, old: None,
"before_replace": lambda now, new: None,
"should_replace": lambda now, new: True,
"after_replace": lambda now, old: None,
"before_delete": lambda value: None,
"should_delete": lambda value: True,
"after_delete": lambda value: None,
"before_copy": lambda value: None,
"should_copy": lambda value: True,
"after_copy": lambda value: None,
"before_save": lambda: None,
"after_save": lambda: None,
"before_export": lambda: None,
"after_export": lambda: None,
"before_import": lambda: None,
"after_import": lambda: None,
"backup_start": lambda: None,
"backup": lambda: None,
"backup_finish": lambda: None,
"before_garbage_clear": lambda: None,
"after_garbage_clear": lambda: None
}