Skip to contents

Opens (or creates) a per-user SQL cache database. This is the opt-in SQL backend and requires DuckDB or RSQLite to be installed.

Usage

morie_db_connect(db_path = NULL)

Arguments

db_path

Optional path to a DuckDB (*.duckdb) or SQLite (*.db) file. Defaults to the MORIE_CACHE_DB env var, else morie.duckdb / morie.db in the per-user cache directory.

Value

A DBI connection object.

Details

Note: the DEFAULT cache used by morie_cache_store() / _load() / _list() needs no SQL backend at all — it uses a zero/light dependency file store: Parquet via nanoparquet (cross-language) when available, else base-R .rds. Install duckdb (or RSQLite), or set MORIE_CACHE_BACKEND=duckdb/sqlite, or pass db_path=, to use SQL instead — DuckDB is preferred (vectorised + columnar, handles multi-GB PUMFs and out-of-core analytical queries); an existing morie.db / morie.duckdb cache is reused for back-compat. For the multi-user server tier, pass your own PostgreSQL con=.

For non-default backends (PostgreSQL, MariaDB, MS SQL Server, ...), construct your own DBI connection and pass it as con to the morie_cache_* and morie_load_dataset functions:


con <- DBI::dbConnect(RPostgres::Postgres(),
  host = "...", dbname = "morie", user = "...", password = "...")
morie_load_dataset("ocp21", con = con)

Examples

# \donttest{
# DuckDB (default when 'duckdb' is installed); pass a '.db' path for SQLite.
if (requireNamespace("duckdb", quietly = TRUE) &&
  requireNamespace("DBI", quietly = TRUE)) {
  tmp <- tempfile(fileext = ".duckdb")
  con <- morie_db_connect(db_path = tmp)
  DBI::dbListTables(con)
  DBI::dbDisconnect(con)
  file.remove(tmp)
}
#> duckdb is keeping downloaded extensions in a temporary directory:
#>  /tmp/RtmpOKyZlD/duckdb/extensions
#> This is removed when the R session ends, so extensions are re-downloaded each session.
#>  To keep them, point `options(duckdb.extension_directory =)` or the `DUCKDB_EXTENSION_DIRECTORY` environment variable at a permanent path.
#> [1] TRUE
# }