-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sqlx drivers, add connection struct
- Loading branch information
Showing
7 changed files
with
157 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
volumes: | ||
psql15: | ||
mysql8: | ||
mysql: | ||
# psql: | ||
|
||
services: | ||
psql15: | ||
image: postgres:15 | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: example | ||
POSTGRES_DB: noir | ||
volumes: | ||
- psql15:/var/lib/postgresql/data | ||
ports: | ||
- 5432:5432 | ||
mysql8: | ||
mysql: | ||
image: mysql:8.0.33 | ||
command: --default-authentication-plugin=mysql_native_password | ||
restart: always | ||
environment: | ||
MYSQL_ROOT_PASSWORD: noir | ||
MYSQL_DATABASE: noir | ||
MYSQL_ROOT_PASSWORD: example | ||
MYSQL_DATABASE: world_x | ||
ports: | ||
- 3306:3306 | ||
volumes: | ||
- mysql8:/var/lib/mysql | ||
- mysql:/var/lib/mysql | ||
- ./dev/:/docker-entrypoint-initdb.d | ||
# psql: | ||
# image: postgres:15 | ||
# environment: | ||
# POSTGRES_USER: postgres | ||
# POSTGRES_PASSWORD: example | ||
# POSTGRES_DB: noir | ||
# volumes: | ||
# - psql:/var/lib/postgresql/data | ||
# ports: | ||
# - 5432:5432 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,53 @@ | ||
use rusqlite::Connection; | ||
use tauri::{AppHandle, State, Manager}; | ||
use std::sync::Mutex; | ||
use tauri::{AppHandle, Manager, State}; | ||
|
||
use crate::database::connections::ConnectedConnection; | ||
|
||
pub struct AppState { | ||
pub db: std::sync::Mutex<Option<Connection>>, | ||
pub db: Mutex<Option<Connection>>, | ||
pub connections: Mutex<Vec<ConnectedConnection>>, | ||
} | ||
|
||
pub trait ServiceAccess { | ||
fn db<F, TResult>(&self, operation: F) -> TResult where F: FnOnce(&Connection) -> TResult; | ||
fn db<F, TResult>(&self, operation: F) -> TResult | ||
where | ||
F: FnOnce(&Connection) -> TResult; | ||
|
||
fn db_mut<F, TResult>(&self, operation: F) -> TResult | ||
where | ||
F: FnOnce(&mut Connection) -> TResult; | ||
|
||
fn db_mut<F, TResult>(&self, operation: F) -> TResult where F: FnOnce(&mut Connection) -> TResult; | ||
fn connections<F, TResult>(&self) -> Mutex<Vec<ConnectedConnection>>; | ||
} | ||
|
||
impl ServiceAccess for AppHandle { | ||
fn db<F, TResult>(&self, operation: F) -> TResult where F: FnOnce(&Connection) -> TResult { | ||
let app_state: State<AppState> = self.state(); | ||
let db_connection_guard = app_state.db.lock().unwrap(); | ||
let db = db_connection_guard.as_ref().unwrap(); | ||
|
||
operation(db) | ||
} | ||
|
||
fn db_mut<F, TResult>(&self, operation: F) -> TResult where F: FnOnce(&mut Connection) -> TResult { | ||
let app_state: State<AppState> = self.state(); | ||
let mut db_connection_guard = app_state.db.lock().unwrap(); | ||
let db = db_connection_guard.as_mut().unwrap(); | ||
|
||
operation(db) | ||
} | ||
fn db<F, TResult>(&self, operation: F) -> TResult | ||
where | ||
F: FnOnce(&Connection) -> TResult, | ||
{ | ||
let app_state: State<AppState> = self.state(); | ||
let db_connection_guard = app_state.db.lock().unwrap(); | ||
let db = db_connection_guard.as_ref().unwrap(); | ||
|
||
operation(db) | ||
} | ||
|
||
fn db_mut<F, TResult>(&self, operation: F) -> TResult | ||
where | ||
F: FnOnce(&mut Connection) -> TResult, | ||
{ | ||
let app_state: State<AppState> = self.state(); | ||
let mut db_connection_guard = app_state.db.lock().unwrap(); | ||
let db = db_connection_guard.as_mut().unwrap(); | ||
|
||
operation(db) | ||
} | ||
|
||
fn connections<F, TResult>(&self) -> Mutex<Vec<ConnectedConnection>> { | ||
let app_state: State<AppState> = self.state(); | ||
let connections_guard = app_state.connections.lock().unwrap(); | ||
|
||
connections_guard.clone().into() | ||
} | ||
} |