{"id":13758729,"url":"https://github.com/TigreGotico/json_database","last_synced_at":"2025-05-10T08:30:45.038Z","repository":{"id":43882762,"uuid":"435869300","full_name":"TigreGotico/json_database","owner":"TigreGotico","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-25T20:37:05.000Z","size":49,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2024-10-30T01:56:54.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TigreGotico.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-07T12:21:18.000Z","updated_at":"2024-10-25T20:37:09.000Z","dependencies_parsed_at":"2024-10-30T01:57:03.047Z","dependency_job_id":null,"html_url":"https://github.com/TigreGotico/json_database","commit_stats":null,"previous_names":["openjarbas/json_database"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TigreGotico%2Fjson_database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TigreGotico%2Fjson_database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TigreGotico%2Fjson_database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TigreGotico%2Fjson_database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TigreGotico","download_url":"https://codeload.github.com/TigreGotico/json_database/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253389521,"owners_count":21900769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T13:00:35.550Z","updated_at":"2025-05-10T08:30:45.029Z","avatar_url":"https://github.com/TigreGotico.png","language":"Python","readme":"# Json Database\n\nPython dict based database with persistence and search capabilities\n\nFor those times when you need something simple and sql is overkill\n\n\n## Features\n\n- pure python\n- save and load from file\n- search recursively by key and key/value pairs\n- fuzzy search\n- supports arbitrary objects\n- supports comments in saved files\n\n## Install\n\n```bash\npip install json_database\n```\n\n\n## 📡 HiveMind Integration\n\nThis project includes a native [hivemind-plugin-manager](https://github.com/JarbasHiveMind/hivemind-plugin-manager) integration, providing seamless interoperability with the HiveMind ecosystem.\n- **Database Plugin**: Provides `hivemind-json-db-plugin` allowing to use JSON-based storage for client credentials and permissions\n  \n## 🐍 Usage\n\n\n### JsonStorage\n\nSometimes you need persistent dicts that you can save and load from file\n\n```python\nfrom json_database import JsonStorage\nfrom os.path import exists\n\nsave_path = \"my_dict.conf\"\n\nmy_config = JsonStorage(save_path)\n\nmy_config[\"lang\"] = \"pt\"\nmy_config[\"secondary_lang\"] = \"en\"\nmy_config[\"email\"] = \"jarbasai@mailfence.com\"\n\n# my_config is a python dict\nassert isinstance(my_config, dict)\n\n# save to file\nmy_config.store()\n\nmy_config[\"lang\"] = \"pt-pt\"\n\n# revert to previous saved file\nmy_config.reload()\nassert my_config[\"lang\"] == \"pt\"\n\n# clear all fields\nmy_config.clear()\nassert my_config == {}\n\n# load from a specific path\nmy_config.load_local(save_path)\nassert my_config == JsonStorage(save_path)\n\n# delete stored file\nmy_config.remove()\nassert not exists(save_path)\n\n# keep working with dict in memory\nprint(my_config)\n```\n\n### JsonDatabase\n\nEver wanted to search a dict?\n\nLet's create a dummy database with users\n\n```python\nfrom json_database import JsonDatabase\n\ndb_path = \"users.db\"\n\nwith JsonDatabase(\"users\", db_path) as db:\n    # add some users to the database\n\n    for user in [\n        {\"name\": \"bob\", \"age\": 12},\n        {\"name\": \"bobby\"},\n        {\"name\": [\"joe\", \"jony\"]},\n        {\"name\": \"john\"},\n        {\"name\": \"jones\", \"age\": 35},\n        {\"name\": \"joey\", \"birthday\": \"may 12\"}]:\n        db.add_item(user)\n        \n    # pretty print database contents\n    db.print()\n\n\n# auto saved when used with context manager\n# db.commit()\n\n\n```\n         \nsearch entries by key\n\n```python\nfrom json_database import JsonDatabase\n\ndb_path = \"users.db\"\n\ndb = JsonDatabase(\"users\", db_path) # load db created in previous example\n\n# search by exact key match\nusers_with_defined_age = db.search_by_key(\"age\")\n\nfor user in users_with_defined_age:\n    print(user[\"name\"], user[\"age\"])\n    \n# fuzzy search\nusers = db.search_by_key(\"birth\", fuzzy=True)\nfor user, conf in users:\n    print(\"matched with confidence\", conf)\n    print(user[\"name\"], user[\"birthday\"])\n```\n\nsearch by key value pair\n\n```python\n# search by key/value pair\nusers_12years_old = db.search_by_value(\"age\", 12)\n\nfor user in users_12years_old:\n    assert user[\"age\"] == 12\n\n# fuzzy search\njon_users = db.search_by_value(\"name\", \"jon\", fuzzy=True)\nfor user, conf in jon_users:\n    print(user[\"name\"])\n    print(\"matched with confidence\", conf)\n    # NOTE that one of the users has a list instead of a string in the name, it also matches\n```\n\nupdating an existing entry\n\n```python\n# get database item\nitem = {\"name\": \"bobby\"}\n\nitem_id = db.get_item_id(item)\n\nif item_id \u003e= 0:\n    new_item = {\"name\": \"don't call me bobby\"}\n    db.update_item(item_id, new_item)\nelse:\n    print(\"item not found in database\")\n\n# clear changes since last commit\ndb.reset()\n```\n\nYou can save arbitrary objects to the database\n\n```python\nfrom json_database import JsonDatabase\n\ndb = JsonDatabase(\"users\", \"~/databases/users.json\")\n\n\nclass User:\n    def __init__(self, email, key=None, data=None):\n        self.email = email\n        self.secret_key = key\n        self.data = data\n\nuser1 = User(\"first@mail.net\", data={\"name\": \"jonas\", \"birthday\": \"12 May\"})\nuser2 = User(\"second@mail.net\", \"secret\", data={\"name\": [\"joe\", \"jony\"], \"age\": 12})\n\n# objects will be \"jsonified\" here, they will no longer be User objects\n# if you need them to be a specific class use some ORM lib instead (SQLAlchemy is great)\ndb.add_item(user1)\ndb.add_item(user2)\n\n# search entries with non empty key\nprint(db.search_by_key(\"secret_key\"))\n\n# search in user provided data\nprint(db.search_by_key(\"birth\", fuzzy=True))\n\n# search entries with a certain value\nprint(db.search_by_value(\"age\", 12))\nprint(db.search_by_value(\"name\", \"jon\", fuzzy=True))\n\n```\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTigreGotico%2Fjson_database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTigreGotico%2Fjson_database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTigreGotico%2Fjson_database/lists"}