Skip to content

Commit

Permalink
New .add_memory_database() method, closes #1247
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 1, 2021
1 parent 47eb885 commit 7c87532
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
3 changes: 3 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ def add_database(self, db, name=None):
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)

Expand Down
29 changes: 20 additions & 9 deletions docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions tests/test_internals_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
Expand Down

0 comments on commit 7c87532

Please sign in to comment.