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

Uploaded Python 3

File details

Details for the file xtdb-0.1.1a0.tar.gz.

File metadata

  • Download URL: xtdb-0.1.1a0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.0 Linux/5.15.0-1037-azure

File hashes

Hashes for xtdb-0.1.1a0.tar.gz
Algorithm Hash digest
SHA256 320bbdc796cc71bb98f09bee70208fba9a186bdd36c192c7b353803e43df0702
MD5 526e45ae2b7e59645c078c2ad71ca250
BLAKE2b-256 36faa885a8077712cda2a66e9e1ea24e8f400c266d8b55af6c7ff1e79e4b1408

See more details on using hashes here.

File details

Details for the file xtdb-0.1.1a0-py3-none-any.whl.

File metadata

  • Download URL: xtdb-0.1.1a0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.0 Linux/5.15.0-1037-azure

File hashes

Hashes for xtdb-0.1.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 5056072024ee2e331fb29e72b115fcb49cfcb679877b2d5c321e88e704a4f1a1
MD5 dfd63cd0865c251635c1d3debc0be421
BLAKE2b-256 098f307b0babb917d9c71d2005308181e9c7b217a8646ef5ec4fe64abca097bf

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