pub struct Database {
pub base: StarterDatabase,
pub auth: Database,
pub config: ServerOptions,
}
Expand description
Database connector
Fields§
§base: StarterDatabase
§auth: Database
§config: ServerOptions
Implementations§
Source§impl Database
impl Database
Sourcepub async fn new(
database_options: DatabaseOpts,
server_options: ServerOptions,
) -> Self
pub async fn new( database_options: DatabaseOpts, server_options: ServerOptions, ) -> Self
Create a new Database
Examples found in repository?
examples/basic.rs (line 23)
10async fn main() {
11 dotenv::dotenv().ok(); // load .env
12
13 let port: u16 = match var("PORT") {
14 Ok(v) => v.parse::<u16>().unwrap(),
15 Err(_) => 8080,
16 };
17
18 set_var("PO_STATIC_DIR", "/static");
19 set_var("PO_NESTED", "@pongo");
20 set_var("PO_STARSTRAW", "/star");
21
22 // create database
23 let database = Database::new(Database::env_options(), ServerOptions::truthy()).await;
24 database.init().await;
25
26 let starstraw_database =
27 SRDatabase::new(Database::env_options(), SRServerOptions::truthy()).await;
28 starstraw_database.init().await;
29
30 // create app
31 let app = Router::new()
32 .nest("/star", starstraw::routes(starstraw_database.clone()))
33 .nest("/@pongo", pongo::dashboard::routes(database.clone()))
34 .nest_service("/static", get_service(ServeDir::new("./static")))
35 .fallback(pongo::api::not_found);
36
37 let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}"))
38 .await
39 .unwrap();
40
41 println!("Starting server at http://localhost:{port}!");
42 axum::serve(listener, app).await.unwrap();
43}
Sourcepub fn env_options() -> DatabaseOpts
pub fn env_options() -> DatabaseOpts
Pull dorsal::DatabaseOpts
from env
Examples found in repository?
examples/basic.rs (line 23)
10async fn main() {
11 dotenv::dotenv().ok(); // load .env
12
13 let port: u16 = match var("PORT") {
14 Ok(v) => v.parse::<u16>().unwrap(),
15 Err(_) => 8080,
16 };
17
18 set_var("PO_STATIC_DIR", "/static");
19 set_var("PO_NESTED", "@pongo");
20 set_var("PO_STARSTRAW", "/star");
21
22 // create database
23 let database = Database::new(Database::env_options(), ServerOptions::truthy()).await;
24 database.init().await;
25
26 let starstraw_database =
27 SRDatabase::new(Database::env_options(), SRServerOptions::truthy()).await;
28 starstraw_database.init().await;
29
30 // create app
31 let app = Router::new()
32 .nest("/star", starstraw::routes(starstraw_database.clone()))
33 .nest("/@pongo", pongo::dashboard::routes(database.clone()))
34 .nest_service("/static", get_service(ServeDir::new("./static")))
35 .fallback(pongo::api::not_found);
36
37 let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}"))
38 .await
39 .unwrap();
40
41 println!("Starting server at http://localhost:{port}!");
42 axum::serve(listener, app).await.unwrap();
43}
Sourcepub async fn init(&self)
pub async fn init(&self)
Init database
Examples found in repository?
examples/basic.rs (line 24)
10async fn main() {
11 dotenv::dotenv().ok(); // load .env
12
13 let port: u16 = match var("PORT") {
14 Ok(v) => v.parse::<u16>().unwrap(),
15 Err(_) => 8080,
16 };
17
18 set_var("PO_STATIC_DIR", "/static");
19 set_var("PO_NESTED", "@pongo");
20 set_var("PO_STARSTRAW", "/star");
21
22 // create database
23 let database = Database::new(Database::env_options(), ServerOptions::truthy()).await;
24 database.init().await;
25
26 let starstraw_database =
27 SRDatabase::new(Database::env_options(), SRServerOptions::truthy()).await;
28 starstraw_database.init().await;
29
30 // create app
31 let app = Router::new()
32 .nest("/star", starstraw::routes(starstraw_database.clone()))
33 .nest("/@pongo", pongo::dashboard::routes(database.clone()))
34 .nest_service("/static", get_service(ServeDir::new("./static")))
35 .fallback(pongo::api::not_found);
36
37 let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}"))
38 .await
39 .unwrap();
40
41 println!("Starting server at http://localhost:{port}!");
42 axum::serve(listener, app).await.unwrap();
43}
Sourcepub async fn sql_fetch_all(
&self,
query: String,
) -> Result<Vec<HashMap<String, String>>>
pub async fn sql_fetch_all( &self, query: String, ) -> Result<Vec<HashMap<String, String>>>
Fetch all results from the database for the given query
Sourcepub async fn sql_execute(&self, query: String) -> Result<()>
pub async fn sql_execute(&self, query: String) -> Result<()>
Execute the given query
and return nothing
Sourcepub async fn pull<T: Serialize + DeserializeOwned + From<String>, M: Serialize + DeserializeOwned>(
&self,
id: String,
namespace: String,
) -> Result<Document<T, M>>
pub async fn pull<T: Serialize + DeserializeOwned + From<String>, M: Serialize + DeserializeOwned>( &self, id: String, namespace: String, ) -> Result<Document<T, M>>
Sourcepub async fn push<T: ToString, M: Serialize>(
&self,
props: DocumentCreate<T, M>,
) -> Result<Document<T, M>>
pub async fn push<T: ToString, M: Serialize>( &self, props: DocumentCreate<T, M>, ) -> Result<Document<T, M>>
Create a a new document
Making sure values are unique should be done before calling push
.
§Arguments:
props
-DocumentCreate
§Returns:
- Full
Document
Sourcepub async fn drop<T: Serialize + DeserializeOwned + From<String>, M: Serialize + DeserializeOwned>(
&self,
id: String,
namespace: String,
) -> Result<()>
pub async fn drop<T: Serialize + DeserializeOwned + From<String>, M: Serialize + DeserializeOwned>( &self, id: String, namespace: String, ) -> Result<()>
Delete an existing document by id
Permission checks should be done before calling drop
.
§Arguments:
id
- the document to deletenamespace
- the namespace the document belongs to
Sourcepub async fn update<T: Serialize + DeserializeOwned + From<String> + ToString, M: Serialize + DeserializeOwned>(
&self,
id: String,
namespace: String,
new_content: String,
) -> Result<()>
pub async fn update<T: Serialize + DeserializeOwned + From<String> + ToString, M: Serialize + DeserializeOwned>( &self, id: String, namespace: String, new_content: String, ) -> Result<()>
Edit an existing document by id
Permission checks should be done before calling update
.
§Arguments:
id
- the document to editnamespace
- the namespace the document belongs tonew_content
- the new content of the paste
Sourcepub async fn update_metadata<T: Serialize + DeserializeOwned + From<String> + ToString, M: Serialize + DeserializeOwned>(
&self,
id: String,
namespace: String,
metadata: M,
) -> Result<()>
pub async fn update_metadata<T: Serialize + DeserializeOwned + From<String> + ToString, M: Serialize + DeserializeOwned>( &self, id: String, namespace: String, metadata: M, ) -> Result<()>
Edit an existing paste’s metadata by url
Permission checks should be done before calling update
.
§Arguments:
id
- the document to editnamespace
- the namespace the document belongs tometadata
- the new metadata of the document
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl !UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more