Name | kybra-simple-db JSON |
Version |
0.2.1
JSON |
| download |
home_page | https://github.com/smart-social-contracts/kybra-simple-db |
Summary | A lightweight key-value database written in Python, intended for use with Kybra on the Internet Computer (IC) |
upload_time | 2025-07-19 07:04:11 |
maintainer | None |
docs_url | None |
author | Smart Social Contracts |
requires_python | >=3.7 |
license | MIT License
Copyright (c) 2025 Smart Social Contracts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|
keywords |
database
key-value
entity
relationship
audit
|
VCS |
 |
bugtrack_url |
|
requirements |
kybra-simple-logging
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Kybra Simple DB
A lightweight key-value database with entity relationships and audit logging capabilities, intended for small to medium-sized applications running on the Internet Computer using Kybra.
[](https://github.com/smart-social-contracts/kybra-simple-db/actions)
[](https://github.com/smart-social-contracts/kybra-simple-db/actions)
[](https://badge.fury.io/py/kybra-simple-db)
[](https://www.python.org/downloads/release/python-3107/)
[](https://github.com/smart-social-contracts/kybra-simple-db/blob/main/LICENSE)
## Features
- **Persistent Storage**: Works with Kybra's StableBTreeMap stable structure for persistent storage on your canister's stable memory so your data persists automatically across canister upgrades.
- **Entity-Relational Database**: Create, read and write entities with OneToOne, OneToMany, ManyToOne, and ManyToMany relationships.
- **Audit Logging**: Track all changes to your data with created/updated timestamps and who created and updated each entity.
- **Ownership**: Assign owners to your data objects to control who can modify them.
## Installation
```bash
pip install kybra-simple-db
```
## Quick Start
The database storage must be initialized before using Kybra Simple DB. Here's an example of how to do it:
```python
from kybra import StableBTreeMap
from kybra_simple_db import Database
# Initialize storage and database
storage = StableBTreeMap[str, str](memory_id=1, max_key_size=100, max_value_size=1000) # Use a unique memory ID for each storage instance
Database.init(db_storage=storage)
```
Read [Kybra's documentation](https://demergent-labs.github.io/kybra/stable_structures.html?highlight=StableBTreeMap) for more information regarding StableBTreeMap and memory IDs.
Next, define your entities:
```python
from kybra_simple_db import (
Database, Entity, String, Integer,
OneToOne, OneToMany, ManyToOne, ManyToMany, TimestampedMixin
)
class Person(Entity, TimestampedMixin):
__alias__ = "name" # Use `name` as the alias field for lookup by `name`
name = String(min_length=2, max_length=50)
age = Integer(min_value=0, max_value=120)
friends = ManyToMany("Person", "friends")
mother = ManyToOne("Person", "children")
children = OneToMany("Person", "mother")
spouse = OneToOne("Person", "spouse")
```
Then use the defined entities to store objects:
```python
# Create and save an object
john = Person(name="John", age=30)
# Update an object's property
john.age = 33 # Type checking and validation happens automatically
# use the `_id` property to load an entity with the [] operator
Person(name="Peter")
peter = Person["Peter"]
# Delete an object
peter.delete()
# Create relationships
alice = Person(name="Alice")
eva = Person(name="Eva")
john.mother = alice
assert john in alice.children
eva.friends = [alice]
assert alice in eva.friends
assert eva in alice.friends
pprint(alice.to_dict()) # Prints the dictionary representation of an object
''' Prints:
{'_id': '2',
'_type': 'Person',
'age': None,
'creator': 'system',
'name': 'Alice',
'owner': 'system',
'relations': {'children': [{'_id': '1', '_type': 'Person'}],
'friends': [{'_id': '3', '_type': 'Person'}]},
'timestamp_created': '2025-04-08 20:45:08.957',
'timestamp_updated': '2025-04-08 20:45:08.957',
'updater': 'system'}
'''
assert Person.count() == 3
assert Person.max_id() == 4
assert Person.instances() == [john, alice, eva]
# Cursor-based pagination
assert Person.load_some(0, 2) == [john, alice]
assert Person.load_some(2, 2) == [eva]
# Retrieve database contents in JSON format
print(Database.get_instance().dump_json(pretty=True))
# Audit log
audit_records = Database.get_instance().get_audit(id_from=0, id_to=5)
pprint(audit_records['0'])
''' Prints:
['save',
1744138342934,
'Person@1',
{'_id': '1',
'_type': 'Person',
'age': 30,
'creator': 'system',
'name': 'John',
'owner': 'system',
'timestamp_created': '2025-04-08 20:52:22.934',
'timestamp_updated': '2025-04-08 20:52:22.934',
'updater': 'system'}]
'''
```
For more usage examples, see the [tests](tests/src/tests).
## API Reference
- **Core**: `Database`, `Entity`
- **Properties**: `String`, `Integer`, `Float`, `Boolean`
- **Relationships**: `OneToOne`, `OneToMany`, `ManyToOne`, `ManyToMany`
- **Mixins**: `TimestampedMixin` (timestamps and ownership tracking)
## Development
### Setup Development Environment
```bash
# Clone the repository
git clone https://github.com/smart-social-contracts/kybra-simple-db.git
cd kybra-simple-db
# Recommended setup
pyenv install 3.10.7
pyenv local 3.10.7
python -m venv venv
source venv/bin/activate
# Install development dependencies
pip install -r requirements-dev.txt
# Running tests
./run_linters.sh && (cd tests && ./run_test.sh && ./run_test_ic.sh)
```
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
[MIT](LICENSE).
Raw data
{
"_id": null,
"home_page": "https://github.com/smart-social-contracts/kybra-simple-db",
"name": "kybra-simple-db",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "database, key-value, entity, relationship, audit",
"author": "Smart Social Contracts",
"author_email": "Smart Social Contracts <contact@smartsocialcontracts.org>",
"download_url": "https://files.pythonhosted.org/packages/d6/21/824de11be16c47bf03c8cfcc9169a36e0ac273852c36fa29823ca5bf9754/kybra_simple_db-0.2.1.tar.gz",
"platform": null,
"description": "# Kybra Simple DB\n\nA lightweight key-value database with entity relationships and audit logging capabilities, intended for small to medium-sized applications running on the Internet Computer using Kybra.\n\n[](https://github.com/smart-social-contracts/kybra-simple-db/actions)\n[](https://github.com/smart-social-contracts/kybra-simple-db/actions)\n[](https://badge.fury.io/py/kybra-simple-db)\n[](https://www.python.org/downloads/release/python-3107/)\n[](https://github.com/smart-social-contracts/kybra-simple-db/blob/main/LICENSE)\n\n## Features\n\n- **Persistent Storage**: Works with Kybra's StableBTreeMap stable structure for persistent storage on your canister's stable memory so your data persists automatically across canister upgrades.\n- **Entity-Relational Database**: Create, read and write entities with OneToOne, OneToMany, ManyToOne, and ManyToMany relationships.\n- **Audit Logging**: Track all changes to your data with created/updated timestamps and who created and updated each entity.\n- **Ownership**: Assign owners to your data objects to control who can modify them.\n\n\n## Installation\n\n```bash\npip install kybra-simple-db\n```\n\n## Quick Start\n\nThe database storage must be initialized before using Kybra Simple DB. Here's an example of how to do it:\n\n```python\nfrom kybra import StableBTreeMap\nfrom kybra_simple_db import Database\n\n# Initialize storage and database\nstorage = StableBTreeMap[str, str](memory_id=1, max_key_size=100, max_value_size=1000) # Use a unique memory ID for each storage instance\nDatabase.init(db_storage=storage)\n```\n\nRead [Kybra's documentation](https://demergent-labs.github.io/kybra/stable_structures.html?highlight=StableBTreeMap) for more information regarding StableBTreeMap and memory IDs.\n\nNext, define your entities:\n\n```python\nfrom kybra_simple_db import (\n Database, Entity, String, Integer,\n OneToOne, OneToMany, ManyToOne, ManyToMany, TimestampedMixin\n)\n\nclass Person(Entity, TimestampedMixin):\n __alias__ = \"name\" # Use `name` as the alias field for lookup by `name`\n name = String(min_length=2, max_length=50)\n age = Integer(min_value=0, max_value=120)\n friends = ManyToMany(\"Person\", \"friends\")\n mother = ManyToOne(\"Person\", \"children\")\n children = OneToMany(\"Person\", \"mother\")\n spouse = OneToOne(\"Person\", \"spouse\")\n```\n\nThen use the defined entities to store objects:\n\n```python\n # Create and save an object\n john = Person(name=\"John\", age=30)\n\n # Update an object's property\n john.age = 33 # Type checking and validation happens automatically\n\n # use the `_id` property to load an entity with the [] operator\n Person(name=\"Peter\")\n peter = Person[\"Peter\"]\n\n # Delete an object\n peter.delete()\n\n # Create relationships\n alice = Person(name=\"Alice\")\n eva = Person(name=\"Eva\")\n john.mother = alice\n assert john in alice.children\n eva.friends = [alice]\n assert alice in eva.friends\n assert eva in alice.friends\n\n pprint(alice.to_dict()) # Prints the dictionary representation of an object\n ''' Prints:\n\n {'_id': '2',\n '_type': 'Person',\n 'age': None,\n 'creator': 'system',\n 'name': 'Alice',\n 'owner': 'system',\n 'relations': {'children': [{'_id': '1', '_type': 'Person'}],\n 'friends': [{'_id': '3', '_type': 'Person'}]},\n 'timestamp_created': '2025-04-08 20:45:08.957',\n 'timestamp_updated': '2025-04-08 20:45:08.957',\n 'updater': 'system'}\n\n '''\n\n assert Person.count() == 3\n assert Person.max_id() == 4\n assert Person.instances() == [john, alice, eva]\n\n # Cursor-based pagination\n assert Person.load_some(0, 2) == [john, alice]\n assert Person.load_some(2, 2) == [eva]\n\n # Retrieve database contents in JSON format\n print(Database.get_instance().dump_json(pretty=True))\n\n # Audit log\n audit_records = Database.get_instance().get_audit(id_from=0, id_to=5)\n pprint(audit_records['0'])\n ''' Prints:\n\n ['save',\n 1744138342934,\n 'Person@1',\n {'_id': '1',\n '_type': 'Person',\n 'age': 30,\n 'creator': 'system',\n 'name': 'John',\n 'owner': 'system',\n 'timestamp_created': '2025-04-08 20:52:22.934',\n 'timestamp_updated': '2025-04-08 20:52:22.934',\n 'updater': 'system'}]\n\n '''\n```\n\nFor more usage examples, see the [tests](tests/src/tests).\n\n## API Reference\n\n- **Core**: `Database`, `Entity`\n- **Properties**: `String`, `Integer`, `Float`, `Boolean`\n- **Relationships**: `OneToOne`, `OneToMany`, `ManyToOne`, `ManyToMany`\n- **Mixins**: `TimestampedMixin` (timestamps and ownership tracking)\n\n## Development\n\n### Setup Development Environment\n\n```bash\n# Clone the repository\ngit clone https://github.com/smart-social-contracts/kybra-simple-db.git\ncd kybra-simple-db\n\n# Recommended setup\npyenv install 3.10.7\npyenv local 3.10.7\npython -m venv venv\nsource venv/bin/activate\n\n# Install development dependencies\npip install -r requirements-dev.txt\n\n# Running tests\n./run_linters.sh && (cd tests && ./run_test.sh && ./run_test_ic.sh)\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\n[MIT](LICENSE).\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2025 Smart Social Contracts\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n ",
"summary": "A lightweight key-value database written in Python, intended for use with Kybra on the Internet Computer (IC)",
"version": "0.2.1",
"project_urls": {
"Homepage": "https://github.com/smart-social-contracts/kybra-simple-db",
"Issues": "https://github.com/smart-social-contracts/kybra-simple-db/issues",
"Repository": "https://github.com/smart-social-contracts/kybra-simple-db.git"
},
"split_keywords": [
"database",
" key-value",
" entity",
" relationship",
" audit"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "cf166ab72b8913c8f63a567145363ea2fa252ddec9bb404ebde8430711f7438a",
"md5": "1907b8c2ea9c6ae2d32090c07bd15f6e",
"sha256": "cfed566da871ebde5c3540af9672d95211a7f6b18832d879dc4142091e39914c"
},
"downloads": -1,
"filename": "kybra_simple_db-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1907b8c2ea9c6ae2d32090c07bd15f6e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 18953,
"upload_time": "2025-07-19T07:04:10",
"upload_time_iso_8601": "2025-07-19T07:04:10.163142Z",
"url": "https://files.pythonhosted.org/packages/cf/16/6ab72b8913c8f63a567145363ea2fa252ddec9bb404ebde8430711f7438a/kybra_simple_db-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d621824de11be16c47bf03c8cfcc9169a36e0ac273852c36fa29823ca5bf9754",
"md5": "35fe46314302e2dbc6f61467f3635809",
"sha256": "1601717aff624c1bbba53ddee82f18682ed9e656250f9055116f34e13e45382e"
},
"downloads": -1,
"filename": "kybra_simple_db-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "35fe46314302e2dbc6f61467f3635809",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 19490,
"upload_time": "2025-07-19T07:04:11",
"upload_time_iso_8601": "2025-07-19T07:04:11.266287Z",
"url": "https://files.pythonhosted.org/packages/d6/21/824de11be16c47bf03c8cfcc9169a36e0ac273852c36fa29823ca5bf9754/kybra_simple_db-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-19 07:04:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "smart-social-contracts",
"github_project": "kybra-simple-db",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "kybra-simple-logging",
"specs": [
[
"==",
"0.1.*"
]
]
}
],
"lcname": "kybra-simple-db"
}