Skip to main content

A Python ORM for XTDB.

Project description

Logo

XTDB Python: A Python ORM for XTDB

Python Versions Stable Version

Tests Pre-commit License

Installation

You can install this project using pip:

$ pip install xtdb

Usage

The following examples assume you have set the XTDB_URI variable in your environment. To start experimenting, you could use the following setup using Docker:

$ docker run -p 3000:3000 -d juxt/xtdb-standalone-rocksdb:1.21.0
$ export XTDB_URI=http://localhost:3000/_xtdb

Using the Client

The XTDBClient supports the full HTTP API spec.

>>> import os
>>> from xtdb.session import XTDBClient, Operation
>>>
>>> client = XTDBClient(os.environ["XTDB_URI"])
>>> client.submit_transaction([Operation.put({"xt/id": "123", "name": "fred"})])
>>>
>>> client.query('{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}')
[[{'name': 'fred', 'xt/id': '123'}]]
>>>
>>> client.get_entity("123")
{'name': 'fred', 'xt/id': '123'}

Take a look at the spec to see the full range of functionality that maps directly to the client.

Using the Datalog module

The datalog module also provides a layer to construct queries with more easily. Given the data from the cities example has been seeded:

>>> from xtdb.datalog import Find, Where
>>>
>>> query = Find("(pull Country [*])") & Find("City") & (Where("City", "City/country", "Country") & Where("City", "City/name", '"Rome"'))
>>> str(query)
{:query {:find [ (pull Country [*]) City] :where [ [ City :City/country Country ] [ City :City/name "Rome" ]]}}
>>>
>>> client.query(query)
[[{'type': 'Country', 'Country/name': 'Italy', 'xt/id': 'c095839f-031f-46ad-85e1-097f634ba4f0'}, '33aa7fa6-b752-4982-a772-d2dbaeda58ae']]

To see more datalog query examples, check out the unit tests.

Using the ORM and Session

Below is an example of how to use the ORM functionality.

import os
from dataclasses import dataclass

from xtdb.orm import Base
from xtdb.query import Query
from xtdb.session import XTDBSession


@dataclass
class TestEntity(Base):
    name: str


@dataclass
class SecondEntity(Base):
    age: int
    test_entity: TestEntity

session = XTDBSession(os.environ["XTDB_URI"])
entity = TestEntity(name="test")

with session:
    session.put(entity)

query = Query(TestEntity).where(TestEntity, name="test")
result = session.query(query)

result[0].dict() #  {"TestEntity/name": "test", "type": "TestEntity", "xt/id": "fe2a3ee0-9254-41dc-91cc-74ad9e2a16db"}

To see more examples, check out the examples directory. Don't hesitate to add your own examples!

Using the CLI for querying

This package also comes with an easy CLI tool to query XTDB. To query XTDB using a plain query you can run

$ echo '{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}' | python -m xtdb
[[{"name": "fred", "xt/id": "123"}]]

To use a query file the command can be modified to the following:

$ cat query.txt
{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}
$
$ python -m xtdb < query.txt
[[{"name": "fred", "xt/id": "123"}]]

Contributing

Installation

To get started, clone the repo and create an environment using Poetry

$ git clone https://github.com/Donnype/xtdb-py.git
$ cd xtdb-py
$ poetry install

Now set up XTDB, for instance using Docker

$ docker run -p 3000:3000 -d juxt/xtdb-standalone-rocksdb:1.21.0

Export the XTDB_URI environment variable to be able to use os.environ["XTDB_URI"] to fetch the endpoint

$ export XTDB_URI=http://localhost:3000/_xtdb

Development

The Makefile has several targets that should make development easier:

$ make utest  # Run unit tests
$ make itest  # Run integration tests
$ make check  # Run all linters
$ make done   # Run all of the above

The CI runs these checks as well. Check out the project page for issues and features to work on.

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

xtdb-0.5.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

xtdb-0.5.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file xtdb-0.5.0.tar.gz.

File metadata

  • Download URL: xtdb-0.5.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure

File hashes

Hashes for xtdb-0.5.0.tar.gz
Algorithm Hash digest
SHA256 259573a169a60ef2b1b65b7268b5467e635b713c90ceef78d0b71eb4c7263d17
MD5 56e3100cb3e6225a2cb4f47f857e33a4
BLAKE2b-256 de5fa3e4e011061bc89b3cec8ed8056e35fd400c9356d2f531834d9315477908

See more details on using hashes here.

File details

Details for the file xtdb-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: xtdb-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure

File hashes

Hashes for xtdb-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a178cb5f523b5b7f4b43fbc826b565137b3ae26f2a65d35cd8656d7b3a95e981
MD5 23a040970f19b5f82b890de23e1997bf
BLAKE2b-256 fb6102d6b7d797944efadb93ce4d3510beb02747f7cd7a7178c465072ac2fe7c

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