This R Package provides a standard interface for performing data operations on various data stores. Currently, a memory and odbc driver-based Storage is supported. To configure an odbc driver-based data store, an ODBC.Configurator is available to retrieve the required configuration parameters from a local .Renviron file. Lastly, this package includes an example service in which a Storage instance is injected into to enable persistence.
Below is an overview of the different functions available on each of the components:
Two utility functions are provided:
open.config.file
Opens the .Renviron file in your IDE. This function only works when using either VS Code or RStudio.
get.config
Retrieves the configuration parameters from your local .Renviron file. By default a preset configuration is assumed when an OS DSN has been set up. A preset type configuration assumes a DSN, UID and PWD variables have been defined in the .Renviron file. If no OS DSN has been set up, get.config can be invoked with type = 'Manual' flag. When retrieving configuration parameters using the type = 'Manual' flag it is assumed DRIVER, SERVER, DATABASE, UID, PWD variables have been defined in the .Renviron file.
Six functions are provided, five of which are common data operations and one function to execute custom queries:
addretrieveretrieve.where.idmodifyremoveCustom data operations can be added using the execute.query function. The execute.query function takes a SQL query, passed in as a character data type, and returns a data frame.
Lastly, the retrieve.where.id and modify operations assumes the data model has an id attribute. The id attribute is used to uniquely identify a record in the data store.
Five functions are provided:
addretrieveretrieve.where.idmodifyremoveThe Todo.Service is an example of how to use the Storage component. The Todo.Service is a simple data service that performs CRUD operations on a Todo data model.
Note: This package is a Standard compliant package.
At the time of writing this README, the package is not available on CRAN. However, it can be installed using devtools.
Validate, Environment, Query:devtools::install_github("https://github.com/FlippieCoetser/Validate")
devtools::install_github("https://github.com/FlippieCoetser/Environment")
devtools::install_github("https://github.com/FlippieCoetser/Query")
devtools::install_github("https://github.com/FlippieCoetser/Storage")
git clone https://github.com/FlippieCoetser/Storage.git
.tar.gz filedevtools::build()
install.packages("path_to_file/tar_gz_file", repos = NULL, type = "source")
There are two ways to load the package:
Three components are loaded into the global namespace when using the library function to load the package.
library(Storage)
When using the package namespace, the three components can be accessed as follows:
Storage::ODBC.Configurator()
Storage::Storage()
Storage::Todo.Service()
Using the Todo.Service component, two use cases will be demonstrated, one using a memory data store and the other using an odbc data store.
configuration
configuration <- list()
Storage component with type = 'memory'.storage <- configuration |> Storage::Storage(type = 'memory')
Note: Two types of configuration sets are support for
odbcdata stores:PresetandManual. WherePresetrequire additional operating system configuration
ODBC.Configurator
odbc.configurator <- Storage::ODBC.Configurator()
.Renviron fileconfiguration <- odbc.configurator[['get.config']]()
Storage component.storage <- configuration |> Storage::Storage()
memory based store, seed the store with mock datatable <- 'Todo'
todos <- data.frame(
id = c('7ab3df6f-2e8f-44b4-87bf-3004cf1c16ae',
'7bfef861-6fe9-46da-9ad2-6a58779ccdcd',
'd3b59bf0-14f0-4444-9ec9-1913e7256ee4'),
task = c('Task.1','Task.2','Task.3'),
status = c('New','New','Done')
)
todos |> storage[['seed.table']](table)
storage instance based on your desired data store and create a new Todo.Service instancetodo.service <- storage |> Todo.Broker() |> Todo.Service()
todo
todo <- 'Task' |> Todo()
todo into the data storetodo |> todo.service[['add']]()
todos in the data storetodos <- todo.service[['retrieve']]()
todo created in a previous step and extract its id
id <- todo[['id']]
todo with the extracted id
todo <- id |> todo.service[['retrieve.where.id']]()
todo retrieved in the previous step and update its status
todo[['status']] <- "Done"
todo in the data storetodo |> todo.service[['modify']]()
todo retrieved in the previous step and extract its id
id <- todo[['id']]
todo with the extracted id
id |> todo.service[['remove']]()