Skip to main content

A Python connector and ORM for XTDB.

Project description

XTDB Python

A Python connector and ORM for XTDB.

Code Integration Pypi release

Installation

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 ORM

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")
session.put(entity)
session.commit()

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

assert result[0].dict() == {"TestEntity/name": "test", "type": "TestEntity", "xt/id": entity.id}

Using only the client

import os

from xtdb.session import XTDBHTTPClient, Operation

client = XTDBHTTPClient(os.environ["XTDB_URI"])
client.submit_transaction([Operation.put({"xt/id": "123", "name": "fred"})])

client.query('{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}')
client.get_entity("123")

Using the CLI tool for querying

Using a query string

$ echo '{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}' | python -m xtdb | jq
[
  [
    {
      "somenil": null,
      "name": "fred",
      "somedict": {
        "c": 3,
        "a": "b"
      },
      "somelist": [
        "jeez",
        123
      ],
      "xt/id": "123"
    }
  ]
]

or a query in a file:

$ cat query.txt
{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}
$ python -m xtdb < query.txt | jq
[
  [
    {
      "somenil": null,
      "name": "fred",
      "somedict": {
        "c": 3,
        "a": "b"
      },
      "somelist": [
        "jeez",
        123
      ],
      "xt/id": "123"
    }
  ]
]

Development

Using Poetry, simply run

poetry install

to create your environment. 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

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.1.0.tar.gz (11.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.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtdb-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.9.5 Linux/5.15.0-71-generic

File hashes

Hashes for xtdb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a6b4787750ec6dafb8652a308c42f5c19c30a0f0088a5b9c5b8c66c499324ca
MD5 6182317ed06a4645f10145fc8d7cf396
BLAKE2b-256 1d83115f76d573638b37473eda633319f5d6f20411994e1713960d22086c3e6c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtdb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.9.5 Linux/5.15.0-71-generic

File hashes

Hashes for xtdb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50a4a863f33b29d0f84407e27d7daab825dd0c1a2dbd3a4cbf904f248fd14d72
MD5 9e34f13f82d8f2836a442a7308dd48cd
BLAKE2b-256 3debb5f4c4795678a0c59ab1d8af875fd4cdd3ea8965ecf7f078f3a68f339a55

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