NanoSQL: lightweight data mapper for SQLite
NanoSQL is a small data mapper for Rust and SQLite. It allows you to quickly
and type-safely define the schema of your database, while giving you full control
over the SQL of your queries.
It essentially removes the boilerplate associated with:
- Defining the types, constraints, and indexes of a table
- Compiling prepared statements and binding parameters to them
- Mapping the returned result set (rows) to a collection of domain objects
- Setting up the generally recommended configuration on an SQLite connection
The library always tries to do the right thing by default. For instance:
- Foreign keys are automatically indexed.
- Batch insertions are automatically wrapped in an IMMEDIATE transaction.
- When you are required to supply bare SQL expressions (for example, in CHECK
constraints or GENERATED columns), derive macros ensure the syntactical validity
of those expressions.
- Identifiers such as table and column names are always quoted in the SQL emitted by
helper types provided by the library. You can freely rename tables and columns, it will
never cause conflicts with either SQL or Rust keywords.
- Result columns (of e.g. SELECT and RETURNING statements) are given
explicit aliases: expression AS 'name'. This ensures portability and future-proof
operation across SQLite versions (because the name of a column without an explicit alias
is not guaranteed).
- Bind parameters and result columns are referenced by name, and not by index, whenever
possible. The library supports and encourages the use of named parameters and result columns.
The parameter prefix to be used ($, @, : or ?) can be
automatically inferred from the context (e.g., whether a parameter type is a struct with
named fields or a tuple) most of the time.
While not a full ORM, NanoSQL does provide convenient helper types and functions
for the most common tasks, such as creating tables, inserting rows, and looking up
records by their primary key.
The library is highly specific to SQLite, and as such, it can readily support most
of its advanced features. For example, you can declare partial indexes, set WAL mode,
and the output of EXPLAIN QUERY PLAN is readily available as a pre-parsed,
pretty-printed tree for you to perform debugging and performance optimization.
To get started, see the official documentation.
To contribute, star and fork the project on GitHub.