Skip to content

Commit 5ce56af

Browse files
committed
feat(async_dialect): add SQLiteDialect_libsql_async
- provides compatibility with create_async_engine and async_session
1 parent ca99866 commit 5ce56af

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

sqlalchemy_libsql/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
from sqlalchemy.dialects import registry as _registry
2+
23
from sqlalchemy_libsql.libsql import SQLiteDialect_libsql
4+
from sqlalchemy_libsql.libsql_async import SQLiteDialect_libsql_async
35

46
__version__ = "0.1.0-pre"
57

68
_registry.register(
79
"sqlite.libsql", "sqlalchemy_libsql", "SQLiteDialect_libsql"
810
)
11+
12+
_registry.register(
13+
"sqlite.libsql_async", "sqlalchemy_libsql", "SQLiteDialect_libsql_async"
14+
)
15+
16+
__all__ = ("SQLiteDialect_libsql", "SQLiteDialect_libsql_async")

sqlalchemy_libsql/libsql_async.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
r"""
2+
.. dialect:: sqlite+libsql_async
3+
:name: libsql_async
4+
:dbapi: libsql_client.dbapi2
5+
:connectstring: sqlite+libsql_async://your-db.your-server.com?authToken=JWT_HERE&secure=true
6+
:url: https://github.com/libsql/libsql-client-py/
7+
8+
Note that this driver is based on the standard SQLAlchemy ``pysqlite``
9+
dialect, the only change is how to connect, accepting remote URL in
10+
addition to the file dialects
11+
12+
Disclaimer: While this dialect allows for async_engine compatibility with
13+
libsql, the dbapi remains synchronous
14+
15+
Driver
16+
------
17+
18+
The ``libsql_client.dbapi2`` offers compatibility with standard library's
19+
``sqlite3.dbapi2``. For local files or ``:memory:``, the standard library
20+
connection is used. Whenever a host is provided, then the connection
21+
will use LibSQL network protocol via ``ws`` (WebSocket) or ``wss``
22+
(secure WebSocket), the decision depends on the presence of ``secure=true``
23+
query parameter.
24+
25+
Connect Strings
26+
---------------
27+
28+
In addition to `Pysqlite
29+
<https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#connect-strings>`_,
30+
this driver accepts URL with user, password, hostname and port.
31+
32+
These will use the LibSQL network protocol on top of WebSockets. The selection
33+
between ``ws://`` and ``wss://` (secure) is defined by the query/search
34+
parameter ``secure=true``. It defaults to ``secure=false``.
35+
36+
If the given URL provides a hostname, then it will default to ``uri=true``.
37+
38+
""" # noqa: E501
39+
40+
from sqlalchemy_libsql.libsql import SQLiteDialect_libsql
41+
42+
43+
class SQLiteDialect_libsql_async(SQLiteDialect_libsql):
44+
driver = "libsql"
45+
# need to be set explicitly
46+
supports_statement_cache = SQLiteDialect_libsql.supports_statement_cache
47+
is_async = True
48+
49+
50+
dialect = SQLiteDialect_libsql_async

0 commit comments

Comments
 (0)