mirror of
https://github.com/simonw/datasette.git
synced 2026-06-03 15:46:59 +02:00
parent
040e42ddca
commit
3b26b7aff0
2 changed files with 2 additions and 72 deletions
|
|
@ -1212,77 +1212,8 @@ Examples: `datasette-saved-queries <https://datasette.io/plugins/datasette-saved
|
|||
canned_queries(datasette, database, actor)
|
||||
------------------------------------------
|
||||
|
||||
``datasette`` - :ref:`internals_datasette`
|
||||
You can use this to access plugin configuration options via ``datasette.plugin_config(your_plugin_name)``, or to execute SQL queries.
|
||||
|
||||
``database`` - string
|
||||
The name of the database.
|
||||
|
||||
``actor`` - dictionary or None
|
||||
The currently authenticated :ref:`actor <authentication_actor>`.
|
||||
|
||||
Use this hook to return a dictionary of additional :ref:`canned query <canned_queries>` definitions for the specified database. The return value should be the same shape as the JSON described in the :ref:`canned query <canned_queries>` documentation.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette import hookimpl
|
||||
|
||||
|
||||
@hookimpl
|
||||
def canned_queries(datasette, database):
|
||||
if database == "mydb":
|
||||
return {
|
||||
"my_query": {
|
||||
"sql": "select * from my_table where id > :min_id"
|
||||
}
|
||||
}
|
||||
|
||||
The hook can alternatively return an awaitable function that returns a list. Here's an example that returns queries that have been stored in the ``saved_queries`` database table, if one exists:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette import hookimpl
|
||||
|
||||
|
||||
@hookimpl
|
||||
def canned_queries(datasette, database):
|
||||
async def inner():
|
||||
db = datasette.get_database(database)
|
||||
if await db.table_exists("saved_queries"):
|
||||
results = await db.execute(
|
||||
"select name, sql from saved_queries"
|
||||
)
|
||||
return {
|
||||
result["name"]: {"sql": result["sql"]}
|
||||
for result in results
|
||||
}
|
||||
|
||||
return inner
|
||||
|
||||
The actor parameter can be used to include the currently authenticated actor in your decision. Here's an example that returns saved queries that were saved by that actor:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datasette import hookimpl
|
||||
|
||||
|
||||
@hookimpl
|
||||
def canned_queries(datasette, database, actor):
|
||||
async def inner():
|
||||
db = datasette.get_database(database)
|
||||
if actor is not None and await db.table_exists(
|
||||
"saved_queries"
|
||||
):
|
||||
results = await db.execute(
|
||||
"select name, sql from saved_queries where actor_id = :id",
|
||||
{"id": actor["id"]},
|
||||
)
|
||||
return {
|
||||
result["name"]: {"sql": result["sql"]}
|
||||
for result in results
|
||||
}
|
||||
|
||||
return inner
|
||||
This hook has been removed. Plugins that need to add saved queries should use
|
||||
the :ref:`plugin_hook_startup` hook and call ``await datasette.add_query(...)``.
|
||||
|
||||
Example: `datasette-saved-queries <https://datasette.io/plugins/datasette-saved-queries>`__
|
||||
|
||||
|
|
|
|||
|
|
@ -249,7 +249,6 @@ If you run ``datasette plugins --all`` it will include default plugins that ship
|
|||
"templates": false,
|
||||
"version": null,
|
||||
"hooks": [
|
||||
"canned_queries",
|
||||
"permission_resources_sql"
|
||||
]
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue