Skip to content

add $db - #10691

Closed
fdncred wants to merge 12 commits into
nushell:mainfrom
fdncred:dollar_db
Closed

add $db#10691
fdncred wants to merge 12 commits into
nushell:mainfrom
fdncred:dollar_db

Conversation

@fdncred

@fdncred fdncred commented Oct 11, 2023 •

Copy link
Copy Markdown
Contributor

Description

Warning

This PR is not finished and is meant as a proof of concept. I will clean this up shortly and continue polishing it if we decide to move forward. There are some changes that may not be wanted, namely depending on rusqlite. I also moved some things around to make access to them easier without having circular references.

Note

To test it out, simply checkout this PR then see the notes below for a simple test. Ideally, we'd want to have commands that do the sql so users don't have to.

This PR adds a $db system variable. In this variable is a in-memory sqlite db that can be used for anything or nothing. It should be accessible via scripts and the repl.

The idea was just a thought like, "Wouldn't it be cool if there was just a database hanging around in memory and I could store anything in it that I need to while working on data.".

The in-memory db is active while nushell is running. When nushell exits the database and all of its contents are gone. Any command that's run from inside the nushell process can access this database. I'm guessing that means that plugins cannot access it since they are separate executables and separate processes, but I haven't tried yet.

I had to move the sqlitedatabase stuff over to nu-protocol because we wouldn't want to add a nu-command dependency to nu-protocol. Also, if you want to have a $db variable, you have to do that in nu-protocol so that you can access it in nu-engine so you don't have circular dependencies.

Note

Update 2023-10-12
I've cleaned up the PR a bit so it's not as "wild" and full of debug statements. You should start by typing $db and be greeted with these instructions. I still have some unwrap()'s but it should compile for you to play with.

❯ $db
Initializing nudb
Here's some things to try:
* $db | schema | table -e
* $db | query db 'insert into nudb (bool1,int1,float1,str1,datetime1) values (2,200,2.0,'str2','1969-04-17T06:00:00-05:00')'
* $db | query db 'select * from nudb'
Now imagine all those examples happening as commands, without sql, in our normal nushell pipelines

╭────┬─────────────╮
│nudb│[table 1 row]│
╰────┴─────────────╯

User-Facing Changes

Tests + Formatting

After Submitting

@fdncred
fdncred marked this pull request as draft October 11, 2023 19:56
@kubouch

kubouch commented Oct 11, 2023

Copy link
Copy Markdown
Contributor

This is interesting. Not sure what I would use it for, but it sounds cool.

@amtoine

amtoine commented Oct 12, 2023

Copy link
Copy Markdown
Member

very curious to see how this goes when it matures 😋

@fdncred

fdncred commented Oct 12, 2023

Copy link
Copy Markdown
Contributor Author

Updates above in the PR description.

@stormasm

stormasm commented Oct 13, 2023 •

Copy link
Copy Markdown
Contributor

So I am kicking the tires on this PR and will add some feedback as I spend more time with it...

Initially I was wondering why the code was being added to nu-protocol
but in practicality its not so bad 😄

Prior to this PR nu-protocol pulled in 131 crates
After this PR nu-protocol pulls in 142 crates.
Which [IMHO] is not significant and the compilation times
of just nu-protocol
surprisingly is about the same.

@stormasm

Copy link
Copy Markdown
Contributor

I have been spending more time working with nu-protocol

and now I am hesitating (once again) to add rusqlite as a dependency to nu-protocol...

It just doesn't feel like the correct spot --- meaning maybe there is another way to solve this problem...

I don't know 😄 but just thought I would add my thoughts as I think about this more...

@fdncred

fdncred commented Oct 22, 2023

Copy link
Copy Markdown
Contributor Author

Like I said in the description, if you want to use $db then it has to be in nu-protocol.

An alternate route would be to instantiate the database in main(), which happens now and also requires a dependency to rusqlite in the root cargo.toml, and then you'd have to have a set of in-mem commands that interface with the in-memory database because it would no longer be accessible via $db. This is possible but it loses a little bit of magic, which may be fine.

The idea is to have a in-memory database accessible at all times, either approach achieves that I think.

@stormasm

Copy link
Copy Markdown
Contributor

Thanks for the added alternative description of it being in main instead...

I like it better the way you have it now with the extra magic 😄

Meaning for now rusqlite has to be in nu-protocol...

And now I have a better understanding of the EnvVars being in the EngineState

pub struct EngineState {
   ...
   pub env_vars: EnvVars,

The depth and breadth of our product continues to expand which is very cool...

It just means there is so much more to keep in our mental stack of all of the moving pieces and parts.

Its good for my brain, just sometimes overwhelming...

@fdncred

fdncred commented Oct 22, 2023

Copy link
Copy Markdown
Contributor Author

I still think this is a cool feature but there doesn't seem to be a lot of interest in it 😭, unfortunately. So, I'll probably end up closing it at some point. I still think people are missing the usefulness of having a db available at any time in memory.

@kubouch

kubouch commented Oct 23, 2023

Copy link
Copy Markdown
Contributor

I'd personally want to see it and try it out, it sounds really cool to me.

@fdncred

fdncred commented Oct 23, 2023

Copy link
Copy Markdown
Contributor Author

@kubouch I merged the latest main into this PR so you can play with it. It'll still have errors because I left some unwraps, but I tested it out and it's fine for playing around.

@amtoine amtoine mentioned this pull request Nov 27, 2023
5 tasks done
@amtoine

amtoine commented Nov 27, 2023

Copy link
Copy Markdown
Member

superseded by #11170

@amtoine amtoine closed this Nov 27, 2023
@fdncred
fdncred deleted the dollar_db branch January 8, 2024 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants