Skip to content

Releases: Aelto/surreal-simple-querybuilder

v0.8.0

22 Feb 16:05
Compare
Choose a tag to compare

v0.7.0

31 May 17:17
Compare
Choose a tag to compare

Full Changelog: v0.6.0...v0.7.0

Notable changes

  • commit ForeignVec<T> now offers a fn len(&self) method to get the length of the inner Vec, whether it holds a list of foreign values or a list of IDs
  • commit add consuming/chainable versions to value_ser functions
  • commit add blanket implementation of KeySerializeControl for Option<T> so we can do Some(struct).with_allowed_value_ser()

v0.6.0

16 Mar 15:11
Compare
Choose a tag to compare

Major changes

Better support for nested objects in parameters

Where(json!({
  post.title: "a title",
  post.author().name: "John"
});

creates a WHERE clause like this: WHERE title = $title AND author.name = $author_name, with variables: { "title": "a title", "author_name": "John" }

A shortcut macro was also added to shorten Where(json!({ foo: bar })) calls: wjson!({ foo: bar })

model macro is able to generate a partial-builder type

more information

Pass the with(partial) flag to the model! macro

model!(User with(partial) {
  id,
  name,
  posts,
  age
})

Will generate a PartialUser that can be used like so:

PartialUser::new()
  name("John")
  .posts(vec![post_1, post_2])
  .ok()?

// yields
{
  "name:": "John",
  "posts": [post_1, post_2]
}

Showcase repository

A showcase repository was created to demonstrate how to use the querybuilder with the official surrealdb client

v0.5.0

04 Mar 22:43
Compare
Choose a tag to compare

Major changes

New: Premade queries & parameters

use surreal_simple_querybuilder::prelude::*;

fn main() {
  let (query, _bindings) = select("*", "user", ());

  assert_eq!(query, "SELECT * FROM user");
}
use surreal_simple_querybuilder::prelude::*;
use serde_json::json;

fn main() {
  let (query, bindings) = select("*", "user", Where(json!({ "name": "John" })));

  assert_eq!(query, "SELECT * FROM user WHERE name = $name");

  // 👇 the bindings were updated with the $name variable
  assert_eq!(bindings.get("name"), Some("John".to_owned())); 
}

more information

Crate features

The crate now support cargo features, by default only querybuilder is enabled. To access the the of features you may need model, queries, foreign or all to get all of them directly.

model proc macro

Removed the build script to improve compile times, the model macro now uses a pre-compiled lalrpop parser.

v0.4.0

27 Nov 19:59
Compare
Choose a tag to compare

SQL serialization, from QueryBuilderSetObject to automatic serialization using models/schemas from the model macro

Removes the QueryBuilderSetObject trait that was used to easily serialize a struct into a series of SQL set statements. The trait was replaced with a set of new utility functions that use an internal SQL serializer and that accept the Schema type (the output of the model!() macro).

The model macro now accepts the pub keyword in front of the fields to mark them as public and serializable:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use surreal_simple_querybuilder::prelude::*;

model!(User {
  // 👇 note how id is not `pub`
  id,

  // 👇 while these two fields are
  pub age,
  pub name,
});

fn main() -> Result<(), SqlSerializeError> {
  use schema::model as user;

  let query = QueryBuilder::new()
    .create(user)
    // 👇 all `pub` fields will be serialized while the others won't.
    .set_model(&user)?
    .build();

  // CREATE User SET age = $age , name = $name
  println!("query: {query}");

  Ok(())
}

QueryBuilder, from custom type QueryBuilderSegment to Cow<str>

The QueryBuilder methods now accept Cow<str> instead of a custom type, allowing the user to pass either an owned string or a reference for more flexibility.

v0.3.0

11 Oct 13:59
44c2963
Compare
Choose a tag to compare

Add support for relations in the model macro.

mod account {
  use surreal_simple_querybuilder::prelude::*;
  use super::project::schema::Project;

  model!(Account {
    id,

    ->manage->Project as managed_projects
  });
}

mod project {
  use surreal_simple_querybuilder::prelude::*;
  use super::project::schema::Project;

  model!(Project {
    id,
    name,

    <-manage<-Account as authors
  });
}

fn main() {
    use account::schema::model as account;

    let query = format!("select {} from {account}", account.managed_projects);
    assert_eq!("select ->manage->Project from Account");

    let query = format!("select {} from {account}", account.managed_projects().name.as_alias("project_names"))
    assert_eq!("select ->manage->Project.name as project_names from Account", query);
}