Skip to main content

Lightweight json-database library for Python

Project description

Sunshine

Sunshine Logo

Lightweight json-database library for Python


Installing database library

pip install SunshineDB

Class

Sunshine - database management class.

Has the following set of methods:

0. push()
1. all()
2. get()
3. update()
4. contains()
5. delete()
6. drop()
7. backup()


Creating database

You just need to create an instance of the Sunshine-class, passing the path to the json-file as an argument.

For example,

from SunshineDB import Sunshine

database: Sunshine = Sunshine('../databases/database.json')

Methods

Method examples will be given using the database variable we set.

push()

Adds an object with the given fields to the database.
Requires one argument:

  • data_to_push (dictionary[string, any]) - the key-value dictionary to be added to the database.

Returns ID.

identifier: int = database.push(
    {
        'name'       : 'Bertram Gilfoyle',
        'job'        : 'Pied Piper Inc.',
        'occupation' : 'Vice President Of Architecture'
    }
)

print(identifier) 
# output >> 22104564398807 
#           ^^^^^^^^^^^^^^
# ID is 14-digit integer

all()

Returns all database objects.

data: list[dict[str, any]] = database.all()

print(data)
# output >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Vice President Of Architecture'
#   }
# ]

get()

Returns a database object using a query, or returns the number of given elements in the first one up to the number specified in the count argument.
Requires two arguments:

  • query (dictionary[string, any]) - a key-value dictionary that will be used to select elements,
  • count (integer) - the number of requested elements.

You cannot use both arguments together.

data: list[dict[str, any]] = database.get(
    query = {
        'job' : 'Pied Piper Inc.'
    }
)

print(data)
# output >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Vice President Of Architecture'
#   }
# ]

# And the same will be displayed if you call the get-method like this
data: list[dict[str, any]] = database.get(count = 1)

update()

Updates a database object with an ID.
Requires two arguments:

  • id (14-digit integer) - numeric identifier,
  • data_to_update (dictionary[string, any]) - the key-value dictionary that will be updated in the database object.

database.update(
    22104564398807, 
    {
        'occupation' : 'Network engineer'
    }
)
# changed to >> 
# [
#   {
#       'id': 22104564398807, 
#       'name': 'Bertram Gilfoyle', 
#       'job': 'Pied Piper Inc.', 
#       'occupation': 'Network engineer'
#   }
# ]

contains()

Checks by query, if an element is contained in the database.
Requires two arguments:

  • key (string),
  • value (any).

These arguments will be searched in the database.

Returns boolean.

data: bool = database.contains('name', 'Bertram Gilfoyle')
print(data)
# output >> True
#           ^^^^
# contains-method returns boolean

data: bool = database.contains('name', 'Dinesh Chugtai')
print(data)
# output >> False

delete()

Removes the object with the given ID from the database.
Requires one argument:

  • id (14-digit integer) - numeric identifier,

database.delete(22104564398807)

# database-file >>
# {
#   "data": []
# }

drop()

Removes all objects from the database.


database.drop()

# database-file >>
# {
#   "data": []
# }

backup()

Creates a database backup at the given path.
Requires one argument:

  • path (string) - path to the folder where the backup-file will be saved.

database.backup('../databases/backups/')



Author

     _      _  _               _ _   
  __| | ___| || |   ___  _   _| | |_ 
 / _` |/ _ \ || |_ / _ \| | | | | __|
| (_| |  __/__   _| (_) | |_| | | |_ 
 \__,_|\___|  |_|  \___/ \__,_|_|\__|

Thank you a lot!


How to reach me

Telegram Badge Gmail Badge

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SunshineDB-1.5.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

SunshineDB-1.5-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page