Skip to main content

A Python connector and ORM for XTDB.

Project description

XTDB Python

A Python connector and ORM for XTDB.

Code Integration Stable Version Python Versions

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 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" ]]}}')
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.2.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.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtdb-0.2.0.tar.gz
  • Upload date:
  • Size: 12.7 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.2.0.tar.gz
Algorithm Hash digest
SHA256 65f4bd2adc444709172c24ebb8d5dd81a7640c384d29f88fd6bd2dd2adfc9181
MD5 54d1e78a23621bc413c35f6dd4b7cf3b
BLAKE2b-256 b3726ba1d2e34260bb065d82958fe33b00cbc718ea3318316a576c0efc632a70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtdb-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20a2e21d0ab5461b887c692024ef7c04ba892c20c8c658e84f016e526d86d596
MD5 76f88a6889ea280d5394d81882c49b48
BLAKE2b-256 cf46e8f3cf4ef6b568b91d90a12e21984f421ffc551e1461b6e33a0a4f97366d

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