Skip to main content

Semantic interoperability for SQLModel — JSON-LD and RDF export with ontology-aware fields (import ontosql)

Project description

OntoSQL

Semantic interoperability for SQLModel — enrich operational models with ontology metadata and export JSON-LD and RDF without leaving Python.

pip install ontosql
pip install "ontosql[fastapi]"   # optional API helpers

Quick start

from sqlmodel import Field, SQLModel
from ontosql import OntoMixin, onto_field, onto_model


@onto_model(type_="schema:Person", iri_template="http://example.org/person/{id}")
class Person(SQLModel, OntoMixin, table=False):
    id: int | None = Field(default=None, primary_key=True)
    name: str = onto_field(ontology="schema:name")


person = Person(id=1, name="Ada Lovelace")
print(person.to_jsonld())
print(person.to_rdf(format="turtle"))

Features (0.1.0)

  • onto_field() — attach ontology CURIEs/IRIs to model fields
  • onto_model() — declare RDF type and instance IRI templates on classes
  • PrefixRegistry — manage namespace prefixes for JSON-LD @context
  • to_jsonld() / to_rdf() — export instances to semantic web formats
  • FastAPI response helpers with content negotiation (ontosql[fastapi])

FastAPI

from fastapi import FastAPI, Request
from ontosql.fastapi import negotiate_onto_response

app = FastAPI()

@app.get("/person/{person_id}")
def get_person(person_id: int, request: Request):
    person = Person(id=person_id, name="Ada Lovelace")
    return negotiate_onto_response(request, person)

See examples/fastapi_demo.py.

Limitations (0.1.0)

  • No RDF import or SHACL generation yet (planned for 0.2+)
  • Foreign-key-only relationships export as @id references, not nested objects (use a nested OntoMixin field for embedded objects)
  • JSON-LD framing requires a future ontosql[jsonld] extra (PyLD)
  • Do not map two fields to the same ontology property; if you do, nested objects are preferred over FK integers (a warning is emitted)

Documentation

Development

See Releasing for the version publish checklist.

pip install -e ".[dev]"
ruff check src tests
ruff format src tests
ty check
pytest --cov=ontosql --cov-fail-under=100

License

MIT — see LICENSE.

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

ontosql-0.1.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

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

ontosql-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file ontosql-0.1.0.tar.gz.

File metadata

  • Download URL: ontosql-0.1.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for ontosql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a2c97a3abc45e50036b04c856d6bf68c673b46687dea58f07dc2d0d775dd9b0
MD5 dfa80faa7deb2bb4027da5ac1dd425f9
BLAKE2b-256 0f082c0c0af2c9b82235a3b8894c0791eb23591428af8531ae411885c099ffc6

See more details on using hashes here.

File details

Details for the file ontosql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ontosql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for ontosql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0ec2292e564363fa5cbe9a7dd509151afcb590c85a822896690757cf891d144
MD5 32cc409ce9e0bc90a1fa1b08c50cd6ff
BLAKE2b-256 901eb8fc1ffde840d678f7205e8502d4f12700f5d63a55febd234d933e030690

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