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 hashes)

Uploaded Source

Built Distribution

xtdb-0.3.0-py3-none-any.whl (14.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page