mirror of
https://github.com/RaisFast/raisfast.git
synced 2026-09-28 08:02:28 +00:00
add database crud operation function to plugin system
This commit is contained in:
+10
-12
@@ -53,18 +53,16 @@ pub async fn find_by_user_and_product(
|
||||
)
|
||||
.map_err(Into::into)
|
||||
} else {
|
||||
let sql = if tenant_id.is_some() {
|
||||
"SELECT * FROM cart_items WHERE user_id = ? AND product_id = ? AND variant_id IS NULL AND tenant_id = ?"
|
||||
} else {
|
||||
"SELECT * FROM cart_items WHERE user_id = ? AND product_id = ? AND variant_id IS NULL"
|
||||
};
|
||||
let mut q = sqlx::query_as::<_, CartItem>(sql)
|
||||
.bind(user_id)
|
||||
.bind(product_id);
|
||||
if let Some(tid) = tenant_id {
|
||||
q = q.bind(tid);
|
||||
}
|
||||
Ok(q.fetch_optional(pool).await?)
|
||||
raisfast_derive::crud_find!(
|
||||
pool,
|
||||
"cart_items",
|
||||
CartItem,
|
||||
"user_id" => user_id,
|
||||
and: ["product_id" => product_id],
|
||||
and_null: ["variant_id"],
|
||||
tenant: tenant_id
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -98,6 +98,85 @@ pub fn register_host_functions(
|
||||
let db_rollback_fn = Function::new(ctx.clone(), move || -> String { hc.db_rollback() })?;
|
||||
host.set("dbRollback", db_rollback_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_insert_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, data: String, options: String| -> String {
|
||||
hc.db_insert(&table, &data, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbInsert", db_insert_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_fetch_one_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, r#where: String, options: String| -> String {
|
||||
hc.db_fetch_one(&table, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbFetchOne", db_fetch_one_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_fetch_all_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, r#where: String, options: String| -> String {
|
||||
hc.db_fetch_all(&table, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbFetchAll", db_fetch_all_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_update_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, data: String, r#where: String, options: String| -> String {
|
||||
hc.db_update(&table, &data, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbUpdate", db_update_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_delete_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, r#where: String, options: String| -> String {
|
||||
hc.db_delete(&table, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbDelete", db_delete_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_count_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, r#where: String, options: String| -> String {
|
||||
hc.db_count(&table, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbCount", db_count_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_increment_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, columns: String, r#where: String, options: String| -> String {
|
||||
hc.db_increment(&table, &columns, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbIncrement", db_increment_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_sum_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, column: String, r#where: String, options: String| -> String {
|
||||
hc.db_sum(&table, &column, &r#where, &options)
|
||||
},
|
||||
)?;
|
||||
host.set("dbSum", db_sum_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_group_by_fn = Function::new(
|
||||
ctx.clone(),
|
||||
move |table: String, options: String| -> String { hc.db_group_by(&table, &options) },
|
||||
)?;
|
||||
host.set("dbGroupBy", db_group_by_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let vfs_read_fn = Function::new(ctx.clone(), move |path: String| -> Option<String> {
|
||||
hc.vfs_read(&path).ok()
|
||||
|
||||
@@ -113,6 +113,94 @@ pub fn register_host_functions(
|
||||
})?;
|
||||
host.set("dbRollback", db_rollback_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_insert_fn = lua.create_function(
|
||||
move |lua, (table, data, options): (String, String, String)| {
|
||||
Ok(mlua::Value::String(
|
||||
lua.create_string(hc.db_insert(&table, &data, &options))?,
|
||||
))
|
||||
},
|
||||
)?;
|
||||
host.set("dbInsert", db_insert_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_fetch_one_fn = lua.create_function(
|
||||
move |lua, (table, r#where, options): (String, String, String)| {
|
||||
Ok(mlua::Value::String(lua.create_string(
|
||||
hc.db_fetch_one(&table, &r#where, &options),
|
||||
)?))
|
||||
},
|
||||
)?;
|
||||
host.set("dbFetchOne", db_fetch_one_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_fetch_all_fn = lua.create_function(
|
||||
move |lua, (table, r#where, options): (String, String, String)| {
|
||||
Ok(mlua::Value::String(lua.create_string(
|
||||
hc.db_fetch_all(&table, &r#where, &options),
|
||||
)?))
|
||||
},
|
||||
)?;
|
||||
host.set("dbFetchAll", db_fetch_all_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_update_fn = lua.create_function(
|
||||
move |lua, (table, data, r#where, options): (String, String, String, String)| {
|
||||
Ok(mlua::Value::String(lua.create_string(
|
||||
hc.db_update(&table, &data, &r#where, &options),
|
||||
)?))
|
||||
},
|
||||
)?;
|
||||
host.set("dbUpdate", db_update_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_delete_fn = lua.create_function(
|
||||
move |lua, (table, r#where, options): (String, String, String)| {
|
||||
Ok(mlua::Value::String(
|
||||
lua.create_string(hc.db_delete(&table, &r#where, &options))?,
|
||||
))
|
||||
},
|
||||
)?;
|
||||
host.set("dbDelete", db_delete_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_count_fn = lua.create_function(
|
||||
move |lua, (table, r#where, options): (String, String, String)| {
|
||||
Ok(mlua::Value::String(
|
||||
lua.create_string(hc.db_count(&table, &r#where, &options))?,
|
||||
))
|
||||
},
|
||||
)?;
|
||||
host.set("dbCount", db_count_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_increment_fn = lua.create_function(
|
||||
move |lua, (table, columns, r#where, options): (String, String, String, String)| {
|
||||
Ok(mlua::Value::String(lua.create_string(
|
||||
hc.db_increment(&table, &columns, &r#where, &options),
|
||||
)?))
|
||||
},
|
||||
)?;
|
||||
host.set("dbIncrement", db_increment_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_sum_fn = lua.create_function(
|
||||
move |lua, (table, column, r#where, options): (String, String, String, String)| {
|
||||
Ok(mlua::Value::String(lua.create_string(
|
||||
hc.db_sum(&table, &column, &r#where, &options),
|
||||
)?))
|
||||
},
|
||||
)?;
|
||||
host.set("dbSum", db_sum_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let db_group_by_fn = lua.create_function(move |lua, (table, options): (String, String)| {
|
||||
Ok(mlua::Value::String(
|
||||
lua.create_string(hc.db_group_by(&table, &options))?,
|
||||
))
|
||||
})?;
|
||||
host.set("dbGroupBy", db_group_by_fn)?;
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
let vfs_read_fn = lua.create_function(move |lua, path: String| match hc.vfs_read(&path) {
|
||||
Ok(content) => Ok(mlua::Value::String(lua.create_string(&content)?)),
|
||||
|
||||
@@ -92,6 +92,75 @@ pub fn register_host_functions(
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn("dbRollback", move || -> String { hc.db_rollback() });
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbInsert",
|
||||
move |table: &str, data: &str, options: &str| -> String {
|
||||
hc.db_insert(table, data, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbFetchOne",
|
||||
move |table: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_fetch_one(table, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbFetchAll",
|
||||
move |table: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_fetch_all(table, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbUpdate",
|
||||
move |table: &str, data: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_update(table, data, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbDelete",
|
||||
move |table: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_delete(table, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbCount",
|
||||
move |table: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_count(table, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbIncrement",
|
||||
move |table: &str, columns: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_increment(table, columns, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn(
|
||||
"dbSum",
|
||||
move |table: &str, column: &str, r#where: &str, options: &str| -> String {
|
||||
hc.db_sum(table, column, r#where, options)
|
||||
},
|
||||
);
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn("dbGroupBy", move |table: &str, options: &str| -> String {
|
||||
hc.db_group_by(table, options)
|
||||
});
|
||||
|
||||
let hc = host_ctx.clone();
|
||||
engine.register_fn("vfsRead", move |path: &str| -> rhai::Dynamic {
|
||||
match hc.vfs_read(path) {
|
||||
@@ -294,6 +363,12 @@ mod tests {
|
||||
let _: String = engine.eval(r#"dbCommit()"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbRollback()"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbPh(1)"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbInsert("t", "{}", "{}")"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbFetchOne("t", "{}", "{}")"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbFetchAll("t", "{}", "{}")"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbUpdate("t", "{}", "{}", "{}")"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbDelete("t", "{}", "{}")"#).unwrap();
|
||||
let _: String = engine.eval(r#"dbCount("t", "{}", "{}")"#).unwrap();
|
||||
let _ = engine.eval::<rhai::Dynamic>(r#"vfsRead("f")"#).unwrap();
|
||||
let _: bool = engine.eval(r#"vfsWrite("f", "c")"#).unwrap();
|
||||
let _: bool = engine.eval(r#"vfsDelete("f")"#).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user