Skip to main content

Spawn MongoDB resources from JSON Schema

Project description

mongospawn is a tool to help spawn MongoDB resources given JSON Schema specifications.

The primary near-term use case is support for the National Microbiome Data Collaborative (NMDC) pilot project. In particular, given a JSON Schema with all array-typed properties and with each array item a $ref reference to one of the JSON Schema definitions (see NMDC example), mongospawn can generate MongoDB $jsonSchema documents to apply as validators for collections in a database that correspond to each of the original JSON Schema's array-typed properties. MongoDB's implementation of JSON Schema does not support $ref, definitions, etc., so mongospawn expands references to generate appropriate per-collection schema documents.

In addition to generating derived schema documents, mongospawn can spawn new databases/collections, with schema validation set, via the pymongo driver, and can also manage access to the spawned resources via mongogrant.

Setup

For development:

pip install -e .[dev]

To update dependency versions:

make update

To use pinned dependencies for reproducible testing:

make

Usage

Example using NMDC's JSON Schema:

from mongospawn.schema import dbschema_from_file, collschemas_for
from pymongo import MongoClient

client = MongoClient()
db = client.nmdc_test

dbschema = dbschema_from_file("nmdc.schema.json")
collschemas = collschemas_for(dbschema)
for name in collschemas:
    db.drop_collection(name)
    db.create_collection(name, validator={"$jsonSchema": collschemas[name]})
    print(f"created {name} collection")
# created activity_set collection
# created biosample_set collection
# created data_object_set collection
# created omics_processing_set collection
# created study_set collection

Now, e.g. if you try to insert a non-conformant JSON document, a pymongo.errors.WriteError will be raised:

db.biosample_set.insert_one({"not_a_real_field": 1})
# => WriteError: Document failed validation...

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

mongospawn-0.5.8.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

mongospawn-0.5.8-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file mongospawn-0.5.8.tar.gz.

File metadata

  • Download URL: mongospawn-0.5.8.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3.post20210118 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for mongospawn-0.5.8.tar.gz
Algorithm Hash digest
SHA256 08cf5b6b9b8d61632f0c985ea27868da754273d9696441778bc50c085d198dd2
MD5 74e9a38d048f8465492a2e0e077c4956
BLAKE2b-256 1a3d27deb021e2584b4c20de021436b10671138c3d06e9c27335483cbeba6a14

See more details on using hashes here.

File details

Details for the file mongospawn-0.5.8-py3-none-any.whl.

File metadata

  • Download URL: mongospawn-0.5.8-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3.post20210118 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for mongospawn-0.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7e01b1209a08b143f57b5b6c391311260c516f6a1ab6d4b620f9a5fb21c0d56d
MD5 cdcc04b046ae97cf44f3d3c87b14dd3a
BLAKE2b-256 1693b5ddb7d73b6118da4986e4f5dcc94e1ec9ffcde055a8af7bdf0bcf6404ba

See more details on using hashes here.

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