Package: xtdb
XTDB Python: A Python ORM forXTDBCheck outthe documentationfor a more complete documentation of the package.InstallationYou can install this project using pip:$pipinstallxtdbUsageThe following examples assume you have set theXTDB_URIvariable in your environment.
To start experimenting, you could use the following setup using Docker:$dockerrun-p3000:3000-djuxt/xtdb-standalone-rocksdb:1.21.0
$exportXTDB_URI=http://localhost:3000/_xtdbUsing the ClientTheXTDBClientsupports the fullHTTP API spec.>>>importos>>>fromxtdb.sessionimportXTDBClient,Operation>>>>>>client=XTDBClient(os.environ["XTDB_URI"])>>>client.submit_tx([Operation.put({"xt/id":"123","name":"fred"})])>>>>>>client.query('{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}')[[{'name':'fred','xt/id':'123'}]]>>>>>>client.get_entity("123"){'name':'fred','xt/id':'123'}Take a look at the spec to see the full range of functionality that maps directly to the client.Using the Datalog moduleThedatalogmodule also provides a layer to construct queries with more easily.
Given the data fromthe cities examplehas been seeded:>>>fromxtdb.datalogimportFind,Where>>>>>>query=Find("(pull Country [*])")&Find("City")&(Where("City","City/country","Country")&Where("City","City/name",'"Rome"'))>>>str(query){:query{:find[(pullCountry[*])City]:where[[City:City/countryCountry][City:City/name"Rome"]]}}>>>>>>client.query(query)[[{'type':'Country','Country/name':'Italy','xt/id':'c095839f-031f-46ad-85e1-097f634ba4f0'},'33aa7fa6-b752-4982-a772-d2dbaeda58ae']]To see more datalog query examples, check out theunit tests.Using the ORM and SessionBelow is an example of how to use the ORM functionality.importosfromdataclassesimportdataclassfromxtdb.ormimportBasefromxtdb.queryimportQueryfromxtdb.sessionimportXTDBSession@dataclassclassTestEntity(Base):name:str@dataclassclassSecondEntity(Base):age:inttest_entity:TestEntitysession=XTDBSession(os.environ["XTDB_URI"])entity=TestEntity(name="test")withsession:session.put(entity)query=Query(TestEntity).where(TestEntity,name="test")result=session.query(query)result[0].dict()#  {"TestEntity/name": "test", "type": "TestEntity", "xt/id": "fe2a3ee0-9254-41dc-91cc-74ad9e2a16db"}To see more examples, check out theexamples directory.
Don't hesitate to add your own examples!Using the CLI for queryingThis package also comes with an easy CLI tool to query XTDB.
Again, set theXTDB_URIvariable in your environment first and you will be able to query XTDB as follows:$echo'{:query {:find [(pull ?e [*])] :where [[ ?e :name "fred" ]]}}'|python-mxtdb[[{"name":"fred","xt/id":"123"}]]To use a query from a file, run:$catquery.txt{:query{:find[(pull?e[*])]:where[[?e:name"fred"]]}}$
$python-mxtdb<query.txt[[{"name":"fred","xt/id":"123"}]]ContributingInstallationTo get started, clone the repo and create an environment using Poetry$gitclonehttps://github.com/Donnype/xtdb-py.git
$cdxtdb-py
$poetryinstallNow set up XTDB, for instance using Docker$dockerrun-p3000:3000-djuxt/xtdb-standalone-rocksdb:1.21.0Export the XTDB_URI environment variable to be able to useos.environ["XTDB_URI"]to fetch the endpoint$exportXTDB_URI=http://localhost:3000/_xtdbDevelopmentTheMakefilehas several targets that should make development easier:$makeutest# Run unit tests$makeitest# Run integration tests$makecheck# Run all linters$makedone# Run all of the aboveThe CI runs these checks as well.
Check out theproject pagefor issues and features to work on.