{"id":14066505,"url":"https://github.com/machow/dbpath","last_synced_at":"2025-04-11T06:55:56.452Z","repository":{"id":77165952,"uuid":"325376227","full_name":"machow/dbpath","owner":"machow","description":"R library for for connecting to databases via URL.","archived":false,"fork":false,"pushed_at":"2022-12-20T20:26:29.000Z","size":82,"stargazers_count":5,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T14:35:08.846Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/machow.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-29T20:01:26.000Z","updated_at":"2023-04-30T20:35:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"f3618148-2714-42f0-ad28-3495e090350e","html_url":"https://github.com/machow/dbpath","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/machow%2Fdbpath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/machow%2Fdbpath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/machow%2Fdbpath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/machow%2Fdbpath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/machow","download_url":"https://codeload.github.com/machow/dbpath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248358555,"owners_count":21090402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:05:08.183Z","updated_at":"2025-04-11T06:55:56.433Z","avatar_url":"https://github.com/machow.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# dbpath\n\n\n\u003c!-- badges: start --\u003e\n[![R build status](https://github.com/machow/dbpath/workflows/R-CMD-check/badge.svg)](https://github.com/machow/dbpath/actions)\n\u003c!-- badges: end --\u003e\n\ndbpath is an R library for creating database connections via a single string (url).\n\n## Install\n\n```{r eval = FALSE}\nremotes::install_github(\"machow/dbpath\")\n```\n\n## Examples\n\n```{r}\nlibrary(dbpath)\n\nsql_url \u003c- dbpath(\"postgresql+RPostgres://some_user:some_password@localhost:5432\")\nsql_url\n```\n\nYou can use the `dbpath` output with either DBI::dbConnect, or dplyr::tbl to create a remote connection.\n\n```{r eval = FALSE}\n# get a database connection\ncon \u003c- DBI::dbConnect(sql_url)\n\n# get a database table called mtcars\ntbl_mtcars \u003c- dplyr::tbl(sql_url, \"mtcars\")\ntbl_mtcars\n```\n\n## URL Format\n\n`dbpath` URLs follow the format below.\n\n```\n\u003cdialect\u003e+\u003cdriver\u003e://\u003cusername\u003e:\u003cpassword\u003e@\u003chost\u003e:\u003cport\u003e/\u003cdatabase\u003e\n```\n\nHere's an example using mysql:\n\n```{r}\nmysql_url \u003c- \"mysql+RMariaDB://root:some_password@localhost\"\n```\n\nIn this case, we're connecting to the mysql dialect, using R's MariaDB package as a driver.\n\nThe code below shows how it translates to making the connection manually.\n\n```{r eval = FALSE}\n# dbpath\nDBI::dbConnect(dbpath(mysql_url))\n\n# manual\nDBI::dbConnect(\n  RMariaDB::MariaDB(),\n  user = \"root\",\n  password = \"some_password\",\n  host = \"localhost\"\n  )\n```\n\nBehind the scenes, `dbpath` uses driver hooks to know that if RMariaDB is the driver, then we need its MariaDB() object.\nNote that the `RMariaDB` in `mysql+RMariaDB` is optional!\n\n## Interoperability with Python\n\n`dbpath's` approach is based on python's SQLAlchemy library.\nThis means that you can use the same string across languages!\n\n\u003ctable width=\"100%\"\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth\u003eR\u003c/th\u003e\n      \u003cth\u003epython\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctr\u003e\n    \u003c!-- shared code --\u003e\n\u003ctd colspan=2\u003e\n\n```{r}\n# one string to rule them all\nsql_url = \"postgresql://user:password@localhost:port/dbname\"\n\n```\n  \n\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003c!-- r example --\u003e\n\u003ctd\u003e\n  \n```{r eval = FALSE}\nlibrary(dbpath)\nDBI::dbconnection(dbpath(sql_url))\n```\n\n\u003c/td\u003e\n\n    \u003c!-- python example --\u003e\n\u003ctd\u003e\n\n```python\nimport sqlalchemy\nsqlalchemy.create_engine(sql_url)\n```\n\n\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n\n## Configuring Driver Selection\n\nThe code below adds a custom driver for SQLite.\n\n```{r}\nlibrary(dbpath)\n\ndriver_registry$set(my_driver = function () RSQLite::SQLite)\n\n# Note the 3 slashes, rather than two, meaning no user name, password, or host\nsqlite_url \u003c- dbpath(\"sqlite+my_driver:///:memory:\")\n\nsqlite_url\n\nDBI::dbConnect(sqlite_url)\n```\n\n### Available Driver Hooks\n\n\n```{r eval = FALSE}\n# see available drivers\ndriver_registry$get()\n\n# see defaults for when no driver is specified\ndriver_defaults$get()\n```\n\nHere are the current driver defaults:\n\n```\n postgresql       mysql     mariadb \n\"RPostgres\"  \"RMariaDB\"  \"RMariaDB\" \n```\n\n## Configuring Driver Connections\n\ndbpath uses an s3 method called `dbpath_params` to get a list of parameters to pass to `DBI::dbConnect` (or `dplyr::tbl`).\n\n```{r}\nurl \u003c- dbpath(\"postgresql://a_user:a_password@localhost/dbname\")\n\ndbpath_params(url)\n```\n\n```\n$drv\n\u003cPqDriver\u003e\n\n$user\n[1] \"a_user\"\n\n$password\n[1] \"a_password\"\n\n$host\n[1] \"localhost\"\n\n$port\n[1] \"\"\n\n$dbname\n[1] \"dbname\"\n```\n\nIn order to support a new driver type, you can register an s3 method for it.\nThe function should return a list of parameters, whose names are the arguments\nthat would be passed to DBI::dbConnect.\n\n```{r}\ndbpath_params.PqDriver \u003c- function(driver, url) {\n  list(\n    drv = driver,\n    user = url$user,\n    password = url$password,\n    host = url$host,\n    port = url$port,\n    \n    # use PqDriver specific argument: dbname\n    dbname = url$database\n  )\n}\n```\n\nYou can get a specific drivers parameters by passing it as the first argument to `dbpath_params`:\n\n```{r}\ndriver \u003c- RPostgres::Postgres()\nclass(driver)                        # \u003cPqDriver\u003e\n\ndbpath_params(driver, url)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmachow%2Fdbpath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmachow%2Fdbpath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmachow%2Fdbpath/lists"}