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 aDSN
,UID
andPWD
variables have been defined in the.Renviron
file. If no OS DSN has been set up,get.config
can be invoked withtype = 'Manual'
flag. When retrieving configuration parameters using thetype = 'Manual'
flag it is assumedDRIVER
,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:
add
retrieve
retrieve.where.id
modify
remove
Custom 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:
add
retrieve
retrieve.where.id
modify
remove
The 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
.
- This package has two dependencies:
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")
- Use Devtools
devtools::install_github("https://github.com/FlippieCoetser/Storage")
- Clone the repository
git clone https://github.com/FlippieCoetser/Storage.git
- Build and Generate
.tar.gz
file
devtools::build()
- Install the package
install.packages("path_to_file/tar_gz_file", repos = NULL, type = "source")
There are two ways to load the package:
- Use the library function to load all available components into the global namespace
- Create a new instance of a component using the package namespace
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.
- Create an empty
configuration
configuration <- list()
- Create a new instance of the
Storage
component withtype = 'memory'
.
storage <- configuration |> Storage::Storage(type = 'memory')
- Create a new instance of
ODBC.Configurator
odbc.configurator <- Storage::ODBC.Configurator()
- retrieve the configuration parameters from the local
.Renviron
file
configuration <- odbc.configurator[['get.config']]()
- Create a new instance of
Storage
component.
storage <- configuration |> Storage::Storage()
- When using a storage with a
memory
based store, seed the store with mock data
table <- '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)
- select a
storage
instance based on your desired data store and create a newTodo.Service
instance
todo.service <- storage |> Todo.Broker() |> Todo.Service()
- Create a new
todo
todo <- 'Task' |> Todo()
- Add the
todo
into the data store
todo |> todo.service[['add']]()
- retrieve all
todos
in the data store
todos <- todo.service[['retrieve']]()
- Use the
todo
created in a previous step and extract itsid
id <- todo[['id']]
- retrieve the
todo
with the extractedid
todo <- id |> todo.service[['retrieve.where.id']]()
- Use the
todo
retrieved in the previous step and update itsstatus
todo[['status']] <- "Done"
- modify the
todo
in the data store
todo |> todo.service[['modify']]()
- Use the
todo
retrieved in the previous step and extract itsid
id <- todo[['id']]
- remove the
todo
with the extractedid
id |> todo.service[['remove']]()