Moonlight is a lightweight JSON-database for Python.
pip install Moonlight
Moonlight - database management class
Has the following set of methods:
0. async push()
1. async all()
2. async get()
3. async update()
5. async delete()
6. async drop()
7. async contains()
8. async length()
9. async count()
You just need to create an instance of the Moonlight-class, passing the path to the JSON file as an argument.
For example,
from Moonlight import Moonlight
database: Moonlight = Moonlight('../databases/database.json')
- filename - path to database .json-file
- primary_key - primary key name (default: 'id')
- show_messages - tuple of messages that will be output during operation (default: ('warning', 'error'))
- 'success'
- 'info'
- 'warning'
- 'error'
Method examples will be given using the database variable we set
Adds an object with the given fields to the database
- data_to_push (dict[str, any]) - the key-value dictionary to be added to the database
Returns id (int).
identifier: int = await database.push(
{
'name' : 'Bertram Gilfoyle',
'job' : 'Pied Piper Inc.',
'occupation' : 'Vice President Of Architecture'
}
)
print(identifier)
# output >> 22104564398807
# ^^^^^^^^^^^^^^
# ID is 14-digit integer
Get all objects from the database
Returns all_objects (list[dict[str, any]])
data: list[dict[str, any]] = await database.all()
print(data)
# output >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Vice President Of Architecture'
# }
# ]
Get object/s from the database by query
- query (dict[str, any]) - the key-value dictionary to find in database
Returns object/s (list[dict[str, any]]).
data: list[dict[str, any]] = await database.get({
'job' : 'Pied Piper Inc.'
})
print(data)
# output >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Vice President Of Architecture'
# }
# ]
Update object in the database
- data_to_update (dict[str, any]) - the key-value dictionary to change in object in database (primary_key in
data_to_update
required!)
Returns id (int).
await database.update(
{
'id' : 22104564398807,
'occupation' : 'Network engineer'
}
)
# changed to >>
# [
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Network engineer'
# }
# ]
Remove object from the database
- id (14-digit int) - identifier of element
Returns object (dict[str, any])
await database.delete(22104564398807)
# database file changed to >>
# {
# "data": []
# }
#
# will returned >>
# {
# 'id': 22104564398807,
# 'name': 'Bertram Gilfoyle',
# 'job': 'Pied Piper Inc.',
# 'occupation': 'Network engineer'
# }
Removes database file
await database.drop()
Checks if key
has a value
in database
- key (str)
- value (any)
Returns contains (bool)
await database.contains('name', 'Bertram Gilfoyle')
# will returned >>
# True
Returns count of objects in database
Returns length (int)
await database.length()
# will returned >>
# 1
Returns count of objects in database where key
is value
- key (str)
- value (any)
Returns count (int)
await database.count('name', 'Bertram Gilfoyle')
# will returned >>
# 1
_ _ _ _ _
__| | ___| || | ___ _ _| | |_
/ _` |/ _ \ || |_ / _ \| | | | | __|
| (_| | __/__ _| (_) | |_| | | |_
\__,_|\___| |_| \___/ \__,_|_|\__|