use serde_json::{Map, Value};
pub const ID_COLUMN: &str = "id";
pub const BODY_COLUMN: &str = "body";
pub const EDGES_TABLE: &str = "valence_edges";
pub fn ensure_table_ddl(table: &str) -> String {
format!(
"CREATE TABLE IF NOT EXISTS {table} (\
{ID_COLUMN} TEXT PRIMARY KEY NOT NULL, \
{BODY_COLUMN} TEXT NOT NULL DEFAULT '{{}}')"
)
}
pub fn ensure_edges_table_ddl() -> String {
format!(
"CREATE TABLE IF NOT EXISTS {EDGES_TABLE} (\
from_table TEXT NOT NULL, \
from_id TEXT NOT NULL, \
edge_type TEXT NOT NULL, \
to_table TEXT NOT NULL, \
to_id TEXT NOT NULL, \
PRIMARY KEY (from_table, from_id, edge_type, to_table, to_id))"
)
}
pub fn ensure_table(table: &str) -> String {
ensure_table_ddl(table)
}
pub fn row_from_body(table: &str, id: &str, body: Value) -> Value {
let mut obj = body.as_object().cloned().unwrap_or_default();
obj.insert(
"id".into(),
Value::Object(Map::from_iter([
("table".into(), Value::String(table.to_string())),
("id".into(), Value::String(id.to_string())),
])),
);
Value::Object(obj)
}
pub fn upsert_body_fields(content: Value) -> Map<String, Value> {
content.as_object().cloned().unwrap_or_default()
}