montydb


Namemontydb JSON
Version 2.5.3 PyPI version JSON
download
home_pagehttps://github.com/davidlatwe/montydb
SummaryMonty, Mongo tinified. MongoDB implemented in Python !
upload_time2024-05-19 20:30:06
maintainerNone
docs_urlNone
authordavidlatwe
requires_python>=3.7
licenseBSD-3-Clause License
keywords monty montydb pymongo mongodb database embedded
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            
<img src="artwork/logo.png" alt="drawing" width="600"/>

[![Python package](https://github.com/davidlatwe/montydb/actions/workflows/python-package.yml/badge.svg)](https://github.com/davidlatwe/montydb/actions/workflows/python-package.yml)
[![Version](http://img.shields.io/pypi/v/montydb.svg?style=flat)](https://pypi.python.org/pypi/montydb)
[![PyPi downloads](https://img.shields.io/pypi/dm/montydb)](https://pypistats.org/packages/montydb)

> Monty, Mongo tinified. MongoDB implemented in Python!

_Inspired by [TinyDB](https://github.com/msiemens/tinydb) and it's extension [TinyMongo](https://github.com/schapman1974/tinymongo)_


## What is it?

A pure Python-implemented database that looks and works like [MongoDB](https://www.mongodb.com/).

```python
>>> from montydb import MontyClient

>>> col = MontyClient(":memory:").db.test
>>> col.insert_many( [{"stock": "A", "qty": 6}, {"stock": "A", "qty": 2}] )
>>> cur = col.find( {"stock": "A", "qty": {"$gt": 4}} )
>>> next(cur)
{'_id': ObjectId('5ad34e537e8dd45d9c61a456'), 'stock': 'A', 'qty': 6}
```

Most of the CRUD operators have been implemented. You can visit [issue #14](https://github.com/davidlatwe/montydb/issues/14) to see the full list.

This project is tested against:

- MongoDB: 3.6, 4.0, 4.2 (4.4 on the way💦)
- Python: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12


## Install

```sh
pip install montydb
```

- optional, to use *real* `bson` in operation (`pymongo` will be installed)
    _For minimum requirements, `montydb` ships with it's own fork of `ObjectId` in `montydb.types`, so you may ignore this option if `ObjectId` is all you need from `bson`_

    ```sh
    pip install montydb[bson]
    ```
- optional, to use lightning memory-mapped db as storage engine
    ```sh
    pip install montydb[lmdb]
    ```


## Storage

🦄 Available storage engines:

* in-memory
* flat-file
* sqlite
* lmdb (lightning memory-mapped db)

Depending on which one you use, you may have to configure the storage engine before you start.

> ⚠️
>
> The configuration process only required on repository creation or modification. And, one repository (the parent level of databases) can only assign one storage engine.

To configure a storage, see flat-file storage for example:

```python
from montydb import set_storage, MontyClient


set_storage(
    # general settings
    
    repository="/db/repo",  # dir path for database to live on disk, default is {cwd}
    storage="flatfile",     # storage name, default "flatfile"
    mongo_version="4.0",    # try matching behavior with this mongodb version
    use_bson=False,         # default None, and will import pymongo's bson if None or True

    # any other kwargs are storage engine settings.
    
    cache_modified=10,       # the only setting that flat-file have
)

# ready to go
```

Once that done, there should be a file named `monty.storage.cfg` saved in your db repository path. It would be `/db/repo` for the above examples.


## Configuration

Now let's moving on to each storage engine's config settings.

### 🌟 In-Memory
  
`memory` storage does not need nor have any configuration, nothing saved to disk.

```python
from montydb import MontyClient


client = MontyClient(":memory:")

# ready to go
```

### 🔰 Flat-File
  
`flatfile` is the default on-disk storage engine.

```python
from montydb import set_storage, MontyClient


set_storage("/db/repo", cache_modified=5)  # optional step
client = MontyClient("/db/repo")  # use current working dir if no path given

# ready to go
```

FlatFile config:

```ini
[flatfile]
cache_modified: 0  # how many document CRUD cached before flush to disk.
```

### 💎 SQLite
  
`sqlite` is NOT the default on-disk storage, need configuration first before getting client.

> Pre-existing sqlite storage file which saved by `montydb<=1.3.0` is not read/writeable after `montydb==2.0.0`.

```python
from montydb import set_storage, MontyClient


set_storage("/db/repo", storage="sqlite")  # required, to set sqlite as engine
client = MontyClient("/db/repo")

# ready to go
```

SQLite config:

```ini
[sqlite]
journal_mode = WAL
check_same_thread =   # Leave it empty as False, or any value will be True
```
Or,

```python
repo = "path_to/repo"
set_storage(
    repository=repo,
    storage="sqlite",
    use_bson=True,
    # sqlite pragma
    journal_mode="WAL",
    # sqlite connection option
    check_same_thread=False,
)
client = MontyClient(repo)
...
```

SQLite write concern:

```python
client = MontyClient("/db/repo",
                     synchronous=1,
                     automatic_index=False,
                     busy_timeout=5000)
```

### 🚀 LMDB (Lightning Memory-Mapped Database)

`lightning` is NOT the default on-disk storage, need configuration first before get client.

> Newly implemented.

```python
from montydb import set_storage, MontyClient


set_storage("/db/repo", storage="lightning")  # required, to set lightning as engine
client = MontyClient("/db/repo")

# ready to go
```

LMDB config:

```ini
[lightning]
map_size: 10485760  # Maximum size database may grow to.
```

## URI

Optionally, You could prefix the repository path with montydb URI scheme.

```python
client = MontyClient("montydb:///db/repo")
```

## Utilities

> Pymongo `bson` may required.

* #### `montyimport`

  Imports content from an Extended JSON file into a MontyCollection instance.
  The JSON file could be generated from `montyexport` or `mongoexport`.

  ```python
  from montydb import open_repo, utils
  

  with open_repo("foo/bar"):
      utils.montyimport("db", "col", "/path/dump.json")
  
  ```

* #### `montyexport`

  Produces a JSON export of data stored in a MontyCollection instance.
  The JSON file could be loaded by `montyimport` or `mongoimport`.

  ```python
  from montydb import open_repo, utils
  

  with open_repo("foo/bar"):
      utils.montyexport("db", "col", "/data/dump.json")
  
  ```

* #### `montyrestore`

  Loads a binary database dump into a MontyCollection instance.
  The BSON file could be generated from `montydump` or `mongodump`.

  ```python
  from montydb import open_repo, utils
  

  with open_repo("foo/bar"):
      utils.montyrestore("db", "col", "/path/dump.bson")
  
  ```

* ####  `montydump`

  Creates a binary export from a MontyCollection instance.
  The BSON file could be loaded by `montyrestore` or `mongorestore`.

  ```python
  from montydb import open_repo, utils
  

  with open_repo("foo/bar"):
      utils.montydump("db", "col", "/data/dump.bson")
  
  ```

* #### `MongoQueryRecorder`

  Record MongoDB query results in a period of time.
  *Requires to access database profiler.*

  This works via filtering the database profile data and reproduce the queries of `find` and `distinct` commands.

  ```python
  from pymongo import MongoClient
  from montydb.utils import MongoQueryRecorder
  
  client = MongoClient()
  recorder = MongoQueryRecorder(client["mydb"])
  recorder.start()
  
  # Make some queries or run the App...
  recorder.stop()
  recorder.extract()
  {<collection_1>: [<doc_1>, <doc_2>, ...], ...}
  
  ```

* ####  `MontyList`

  Experimental, a subclass of `list`, combined the common CRUD methods from Mongo's Collection and Cursor.

  ```python
  from montydb.utils import MontyList
  
  mtl = MontyList([1, 2, {"a": 1}, {"a": 5}, {"a": 8}])
  mtl.find({"a": {"$gt": 3}})
  MontyList([{'a': 5}, {'a': 8}])
  
  ```

## Development

montydb uses [Poetry](https://python-poetry.org/) to make it easy manage dependencies and set up the development environment. 

### Initial setup

After cloning the repository, you need to run the following commands to set up the development environment:

```bash
make install
```

This will create a virtual environment and download the required dependencies.

### updating dependencies

To keep dependencies updated after git operations such as local updates or merging changes into local dev branch

```bash
make update
```
### Makefile

A makefile is used to simplify common operations such as updating, testing, and deploying etc.

```bash
make or make help

install                        install all dependencies locally
update                         update project dependencies locally (run after git update)
ci                             Run all checks (codespell, lint, bandit, test)
test                           Run tests
lint                           Run linting with flake8
codespell                      Find typos with codespell
bandit                         Run static security analysis with bandit
build                          Build project using poetry
clean                          Clean project
```

### Run mongo docker image
Most of our tests compare montydb CRUD results against real mongodb instance, therefore we must have a running
mongodb before testing.

For example, if we want to test against mongo 4.4:
```shell
docker run --name monty-4.4 -p 30044:27017 -d mongo:4.4
```

### Tests
```shell
poetry run pytest --storage {storage engin name} --mongodb {mongo instance url} [--use-bson]
```
Example:
```shell
poetry run pytest --storage memory --mongodb localhost:30044 --use-bson
```

## Why did I make this?

Mainly for personal skill practicing and fun.

I work in the VFX industry and some of my production needs (mostly edge-case) requires to run in a limited environment (e.g. outsourced render farms), which may have problem to run or connect a MongoDB instance. And I found this project really helps.

---

<p align=center>
    <a href="https://jb.gg/OpenSource"><i>This project is supported by JetBrains</i></a>
</p>

<p align="center">
    <img src="artwork/icon.png" alt="drawing" width="100"/>
  &nbsp;&nbsp;
    <img src="artwork/jetbrains.png" alt="drawing" width="100"/>
</p>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/davidlatwe/montydb",
    "name": "montydb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "monty, montydb, pymongo, mongodb, database, embedded",
    "author": "davidlatwe",
    "author_email": "davidlatwe@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e0/98/8f5b8f93a4b068d8de415d82fb6f196590053fa5f7911d7f578fcc6042a2/montydb-2.5.3.tar.gz",
    "platform": null,
    "description": "\n<img src=\"artwork/logo.png\" alt=\"drawing\" width=\"600\"/>\n\n[![Python package](https://github.com/davidlatwe/montydb/actions/workflows/python-package.yml/badge.svg)](https://github.com/davidlatwe/montydb/actions/workflows/python-package.yml)\n[![Version](http://img.shields.io/pypi/v/montydb.svg?style=flat)](https://pypi.python.org/pypi/montydb)\n[![PyPi downloads](https://img.shields.io/pypi/dm/montydb)](https://pypistats.org/packages/montydb)\n\n> Monty, Mongo tinified. MongoDB implemented in Python!\n\n_Inspired by [TinyDB](https://github.com/msiemens/tinydb) and it's extension [TinyMongo](https://github.com/schapman1974/tinymongo)_\n\n\n## What is it?\n\nA pure Python-implemented database that looks and works like [MongoDB](https://www.mongodb.com/).\n\n```python\n>>> from montydb import MontyClient\n\n>>> col = MontyClient(\":memory:\").db.test\n>>> col.insert_many( [{\"stock\": \"A\", \"qty\": 6}, {\"stock\": \"A\", \"qty\": 2}] )\n>>> cur = col.find( {\"stock\": \"A\", \"qty\": {\"$gt\": 4}} )\n>>> next(cur)\n{'_id': ObjectId('5ad34e537e8dd45d9c61a456'), 'stock': 'A', 'qty': 6}\n```\n\nMost of the CRUD operators have been implemented. You can visit [issue #14](https://github.com/davidlatwe/montydb/issues/14) to see the full list.\n\nThis project is tested against:\n\n- MongoDB: 3.6, 4.0, 4.2 (4.4 on the way\ud83d\udca6)\n- Python: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12\n\n\n## Install\n\n```sh\npip install montydb\n```\n\n- optional, to use *real* `bson` in operation (`pymongo` will be installed)\n    _For minimum requirements, `montydb` ships with it's own fork of `ObjectId` in `montydb.types`, so you may ignore this option if `ObjectId` is all you need from `bson`_\n\n    ```sh\n    pip install montydb[bson]\n    ```\n- optional, to use lightning memory-mapped db as storage engine\n    ```sh\n    pip install montydb[lmdb]\n    ```\n\n\n## Storage\n\n\ud83e\udd84 Available storage engines:\n\n* in-memory\n* flat-file\n* sqlite\n* lmdb (lightning memory-mapped db)\n\nDepending on which one you use, you may have to configure the storage engine before you start.\n\n> \u26a0\ufe0f\n>\n> The configuration process only required on repository creation or modification. And, one repository (the parent level of databases) can only assign one storage engine.\n\nTo configure a storage, see flat-file storage for example:\n\n```python\nfrom montydb import set_storage, MontyClient\n\n\nset_storage(\n    # general settings\n    \n    repository=\"/db/repo\",  # dir path for database to live on disk, default is {cwd}\n    storage=\"flatfile\",     # storage name, default \"flatfile\"\n    mongo_version=\"4.0\",    # try matching behavior with this mongodb version\n    use_bson=False,         # default None, and will import pymongo's bson if None or True\n\n    # any other kwargs are storage engine settings.\n    \n    cache_modified=10,       # the only setting that flat-file have\n)\n\n# ready to go\n```\n\nOnce that done, there should be a file named `monty.storage.cfg` saved in your db repository path. It would be `/db/repo` for the above examples.\n\n\n## Configuration\n\nNow let's moving on to each storage engine's config settings.\n\n### \ud83c\udf1f In-Memory\n  \n`memory` storage does not need nor have any configuration, nothing saved to disk.\n\n```python\nfrom montydb import MontyClient\n\n\nclient = MontyClient(\":memory:\")\n\n# ready to go\n```\n\n### \ud83d\udd30 Flat-File\n  \n`flatfile` is the default on-disk storage engine.\n\n```python\nfrom montydb import set_storage, MontyClient\n\n\nset_storage(\"/db/repo\", cache_modified=5)  # optional step\nclient = MontyClient(\"/db/repo\")  # use current working dir if no path given\n\n# ready to go\n```\n\nFlatFile config:\n\n```ini\n[flatfile]\ncache_modified: 0  # how many document CRUD cached before flush to disk.\n```\n\n### \ud83d\udc8e SQLite\n  \n`sqlite` is NOT the default on-disk storage, need configuration first before getting client.\n\n> Pre-existing sqlite storage file which saved by `montydb<=1.3.0` is not read/writeable after `montydb==2.0.0`.\n\n```python\nfrom montydb import set_storage, MontyClient\n\n\nset_storage(\"/db/repo\", storage=\"sqlite\")  # required, to set sqlite as engine\nclient = MontyClient(\"/db/repo\")\n\n# ready to go\n```\n\nSQLite config:\n\n```ini\n[sqlite]\njournal_mode = WAL\ncheck_same_thread =   # Leave it empty as False, or any value will be True\n```\nOr,\n\n```python\nrepo = \"path_to/repo\"\nset_storage(\n    repository=repo,\n    storage=\"sqlite\",\n    use_bson=True,\n    # sqlite pragma\n    journal_mode=\"WAL\",\n    # sqlite connection option\n    check_same_thread=False,\n)\nclient = MontyClient(repo)\n...\n```\n\nSQLite write concern:\n\n```python\nclient = MontyClient(\"/db/repo\",\n                     synchronous=1,\n                     automatic_index=False,\n                     busy_timeout=5000)\n```\n\n### \ud83d\ude80 LMDB (Lightning Memory-Mapped Database)\n\n`lightning` is NOT the default on-disk storage, need configuration first before get client.\n\n> Newly implemented.\n\n```python\nfrom montydb import set_storage, MontyClient\n\n\nset_storage(\"/db/repo\", storage=\"lightning\")  # required, to set lightning as engine\nclient = MontyClient(\"/db/repo\")\n\n# ready to go\n```\n\nLMDB config:\n\n```ini\n[lightning]\nmap_size: 10485760  # Maximum size database may grow to.\n```\n\n## URI\n\nOptionally, You could prefix the repository path with montydb URI scheme.\n\n```python\nclient = MontyClient(\"montydb:///db/repo\")\n```\n\n## Utilities\n\n> Pymongo `bson` may required.\n\n* #### `montyimport`\n\n  Imports content from an Extended JSON file into a MontyCollection instance.\n  The JSON file could be generated from `montyexport` or `mongoexport`.\n\n  ```python\n  from montydb import open_repo, utils\n  \n\n  with open_repo(\"foo/bar\"):\n      utils.montyimport(\"db\", \"col\", \"/path/dump.json\")\n  \n  ```\n\n* #### `montyexport`\n\n  Produces a JSON export of data stored in a MontyCollection instance.\n  The JSON file could be loaded by `montyimport` or `mongoimport`.\n\n  ```python\n  from montydb import open_repo, utils\n  \n\n  with open_repo(\"foo/bar\"):\n      utils.montyexport(\"db\", \"col\", \"/data/dump.json\")\n  \n  ```\n\n* #### `montyrestore`\n\n  Loads a binary database dump into a MontyCollection instance.\n  The BSON file could be generated from `montydump` or `mongodump`.\n\n  ```python\n  from montydb import open_repo, utils\n  \n\n  with open_repo(\"foo/bar\"):\n      utils.montyrestore(\"db\", \"col\", \"/path/dump.bson\")\n  \n  ```\n\n* ####  `montydump`\n\n  Creates a binary export from a MontyCollection instance.\n  The BSON file could be loaded by `montyrestore` or `mongorestore`.\n\n  ```python\n  from montydb import open_repo, utils\n  \n\n  with open_repo(\"foo/bar\"):\n      utils.montydump(\"db\", \"col\", \"/data/dump.bson\")\n  \n  ```\n\n* #### `MongoQueryRecorder`\n\n  Record MongoDB query results in a period of time.\n  *Requires to access database profiler.*\n\n  This works via filtering the database profile data and reproduce the queries of `find` and `distinct` commands.\n\n  ```python\n  from pymongo import MongoClient\n  from montydb.utils import MongoQueryRecorder\n  \n  client = MongoClient()\n  recorder = MongoQueryRecorder(client[\"mydb\"])\n  recorder.start()\n  \n  # Make some queries or run the App...\n  recorder.stop()\n  recorder.extract()\n  {<collection_1>: [<doc_1>, <doc_2>, ...], ...}\n  \n  ```\n\n* ####  `MontyList`\n\n  Experimental, a subclass of `list`, combined the common CRUD methods from Mongo's Collection and Cursor.\n\n  ```python\n  from montydb.utils import MontyList\n  \n  mtl = MontyList([1, 2, {\"a\": 1}, {\"a\": 5}, {\"a\": 8}])\n  mtl.find({\"a\": {\"$gt\": 3}})\n  MontyList([{'a': 5}, {'a': 8}])\n  \n  ```\n\n## Development\n\nmontydb uses [Poetry](https://python-poetry.org/) to make it easy manage dependencies and set up the development environment. \n\n### Initial setup\n\nAfter cloning the repository, you need to run the following commands to set up the development environment:\n\n```bash\nmake install\n```\n\nThis will create a virtual environment and download the required dependencies.\n\n### updating dependencies\n\nTo keep dependencies updated after git operations such as local updates or merging changes into local dev branch\n\n```bash\nmake update\n```\n### Makefile\n\nA makefile is used to simplify common operations such as updating, testing, and deploying etc.\n\n```bash\nmake or make help\n\ninstall                        install all dependencies locally\nupdate                         update project dependencies locally (run after git update)\nci                             Run all checks (codespell, lint, bandit, test)\ntest                           Run tests\nlint                           Run linting with flake8\ncodespell                      Find typos with codespell\nbandit                         Run static security analysis with bandit\nbuild                          Build project using poetry\nclean                          Clean project\n```\n\n### Run mongo docker image\nMost of our tests compare montydb CRUD results against real mongodb instance, therefore we must have a running\nmongodb before testing.\n\nFor example, if we want to test against mongo 4.4:\n```shell\ndocker run --name monty-4.4 -p 30044:27017 -d mongo:4.4\n```\n\n### Tests\n```shell\npoetry run pytest --storage {storage engin name} --mongodb {mongo instance url} [--use-bson]\n```\nExample:\n```shell\npoetry run pytest --storage memory --mongodb localhost:30044 --use-bson\n```\n\n## Why did I make this?\n\nMainly for personal skill practicing and fun.\n\nI work in the VFX industry and some of my production needs (mostly edge-case) requires to run in a limited environment (e.g. outsourced render farms), which may have problem to run or connect a MongoDB instance. And I found this project really helps.\n\n---\n\n<p align=center>\n    <a href=\"https://jb.gg/OpenSource\"><i>This project is supported by JetBrains</i></a>\n</p>\n\n<p align=\"center\">\n    <img src=\"artwork/icon.png\" alt=\"drawing\" width=\"100\"/>\n  &nbsp;&nbsp;\n    <img src=\"artwork/jetbrains.png\" alt=\"drawing\" width=\"100\"/>\n</p>\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause License",
    "summary": "Monty, Mongo tinified. MongoDB implemented in Python !",
    "version": "2.5.3",
    "project_urls": {
        "Homepage": "https://github.com/davidlatwe/montydb",
        "Repository": "https://github.com/davidlatwe/montydb"
    },
    "split_keywords": [
        "monty",
        " montydb",
        " pymongo",
        " mongodb",
        " database",
        " embedded"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d42dc156beaae6bd35a4d603a5e6b091fd10f7bbfdc8df374587909b466edd82",
                "md5": "10f8fbcf8004cd7deb099788de73ce5f",
                "sha256": "bf575d00f3fcf72caf8508d0740454da90fee1ebb4f6192ada5b5ad06c33acd6"
            },
            "downloads": -1,
            "filename": "montydb-2.5.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "10f8fbcf8004cd7deb099788de73ce5f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 74479,
            "upload_time": "2024-05-19T20:30:02",
            "upload_time_iso_8601": "2024-05-19T20:30:02.644077Z",
            "url": "https://files.pythonhosted.org/packages/d4/2d/c156beaae6bd35a4d603a5e6b091fd10f7bbfdc8df374587909b466edd82/montydb-2.5.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0988f5b8f93a4b068d8de415d82fb6f196590053fa5f7911d7f578fcc6042a2",
                "md5": "605ad2488efdb347d47b80160374d552",
                "sha256": "10591b5c4e1b44d01faa18c5c001a4c738af52fdf8cac04b744a17e352a743ef"
            },
            "downloads": -1,
            "filename": "montydb-2.5.3.tar.gz",
            "has_sig": false,
            "md5_digest": "605ad2488efdb347d47b80160374d552",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 64040,
            "upload_time": "2024-05-19T20:30:06",
            "upload_time_iso_8601": "2024-05-19T20:30:06.936475Z",
            "url": "https://files.pythonhosted.org/packages/e0/98/8f5b8f93a4b068d8de415d82fb6f196590053fa5f7911d7f578fcc6042a2/montydb-2.5.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-19 20:30:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "davidlatwe",
    "github_project": "montydb",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "montydb"
}
        
Elapsed time: 0.24775s