simonw / sqlite-utils

Python CLI utility and library for manipulating SQLite databases
https://sqlite-utils.datasette.io
Apache License 2.0
1.58k stars 106 forks source link

Plugin system #567

Closed asg017 closed 1 year ago

asg017 commented 1 year ago

I'd like there to be a plugin system for sqlite-utils, similar to the datasette/llm plugins. I'd like to make plugins that would do things like:

A few real-world use-cases of plugins I'd like to see in sqlite-utils:

asg017 commented 1 year ago

Another use-case: I want to make a sqlite-utils plugin that'll help me insert data into Datasette.

sqlite-utils insert-datasette \
  --token $DATASETTE_API_KEY \
  https://latest.datasette.io/fixtures/my-table \
  'select ...'

This could also be a datasette plugin (ex datasette upload-data ..., but you can also think of sqlite-utils plugins that upload to S3, a postgres table, other DBMS's, etc.)

simonw commented 1 year ago

I really like this. I'm also interested in:

The upload-data-to-Datasette problem is planned to be solved by a future version of https://github.com/simonw/dclient

eyeseast commented 1 year ago

Would this possibly make a bunch of x-to-sqlite tools obsolete? Or nudge some to become plugins?

simonw commented 1 year ago

Would this possibly make a bunch of x-to-sqlite tools obsolete? Or nudge some to become plugins?

Yeah, it could do! That's not a terrible idea to be honest, those things have really been proliferating.

Alternatively, they could each register themselves as plugins in addition - so if you install e.g. pocket-to-sqlite you could then optionally also run it as sqlite-utils pocket-to-sqlite ...

The benefit there is for people who install sqlite-utils from Homebrew, where it gets its own virtual environment. They could run:

brew install sqlite-utils
sqlite-utils install pocket-to-sqlite

sqlite-utils pocket-to-sqlite ...
simonw commented 1 year ago

I'm going to start by adding the register_command hook using the exact same pattern as Datasette and LLM.

simonw commented 1 year ago

Here's documentation for the new plugins mechanism, including a very short tutorial on writing a new plugin (inspired by https://llm.datasette.io/en/stable/plugins/tutorial-model-plugin.html):

https://sqlite-utils.datasette.io/en/latest/plugins.html

eyeseast commented 1 year ago

Cool. I might try to add a geojson plugin that handles both input and output. That would help me out a lot.

simonw commented 1 year ago

Splitting off an issue for prepare_connection() since Alex got the PR in seconds before I shipped 3.34!

simonw commented 1 year ago

Now that we have two plugin hooks I'm closing this issue (we can open other issues for further hooks).