diff --git a/.gitignore b/.gitignore index 7445558..7e920a4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ /.idea /.bruno -**/target/ \ No newline at end of file +**/target/ + +# surrealdb-migrations configuration file, an example will be provided +.surrealdb \ No newline at end of file diff --git a/.surrealdb.example b/.surrealdb.example new file mode 100644 index 0000000..5828f1f --- /dev/null +++ b/.surrealdb.example @@ -0,0 +1,10 @@ +[core] + path = "./surrealdb-migrations" + schema = "full" + +[db] + address = "ws://localhost:8000" + username = "root" + password = "root" + ns = "kromer" + db = "kromer" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 036712a..f4f5ebd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -267,19 +267,13 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.21.0" +version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" dependencies = [ "gimli", ] -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "adler2" version = "2.0.0" @@ -368,56 +362,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anstream" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" - -[[package]] -name = "anstyle-parse" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" -dependencies = [ - "windows-sys 0.59.0", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" -dependencies = [ - "anstyle", - "once_cell", - "windows-sys 0.59.0", -] - [[package]] name = "any_ascii" version = "0.3.2" @@ -646,17 +590,17 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "backtrace" -version = "0.3.71" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" dependencies = [ "addr2line", - "cc", "cfg-if", "libc", - "miniz_oxide 0.7.4", + "miniz_oxide", "object", "rustc-demangle", + "windows-targets", ] [[package]] @@ -985,15 +929,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "chrono-human-duration" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19283df3a144dfdeee6568e42ad7f0939d3c261bcdfe954b1a1098f6f7c1b908" -dependencies = [ - "chrono", -] - [[package]] name = "ciborium" version = "0.2.2" @@ -1039,8 +974,8 @@ checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", "bitflags 1.3.2", - "clap_derive 3.2.25", - "clap_lex 0.2.4", + "clap_derive", + "clap_lex", "indexmap 1.9.3", "once_cell", "strsim 0.10.0", @@ -1048,28 +983,6 @@ dependencies = [ "textwrap", ] -[[package]] -name = "clap" -version = "4.5.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8eb5e908ef3a6efbe1ed62520fb7287959888c88485abe072543190ecc66783" -dependencies = [ - "clap_builder", - "clap_derive 4.5.24", -] - -[[package]] -name = "clap_builder" -version = "4.5.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96b01801b5fc6a0a232407abc821660c9c6d25a1cafc0d4f85f29fb8d9afc121" -dependencies = [ - "anstream", - "anstyle", - "clap_lex 0.7.4", - "strsim 0.11.1", -] - [[package]] name = "clap_derive" version = "3.2.25" @@ -1083,18 +996,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "clap_derive" -version = "4.5.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" -dependencies = [ - "heck 0.5.0", - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "clap_lex" version = "0.2.4" @@ -1104,68 +1005,6 @@ dependencies = [ "os_str_bytes", ] -[[package]] -name = "clap_lex" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" - -[[package]] -name = "cli-table" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adfbb116d9e2c4be7011360d0c0bee565712c11e969c9609b25b619366dc379d" -dependencies = [ - "cli-table-derive", - "csv", - "termcolor", - "unicode-width", -] - -[[package]] -name = "cli-table-derive" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e83a93253aaae7c74eb7428ce4faa6e219ba94886908048888701819f82fb94" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "color-eyre" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5" -dependencies = [ - "backtrace", - "color-spantrace", - "eyre", - "indenter", - "once_cell", - "owo-colors", - "tracing-error", -] - -[[package]] -name = "color-spantrace" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2" -dependencies = [ - "once_cell", - "owo-colors", - "tracing-core", - "tracing-error", -] - -[[package]] -name = "colorchoice" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" - [[package]] name = "concurrent-queue" version = "2.5.0" @@ -1175,26 +1014,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "const-random" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" -dependencies = [ - "const-random-macro", -] - -[[package]] -name = "const-random-macro" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" -dependencies = [ - "getrandom", - "once_cell", - "tiny-keccak", -] - [[package]] name = "constant_time_eq" version = "0.3.1" @@ -1207,15 +1026,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" -[[package]] -name = "convert_case" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "cookie" version = "0.16.2" @@ -1301,27 +1111,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "csv" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf" -dependencies = [ - "csv-core", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "csv-core" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" -dependencies = [ - "memchr", -] - [[package]] name = "darling" version = "0.20.10" @@ -1392,7 +1181,7 @@ version = "0.99.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ - "convert_case 0.4.0", + "convert_case", "proc-macro2", "quote", "rustc_version", @@ -1405,15 +1194,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" -[[package]] -name = "diffy" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d3041965b7a63e70447ec818a46b1e5297f7fcae3058356d226c02750c4e6cb" -dependencies = [ - "nu-ansi-term 0.50.1", -] - [[package]] name = "digest" version = "0.10.7" @@ -1457,15 +1237,6 @@ dependencies = [ "syn 2.0.96", ] -[[package]] -name = "dlv-list" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" -dependencies = [ - "const-random", -] - [[package]] name = "dmp" version = "0.2.0" @@ -1554,16 +1325,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "eyre" -version = "0.6.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" -dependencies = [ - "indenter", - "once_cell", -] - [[package]] name = "fastrand" version = "2.3.0" @@ -1583,7 +1344,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" dependencies = [ "crc32fast", - "miniz_oxide 0.8.3", + "miniz_oxide", ] [[package]] @@ -1607,12 +1368,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "fs_extra" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" - [[package]] name = "fst" version = "0.4.7" @@ -1817,9 +1572,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "h2" @@ -2243,31 +1998,6 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8a5a9a0ff0086c7a148acb942baaabeadf9504d10400b5a05645853729b9cd2" -[[package]] -name = "include_dir" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" -dependencies = [ - "include_dir_macros", -] - -[[package]] -name = "include_dir_macros" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" -dependencies = [ - "proc-macro2", - "quote", -] - -[[package]] -name = "indenter" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" - [[package]] name = "indexmap" version = "1.9.3" @@ -2305,12 +2035,6 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" -[[package]] -name = "is_terminal_polyfill" -version = "1.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" - [[package]] name = "itertools" version = "0.10.5" @@ -2338,15 +2062,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.14" @@ -2404,7 +2119,6 @@ dependencies = [ "serde", "serde_json", "surrealdb", - "surrealdb-migrations", "thiserror 2.0.11", "tokio", "tracing", @@ -2627,15 +2341,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "miniz_oxide" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" -dependencies = [ - "adler", -] - [[package]] name = "miniz_oxide" version = "0.8.3" @@ -2680,7 +2385,7 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bddcd3bf5144b6392de80e04c347cd7fab2508f6df16a85fc496ecd5cec39bc" dependencies = [ - "clap 3.2.25", + "clap", "rand", ] @@ -2756,15 +2461,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "nu-ansi-term" -version = "0.50.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4a28e057d01f97e61255210fcff094d74ed0466038633e95017f5beb68e4399" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "num-bigint" version = "0.4.6" @@ -2821,9 +2517,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.2" +version = "0.36.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" dependencies = [ "memchr", ] @@ -2855,16 +2551,6 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" -[[package]] -name = "ordered-multimap" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" -dependencies = [ - "dlv-list", - "hashbrown 0.14.5", -] - [[package]] name = "os_str_bytes" version = "6.6.1" @@ -2877,12 +2563,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" -[[package]] -name = "owo-colors" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" - [[package]] name = "parking" version = "2.2.1" @@ -3590,17 +3270,6 @@ dependencies = [ "smallvec", ] -[[package]] -name = "rust-ini" -version = "0.21.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e310ef0e1b6eeb79169a1171daf9abcb87a2e17c03bee2c4bb100b55c75409f" -dependencies = [ - "cfg-if", - "ordered-multimap", - "trim-in-place", -] - [[package]] name = "rust-stemmers" version = "1.2.0" @@ -4010,15 +3679,6 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -[[package]] -name = "sqlparser" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a875d8cd437cc8a97e9aeaeea352ec9a19aea99c23e9effb17757291de80b08" -dependencies = [ - "log", -] - [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -4216,7 +3876,6 @@ dependencies = [ "rayon", "reblessive", "regex", - "reqwest", "revision", "ring", "rmpv", @@ -4259,32 +3918,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "surrealdb-migrations" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e19395851b0d9b9e3cc405e5a7b2ff7ea0dd16981b5fac4865b00875dcca7b1" -dependencies = [ - "chrono", - "chrono-human-duration", - "clap 4.5.26", - "cli-table", - "color-eyre", - "convert_case 0.6.0", - "diffy", - "fs_extra", - "include_dir", - "itertools 0.14.0", - "names", - "regex", - "rust-ini", - "serde", - "serde_json", - "sqlparser", - "surrealdb", - "tokio", -] - [[package]] name = "syn" version = "1.0.109" @@ -4631,16 +4264,6 @@ dependencies = [ "valuable", ] -[[package]] -name = "tracing-error" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db" -dependencies = [ - "tracing", - "tracing-subscriber", -] - [[package]] name = "tracing-log" version = "0.2.0" @@ -4658,7 +4281,7 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" dependencies = [ - "nu-ansi-term 0.46.0", + "nu-ansi-term", "sharded-slab", "smallvec", "thread_local", @@ -4677,12 +4300,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "trim-in-place" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343e926fc669bc8cde4fa3129ab681c63671bae288b1f1081ceee6d9d37904fc" - [[package]] name = "try-lock" version = "0.2.5" @@ -4770,12 +4387,6 @@ dependencies = [ "unicode-script", ] -[[package]] -name = "unicode-segmentation" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" - [[package]] name = "unicode-width" version = "0.1.14" @@ -4829,12 +4440,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - [[package]] name = "uuid" version = "1.12.0" diff --git a/Cargo.toml b/Cargo.toml index 97c630e..14558c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,3 @@ actix-ws = "0.3.0" futures-util = "0.3.31" regex = "1.11.1" once_cell = "1.20.2" -surrealdb-migrations = "2.1.0" diff --git a/README.md b/README.md index a8f52e8..1d21337 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # Kromer -Centralized economy server for Minecraft servers. \ No newline at end of file +Centralized economy server for Minecraft servers. + +# Development + +Kromer is currently written in Rust, uses SurrealDB for a database, and is deployed using Docker. + +## Rust and Cargo setup + +Ensure that your Rust toolchain is [installed](https://www.rust-lang.org/tools/install) and [properly configured](https://rust-lang.github.io/rustup/concepts/toolchains.html). + +Running `cargo build` or `cargo run` will automatically pull the dependencies, and compile a binary executable. + +To simplify database schema maintenance, the cargo package `surrealdb-migrations` is used. To work with applying migrations manually, run `cargo install surrealdb-migrations`. You'll need to make sure that your Cargo binaries directory is correctly setup with your PATH variables (typically, this looks like adding `~/.cargo/bin` to your PATH). + + +## SurrealDB + +The easiest way to get started with SurrealDB is to use Docker Compose, you can find a starter `docker-compose.yml` [in this repository](https://github.com/surrealdb/docker.surrealdb.com/blob/main/docker-compose.yml). + +After you have Surreal running, you should configure your `.env` file to match your docker configuration. If you are working with migrations manually, create the file `.surrealdb` in the root directory based off the example. The executable will let you know if there are any configuration errors. + +The database can be inspected by visting https://surrealist.app and connecting, then selecting the appropriately named namespace and database per your compose/env variables. + +## SurrealQL Extension + +It is recommended to use the extension [`SurrealQL`](https://marketplace.visualstudio.com/items?itemName=surrealdb.surrealql) inside of VSCode when editing SurrealQL files. \ No newline at end of file diff --git a/surrealdb-migrations/events/transfer_balance.surql b/surrealdb-migrations/events/transfer_balance.surql new file mode 100644 index 0000000..f0627ec --- /dev/null +++ b/surrealdb-migrations/events/transfer_balance.surql @@ -0,0 +1,6 @@ +DEFINE EVENT OVERWRITE transfer_balance ON transaction WHEN $event = 'CREATE' THEN { +LET $from = $after.from; +LET $to = $after.to; +LET $amount = $after.amount; +RETURN fn::transfer_balance($from, $to, $amount); +}; \ No newline at end of file diff --git a/surrealdb-migrations/migrations/definitions/_initial.json b/surrealdb-migrations/migrations/definitions/_initial.json new file mode 100644 index 0000000..f553e77 --- /dev/null +++ b/surrealdb-migrations/migrations/definitions/_initial.json @@ -0,0 +1 @@ +{"schemas":"DEFINE FUNCTION OVERWRITE fn::create_wallet($initial_balance: option) {\nLET $address = rand::string(10).lowercase();\nLET $password = rand::string(16);\nLET $hash = crypto::argon2::generate($password);\nLET $wallet = (CREATE wallet CONTENT { address: $address, balance: $initial_balance OR 0f, hash: $hash });\nRETURN { address: $address, password: $password, wallet: $wallet.first() };\n} PERMISSIONS FULL;\n\nDEFINE FUNCTION OVERWRITE fn::transfer_balance($from: record, $to: record | record, $amount: float) {\nUPDATE $from SET balance -= $amount;\nUPDATE $from SET total_out += $amount;\nRETURN IF record::tb($to) == 'name' {\nLET $owner = $to.owner;\nUPDATE $owner SET balance += $amount;\nUPDATE $owner SET total_in += $amount;\n} ELSE {\nUPDATE $to SET balance += $amount;\nUPDATE $to SET total_in += $amount;\n};\n} PERMISSIONS FULL;\nOPTION IMPORT;\n\nDEFINE TABLE OVERWRITE name TYPE NORMAL SCHEMALESS PERMISSIONS NONE;\n\nDEFINE FIELD OVERWRITE last_transfered ON name TYPE option PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE last_updated ON name TYPE datetime DEFAULT time::now() PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE name ON name TYPE string PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE original_owner ON name TYPE option> PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE owner ON name TYPE record PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE registered ON name TYPE datetime DEFAULT time::now() PERMISSIONS FULL;\nDEFINE TABLE OVERWRITE player TYPE ANY SCHEMALESS PERMISSIONS NONE;\n\nDEFINE FIELD OVERWRITE joined_at ON player TYPE datetime DEFAULT time::now() PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE name ON player TYPE string PERMISSIONS FULL;\nDEFINE TABLE OVERWRITE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD OVERWRITE script_name ON script_migration TYPE string;\nDEFINE FIELD OVERWRITE executed_at ON script_migration TYPE datetime VALUE time::now() READONLY;\nOPTION IMPORT;\n\nDEFINE TABLE OVERWRITE transaction TYPE NORMAL SCHEMAFULL PERMISSIONS NONE;\n\nDEFINE FIELD OVERWRITE amount ON transaction TYPE float PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE from ON transaction TYPE record PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE metadata ON transaction TYPE option PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE timestamp ON transaction TYPE datetime DEFAULT time::now() PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE to ON transaction TYPE record | record PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE transaction_type ON transaction TYPE 'unknown' | 'mined' | 'name_purchase' | 'transfer' PERMISSIONS FULL;\n\n\nDEFINE EVENT OVERWRITE transfer_balance ON transaction WHEN $event = 'CREATE' THEN {\nLET $from = $after.from;\nLET $to = $after.to;\nLET $amount = $after.amount;\nRETURN fn::transfer_balance($from, $to, $amount);\n};\n\nDEFINE TABLE OVERWRITE wallet TYPE NORMAL SCHEMAFULL PERMISSIONS NONE;\n\nDEFINE FIELD OVERWRITE address ON wallet TYPE string PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE balance ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE created_at ON wallet TYPE datetime DEFAULT time::now() PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE hash ON wallet TYPE string PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE is_shared ON wallet TYPE bool DEFAULT false PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE total_in ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL;\nDEFINE FIELD OVERWRITE total_out ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL;\n\nDEFINE TABLE OVERWRITE owns TYPE RELATION IN player OUT wallet SCHEMAFULL PERMISSIONS NONE;","events":"DEFINE EVENT OVERWRITE transfer_balance ON transaction WHEN $event = 'CREATE' THEN {\nLET $from = $after.from;\nLET $to = $after.to;\nLET $amount = $after.amount;\nRETURN fn::transfer_balance($from, $to, $amount);\n};"} \ No newline at end of file diff --git a/surrealdb-migrations/schemas/functions.surql b/surrealdb-migrations/schemas/functions.surql new file mode 100644 index 0000000..0928a11 --- /dev/null +++ b/surrealdb-migrations/schemas/functions.surql @@ -0,0 +1,20 @@ +DEFINE FUNCTION OVERWRITE fn::create_wallet($initial_balance: option) { +LET $address = rand::string(10).lowercase(); +LET $password = rand::string(16); +LET $hash = crypto::argon2::generate($password); +LET $wallet = (CREATE wallet CONTENT { address: $address, balance: $initial_balance OR 0f, hash: $hash }); +RETURN { address: $address, password: $password, wallet: $wallet.first() }; +} PERMISSIONS FULL; + +DEFINE FUNCTION OVERWRITE fn::transfer_balance($from: record, $to: record | record, $amount: float) { +UPDATE $from SET balance -= $amount; +UPDATE $from SET total_out += $amount; +RETURN IF record::tb($to) == 'name' { +LET $owner = $to.owner; +UPDATE $owner SET balance += $amount; +UPDATE $owner SET total_in += $amount; +} ELSE { +UPDATE $to SET balance += $amount; +UPDATE $to SET total_in += $amount; +}; +} PERMISSIONS FULL; \ No newline at end of file diff --git a/surrealdb-migrations/schemas/name.surql b/surrealdb-migrations/schemas/name.surql new file mode 100644 index 0000000..99aa219 --- /dev/null +++ b/surrealdb-migrations/schemas/name.surql @@ -0,0 +1,8 @@ +DEFINE TABLE OVERWRITE name TYPE NORMAL SCHEMALESS PERMISSIONS NONE; + +DEFINE FIELD OVERWRITE last_transfered ON name TYPE option PERMISSIONS FULL; +DEFINE FIELD OVERWRITE last_updated ON name TYPE datetime DEFAULT time::now() PERMISSIONS FULL; +DEFINE FIELD OVERWRITE name ON name TYPE string PERMISSIONS FULL; +DEFINE FIELD OVERWRITE original_owner ON name TYPE option> PERMISSIONS FULL; +DEFINE FIELD OVERWRITE owner ON name TYPE record PERMISSIONS FULL; +DEFINE FIELD OVERWRITE registered ON name TYPE datetime DEFAULT time::now() PERMISSIONS FULL; \ No newline at end of file diff --git a/surrealdb-migrations/schemas/player.surql b/surrealdb-migrations/schemas/player.surql new file mode 100644 index 0000000..a5ab236 --- /dev/null +++ b/surrealdb-migrations/schemas/player.surql @@ -0,0 +1,4 @@ +DEFINE TABLE OVERWRITE player TYPE ANY SCHEMALESS PERMISSIONS NONE; + +DEFINE FIELD OVERWRITE joined_at ON player TYPE datetime DEFAULT time::now() PERMISSIONS FULL; +DEFINE FIELD OVERWRITE name ON player TYPE string PERMISSIONS FULL; \ No newline at end of file diff --git a/surrealdb-migrations/schemas/script_migration.surql b/surrealdb-migrations/schemas/script_migration.surql new file mode 100644 index 0000000..55e693b --- /dev/null +++ b/surrealdb-migrations/schemas/script_migration.surql @@ -0,0 +1,7 @@ +DEFINE TABLE OVERWRITE script_migration SCHEMAFULL + PERMISSIONS + FOR select FULL + FOR create, update, delete NONE; + +DEFINE FIELD OVERWRITE script_name ON script_migration TYPE string; +DEFINE FIELD OVERWRITE executed_at ON script_migration TYPE datetime VALUE time::now() READONLY; \ No newline at end of file diff --git a/surrealdb-migrations/schemas/transaction.surql b/surrealdb-migrations/schemas/transaction.surql new file mode 100644 index 0000000..9ec6142 --- /dev/null +++ b/surrealdb-migrations/schemas/transaction.surql @@ -0,0 +1,9 @@ +DEFINE TABLE OVERWRITE transaction TYPE NORMAL SCHEMAFULL PERMISSIONS NONE; + +DEFINE FIELD OVERWRITE amount ON transaction TYPE float PERMISSIONS FULL; +DEFINE FIELD OVERWRITE from ON transaction TYPE record PERMISSIONS FULL; +DEFINE FIELD OVERWRITE metadata ON transaction TYPE option PERMISSIONS FULL; +DEFINE FIELD OVERWRITE timestamp ON transaction TYPE datetime DEFAULT time::now() PERMISSIONS FULL; +DEFINE FIELD OVERWRITE to ON transaction TYPE record | record PERMISSIONS FULL; +DEFINE FIELD OVERWRITE transaction_type ON transaction TYPE 'unknown' | 'mined' | 'name_purchase' | 'transfer' PERMISSIONS FULL; + diff --git a/surrealdb-migrations/schemas/wallet.surql b/surrealdb-migrations/schemas/wallet.surql new file mode 100644 index 0000000..1fa3b02 --- /dev/null +++ b/surrealdb-migrations/schemas/wallet.surql @@ -0,0 +1,12 @@ + +DEFINE TABLE OVERWRITE wallet TYPE NORMAL SCHEMAFULL PERMISSIONS NONE; + +DEFINE FIELD OVERWRITE address ON wallet TYPE string PERMISSIONS FULL; +DEFINE FIELD OVERWRITE balance ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL; +DEFINE FIELD OVERWRITE created_at ON wallet TYPE datetime DEFAULT time::now() PERMISSIONS FULL; +DEFINE FIELD OVERWRITE hash ON wallet TYPE string PERMISSIONS FULL; +DEFINE FIELD OVERWRITE is_shared ON wallet TYPE bool DEFAULT false PERMISSIONS FULL; +DEFINE FIELD OVERWRITE total_in ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL; +DEFINE FIELD OVERWRITE total_out ON wallet TYPE float DEFAULT 0 PERMISSIONS FULL; + +DEFINE TABLE OVERWRITE owns TYPE RELATION IN player OUT wallet SCHEMAFULL PERMISSIONS NONE; \ No newline at end of file