|
1 | 1 | sqlalchemy-libsql |
2 | 2 | ================= |
3 | 3 |
|
4 | | -A `LibSQL <https://libsql.org/>`_ dialect for SQLAlchemy. |
5 | | - |
6 | | -This dialect requires SQLAlchemy 2.0 or later. |
| 4 | +A `libSQL <https://libsql.org/>`_ dialect for `SQLAlchemy <https://www.sqlalchemy.org/>`_. |
7 | 5 |
|
8 | 6 |
|
9 | 7 | Pre-requisites |
10 | 8 | -------------- |
11 | 9 |
|
12 | | -- Running instance of https://github.com/libsql/sqld. You can easily get one at https://turso.tech/ |
13 | | -- Alternatively a https://github.com/libsql/hrana-test-server, a pure-python implementation |
| 10 | +You must have a running instance of `sqld <https://github.com/libsql/sqld>`_, |
| 11 | +which is the libSQL server mode. There are several supported options: |
| 12 | + |
| 13 | +- `Build and run an instance |
| 14 | + <https://github.com/libsql/sqld/blob/main/docs/BUILD-RUN.md>`_ on your local |
| 15 | + machine. |
| 16 | +- Use an instance managed by `Turso <https://turso.tech/>`_. |
| 17 | +- Use the `libSQL test server <https://github.com/libsql/hrana-test-server>`_ |
| 18 | + implemented in python |
14 | 19 |
|
15 | 20 | Co-requisites |
16 | 21 | ------------- |
17 | 22 |
|
18 | | -This dialect requires SQLAlchemy and libsql_client. They are specified as requirements so ``pip`` |
19 | | -will install them if they are not already in place. To install, just:: |
| 23 | +This dialect requires the python packages `SQLAlchemy |
| 24 | +<https://pypi.org/project/SQLAlchemy/>`__ (version 2.0 or later) and |
| 25 | +`libsql_client <https://pypi.org/project/libsql-client/>`_. They are specified |
| 26 | +as requirements so ``pip`` will install them if they are not already in place. |
| 27 | +To install, just:: |
20 | 28 |
|
21 | 29 | pip install sqlalchemy-libsql |
22 | 30 |
|
23 | 31 | Getting Started |
24 | 32 | --------------- |
25 | 33 |
|
26 | | -Create an URL that points to your libsql database. |
27 | | -Then, in your Python app, you can connect to the database via:: |
| 34 | +You must construct a special URL that SQLAlchemy can use to locate your |
| 35 | +database. This will be different than the usual HTTP or websocket URLs that you |
| 36 | +normally use with the libSQL client SDKs. |
28 | 37 |
|
29 | | - from sqlalchemy import create_engine |
30 | | - engine = create_engine("sqlite+libsql://your-db.your-server.com?authToken=JWT_HERE&secure=true") |
| 38 | +If you are running an instance of sqld on your own machine, normally listening |
| 39 | +at 127.0.0.1 port 8080, the SQLAlchemy URL looks like this:: |
| 40 | + |
| 41 | + sqlite+libsql://127.0.0.1:8080 |
| 42 | + |
| 43 | +If your sqld instance is configured to use SSL with some hostname, and requires |
| 44 | +authentication with a database token (including Turso databases), you must |
| 45 | +provide two additional configurations in the query string of the URL:: |
31 | 46 |
|
32 | | -Note that ``secure=true`` query/search parameter will force the usage of |
33 | | -secure WebSockets (``wss://``) to connect to the remote server. |
| 47 | + sqlite+libsql://your-database-hostname/?authToken=your-auth-token&secure=true |
| 48 | + |
| 49 | +``your-database-hostname`` and ``your-auth-token`` above are unique to your |
| 50 | +database. ``secure=true`` specifies the use of SSL. |
| 51 | + |
| 52 | +You can then pass this URL to SQLAlchemy:: |
| 53 | + |
| 54 | + from sqlalchemy import create_engine |
| 55 | + engine = create_engine(url) |
34 | 56 |
|
35 | 57 | Development |
36 | 58 | ----------- |
|
0 commit comments