2022-12-30 08:23:19 +00:00
|
|
|
//! This library provides a low-level database library implementation, a remote client
|
|
|
|
//! and a query language definition, for [SurrealDB](https://surrealdb.com), the ultimate cloud database for
|
2022-08-28 12:18:12 +00:00
|
|
|
//! tomorrow's applications. SurrealDB is a scalable, distributed, collaborative, document-graph
|
|
|
|
//! database for the realtime web.
|
2022-05-03 23:38:16 +00:00
|
|
|
//!
|
2022-08-28 12:18:12 +00:00
|
|
|
//! This library can be used to start an embedded in-memory datastore, an embedded datastore
|
|
|
|
//! persisted to disk, a browser-based embedded datastore backed by IndexedDB, or for connecting
|
|
|
|
//! to a distributed [TiKV](https://tikv.org) key-value store.
|
2022-12-30 08:23:19 +00:00
|
|
|
//!
|
|
|
|
//! It also enables simple and advanced querying of a remote SurrealDB server from
|
|
|
|
//! server-side or client-side code. All connections to SurrealDB are made over WebSockets by default,
|
|
|
|
//! and automatically reconnect when the connection is terminated.
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2024-03-20 17:11:55 +00:00
|
|
|
//! use std::borrow::Cow;
|
2022-12-30 08:23:19 +00:00
|
|
|
//! use serde::{Serialize, Deserialize};
|
|
|
|
//! use serde_json::json;
|
2024-03-20 17:11:55 +00:00
|
|
|
//! use surrealdb::{Error, Surreal};
|
2022-12-30 08:23:19 +00:00
|
|
|
//! use surrealdb::opt::auth::Root;
|
2023-01-07 08:32:18 +00:00
|
|
|
//! use surrealdb::engine::remote::ws::Ws;
|
2022-12-30 08:23:19 +00:00
|
|
|
//!
|
|
|
|
//! #[derive(Serialize, Deserialize)]
|
|
|
|
//! struct Person {
|
2024-03-20 17:11:55 +00:00
|
|
|
//! title: String,
|
2022-12-30 08:23:19 +00:00
|
|
|
//! name: Name,
|
|
|
|
//! marketing: bool,
|
|
|
|
//! }
|
|
|
|
//!
|
2024-03-20 17:11:55 +00:00
|
|
|
//! // Pro tip: Replace String with Cow<'static, str> to
|
|
|
|
//! // avoid unnecessary heap allocations when inserting
|
|
|
|
//!
|
|
|
|
//! #[derive(Serialize, Deserialize)]
|
|
|
|
//! struct Name {
|
|
|
|
//! first: Cow<'static, str>,
|
|
|
|
//! last: Cow<'static, str>,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // Install at https://surrealdb.com/install
|
|
|
|
//! // and use `surreal start --user root --pass root`
|
|
|
|
//! // to start a working database to take the following queries
|
|
|
|
|
|
|
|
//! // See the results via `surreal sql --ns namespace --db database --pretty`
|
|
|
|
//! // or https://surrealist.app/
|
|
|
|
//! // followed by the query `SELECT * FROM person;`
|
|
|
|
|
2022-12-30 08:23:19 +00:00
|
|
|
//! #[tokio::main]
|
2024-03-20 17:11:55 +00:00
|
|
|
//! async fn main() -> Result<(), Error> {
|
2022-12-30 08:23:19 +00:00
|
|
|
//! let db = Surreal::new::<Ws>("localhost:8000").await?;
|
|
|
|
//!
|
|
|
|
//! // Signin as a namespace, database, or root user
|
|
|
|
//! db.signin(Root {
|
|
|
|
//! username: "root",
|
|
|
|
//! password: "root",
|
|
|
|
//! }).await?;
|
|
|
|
//!
|
|
|
|
//! // Select a specific namespace / database
|
|
|
|
//! db.use_ns("namespace").use_db("database").await?;
|
|
|
|
//!
|
|
|
|
//! // Create a new person with a random ID
|
2023-04-28 11:20:57 +00:00
|
|
|
//! let created: Vec<Person> = db.create("person")
|
2022-12-30 08:23:19 +00:00
|
|
|
//! .content(Person {
|
|
|
|
//! title: "Founder & CEO".into(),
|
|
|
|
//! name: Name {
|
|
|
|
//! first: "Tobie".into(),
|
|
|
|
//! last: "Morgan Hitchcock".into(),
|
|
|
|
//! },
|
|
|
|
//! marketing: true,
|
|
|
|
//! })
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! // Create a new person with a specific ID
|
2023-04-28 11:20:57 +00:00
|
|
|
//! let created: Option<Person> = db.create(("person", "jaime"))
|
2022-12-30 08:23:19 +00:00
|
|
|
//! .content(Person {
|
|
|
|
//! title: "Founder & COO".into(),
|
|
|
|
//! name: Name {
|
|
|
|
//! first: "Jaime".into(),
|
|
|
|
//! last: "Morgan Hitchcock".into(),
|
|
|
|
//! },
|
|
|
|
//! marketing: false,
|
|
|
|
//! })
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! // Update a person record with a specific ID
|
2023-04-28 11:20:57 +00:00
|
|
|
//! let updated: Option<Person> = db.update(("person", "jaime"))
|
2022-12-30 08:23:19 +00:00
|
|
|
//! .merge(json!({"marketing": true}))
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! // Select all people records
|
|
|
|
//! let people: Vec<Person> = db.select("person").await?;
|
|
|
|
//!
|
|
|
|
//! // Perform a custom advanced query
|
2024-03-20 17:11:55 +00:00
|
|
|
//! let query = r#"
|
2022-12-30 08:23:19 +00:00
|
|
|
//! SELECT marketing, count()
|
|
|
|
//! FROM type::table($table)
|
|
|
|
//! GROUP BY marketing
|
2023-02-09 10:28:06 +00:00
|
|
|
//! "#;
|
2022-12-30 08:23:19 +00:00
|
|
|
//!
|
2024-03-20 17:11:55 +00:00
|
|
|
//! let groups = db.query(query)
|
2022-12-30 08:23:19 +00:00
|
|
|
//! .bind(("table", "person"))
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//! ```
|
2022-05-03 23:38:16 +00:00
|
|
|
|
2022-12-07 19:30:29 +00:00
|
|
|
#![doc(html_favicon_url = "https://surrealdb.s3.amazonaws.com/favicon.png")]
|
|
|
|
#![doc(html_logo_url = "https://surrealdb.s3.amazonaws.com/icon.png")]
|
2022-12-30 08:23:19 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2024-02-02 22:10:47 +00:00
|
|
|
#![cfg_attr(test, deny(warnings))]
|
2022-12-07 19:30:29 +00:00
|
|
|
|
2024-04-03 09:54:12 +00:00
|
|
|
#[cfg(all(target_arch = "wasm32", feature = "ml"))]
|
|
|
|
compile_error!("The `ml` feature is not supported on the `wasm32` architecture.");
|
|
|
|
|
2022-02-22 14:16:50 +00:00
|
|
|
#[macro_use]
|
2024-02-02 22:10:47 +00:00
|
|
|
extern crate tracing;
|
2022-02-22 14:16:50 +00:00
|
|
|
|
2023-11-28 14:53:40 +00:00
|
|
|
#[doc(hidden)]
|
2024-04-03 09:54:12 +00:00
|
|
|
pub use surrealdb_core::*;
|
2024-02-02 22:10:47 +00:00
|
|
|
|
2024-08-21 12:34:16 +00:00
|
|
|
pub use uuid::Uuid;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod mac;
|
|
|
|
mod api;
|
2022-02-22 14:16:50 +00:00
|
|
|
|
2022-12-30 08:23:19 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// Channels for receiving a SurrealQL database export
|
2022-05-15 19:38:46 +00:00
|
|
|
pub mod channel {
|
2023-11-20 19:08:07 +00:00
|
|
|
pub use channel::bounded;
|
|
|
|
pub use channel::unbounded;
|
2022-05-15 19:38:46 +00:00
|
|
|
pub use channel::Receiver;
|
|
|
|
pub use channel::Sender;
|
|
|
|
}
|
2022-12-07 19:30:29 +00:00
|
|
|
|
2022-12-30 08:23:19 +00:00
|
|
|
/// Different error types for embedded and remote databases
|
|
|
|
pub mod error {
|
|
|
|
pub use crate::api::err::Error as Api;
|
2024-08-21 12:34:16 +00:00
|
|
|
pub use surrealdb_core::err::Error as Db;
|
2023-11-13 17:19:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-21 12:34:16 +00:00
|
|
|
#[cfg(feature = "protocol-http")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use api::headers;
|
2023-11-13 17:19:47 +00:00
|
|
|
|
2024-08-21 12:34:16 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use api::{
|
|
|
|
engine, method, opt,
|
|
|
|
value::{
|
|
|
|
self, Action, Bytes, Datetime, Notification, Number, Object, RecordId, RecordIdKey, Value,
|
|
|
|
},
|
|
|
|
Connect, Connection, Response, Result, Surreal,
|
|
|
|
};
|
2023-11-13 17:19:47 +00:00
|
|
|
|
2022-12-30 08:23:19 +00:00
|
|
|
/// An error originating from the SurrealDB client library
|
2023-05-22 20:02:48 +00:00
|
|
|
#[derive(Debug, thiserror::Error, serde::Serialize)]
|
2022-12-30 08:23:19 +00:00
|
|
|
pub enum Error {
|
|
|
|
/// An error with an embedded storage engine
|
2023-01-08 14:51:36 +00:00
|
|
|
#[error("{0}")]
|
2022-12-30 08:23:19 +00:00
|
|
|
Db(#[from] crate::error::Db),
|
|
|
|
/// An error with a remote database instance
|
2023-01-08 14:51:36 +00:00
|
|
|
#[error("{0}")]
|
2022-12-30 08:23:19 +00:00
|
|
|
Api(#[from] crate::error::Api),
|
|
|
|
}
|