12个不稳定版本 (3个破坏性版本)

0.3.2 2021年4月24日
0.3.1 2021年4月24日
0.2.5 2021年4月3日
0.2.2 2021年3月20日
0.0.0 2020年11月8日

数据结构 中排名 557

每月下载 25
用于 7 个crate(6个直接使用)

MIT/Apache

305KB
5K SLoC

entity-rs:实体数据结构的库和宏

Build Status Crates.io Docs.rs entity: rustc 1.49+ entity_macros: rustc 1.49+

一个基于 TAO,Facebook的社交图谱分布式数据库 的简单框架。

需要Rust 1.49+。

入门

安装

通过将以下行添加到您的Cargo.toml将Entity导入您的项目。 entity_macros 包含用于派生和/或转换您的数据以与支持的数据库和查询兼容的宏。

[dependencies]
entity = "0.3.0"

对于大多数用例,您还希望导入宏,这将引入entity_macros crate

[dependencies]
entity = { version = "0.3.0", features = ["macros"] }

使用宏定义数据

以下是一个定义包含agename字段以及指向许多其他User实例(在边名friends下)的引用的User数据结构的示例。

use entity::simple_ent;

#[simple_ent]
struct User {
    name: String,
    age: u8,

    #[ent(edge)]
    friends: Vec<User>,
}

这为与任何数据库工作的User结构生成了各种特性和额外的数据结构。

按id加载ent

use entity::EntLoader;

let db = /* entity::WeakDatabaseRc instance */;

// Loads the user from the provided database reference
let user = User::load_from_db_strict(db, 123).expect("Database available");

// Loads the user from the globally-set database
let user = User::load_strict(123).expect("Database available");

访问ent字段

实现Ent特质的每个对象都能够访问各种常见数据,包括抽象字段信息

use entity::{Ent, Primitive, Value};

let user = /* User instance */;

// Access a list of all field names
assert_eq!(user.field_names(), vec![String::from("name"), String::from("age")]);

// Access individual field values, which are exposed using entity's
// generic Value enum
assert_eq!(user.field("name"), Some(Value::Text(/* ... */)));
assert_eq!(user.field("age"), Some(Value::Primitive(Primitive::Number(/* ... */))));

当使用宏生成ent时,也提供了如以下所示的类型访问器

let user = /* User instance */;

// Accesses fields and returns a reference to their actual type NOT
// wrapped in entity's generic Value enum
assert_eq!(user.get_name(), &String::from("..."));
assert_eq!(user.get_age(), &123);

访问ent边

实现Ent特质的每个对象都能够访问各种抽象边信息

use entity::{Ent, EdgeValue};

let user = /* User instance */;

// Access a list of all edge names
assert_eq!(user.edge_names(), vec![String::from("friends")]);

// Access specific edge information (does not load it)
assert_eq!(user.edge("friends"), Some(EdgeValue::Many(vec![124, 125, /* ... */])));

// Load an edge by name, returning a Vec<Box<dyn Ent>>
let friends: Vec<Box<dyn Ent>> = user.load_edge("friends").expect("Database available");

当使用宏生成ent时,也提供了如以下所示的类型边访问器

let user = /* User instance */;

// Access the ids of ents referenced by the edge
assert_eq!(user.my_friends_ids(), vec![124, 125, /* ... */]);

// Load the ents referenced by the edge into memory
let friends: Vec<User> = user.load_friends().expect("Database available");

查询ent

除了按id加载ents之外,entity-rs提供的完整套件还包括使用查询和相关谓词的概念查询任意数据结构的能力

use entity::{EntQuery, Predicate as P, Query};

let db = /* WeakDatabaseRc instance */;

// Produce a new query to search for ents with an age field that is 18 or higher
let ents: Vec<Box<dyn Ent>> = Query::default()
  .where_field("age", P::greater_than_or_equals(18))
  .execute_with_db(db)
  .expect("Database available");

// If the global database has been configured, can also be used in this manner
let ents: Vec<Box<dyn Ent>> = Query::default()
  .where_field("age", P::greater_than_or_equals(18))
  .execute()
  .expect("Database available");

当使用宏生成ent时,还创建了一个名为{Ent}Query的伴随查询结构,它为查询提供了更严格的类型

use entity::{EntQuery, TypedPredicate as P};

let db = /* WeakDatabaseRc instance */;

// Produce a new query to search for ents with an age field that is 18 or higher
let users: Vec<User> = User::query()
  .where_age(P::greater_than_or_equals(18))
  .execute_with_db(db)
  .expect("Database available");

// If the global database has been configured, can also be used in this manner
let users: Vec<User> = User::query()
  .where_age(P::greater_than_or_equals(18))
  .execute()
  .expect("Database available");

示例

  • async-graphql:使用 entity-rsasync-graphql 的示例
  • inmemory:使用 entity-rs 与自定义内存数据库的示例
  • sled:使用 entity-rssled 的示例

功能标志

Entity 提供了一些功能标志

  • global - 启用使用全局变量存储的数据库,提供创建和检索 ents 的快捷方式。
  • macros - 启用 ents 的宏,并为 ents 提供更简洁的声明性 API。 (直接导入 entity_macros)
  • serde-1 - 通过使用 typetag,为 ents 提供serde 序列化模块和相关功能。这要求所有 ents 实现 SerializeDeserialize
    • 需要将 serdetypetag 包含在依赖项中。

依赖项

~1.7–2.6MB
~54K SLoC