ropensci / nodbi

Document DBI connector for R
https://docs.ropensci.org/nodbi
Other
76 stars 6 forks source link

Function to run raw commands #43

Closed GitHunter0 closed 2 years ago

GitHunter0 commented 2 years ago

Hi folks, first let me say nodbi is a really great project that I hope keeps flourishing.

Is there a way to run raw database commands (for operations that nodbi currently does not implement), analogous to mongolite's run()?

I have also 2 aside questions if I may: (1) How can I disconnect from the database? I could not find in nodbi a command like mongolite's $disconnect() (2) Since in commands like docdb_query(key = "...") we cannot change the key (we would receive a warning message), why make mandatory to provide it every time? In mongolite, for example, $find() just requires the query paramater.

Thank you

rfhb commented 2 years ago

Hi and many thanks, sorry, could not reply earlier.

Are you looking to run a few specific commands across all backends? Which commands are you thinking of? If the command would not have analogous or corresponding commands across the backends, it may be easier to just call the backend-specific function, for example:

dbc <- nodbi::src_mongo(
    db = "my_db", 
    collection = "my_coll")

mongolite::mongo(
  collection = dbc$collection,
  db = dbc$db,
  url = dbc$url)$run(command = '{"ping": 1}')

In analogy, the other backends can be used with a nodbi connection object.

Re (1): manually disconnecting is not necessary for several (e.g. PostgreSQL, MongoDB) if not all backends. A method could be added but R's garbage collection should be sufficient. Did you run into any problems?

Re (2): this is for consistency of the main parameters across nodbi functions. I will review if simplifying the function signature e.g. for nodbi::docdb_query() would be a good gain.

Thanks again

GitHunter0 commented 2 years ago

Hi @rfhb , thank you for the feedback.

If the command would not have analogous or corresponding commands across the backends,

That's my case, I just thought it would be nice to use one single connection (in this case nodbi) for every possible operation. That being said, the trick of using dbc$... is handy and indeed reduces the hurdle.

(1) I've not run into problems yet, I will stick with the garbage collection solution and see if any issue appears.

(2) Understood.

I also opened this issue in connections package which I believe would be a great fit for nodbi.

Thanks again and congratulations on the project.