mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
New .add_memory_database() method, closes #1247
This commit is contained in:
parent
47eb885cc2
commit
7c87532acc
3 changed files with 25 additions and 11 deletions
|
|
@ -390,6 +390,9 @@ class Datasette:
|
||||||
self.databases[name] = db
|
self.databases[name] = db
|
||||||
return 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):
|
def remove_database(self, name):
|
||||||
self.databases.pop(name)
|
self.databases.pop(name)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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``.
|
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
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
@ -284,14 +302,7 @@ To create a shared in-memory database named ``statistics``, use the following:
|
||||||
memory_name="statistics"
|
memory_name="statistics"
|
||||||
))
|
))
|
||||||
|
|
||||||
This database will be served at ``/statistics``.
|
Using either of these pattern will result in the in-memory database being 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)
|
|
||||||
|
|
||||||
.. _datasette_remove_database:
|
.. _datasette_remove_database:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -479,9 +479,9 @@ async def test_attached_databases(app_client_two_attached_databases_crossdb_enab
|
||||||
async def test_database_memory_name(app_client):
|
async def test_database_memory_name(app_client):
|
||||||
ds = app_client.ds
|
ds = app_client.ds
|
||||||
foo1 = ds.add_database(Database(ds, memory_name="foo"))
|
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"))
|
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):
|
for db in (foo1, foo2, bar1, bar2):
|
||||||
table_names = await db.table_names()
|
table_names = await db.table_names()
|
||||||
assert table_names == []
|
assert table_names == []
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue