From 5ce56af3565897ff0c80230d9ec11f5b48e69260 Mon Sep 17 00:00:00 2001 From: Douglas Montes Date: Tue, 3 Oct 2023 13:31:23 -0300 Subject: [PATCH] feat(async_dialect): add SQLiteDialect_libsql_async - provides compatibility with create_async_engine and async_session --- sqlalchemy_libsql/__init__.py | 8 +++++ sqlalchemy_libsql/libsql_async.py | 50 +++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 sqlalchemy_libsql/libsql_async.py diff --git a/sqlalchemy_libsql/__init__.py b/sqlalchemy_libsql/__init__.py index 51f45a7..757486a 100644 --- a/sqlalchemy_libsql/__init__.py +++ b/sqlalchemy_libsql/__init__.py @@ -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") diff --git a/sqlalchemy_libsql/libsql_async.py b/sqlalchemy_libsql/libsql_async.py new file mode 100644 index 0000000..8af774e --- /dev/null +++ b/sqlalchemy_libsql/libsql_async.py @@ -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 +`_, +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