Skip to content

Commit

Permalink
feat(async_dialect): add SQLiteDialect_libsql_async
Browse files Browse the repository at this point in the history
- provides compatibility with create_async_engine and async_session
  • Loading branch information
douglasmdev committed Oct 4, 2023
1 parent ca99866 commit 5ce56af
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sqlalchemy_libsql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from sqlalchemy.dialects import registry as _registry

from sqlalchemy_libsql.libsql import SQLiteDialect_libsql
from sqlalchemy_libsql.libsql_async import SQLiteDialect_libsql_async

__version__ = "0.1.0-pre"

_registry.register(
"sqlite.libsql", "sqlalchemy_libsql", "SQLiteDialect_libsql"
)

_registry.register(
"sqlite.libsql_async", "sqlalchemy_libsql", "SQLiteDialect_libsql_async"
)

__all__ = ("SQLiteDialect_libsql", "SQLiteDialect_libsql_async")
50 changes: 50 additions & 0 deletions sqlalchemy_libsql/libsql_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
r"""
.. dialect:: sqlite+libsql_async
:name: libsql_async
:dbapi: libsql_client.dbapi2
:connectstring: sqlite+libsql_async://your-db.your-server.com?authToken=JWT_HERE&secure=true
:url: https://github.com/libsql/libsql-client-py/
Note that this driver is based on the standard SQLAlchemy ``pysqlite``
dialect, the only change is how to connect, accepting remote URL in
addition to the file dialects
Disclaimer: While this dialect allows for async_engine compatibility with
libsql, the dbapi remains synchronous
Driver
------
The ``libsql_client.dbapi2`` offers compatibility with standard library's
``sqlite3.dbapi2``. For local files or ``:memory:``, the standard library
connection is used. Whenever a host is provided, then the connection
will use LibSQL network protocol via ``ws`` (WebSocket) or ``wss``
(secure WebSocket), the decision depends on the presence of ``secure=true``
query parameter.
Connect Strings
---------------
In addition to `Pysqlite
<https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#connect-strings>`_,
this driver accepts URL with user, password, hostname and port.
These will use the LibSQL network protocol on top of WebSockets. The selection
between ``ws://`` and ``wss://` (secure) is defined by the query/search
parameter ``secure=true``. It defaults to ``secure=false``.
If the given URL provides a hostname, then it will default to ``uri=true``.
""" # noqa: E501

from sqlalchemy_libsql.libsql import SQLiteDialect_libsql


class SQLiteDialect_libsql_async(SQLiteDialect_libsql):
driver = "libsql"
# need to be set explicitly
supports_statement_cache = SQLiteDialect_libsql.supports_statement_cache
is_async = True


dialect = SQLiteDialect_libsql_async

0 comments on commit 5ce56af

Please sign in to comment.