New .add_memory_database() method, closes #1247

pull/1252/head
Simon Willison 2021-02-28 20:02:18 -08:00
rodzic 47eb885cc2
commit 7c87532acc
3 zmienionych plików z 25 dodań i 11 usunięć

Wyświetl plik

@ -390,6 +390,9 @@ class Datasette:
self.databases[name] = db
return db
def add_memory_database(self, memory_name):
return self.add_database(Database(self, memory_name=memory_name))
def remove_database(self, name):
self.databases.pop(name)

Wyświetl plik

@ -273,7 +273,25 @@ The ``db`` parameter should be an instance of the ``datasette.database.Database`
This will add a mutable database and serve it at ``/my-new-database``.
To create a shared in-memory database named ``statistics``, use the following:
``.add_database()`` returns the Database instance, with its name set as the ``database.name`` attribute. Any time you are working with a newly added database you should use the return value of ``.add_database()``, for example:
.. code-block:: python
db = datasette.add_database(Database(datasette, memory_name="statistics"))
await db.execute_write("CREATE TABLE foo(id integer primary key)", block=True)
.. _datasette_add_memory_database:
.add_memory_database(name)
--------------------------
Adds a shared in-memory database with the specified name:
.. code-block:: python
datasette.add_memory_database("statistics")
This is a shortcut for the following:
.. code-block:: python
@ -284,14 +302,7 @@ To create a shared in-memory database named ``statistics``, use the following:
memory_name="statistics"
))
This database will be served at ``/statistics``.
``.add_database()`` returns the Database instance, with its name set as the ``database.name`` attribute. Any time you are working with a newly added database you should use the return value of ``.add_database()``, for example:
.. code-block:: python
db = datasette.add_database(Database(datasette, memory_name="statistics"))
await db.execute_write("CREATE TABLE foo(id integer primary key)", block=True)
Using either of these pattern will result in the in-memory database being served at ``/statistics``.
.. _datasette_remove_database:

Wyświetl plik

@ -479,9 +479,9 @@ async def test_attached_databases(app_client_two_attached_databases_crossdb_enab
async def test_database_memory_name(app_client):
ds = app_client.ds
foo1 = ds.add_database(Database(ds, memory_name="foo"))
foo2 = ds.add_database(Database(ds, memory_name="foo"))
foo2 = ds.add_memory_database("foo")
bar1 = ds.add_database(Database(ds, memory_name="bar"))
bar2 = ds.add_database(Database(ds, memory_name="bar"))
bar2 = ds.add_memory_database("bar")
for db in (foo1, foo2, bar1, bar2):
table_names = await db.table_names()
assert table_names == []