Skip to main content

A lightweight, file-based NoSQL key-value database using sqlite3, designed for simple and practical uses without external dependencies.

Project description

keyv

Transform SQLite into a powerful and fast key-value store.

keyv is a lightweight library that turns SQLite into a NoSQL key-value database, combining the best of both worlds: the simplicity of key-value stores with the reliability of SQLite. Perfect for applications that need a robust local database without the complexity of a full DBMS.

Usage

pip install keyv
import keyv

# initialize the database
db = keyv.connect('keyv.db') # or .anything, since is sqlite3 engine, you can choose any

# create or get a collection
collection = db.collection('my_collection')

# insert a key-value pair
collection.set('key1', 'value1')

# by default the .set method will replace existing values
# if you want to prevent overwriting existing keys, you can
# use replace_if_exists=False
collection.set('key1', 'value1', replace_if_exists=False)

# retrieve a value by key
collection.get('key1')
# >>> 'value1'
# note: the get method returns None for non-existing keys

# update a value
collection.update('key1', 123)
collection.get('key1')
# >>> 123

# delete a key-value pair
collection.delete('key1')

# search for keys by value
collection.set('key2', 'common_value')
collection.set('key3', 'common_value')
collection.search('common_value')
# >>> ['key2', 'key3']

# retrieve all keys, values and items
collection.keys()
# >>> ['key2', 'key3']

# retrieve all values
collection.values()
# >>> ['common_value', 'common_value']

# retrieve all key-value pairs
collection.items()
# >>> [('key2', 'common_value'), ('key3', 'common_value')]

# check if a key exists
collection.key_exists('key2')
# >>> True

# rename a collection
collection.change_name('new_collection_name')

# delete a collection
db.delete_collection('new_collection_name')

# get list of all collections
db.collections()
# >>> ['collection1', 'collection2', ...]

# iterate over key-value pairs
for key, value in collection.iteritems():
    print(f"{key}: {value}")

# iterate over keys
for key in collection.iterkeys():
    print(key)

# iterate over values
for value in collection.itervalues():
    print(value)

# get with default value for missing keys
collection.get('missing_key', default='default_value')
# >>> 'default_value'

# raise exception for missing keys
try:
    collection.get('missing_key', raise_if_missing=True)
except ValueError:
    print("Key not found!")

Collections allow you to organize your data into separate namespaces within the same database file. In practice, they are tables inside the SQLite file.

Quick Start

For simple use cases, you can create a default collection right away:

# Create a database with a default collection
db = keyv.connect('keyv.db').collection('main')

# Now you can use the collection methods directly
db.set('key1', 'value1')
db.get('key1')
# >>> 'value1'

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a PR.

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

keyv-0.4.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keyv-0.4.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file keyv-0.4.0.tar.gz.

File metadata

  • Download URL: keyv-0.4.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Windows/11

File hashes

Hashes for keyv-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7433d89c62215a168412dc3a6d2596af2b4ac114ee88bdf9e0d0c6bedd91fa3a
MD5 8b9b53a21a8b498399574f798cd1be62
BLAKE2b-256 13ab3388700107beb6cbc2ce9eb5bf698216e5b3a36deb8049db262ca3a77e67

See more details on using hashes here.

File details

Details for the file keyv-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: keyv-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Windows/11

File hashes

Hashes for keyv-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 316b3b0a94ead0f0478e0e34e5571d195516f9758c3d876e4d3735bf12173daf
MD5 73da689ffc73ae325eee7bd3089495f9
BLAKE2b-256 187c0136321372ee4245df0cf82e8bd153462653b8d5d0b40e428f627eb662a4

See more details on using hashes here.

Supported by

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