Package: unofficial-surreal-database
SurrealDB Python client library.An unofficial client library for SurrealDB usinghttpx.Installationpipinstallunofficial-surreal-databaseAPIThis library includes aSurrealDBclass that can be used to interact with the SurrealDB server.SurrealDBTheSurrealDBclass is the main class for interacting with the SurrealDB server. Additionally,AsyncSurrealDB, uses the same API asSurrealDB, but useshttpx.AsyncClientinstead ofhttpx.Client. This is useful for asynchronous applications.Both classes can be instantiated with the following arguments:username(str): The username to use when connecting to the server.password(str): The password to use when connecting to the server.namespace(str): The namespace to query.database(str): The database to query.url(str): The URL to connect to. Defaults tohttp://localhost:8000/sql(the default port for SurrealDB).TheSurrealDBclass can be used as a context manager, which will automatically close thehttpx.Clientconnection when the context is exited.TheSurrealDBclass has the following methods:SurrealDB.signinSigns in to the SurrealDB server. This method can be used to sign in to the server if theSurrealDBinstance was instantiated without a username and password.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin(username="root",password="root")SurrealDB.signupSame asSurrealDB.signin.SurrealDB.useSets the namespace and database to use for queries.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.use(namespace="my_namespace",database="my_database")SurrealDB.queryQueries the SurrealDB server.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin(username="root",password="root")db.use(namespace="my_namespace",database="my_database")result=db.query("SELECT * FROM users")>>>result[{"id":1,"name":"John Doe",},{"id":2,"name":"Jane Doe",}]SurrealDB.selectWrapper onSurrealDB.querythat allows you to select a table, or record from a table.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin("root","root")db.use("test","test")result=db.select("users")>>>result[{"id":1,"name":"John Doe",},{"id":2,"name":"Jane Doe",}]result=db.select("users:2")>>>result[{"id":2,"name":"Jane Doe",}]SurrealDB.createCreate a record in the database.Takes keyword arguments for the record to create, and a first parameter as the record, or record and identifier.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin("root","root")db.use("test","test")result=db.create("users:1",name="John Doe")>>>result[{"id":1,"name":"John Doe",}]SurrealDB.changeChange a record in the database.Takes keyword arguments for the record to change, and a first parameter as the record and identifier.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin("root","root")db.use("test","test")result=db.change("users:1",age=42)>>>result[{"id":1,"name":"John Doe","age":42,}]SurrealDB.deleteDelete a record in the database.Takes a first parameter as the record and identifier, with an optionalwhereparameter, to delete all items that match the where clause.fromsurrealdbimportSurrealDBwithSurrealDB()asdb:db.signin("root","root")db.use("test","test")result=db.delete("users",where="age > 40")>>>result[]SurrealDB.closeManually close thehttpx.Clientconnection. This is done for you when using theSurrealDBclass as a context manager.ReferenceTheReferenceclass is used to represent a reference to a record in the database. It can be instantiated with the following arguments:table(str): The table the record exists in.record_id(str): The record identifier.TheReferenceclass has no methods for ease of use.fromsurrealdbimportReference,SurrealDBwithSurrealDB()asdb:db.signin("root","root")db.use("test","test")db.create("category:work",name="Work")db.create("note:1",title="Meeting",category=Reference("category","work"),)result=db.query("""SELECT *FROMnoteWHEREcategory = category:workFETCHcategory;""")>>>result[{"category":{"id":"category:work","name":"Work"},"id":"note:1","title":"Meeting"}]