Surreal simple querybuilder
A simple query-builder for the Surreal Query Language, for SurrealDB. Aims at being simple to use and not too verbose first.
Summary
- Surreal simple querybuilder
- Summary
- Why a query-builder
- SQL injections
- Compiler requirements/features
- Examples
Why a query-builder
Query builders allow you to dynamically build your queries with some compile time checks to ensure they result in valid SQL queries. Unlike ORMs, query-builders are built to be lightweight and easy to use, meaning you decide when and where to use one. You could stick to hard coded string for the simple queries but use a builder for complex ones that require parameters & variables and may change based on these variables for example.
While the crate is first meant as a query-building utility, it also comes with macros and generic types that may help you while managing you SQL models in your rust code. Refer to the node macro and the Foreign type example
SQL injections
The strings you pass to the query builder are not sanitized in any way. Please use
parameters in your queries like SET username = $username
with surrealdb parameters to avoid injection issues.
However the crate comes with utility functions to easily create parameterized fields, refer to the NodeBuilder
trait.
Compiler requirements/features
The crate uses const expressions for its model creation macros in order to use stack based arrays with sizes deduced by the compiler. For this reason any program using the crate has to add the following at the root of the main file:
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
Examples
- A series of examples are available to offer a guided introduction to the core features of the crate
- An all-in-one exapmle can be found in the
tests project
. - For an explanation of what each component in the crate does, refer to the chapters below.
The model
macro
The model
macro allows you to quickly create structs (aka models) with fields
that match the nodes of your database.
use *;
model!;
This allows you to have compile time checked constants for your fields, allowing you to reference them while building your queries without fearing of making a typo or using a field you renamed long time ago.
public & private fields in models
The QueryBuilder type offers a series of methods to quickly list the fields of your
models in SET or UPDATE statements so you don't have to write the fields and the
variable names one by one. Since you may not want to serialize some of the fields
like the id
for example the model macro has the pub
keyword to mark a field
as serializable. Any field without the pub
keyword in front of it will not
be serialized by these methods.
model!
Relations between your models
If you wish to include relations (aka edges) in your models, the model
macro
has a special syntax for them:
The NodeBuilder
traits
These traits add a few utility functions to the String
and str
types that can
be used alongside the querybuilder for even more flexibility.
use *;
let my_label = "John".as_named_label;
assert_eq!;
let my_relation = my_label
.with
.with;
assert_eq!;
The QueryBuilder
type
It allows you to dynamically build complex or simple queries out of segments and easy to use methods.
use *;
let query = new
.select
.from
.build;
assert_eq!;
use *;
let should_fetch_authors = false;
let query = new
.select
.from
.if_then
.build;
assert_eq!;
let should_fetch_authors = true;
let query = new
.select
.from
.if_then
.build;
assert_eq!;
The ForeignKey
and Foreign
types
SurrealDB has the ability to fetch the data out of foreign keys. For example:
create Author:JussiAdlerOlsen set name = "Jussi Adler-Olsen";
create File set name = "Journal 64", author = Author:JussiAdlerOlsen;
select * from File;
select * from File fetch author;
which gives us
// without FETCH author
// with FETCH author
The "issue" with this functionality is that our results may either contain an ID
to the author, no value, or the fully fetched author with its data depending on
the query and whether it includes fetch
or not.
The ForeignKey
types comes to the rescue. It is an enum with 3 variants:
- The loaded data for when it was fetched
- The key data for when it was just an ID
- The unloaded data when it was null (if you wish to support missing data you must use the
#serde(default)
attribute to the field)
The type comes with an implementation of the Deserialize and Serialize serde traits
so that it can fallback to whatever data it finds or needs. However any type that
is referenced by a ForeignKey
must implement the IntoKey
trait that allows it
to safely serialize it into an ID during serialization.
/// For the tests, and as an example we are creating what could be an Account in
/// a simple database.
ForeignKey
and loaded data during serialization
A ForeignKey
always tries to serialize itself into an ID by default. Meaning that
if the foreign-key holds a value and not an ID, it will call the IntoKey
trait on
the value in order to get an ID to serialize.
There are cases where this may pose a problem, for example in an API where you wish
to serialize a struct with ForeignKey
fields so the users can get all the data
they need in a single request.
By default if you were to serialize a File
(from the example above) struct
with a fetched author
, it would automatically be converted into the author's id.
The ForeignKey
struct offers two methods to control this behaviour:
// ...imagine `query` is a function to send a query and get the first result...
let file: File = query;
file.author.allow_value_serialize;
// ... serializing `file` will now serialize its author field as-is.
// to go back to the default behaviour
file.author.disallow_value_serialize;
You may note that mutability is not needed, the methods use interior mutability to work even on immutable ForeignKeys if needed.