Skip to content

Commit

Permalink
Merge pull request #88 from surveydown-dev/Dev-Branch
Browse files Browse the repository at this point in the history
Dev branch
  • Loading branch information
Buneabt authored Sep 6, 2024
2 parents 16864af + f76e5d7 commit 9a4ecba
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
90 changes: 54 additions & 36 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,65 @@
#' This function establishes a connection pool to a Supabase database and sets
#' up automatic cleanup when the Shiny session ends.
#'
#' @param host Character string. The host address of the Supabase database.
#' @param dbname Character string. The name of the Supabase database.
#' @param port Integer. The port number for the Supabase database connection.
#' @param user Character string. The username for the Supabase database
#' connection.
#' @param table Character string. The name of the table to interact with in
#' the Supabase database.
#' @param password Character string. The password for the Supabase database
#' connection. NOTE: While you can provide a hard-coded password here, we do
#' NOT recommend doing so for security purposes. Instead, you should establish
#' a password with `surveydown::sd_set_password()`, which will create a local
#' .Renviron file that stores you password as a SURVEYDOWN_PASSWORD environment
#' variable. The `password` argument uses this as the default value, so if you
#' set a password properly with `surveydown::sd_set_password()`, then you can
#' safely ignore using the `password` argument here.
#' @param gssencmode Character string. The GSS encryption mode for the database
#' connection. Defaults to `"prefer"`. NOTE: If you have verified all
#' connection details are correct but still cannot access the database,
#' consider setting this to `"disable"`. This can be necessary if you're on a
#' secure connection, such as a VPN.
#' @param pause Logical. If TRUE, data will be saved to a local CSV file instead of the database. Defaults to FALSE.
#' @param min_size Integer. The minimum number of connections in the pool. Defaults to 1.
#' @param max_size Integer. The maximum number of connections in the pool. Defaults to Inf.
#' @param host Character string. The host address of the supabase database.
#' @param dbname Character string. The name of the supabase database.
#' @param port Integer. The port number for the supabase database connection.
#' @param user Character string. The username for the supabase database connection.
#' @param table Character string. The name of the table to interact with in the supabase database.
#' @param password Character string. The password for the supabase database connection.
#' Defaults to the value of the SURVEYDOWN_PASSWORD environment variable.
#' @param gssencmode Character string. The GSS encryption mode for the database connection. Defaults to "prefer".
#' @param ignore Logical. If TRUE, data will be saved to a local CSV file instead of the database. Defaults to FALSE.
#'
#' @return A list containing the database connection pool (`db`) and the table name (`table`),
#' or NULL if in pause mode or if there's an error.
#' @details The function checks for the presence of all required parameters and attempts to
#' establish a connection to the supabase database. If successful, it returns a list containing
#' the database connection object and the table name. The user must have created the specified
#' table in supabase beforehand. If ignore mode is enabled, the function returns NULL and data
#' will be saved to a local CSV file. The password is obtained from the SURVEYDOWN_PASSWORD
#' environment variable by default, but can be overridden by explicitly passing a value.
#'
#' @return A list containing the database connection object (`db`) and the table name (`table`),
#' or NULL if in ignore mode.
#'
#' @note The user must create their own table inside supabase in order to make additions.
#'
#' @examples
#' \dontrun{
#' # Assuming SURVEYDOWN_PASSWORD is set in .Renviron
#' db_connection <- sd_database(
#' host = "aws-0-us-west-1.pooler.supabase.com",
#' dbname = "postgres",
#' port = "6---",
#' user = "postgres.k----------i",
#' table = "your-table-name",
#' ignore = FALSE
#' )
#'
#' # Explicitly providing the password
#' db_connection <- sd_database(
#' host = "aws-0-us-west-1.pooler.supabase.com",
#' dbname = "postgres",
#' port = "6---",
#' user = "postgres.k----------i",
#' table = "your-table-name",
#' password = "your-password",
#' ignore = FALSE
#' )
#' }
#'
#' @export
sd_database <- function(
host = NULL,
dbname = NULL,
port = NULL,
user = NULL,
table = NULL,
password = Sys.getenv("SURVEYDOWN_PASSWORD"),
gssencmode = "prefer",
pause = FALSE,
min_size = 1,
max_size = Inf
host = NULL,
dbname = NULL,
port = NULL,
user = NULL,
table = NULL,
password = Sys.getenv("SURVEYDOWN_PASSWORD"),
gssencmode = "prefer",
ignore = TRUE
) {
if (pause) {

if (!ignore) {
message("Database connection paused. Saving data to local CSV file.")
return(NULL)
}
Expand Down
22 changes: 21 additions & 1 deletion R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ sd_server <- function(input, output, session, config, db = NULL) {
time_start <- get_utc_timestamp()
session_id <- session$token

# Check if db is NULL (either left blank or specified with pause = TRUE)
# Check if db is NULL (either left blank or specified with ignore = TRUE)
pause_mode <- is.null(db)

# Create local objects from config file
Expand Down Expand Up @@ -568,6 +568,26 @@ admin_enable <- function(input, output, session, db) {
return_to_survey()
})

#Pause Survey - Pause DB Section

observeEvent(input$pause_survey, {
#Code here that write to the table to change row value from 0 -> 1 and back if it happens
data <- DBI::dbReadTable(db$db, paste0(db$table, "_admin_table"))
#Read table value in, change it from true to false


#Add in Sd_server if(survey_paused == TRUE)
#Create and display a blank page that says the survey is pause


})








# Download Data button functionality
output$download_data <- downloadHandler(
Expand Down

0 comments on commit 9a4ecba

Please sign in to comment.