Skip to main content

A Python ORM for XTDB.

Project description

Logo

XTDB Python: A Python ORM for XTDB

Tests Stable Version Python Versions

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" ]]}}')
client.get_entity("123")

Using the ORM

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")
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}

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 | jq
[
  [
    {
      "xt/id": "123"
      "name": "fred",
      "somenil": null,
      "somedict": {
        "c": 3,
        "a": "b"
      },
    }
  ]
]

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 | jq
[
  [
    {
      "xt/id": "123"
      "name": "fred",
      "somenil": null,
      "somedict": {
        "c": 3,
        "a": "b"
      },
    }
  ]
]

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.3.0.tar.gz (14.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.3.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xtdb-0.3.0.tar.gz
  • Upload date:
  • Size: 14.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.3.0.tar.gz
Algorithm Hash digest
SHA256 fd1637f0e30161f0363ebf1a9d1b536a869ba55fd29bfce0a014adcdb1d136b5
MD5 3cfc4da6b74545346ee3da34a3944a04
BLAKE2b-256 e076c2fec5537d0668d379b7052530ee2161984a1dda67e604456cbda0920da7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xtdb-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d57bff2c5367ab1988c1347c82a8aad96c30db9d27a2664640ff275e2f3f7bc
MD5 125c768ac65c9dd0968fd98100a525a1
BLAKE2b-256 41c393f71ab8c0cdd2c41648d3f86d7d4706ae50e07dce5c49938b9f42f7449c

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