use serde_json::{json, Map};
use rust_store_core::command::plan_query;
use rust_store_core::dialect::{translate, Backend};
use rust_store_core::schema::Registry;
fn main() -> Result<(), String> {
let mut registry = Registry::new();
registry.register(&json!({
"name": "Post",
"collection": "posts",
"timestamps": true,
"fields": {
"title": { "type": "string" },
"status": { "type": "string", "default": "draft" },
"views": { "type": "int" }
},
"relations": {}
}))?;
let gql = "Post($condition:@c0,$sort:@s1,$limit:@l0){title, status, views}";
let mut params = Map::new();
params.insert("c0".to_string(), json!({ "status": "published" }));
params.insert("s1".to_string(), json!({ "views": -1 }));
params.insert("l0".to_string(), json!(20));
let plan = plan_query(gql, ¶ms, ®istry, None)?;
println!("collection = {}", plan.collection);
println!("mode = {}", plan.mode.as_str());
for cmd in &plan.commands {
println!("command = {}", serde_json::to_string(cmd).unwrap());
let sql = translate(Backend::Postgres, cmd, ®istry)?;
println!("sql = {}", serde_json::to_string(&sql).unwrap());
}
Ok(())
}