diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 5c44859c..4f62a599 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -71,7 +71,6 @@ fuzzy-matcher = "0.3.7" geo = { version = "0.23.0", features = ["use-serde"] } indexmap = { version = "1.9.2", features = ["serde"] } indxdb = { version = "0.2.0", optional = true } -js = { version = "0.1.7", package = "rquickjs", features = ["bindgen", "classes", "futures", "loader", "macro", "properties", "parallel"], optional = true } lexical-sort = "0.3.1" log = "0.4.17" md-5 = "0.10.5" @@ -100,6 +99,21 @@ tokio-util = { version = "0.7.4", optional = true, features = ["compat"] } trice = "0.1.0" url = "2.3.1" +[dependencies.js] +optional = true +version = "0.1.7" +package = "rquickjs" +features = [ + "array-buffer", + "bindgen", + "classes", + "futures", + "loader", + "macro", + "parallel", + "properties", +] + [dev-dependencies] time = { version = "0.3.17", features = ["serde"] } tokio = { version = "1.22.0", features = ["macros", "rt", "rt-multi-thread"] } diff --git a/lib/src/fnc/script/main.rs b/lib/src/fnc/script/main.rs index a309aceb..188c4a9a 100644 --- a/lib/src/fnc/script/main.rs +++ b/lib/src/fnc/script/main.rs @@ -1,12 +1,14 @@ use super::classes; use super::executor::Executor; use super::globals; +use super::modules; use super::modules::loader; use super::modules::resolver; use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; use js::Function; +use js::Module; use js::Promise; use js::Rest; use js::This; @@ -46,13 +48,18 @@ pub async fn run( let res: Result, js::Error> = ctx.with(|ctx| { // Get the context global object let global = ctx.globals(); - // Register the fetch function as a global object + // Register the surrealdb module as a global object + global.set( + "surrealdb", + Module::new_def::(ctx, "surrealdb")? + .eval()? + .get::<_, js::Value>("default")?, + )?; + // Register the fetch function to the globals global.init_def::()?; - // Register the Duration type as a global class + // Register the special SurrealDB types as classes global.init_def::()?; - // Register the Record type as a global class global.init_def::()?; - // Register the Uuid type as a global class global.init_def::()?; // Attempt to compile the script let res = ctx.compile("script", src)?; diff --git a/lib/src/fnc/script/modules/surrealdb.rs b/lib/src/fnc/script/modules/surrealdb.rs deleted file mode 100644 index 4e498dac..00000000 --- a/lib/src/fnc/script/modules/surrealdb.rs +++ /dev/null @@ -1,6 +0,0 @@ -#[js::bind(module, public)] -#[quickjs(bare)] -#[allow(non_upper_case_globals)] -pub mod package { - pub const version: &str = crate::env::VERSION; -} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/array.rs b/lib/src/fnc/script/modules/surrealdb/functions/array.rs new file mode 100644 index 00000000..4d61724e --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/array.rs @@ -0,0 +1,62 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("combine")?; + module.add("complement")?; + module.add("concat")?; + module.add("difference")?; + module.add("distinct")?; + module.add("insert")?; + module.add("intersect")?; + module.add("len")?; + module.add("sort")?; + module.add("union")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("combine", Func::from(|v: Any| run("array::combine", v.0)))?; + module.set("complement", Func::from(|v: Any| run("array::complement", v.0)))?; + module.set("concat", Func::from(|v: Any| run("array::concat", v.0)))?; + module.set("difference", Func::from(|v: Any| run("array::difference", v.0)))?; + module.set("distinct", Func::from(|v: Any| run("array::distinct", v.0)))?; + module.set("insert", Func::from(|v: Any| run("array::insert", v.0)))?; + module.set("intersect", Func::from(|v: Any| run("array::intersect", v.0)))?; + module.set("len", Func::from(|v: Any| run("array::len", v.0)))?; + module.set("sort", Func::from(|v: Any| run("array::sort", v.0)))?; + module.set("union", Func::from(|v: Any| run("array::union", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("combine", Func::from(|v: Any| run("array::combine", v.0)))?; + default.set("complement", Func::from(|v: Any| run("array::complement", v.0)))?; + default.set("concat", Func::from(|v: Any| run("array::concat", v.0)))?; + default.set("difference", Func::from(|v: Any| run("array::difference", v.0)))?; + default.set("distinct", Func::from(|v: Any| run("array::distinct", v.0)))?; + default.set("insert", Func::from(|v: Any| run("array::insert", v.0)))?; + default.set("intersect", Func::from(|v: Any| run("array::intersect", v.0)))?; + default.set("len", Func::from(|v: Any| run("array::len", v.0)))?; + default.set("sort", Func::from(|v: Any| run("array::sort", v.0)))?; + default.set("union", Func::from(|v: Any| run("array::union", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/crypto.rs b/lib/src/fnc/script/modules/surrealdb/functions/crypto.rs new file mode 100644 index 00000000..ed9b0790 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/crypto.rs @@ -0,0 +1,62 @@ +use super::super::pkg; +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +mod argon2; +mod bcrypt; +mod pbkdf2; +mod scrypt; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("md5")?; + module.add("sha1")?; + module.add("sha256")?; + module.add("sha512")?; + module.add("argon2")?; + module.add("bcrypt")?; + module.add("pbkdf2")?; + module.add("scrypt")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("md5", Func::from(|v: Any| run("crypto::md5", v.0)))?; + module.set("sha1", Func::from(|v: Any| run("crypto::sha1", v.0)))?; + module.set("sha256", Func::from(|v: Any| run("crypto::sha256", v.0)))?; + module.set("sha512", Func::from(|v: Any| run("crypto::sha512", v.0)))?; + module.set("argon2", pkg::(ctx, "argon2"))?; + module.set("bcrypt", pkg::(ctx, "bcrypt"))?; + module.set("pbkdf2", pkg::(ctx, "pbkdf2"))?; + module.set("scrypt", pkg::(ctx, "scrypt"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("md5", Func::from(|v: Any| run("crypto::md5", v.0)))?; + default.set("sha1", Func::from(|v: Any| run("crypto::sha1", v.0)))?; + default.set("sha256", Func::from(|v: Any| run("crypto::sha256", v.0)))?; + default.set("sha512", Func::from(|v: Any| run("crypto::sha512", v.0)))?; + default.set("argon2", pkg::(ctx, "argon2"))?; + default.set("bcrypt", pkg::(ctx, "bcrypt"))?; + default.set("pbkdf2", pkg::(ctx, "pbkdf2"))?; + default.set("scrypt", pkg::(ctx, "scrypt"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/crypto/argon2.rs b/lib/src/fnc/script/modules/surrealdb/functions/crypto/argon2.rs new file mode 100644 index 00000000..cc522385 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/crypto/argon2.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("compare")?; + module.add("generate")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("compare", Func::from(|v: Any| run("crypto::argon2::compare", v.0)))?; + module.set("generate", Func::from(|v: Any| run("crypto::argon2::generate", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("compare", Func::from(|v: Any| run("crypto::argon2::compare", v.0)))?; + default.set("generate", Func::from(|v: Any| run("crypto::argon2::generate", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/crypto/bcrypt.rs b/lib/src/fnc/script/modules/surrealdb/functions/crypto/bcrypt.rs new file mode 100644 index 00000000..e7bedcef --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/crypto/bcrypt.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("compare")?; + module.add("generate")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("compare", Func::from(|v: Any| run("bcrypt::argon2::compare", v.0)))?; + module.set("generate", Func::from(|v: Any| run("bcrypt::argon2::generate", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("compare", Func::from(|v: Any| run("bcrypt::argon2::compare", v.0)))?; + default.set("generate", Func::from(|v: Any| run("bcrypt::argon2::generate", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/crypto/pbkdf2.rs b/lib/src/fnc/script/modules/surrealdb/functions/crypto/pbkdf2.rs new file mode 100644 index 00000000..e3f92157 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/crypto/pbkdf2.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("compare")?; + module.add("generate")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("compare", Func::from(|v: Any| run("pbkdf2::argon2::compare", v.0)))?; + module.set("generate", Func::from(|v: Any| run("pbkdf2::argon2::generate", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("compare", Func::from(|v: Any| run("pbkdf2::argon2::compare", v.0)))?; + default.set("generate", Func::from(|v: Any| run("pbkdf2::argon2::generate", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/crypto/scrypt.rs b/lib/src/fnc/script/modules/surrealdb/functions/crypto/scrypt.rs new file mode 100644 index 00000000..b82b0c7b --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/crypto/scrypt.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("compare")?; + module.add("generate")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("compare", Func::from(|v: Any| run("scrypt::argon2::compare", v.0)))?; + module.set("generate", Func::from(|v: Any| run("scrypt::argon2::generate", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("compare", Func::from(|v: Any| run("scrypt::argon2::compare", v.0)))?; + default.set("generate", Func::from(|v: Any| run("scrypt::argon2::generate", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/duration.rs b/lib/src/fnc/script/modules/surrealdb/functions/duration.rs new file mode 100644 index 00000000..881f42af --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/duration.rs @@ -0,0 +1,50 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("days")?; + module.add("hours")?; + module.add("mins")?; + module.add("secs")?; + module.add("weeks")?; + module.add("years")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("days", Func::from(|v: Any| run("duration::days", v.0)))?; + module.set("hours", Func::from(|v: Any| run("duration::hours", v.0)))?; + module.set("mins", Func::from(|v: Any| run("duration::mins", v.0)))?; + module.set("secs", Func::from(|v: Any| run("duration::secs", v.0)))?; + module.set("weeks", Func::from(|v: Any| run("duration::weeks", v.0)))?; + module.set("years", Func::from(|v: Any| run("duration::years", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("days", Func::from(|v: Any| run("duration::days", v.0)))?; + default.set("hours", Func::from(|v: Any| run("duration::hours", v.0)))?; + default.set("mins", Func::from(|v: Any| run("duration::mins", v.0)))?; + default.set("secs", Func::from(|v: Any| run("duration::secs", v.0)))?; + default.set("weeks", Func::from(|v: Any| run("duration::weeks", v.0)))?; + default.set("years", Func::from(|v: Any| run("duration::years", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/geo.rs b/lib/src/fnc/script/modules/surrealdb/functions/geo.rs new file mode 100644 index 00000000..abc9b8bd --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/geo.rs @@ -0,0 +1,50 @@ +use super::super::pkg; +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +mod hash; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("area")?; + module.add("bearing")?; + module.add("centroid")?; + module.add("distance")?; + module.add("hash")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("area", Func::from(|v: Any| run("geo::area", v.0)))?; + module.set("bearing", Func::from(|v: Any| run("geo::bearing", v.0)))?; + module.set("centroid", Func::from(|v: Any| run("geo::centroid", v.0)))?; + module.set("distance", Func::from(|v: Any| run("geo::distance", v.0)))?; + module.set("hash", pkg::(ctx, "hash"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("area", Func::from(|v: Any| run("geo::area", v.0)))?; + default.set("bearing", Func::from(|v: Any| run("geo::bearing", v.0)))?; + default.set("centroid", Func::from(|v: Any| run("geo::centroid", v.0)))?; + default.set("distance", Func::from(|v: Any| run("geo::distance", v.0)))?; + default.set("hash", pkg::(ctx, "hash"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/geo/hash.rs b/lib/src/fnc/script/modules/surrealdb/functions/geo/hash.rs new file mode 100644 index 00000000..fe51e6df --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/geo/hash.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("encode")?; + module.add("decode")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("encode", Func::from(|v: Any| run("geo::hash::encode", v.0)))?; + module.set("decode", Func::from(|v: Any| run("geo::hash::decode", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("encode", Func::from(|v: Any| run("geo::hash::encode", v.0)))?; + default.set("decode", Func::from(|v: Any| run("geo::hash::decode", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/http.rs b/lib/src/fnc/script/modules/surrealdb/functions/http.rs new file mode 100644 index 00000000..bea72043 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/http.rs @@ -0,0 +1,51 @@ +use super::fut; +use crate::sql::value::Value; +use js::Async; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("head")?; + module.add("get")?; + module.add("put")?; + module.add("post")?; + module.add("patch")?; + module.add("delete")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("head", Func::from(Async(|v: Any| fut("http::head", v.0))))?; + module.set("get", Func::from(Async(|v: Any| fut("http::get", v.0))))?; + module.set("put", Func::from(Async(|v: Any| fut("http::put", v.0))))?; + module.set("post", Func::from(Async(|v: Any| fut("http::post", v.0))))?; + module.set("patch", Func::from(Async(|v: Any| fut("http::patch", v.0))))?; + module.set("delete", Func::from(Async(|v: Any| fut("http::delete", v.0))))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("head", Func::from(Async(|v: Any| fut("http::head", v.0))))?; + default.set("get", Func::from(Async(|v: Any| fut("http::get", v.0))))?; + default.set("put", Func::from(Async(|v: Any| fut("http::put", v.0))))?; + default.set("post", Func::from(Async(|v: Any| fut("http::post", v.0))))?; + default.set("patch", Func::from(Async(|v: Any| fut("http::patch", v.0))))?; + default.set("delete", Func::from(Async(|v: Any| fut("http::delete", v.0))))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/is.rs b/lib/src/fnc/script/modules/surrealdb/functions/is.rs new file mode 100644 index 00000000..2baf484e --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/is.rs @@ -0,0 +1,68 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("alphanum")?; + module.add("alpha")?; + module.add("ascii")?; + module.add("domain")?; + module.add("email")?; + module.add("hexadecimal")?; + module.add("latitude")?; + module.add("longitude")?; + module.add("numeric")?; + module.add("semver")?; + module.add("url")?; + module.add("uuid")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?; + module.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?; + module.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?; + module.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?; + module.set("email", Func::from(|v: Any| run("is::email", v.0)))?; + module.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?; + module.set("latitude", Func::from(|v: Any| run("is::latitude", v.0)))?; + module.set("longitude", Func::from(|v: Any| run("is::longitude", v.0)))?; + module.set("numeric", Func::from(|v: Any| run("is::numeric", v.0)))?; + module.set("semver", Func::from(|v: Any| run("is::semver", v.0)))?; + module.set("url", Func::from(|v: Any| run("is::url", v.0)))?; + module.set("uuid", Func::from(|v: Any| run("is::uuid", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("alphanum", Func::from(|v: Any| run("is::alphanum", v.0)))?; + default.set("alpha", Func::from(|v: Any| run("is::alpha", v.0)))?; + default.set("ascii", Func::from(|v: Any| run("is::ascii", v.0)))?; + default.set("domain", Func::from(|v: Any| run("is::domain", v.0)))?; + default.set("email", Func::from(|v: Any| run("is::email", v.0)))?; + default.set("hexadecimal", Func::from(|v: Any| run("is::hexadecimal", v.0)))?; + default.set("latitude", Func::from(|v: Any| run("is::latitude", v.0)))?; + default.set("longitude", Func::from(|v: Any| run("is::longitude", v.0)))?; + default.set("numeric", Func::from(|v: Any| run("is::numeric", v.0)))?; + default.set("semver", Func::from(|v: Any| run("is::semver", v.0)))?; + default.set("url", Func::from(|v: Any| run("is::url", v.0)))?; + default.set("uuid", Func::from(|v: Any| run("is::uuid", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/math.rs b/lib/src/fnc/script/modules/surrealdb/functions/math.rs new file mode 100644 index 00000000..94075133 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/math.rs @@ -0,0 +1,104 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("abs")?; + module.add("bottom")?; + module.add("ceil")?; + module.add("fixed")?; + module.add("floor")?; + module.add("interquartile")?; + module.add("max")?; + module.add("mean")?; + module.add("median")?; + module.add("midhinge")?; + module.add("min")?; + module.add("mode")?; + module.add("nearestrank")?; + module.add("percentile")?; + module.add("pow")?; + module.add("product")?; + module.add("round")?; + module.add("spread")?; + module.add("sqrt")?; + module.add("stddev")?; + module.add("sum")?; + module.add("top")?; + module.add("trimean")?; + module.add("variance")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("abs", Func::from(|v: Any| run("math::abs", v.0)))?; + module.set("bottom", Func::from(|v: Any| run("math::bottom", v.0)))?; + module.set("ceil", Func::from(|v: Any| run("math::ceil", v.0)))?; + module.set("fixed", Func::from(|v: Any| run("math::fixed", v.0)))?; + module.set("floor", Func::from(|v: Any| run("math::floor", v.0)))?; + module.set("interquartile", Func::from(|v: Any| run("math::interquartile", v.0)))?; + module.set("max", Func::from(|v: Any| run("math::max", v.0)))?; + module.set("mean", Func::from(|v: Any| run("math::mean", v.0)))?; + module.set("median", Func::from(|v: Any| run("math::median", v.0)))?; + module.set("midhinge", Func::from(|v: Any| run("math::midhinge", v.0)))?; + module.set("min", Func::from(|v: Any| run("math::min", v.0)))?; + module.set("mode", Func::from(|v: Any| run("math::mode", v.0)))?; + module.set("nearestrank", Func::from(|v: Any| run("math::nearestrank", v.0)))?; + module.set("percentile", Func::from(|v: Any| run("math::percentile", v.0)))?; + module.set("pow", Func::from(|v: Any| run("math::pow", v.0)))?; + module.set("product", Func::from(|v: Any| run("math::product", v.0)))?; + module.set("round", Func::from(|v: Any| run("math::round", v.0)))?; + module.set("spread", Func::from(|v: Any| run("math::spread", v.0)))?; + module.set("sqrt", Func::from(|v: Any| run("math::sqrt", v.0)))?; + module.set("stddev", Func::from(|v: Any| run("math::stddev", v.0)))?; + module.set("sum", Func::from(|v: Any| run("math::sum", v.0)))?; + module.set("top", Func::from(|v: Any| run("math::top", v.0)))?; + module.set("trimean", Func::from(|v: Any| run("math::trimean", v.0)))?; + module.set("variance", Func::from(|v: Any| run("math::variance", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("abs", Func::from(|v: Any| run("math::abs", v.0)))?; + default.set("bottom", Func::from(|v: Any| run("math::bottom", v.0)))?; + default.set("ceil", Func::from(|v: Any| run("math::ceil", v.0)))?; + default.set("fixed", Func::from(|v: Any| run("math::fixed", v.0)))?; + default.set("floor", Func::from(|v: Any| run("math::floor", v.0)))?; + default.set("interquartile", Func::from(|v: Any| run("math::interquartile", v.0)))?; + default.set("max", Func::from(|v: Any| run("math::max", v.0)))?; + default.set("mean", Func::from(|v: Any| run("math::mean", v.0)))?; + default.set("median", Func::from(|v: Any| run("math::median", v.0)))?; + default.set("midhinge", Func::from(|v: Any| run("math::midhinge", v.0)))?; + default.set("min", Func::from(|v: Any| run("math::min", v.0)))?; + default.set("mode", Func::from(|v: Any| run("math::mode", v.0)))?; + default.set("nearestrank", Func::from(|v: Any| run("math::nearestrank", v.0)))?; + default.set("percentile", Func::from(|v: Any| run("math::percentile", v.0)))?; + default.set("pow", Func::from(|v: Any| run("math::pow", v.0)))?; + default.set("product", Func::from(|v: Any| run("math::product", v.0)))?; + default.set("round", Func::from(|v: Any| run("math::round", v.0)))?; + default.set("spread", Func::from(|v: Any| run("math::spread", v.0)))?; + default.set("sqrt", Func::from(|v: Any| run("math::sqrt", v.0)))?; + default.set("stddev", Func::from(|v: Any| run("math::stddev", v.0)))?; + default.set("sum", Func::from(|v: Any| run("math::sum", v.0)))?; + default.set("top", Func::from(|v: Any| run("math::top", v.0)))?; + default.set("trimean", Func::from(|v: Any| run("math::trimean", v.0)))?; + default.set("variance", Func::from(|v: Any| run("math::variance", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/meta.rs b/lib/src/fnc/script/modules/surrealdb/functions/meta.rs new file mode 100644 index 00000000..ceedf073 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/meta.rs @@ -0,0 +1,41 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("id")?; + module.add("table")?; + module.add("tb")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("id", Func::from(|v: Any| run("meta::id", v.0)))?; + module.set("table", Func::from(|v: Any| run("meta::table", v.0)))?; + module.set("tb", Func::from(|v: Any| run("meta::tb", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("id", Func::from(|v: Any| run("meta::id", v.0)))?; + default.set("table", Func::from(|v: Any| run("meta::table", v.0)))?; + default.set("tb", Func::from(|v: Any| run("meta::tb", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/mod.rs b/lib/src/fnc/script/modules/surrealdb/functions/mod.rs new file mode 100644 index 00000000..b5a61f64 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/mod.rs @@ -0,0 +1,122 @@ +use super::pkg; +use crate::ctx::Context; +use crate::fnc; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +mod array; +mod crypto; +mod duration; +mod geo; +mod http; +mod is; +mod math; +mod meta; +mod parse; +mod rand; +mod session; +mod string; +mod time; +mod r#type; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("array")?; + module.add("count")?; + module.add("crypto")?; + module.add("duration")?; + module.add("geo")?; + module.add("http")?; + module.add("is")?; + module.add("math")?; + module.add("meta")?; + module.add("not")?; + module.add("parse")?; + module.add("rand")?; + module.add("string")?; + module.add("time")?; + module.add("type")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("array", pkg::(ctx, "array"))?; + module.set("count", Func::from(|v: Any| run("count", v.0)))?; + module.set("crypto", pkg::(ctx, "crypto"))?; + module.set("duration", pkg::(ctx, "duration"))?; + module.set("geo", pkg::(ctx, "geo"))?; + module.set("http", pkg::(ctx, "http"))?; + module.set("is", pkg::(ctx, "is"))?; + module.set("math", pkg::(ctx, "math"))?; + module.set("meta", pkg::(ctx, "meta"))?; + module.set("not", Func::from(|v: Any| run("not", v.0)))?; + module.set("parse", pkg::(ctx, "parse"))?; + module.set("rand", pkg::(ctx, "rand"))?; + module.set("string", pkg::(ctx, "string"))?; + module.set("time", pkg::(ctx, "time"))?; + module.set("type", pkg::(ctx, "type"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("array", pkg::(ctx, "array"))?; + default.set("count", Func::from(|v: Any| run("count", v.0)))?; + default.set("crypto", pkg::(ctx, "crypto"))?; + default.set("duration", pkg::(ctx, "duration"))?; + default.set("geo", pkg::(ctx, "geo"))?; + default.set("http", pkg::(ctx, "http"))?; + default.set("is", pkg::(ctx, "is"))?; + default.set("math", pkg::(ctx, "math"))?; + default.set("meta", pkg::(ctx, "meta"))?; + default.set("not", Func::from(|v: Any| run("not", v.0)))?; + default.set("parse", pkg::(ctx, "parse"))?; + default.set("rand", pkg::(ctx, "rand"))?; + default.set("string", pkg::(ctx, "string"))?; + default.set("time", pkg::(ctx, "time"))?; + default.set("type", pkg::(ctx, "type"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} + +fn run(name: &str, args: Vec) -> Result { + // Create a default context + let ctx = Context::background(); + // Process the called function + let res = fnc::synchronous(&ctx, name, args); + // Convert any response error + res.map_err(|err| js::Error::Exception { + message: err.to_string(), + file: String::from(""), + line: -1, + stack: String::from(""), + }) +} + +async fn fut(name: &str, args: Vec) -> Result { + // Create a default context + let ctx = Context::background(); + // Process the called function + let res = fnc::asynchronous(&ctx, name, args).await; + // Convert any response error + res.map_err(|err| js::Error::Exception { + message: err.to_string(), + file: String::from(""), + line: -1, + stack: String::from(""), + }) +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/parse.rs b/lib/src/fnc/script/modules/surrealdb/functions/parse.rs new file mode 100644 index 00000000..5f2135ac --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/parse.rs @@ -0,0 +1,36 @@ +use super::super::pkg; +use js::Created; +use js::Ctx; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Result; + +mod email; +mod url; + +pub struct Package; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("email")?; + module.add("url")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("email", pkg::(ctx, "email"))?; + module.set("url", pkg::(ctx, "url"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("email", pkg::(ctx, "email"))?; + default.set("url", pkg::(ctx, "url"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/parse/email.rs b/lib/src/fnc/script/modules/surrealdb/functions/parse/email.rs new file mode 100644 index 00000000..d80e9cbd --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/parse/email.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("host")?; + module.add("user")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("host", Func::from(|v: Any| run("parse::email::host", v.0)))?; + module.set("user", Func::from(|v: Any| run("parse::email::user", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("host", Func::from(|v: Any| run("parse::email::host", v.0)))?; + default.set("user", Func::from(|v: Any| run("parse::email::user", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/parse/url.rs b/lib/src/fnc/script/modules/surrealdb/functions/parse/url.rs new file mode 100644 index 00000000..260a31a5 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/parse/url.rs @@ -0,0 +1,53 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("domain")?; + module.add("fragment")?; + module.add("host")?; + module.add("path")?; + module.add("port")?; + module.add("query")?; + module.add("scheme")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("domain", Func::from(|v: Any| run("parse::url::domain", v.0)))?; + module.set("fragment", Func::from(|v: Any| run("parse::url::fragment", v.0)))?; + module.set("host", Func::from(|v: Any| run("parse::url::host", v.0)))?; + module.set("path", Func::from(|v: Any| run("parse::url::path", v.0)))?; + module.set("port", Func::from(|v: Any| run("parse::url::port", v.0)))?; + module.set("query", Func::from(|v: Any| run("parse::url::query", v.0)))?; + module.set("scheme", Func::from(|v: Any| run("parse::url::scheme", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("domain", Func::from(|v: Any| run("parse::url::domain", v.0)))?; + default.set("fragment", Func::from(|v: Any| run("parse::url::fragment", v.0)))?; + default.set("host", Func::from(|v: Any| run("parse::url::host", v.0)))?; + default.set("path", Func::from(|v: Any| run("parse::url::path", v.0)))?; + default.set("port", Func::from(|v: Any| run("parse::url::port", v.0)))?; + default.set("query", Func::from(|v: Any| run("parse::url::query", v.0)))?; + default.set("scheme", Func::from(|v: Any| run("parse::url::scheme", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/rand.rs b/lib/src/fnc/script/modules/surrealdb/functions/rand.rs new file mode 100644 index 00000000..dd77ce5f --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/rand.rs @@ -0,0 +1,59 @@ +use super::super::pkg; +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +mod uuid; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("bool")?; + module.add("enum")?; + module.add("float")?; + module.add("guid")?; + module.add("int")?; + module.add("string")?; + module.add("time")?; + module.add("uuid")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("bool", Func::from(|v: Any| run("rand::bool", v.0)))?; + module.set("enum", Func::from(|v: Any| run("rand::enum", v.0)))?; + module.set("float", Func::from(|v: Any| run("rand::float", v.0)))?; + module.set("guid", Func::from(|v: Any| run("rand::guid", v.0)))?; + module.set("int", Func::from(|v: Any| run("rand::int", v.0)))?; + module.set("string", Func::from(|v: Any| run("rand::string", v.0)))?; + module.set("time", Func::from(|v: Any| run("rand::time", v.0)))?; + module.set("uuid", pkg::(ctx, "uuid"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("bool", Func::from(|v: Any| run("rand::bool", v.0)))?; + default.set("enum", Func::from(|v: Any| run("rand::enum", v.0)))?; + default.set("float", Func::from(|v: Any| run("rand::float", v.0)))?; + default.set("guid", Func::from(|v: Any| run("rand::guid", v.0)))?; + default.set("int", Func::from(|v: Any| run("rand::int", v.0)))?; + default.set("string", Func::from(|v: Any| run("rand::string", v.0)))?; + default.set("time", Func::from(|v: Any| run("rand::time", v.0)))?; + default.set("uuid", pkg::(ctx, "uuid"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/rand/uuid.rs b/lib/src/fnc/script/modules/surrealdb/functions/rand/uuid.rs new file mode 100644 index 00000000..c47e85b3 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/rand/uuid.rs @@ -0,0 +1,38 @@ +use super::super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("v4")?; + module.add("v7")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("v4", Func::from(|v: Any| run("rand::uuid::v4", v.0)))?; + module.set("v7", Func::from(|v: Any| run("rand::uuid::v7", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("v4", Func::from(|v: Any| run("rand::uuid::v4", v.0)))?; + default.set("v7", Func::from(|v: Any| run("rand::uuid::v7", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/session.rs b/lib/src/fnc/script/modules/surrealdb/functions/session.rs new file mode 100644 index 00000000..81556b23 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/session.rs @@ -0,0 +1,56 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("db")?; + module.add("id")?; + module.add("ip")?; + module.add("ns")?; + module.add("origin")?; + module.add("sc")?; + module.add("sd")?; + module.add("token")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("db", Func::from(|v: Any| run("session::db", v.0)))?; + module.set("id", Func::from(|v: Any| run("session::id", v.0)))?; + module.set("ip", Func::from(|v: Any| run("session::ip", v.0)))?; + module.set("ns", Func::from(|v: Any| run("session::ns", v.0)))?; + module.set("origin", Func::from(|v: Any| run("session::origin", v.0)))?; + module.set("sc", Func::from(|v: Any| run("session::sc", v.0)))?; + module.set("sd", Func::from(|v: Any| run("session::sd", v.0)))?; + module.set("token", Func::from(|v: Any| run("session::token", v.0)))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("db", Func::from(|v: Any| run("session::db", v.0)))?; + default.set("id", Func::from(|v: Any| run("session::id", v.0)))?; + default.set("ip", Func::from(|v: Any| run("session::ip", v.0)))?; + default.set("ns", Func::from(|v: Any| run("session::ns", v.0)))?; + default.set("origin", Func::from(|v: Any| run("session::origin", v.0)))?; + default.set("sc", Func::from(|v: Any| run("session::sc", v.0)))?; + default.set("sd", Func::from(|v: Any| run("session::sd", v.0)))?; + default.set("token", Func::from(|v: Any| run("session::token", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/string.rs b/lib/src/fnc/script/modules/surrealdb/functions/string.rs new file mode 100644 index 00000000..eeceaa8b --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/string.rs @@ -0,0 +1,77 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("concat")?; + module.add("endsWith")?; + module.add("join")?; + module.add("length")?; + module.add("lowercase")?; + module.add("repeat")?; + module.add("replace")?; + module.add("reverse")?; + module.add("slice")?; + module.add("slug")?; + module.add("split")?; + module.add("startsWith")?; + module.add("trim")?; + module.add("uppercase")?; + module.add("words")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("concat", Func::from(|v: Any| run("string::concat", v.0)))?; + module.set("endsWith", Func::from(|v: Any| run("string::endsWith", v.0)))?; + module.set("join", Func::from(|v: Any| run("string::join", v.0)))?; + module.set("length", Func::from(|v: Any| run("string::length", v.0)))?; + module.set("lowercase", Func::from(|v: Any| run("string::lowercase", v.0)))?; + module.set("repeat", Func::from(|v: Any| run("string::repeat", v.0)))?; + module.set("replace", Func::from(|v: Any| run("string::replace", v.0)))?; + module.set("reverse", Func::from(|v: Any| run("string::reverse", v.0)))?; + module.set("slice", Func::from(|v: Any| run("string::slice", v.0)))?; + module.set("slug", Func::from(|v: Any| run("string::slug", v.0)))?; + module.set("split", Func::from(|v: Any| run("string::split", v.0)))?; + module.set("startsWith", Func::from(|v: Any| run("string::startsWith", v.0)))?; + module.set("trim", Func::from(|v: Any| run("string::trim", v.0)))?; + module.set("uppercase", Func::from(|v: Any| run("string::uppercase", v.0)))?; + module.set("words", Func::from(|v: Any| run("string::words", v.0)))?; + // Set default export + let default = Object::new(ctx)?; + default.set("concat", Func::from(|v: Any| run("string::concat", v.0)))?; + default.set("endsWith", Func::from(|v: Any| run("string::endsWith", v.0)))?; + default.set("join", Func::from(|v: Any| run("string::join", v.0)))?; + default.set("length", Func::from(|v: Any| run("string::length", v.0)))?; + default.set("lowercase", Func::from(|v: Any| run("string::lowercase", v.0)))?; + default.set("repeat", Func::from(|v: Any| run("string::repeat", v.0)))?; + default.set("replace", Func::from(|v: Any| run("string::replace", v.0)))?; + default.set("reverse", Func::from(|v: Any| run("string::reverse", v.0)))?; + default.set("slice", Func::from(|v: Any| run("string::slice", v.0)))?; + default.set("slug", Func::from(|v: Any| run("string::slug", v.0)))?; + default.set("split", Func::from(|v: Any| run("string::split", v.0)))?; + default.set("startsWith", Func::from(|v: Any| run("string::startsWith", v.0)))?; + default.set("trim", Func::from(|v: Any| run("string::trim", v.0)))?; + default.set("uppercase", Func::from(|v: Any| run("string::uppercase", v.0)))?; + default.set("words", Func::from(|v: Any| run("string::words", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/time.rs b/lib/src/fnc/script/modules/surrealdb/functions/time.rs new file mode 100644 index 00000000..0681c1a3 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/time.rs @@ -0,0 +1,77 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("day")?; + module.add("floor")?; + module.add("group")?; + module.add("hour")?; + module.add("mins")?; + module.add("month")?; + module.add("nano")?; + module.add("now")?; + module.add("round")?; + module.add("secs")?; + module.add("unix")?; + module.add("wday")?; + module.add("week")?; + module.add("yday")?; + module.add("year")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("day", Func::from(|v: Any| run("time::day", v.0)))?; + module.set("floor", Func::from(|v: Any| run("time::floor", v.0)))?; + module.set("group", Func::from(|v: Any| run("time::group", v.0)))?; + module.set("hour", Func::from(|v: Any| run("time::hour", v.0)))?; + module.set("mins", Func::from(|v: Any| run("time::mins", v.0)))?; + module.set("month", Func::from(|v: Any| run("time::month", v.0)))?; + module.set("nano", Func::from(|v: Any| run("time::nano", v.0)))?; + module.set("now", Func::from(|v: Any| run("time::now", v.0)))?; + module.set("round", Func::from(|v: Any| run("time::round", v.0)))?; + module.set("secs", Func::from(|v: Any| run("time::secs", v.0)))?; + module.set("unix", Func::from(|v: Any| run("time::unix", v.0)))?; + module.set("wday", Func::from(|v: Any| run("time::wday", v.0)))?; + module.set("week", Func::from(|v: Any| run("time::week", v.0)))?; + module.set("yday", Func::from(|v: Any| run("time::yday", v.0)))?; + module.set("year", Func::from(|v: Any| run("time::year", v.0)))?; + // Set default export + let default = Object::new(ctx)?; + default.set("day", Func::from(|v: Any| run("time::day", v.0)))?; + default.set("floor", Func::from(|v: Any| run("time::floor", v.0)))?; + default.set("group", Func::from(|v: Any| run("time::group", v.0)))?; + default.set("hour", Func::from(|v: Any| run("time::hour", v.0)))?; + default.set("mins", Func::from(|v: Any| run("time::mins", v.0)))?; + default.set("month", Func::from(|v: Any| run("time::month", v.0)))?; + default.set("nano", Func::from(|v: Any| run("time::nano", v.0)))?; + default.set("now", Func::from(|v: Any| run("time::now", v.0)))?; + default.set("round", Func::from(|v: Any| run("time::round", v.0)))?; + default.set("secs", Func::from(|v: Any| run("time::secs", v.0)))?; + default.set("unix", Func::from(|v: Any| run("time::unix", v.0)))?; + default.set("wday", Func::from(|v: Any| run("time::wday", v.0)))?; + default.set("week", Func::from(|v: Any| run("time::week", v.0)))?; + default.set("yday", Func::from(|v: Any| run("time::yday", v.0)))?; + default.set("year", Func::from(|v: Any| run("time::year", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/functions/type.rs b/lib/src/fnc/script/modules/surrealdb/functions/type.rs new file mode 100644 index 00000000..640c0dd9 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/functions/type.rs @@ -0,0 +1,68 @@ +use super::run; +use crate::sql::value::Value; +use js::Created; +use js::Ctx; +use js::Func; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Rest; +use js::Result; + +pub struct Package; + +type Any = Rest; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("bool")?; + module.add("datetime")?; + module.add("decimal")?; + module.add("duration")?; + module.add("float")?; + module.add("int")?; + module.add("number")?; + module.add("point")?; + module.add("regex")?; + module.add("string")?; + module.add("table")?; + module.add("thing")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("bool", Func::from(|v: Any| run("type::bool", v.0)))?; + module.set("datetime", Func::from(|v: Any| run("type::datetime", v.0)))?; + module.set("decimal", Func::from(|v: Any| run("type::decimal", v.0)))?; + module.set("duration", Func::from(|v: Any| run("type::duration", v.0)))?; + module.set("float", Func::from(|v: Any| run("type::float", v.0)))?; + module.set("int", Func::from(|v: Any| run("type::int", v.0)))?; + module.set("number", Func::from(|v: Any| run("type::number", v.0)))?; + module.set("point", Func::from(|v: Any| run("type::point", v.0)))?; + module.set("regex", Func::from(|v: Any| run("type::regex", v.0)))?; + module.set("string", Func::from(|v: Any| run("type::string", v.0)))?; + module.set("table", Func::from(|v: Any| run("type::table", v.0)))?; + module.set("thing", Func::from(|v: Any| run("type::thing", v.0)))?; + // Set default export + let default = Object::new(ctx)?; + default.set("bool", Func::from(|v: Any| run("type::bool", v.0)))?; + default.set("datetime", Func::from(|v: Any| run("type::datetime", v.0)))?; + default.set("decimal", Func::from(|v: Any| run("type::decimal", v.0)))?; + default.set("duration", Func::from(|v: Any| run("type::duration", v.0)))?; + default.set("float", Func::from(|v: Any| run("type::float", v.0)))?; + default.set("int", Func::from(|v: Any| run("type::int", v.0)))?; + default.set("number", Func::from(|v: Any| run("type::number", v.0)))?; + default.set("point", Func::from(|v: Any| run("type::point", v.0)))?; + default.set("regex", Func::from(|v: Any| run("type::regex", v.0)))?; + default.set("string", Func::from(|v: Any| run("type::string", v.0)))?; + default.set("table", Func::from(|v: Any| run("type::table", v.0)))?; + default.set("thing", Func::from(|v: Any| run("type::thing", v.0)))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} diff --git a/lib/src/fnc/script/modules/surrealdb/mod.rs b/lib/src/fnc/script/modules/surrealdb/mod.rs new file mode 100644 index 00000000..90ad24ac --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb/mod.rs @@ -0,0 +1,42 @@ +use js::Created; +use js::Ctx; +use js::Loaded; +use js::Module; +use js::ModuleDef; +use js::Native; +use js::Object; +use js::Result; +use js::Value; + +mod functions; + +pub struct Package; + +impl ModuleDef for Package { + fn load<'js>(_ctx: Ctx<'js>, module: &Module<'js, Created>) -> Result<()> { + module.add("default")?; + module.add("functions")?; + module.add("version")?; + Ok(()) + } + + fn eval<'js>(ctx: Ctx<'js>, module: &Module<'js, Loaded>) -> Result<()> { + // Set specific exports + module.set("functions", pkg::(ctx, "functions"))?; + module.set("version", env!("CARGO_PKG_VERSION"))?; + // Set default exports + let default = Object::new(ctx)?; + default.set("functions", pkg::(ctx, "functions"))?; + default.set("version", env!("CARGO_PKG_VERSION"))?; + module.set("default", default)?; + // Everything ok + Ok(()) + } +} + +fn pkg<'js, D>(ctx: Ctx<'js>, name: &str) -> Result> +where + D: ModuleDef, +{ + Module::new_def::(ctx, name)?.eval()?.get::<_, js::Value>("default") +} diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index 6b515f10..a8036124 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -239,8 +239,8 @@ fn function_array(i: &str) -> IResult<&str, &str> { tag("array::complement"), tag("array::concat"), tag("array::difference"), - tag("array::flatten"), tag("array::distinct"), + tag("array::flatten"), tag("array::insert"), tag("array::intersect"), tag("array::len"), @@ -372,8 +372,8 @@ fn function_parse(i: &str) -> IResult<&str, &str> { tag("parse::url::domain"), tag("parse::url::fragment"), tag("parse::url::host"), - tag("parse::url::port"), tag("parse::url::path"), + tag("parse::url::port"), tag("parse::url::query"), tag("parse::url::scheme"), ))(i)