Skip to main content

Crate runlight_sqlx

Crate runlight_sqlx 

Source
Expand description

Runlight’s tables in the app’s own database, through sqlx and the app’s own pool.

use sqlx::sqlite::{SqliteConnectOptions, SqlitePool};

let pool = SqlitePool::connect_with(SqliteConnectOptions::new().filename("data/runlight.db").create_if_missing(true)).await?;
let store = runlight_sqlx::sqlite(pool);

The tables are the TypeScript SDK’s, statement for statement, so a Rust process can share a database with a Node or PHP one. Each database is a feature of the crate: sqlite, postgres, and mysql (MySQL 8.0.20 or MariaDB 11.4 and later).

Values reach the database as the TypeScript drivers send them. SQLite binds them typed, as better-sqlite3 does. Postgres and MySQL get them in the statement’s text, as node-postgres’s untyped parameters and mysql2’s client-side escaping do, so every statement means the same to the server as it does from Node: a value Postgres would infer the type of is written as a literal it infers the type of in the same way.

Structs§

MysqlDb
The store’s driver over a MySQL pool. SQL written for SQLite and Postgres is sent as MySQL reads it: a “quoted” name in backticks, a backslash inside ‘text’ doubled, and each value written into the statement, escaped, as mysql2 fills them in.
PostgresDb
The store’s driver over a Postgres pool. Values go in the statement’s text as literals of unknown type, so Postgres infers each one’s type as it does for node-postgres’s untyped parameters.
SqliteDb
The pool, and the one connection of it the store holds. Statements and transactions take turns, so a request’s insert can never land inside an import’s open transaction, and a rollback can only undo the transaction’s own writes.

Functions§

connect
A store from a connection URL: sqlite: (or a path), postgres://, or mysql:// and mariadb://, with a pool of its own, as the standalone server and the CLI open one. A new SQLite file is made for its owner and group alone (0660).
connect_with_prefix
connect, with every table and index name starting with prefix in place of rl_, so two apps sharing one database each keep a set of their own: up to 32 letters, digits, and underscores, not starting with a digit.
mysql
Runlight’s tables in MySQL 8.0.20 or MariaDB 11.4 and later, on the app’s pool, which needs at least two connections. Text is utf8mb4 with a binary collation, so it compares and sorts by code point, as SQLite and Postgres do. Values are written into each statement escaped with backslashes, so each statement first takes NO_BACKSLASH_ESCAPES out of its connection’s sql_mode.
mysql_with_prefix
mysql, with every table and index name starting with prefix in place of rl_, so two apps sharing one database each keep a set of their own: up to 32 letters, digits, and underscores, not starting with a digit.
postgres
Runlight’s tables in Postgres, on the app’s pool. The pool needs at least two connections: some work holds one while it reads through another.
postgres_with_prefix
postgres, with every table and index name starting with prefix in place of rl_, so two apps sharing one database each keep a set of their own: up to 32 letters, digits, and underscores, not starting with a digit.
sqlite
Runlight’s tables in a SQLite database, on one connection of the app’s pool, which the store holds for its statements in turn (an in-memory database is one per connection, and SQLite has one writer at a time anyway). The connection is put in WAL mode with a busy timeout of five seconds.
sqlite_with_prefix
sqlite, with every table and index name starting with prefix in place of rl_, so two apps sharing one database each keep a set of their own: up to 32 letters, digits, and underscores, not starting with a digit.