Native DB 开源项目教程
native_dbDrop-in embedded database in Rust项目地址:https://gitcode.com/gh_mirrors/na/native_db
1. 项目的目录结构及介绍
Native DB 项目的目录结构如下:
native_db/
├── benches/
├── native_db_macro/
├── src/
├── tests/
├── .gitignore
├── Cargo.toml
├── Cross.toml
├── LICENSE
├── README.md
├── build.rs
├── cargo_publish.sh
├── justfile
├── release-config.js
├── renovate.json
├── version_update.sh
目录结构介绍
benches/
: 包含性能测试文件。native_db_macro/
: 包含宏定义文件。src/
: 包含项目的主要源代码文件。tests/
: 包含测试文件。.gitignore
: Git 忽略文件配置。Cargo.toml
: Rust 项目的依赖和配置文件。Cross.toml
: 用于跨平台编译的配置文件。LICENSE
: 项目的许可证文件。README.md
: 项目说明文档。build.rs
: 构建脚本。cargo_publish.sh
: 用于发布项目的脚本。justfile
: 任务运行器配置文件。release-config.js
: 发布配置文件。renovate.json
: 依赖更新配置文件。version_update.sh
: 版本更新脚本。
2. 项目的启动文件介绍
项目的启动文件是 src/main.rs
。这个文件是 Rust 项目的入口点,负责初始化和启动数据库。
src/main.rs
文件内容示例
use native_db::*;
use once_cell::sync::Lazy;
// 定义模型
static MODELS: Lazy<Models> = Lazy::new(|| {
let mut models = Models::new();
models.define::<data::v1::Person>().unwrap();
models
});
fn main() -> Result<(), db_type::Error> {
// 创建数据库
let db = Builder::new().create_in_memory(&MODELS);
Ok(())
}
启动文件介绍
use native_db::*;
: 导入native_db
库的所有内容。use once_cell::sync::Lazy;
: 导入once_cell
库的Lazy
模块,用于定义静态变量。static MODELS: Lazy<Models> = Lazy::new(|| { ... });
: 定义静态模型变量。fn main() -> Result<(), db_type::Error> { ... }
: 主函数,负责创建数据库实例。
3. 项目的配置文件介绍
项目的配置文件主要是 Cargo.toml
和 Cross.toml
。
Cargo.toml
文件内容示例
[package]
name = "native_db"
version = "0.7.1"
edition = "2018"
[dependencies]
chrono = { version = "^0.4", optional = true }
native_db_macro = { version = "^0.7.1", features = ["normal"] }
native_model = { version = "^0.4.19", features = ["normal"] }
redb = { version = "^2.1.1", features = ["normal"] }
serde = { version = "^1.0", optional = true }
thiserror = { version = "^1.0", features = ["normal"] }
tokio = { version = "^1.38", optional = true }
uuid = { version = "^1.10.0", optional = true }
[dev-dependencies]
assert_fs = { version = "^1.1.1" }
bincode = { version = "2.0.0-rc.3" }
criterion = { version = "^0.5.1" }
dinghy-test = { version = "^0.7.2" }
doc-comment = { version = "^0.3.3" }
include_dir = { version = "^0.7" }
itertools = { version = "^0.13" }
once_cell = { version = "^1.19" }
rand = { version = "^0.8.
native_dbDrop-in embedded database in Rust项目地址:https://gitcode.com/gh_mirrors/na/native_db