All Projects → Swoorup → Dgraph Rs

Swoorup / Dgraph Rs

Licence: mit
A dgraph client driver written in rust ⛺

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to Dgraph Rs

Grpc Rust
Rust implementation of gRPC
Stars: ✭ 1,139 (+1179.78%)
Mutual labels:  grpc
Grpc Demos
Demos for my talk Beyond HTTP in ASP.NET Core 3.0 with gRPC
Stars: ✭ 74 (-16.85%)
Mutual labels:  grpc
Flyte
Accelerate your ML and Data workflows to production. Flyte is a production grade orchestration system for your Data and ML workloads. It has been battle tested at Lyft, Spotify, freenome and others and truly open-source.
Stars: ✭ 1,242 (+1295.51%)
Mutual labels:  grpc
Grpc Auth Example
Examples of client authentication with gRPC
Stars: ✭ 65 (-26.97%)
Mutual labels:  grpc
Guard
GRPC Wireguard Server to manage tunnels
Stars: ✭ 71 (-20.22%)
Mutual labels:  grpc
Our pc
Our Procedure Calls
Stars: ✭ 73 (-17.98%)
Mutual labels:  grpc
Grpcalchemy
The Python micro framework for building gPRC application.
Stars: ✭ 63 (-29.21%)
Mutual labels:  grpc
Docker Cloud Platform
使用Docker构建云平台,Docker云平台系列共三讲,Docker基础、Docker进阶、基于Docker的云平台方案。OpenStack+Docker+RestAPI+OAuth/HMAC+RabbitMQ/ZMQ+OpenResty/HAProxy/Nginx/APIGateway+Bootstrap/AngularJS+Ansible+K8S/Mesos/Marathon构建/探索微服务最佳实践。
Stars: ✭ 86 (-3.37%)
Mutual labels:  grpc
Gnes
GNES is Generic Neural Elastic Search, a cloud-native semantic search system based on deep neural network.
Stars: ✭ 1,178 (+1223.6%)
Mutual labels:  grpc
Charon
Authorization and authentication service.
Stars: ✭ 79 (-11.24%)
Mutual labels:  grpc
Thingsboard
Open-source IoT Platform - Device management, data collection, processing and visualization.
Stars: ✭ 10,526 (+11726.97%)
Mutual labels:  grpc
Libra Grpc
A lightweight JavaScript library for Libra
Stars: ✭ 69 (-22.47%)
Mutual labels:  grpc
Php Zipkin Demo
Laravel + go-micro + grpc + Zipkin
Stars: ✭ 74 (-16.85%)
Mutual labels:  grpc
App
Reusable framework for micro services & command line tools
Stars: ✭ 66 (-25.84%)
Mutual labels:  grpc
Grpcweb Example
An example implementation of a GopherJS client and a Go server using the Improbable gRPC-Web implementation
Stars: ✭ 85 (-4.49%)
Mutual labels:  grpc
Prometheus Proxy
Prometheus Proxy
Stars: ✭ 63 (-29.21%)
Mutual labels:  grpc
Drachtio Freeswitch Modules
A collection of open-sourced freeswitch modules that I use in various drachtio applications
Stars: ✭ 73 (-17.98%)
Mutual labels:  grpc
Grpc Swift
The Swift language implementation of gRPC.
Stars: ✭ 1,270 (+1326.97%)
Mutual labels:  grpc
Lile
Easily generate gRPC services in Go ⚡️
Stars: ✭ 1,271 (+1328.09%)
Mutual labels:  grpc
Grpcdump
Tool for capture and parse grpc traffic
Stars: ✭ 75 (-15.73%)
Mutual labels:  grpc

A rust client for dgraph

crates.io version Build Status

Dgraph Rust client which communicates with the server using gRPC.

Before using this client, it is highly recommended to go through tour.dgraph.io and docs.dgraph.io to understand how to run and work with Dgraph.

Table of contents

Prerequisites

dgraph supports only Dgraph versions 1.1 or higher.

Since this crate uses grpcio, which is a wrapper around C++ library, there are certain prerequisites needed before it can be installed. You can find them in grpcio documentation.

Installation

dgraph is available on crates.io. Add the following dependency to your Cargo.toml.

[dependencies]
dgraph = "0.4.0"

There are also a couple of passthrough grpcio features available:

  • openssl
  • openssl-vendored

Those are described in grpcio documentation.

Using a client

Create a client

Dgraph object can be initialised by passing it a list of dgraph::DgraphClient clients as a vector. Connecting to multiple Dgraph servers in the same cluster allows for better distribution of workload. The library provides a macro to do so.

The following code snippet shows just one connection.

let dgraph = make_dgraph!(dgraph::new_dgraph_client("localhost:9080"));

Alternatively, secure client can be used:

fn open_cert_file(path: &str) -> Vec<u8> {
  ...
}

let root_ca = open_cert_file("./tls/ca.crt");
let cert = open_cert_file("./tls/client.user.crt");
let private_key = open_cert_file("./tls/client.user.key");

let dgraph = make_dgraph!(dgraph::new_secure_dgraph_client(
    "localhost:9080",
    root_ca,
    cert,
    private_key
));

Alter the database

To set the schema, create an instance of dgraph::Operation and use the Alter endpoint.

let op = dgraph::Operation{
  schema: "name: string @index(exact) .".to_string(), ..Default::default()
};
let result = dgraph.alter(&op);
// Check error

Operation contains other fields as well, including DropAttr and DropAll. DropAll is useful if you wish to discard all the data, and start from a clean slate, without bringing the instance down. DropAttr is used to drop all the data related to a predicate.

Create a transaction

To create a transaction, call dgraph.new_txn(), which returns a dgraph::Txn object. This operation incurs no network overhead.

Once dgraph::Txn goes out of scope, txn.discard() is automatically called via the Drop trait. Calling txn.discard() after txn.commit() is a no-op and calling this multiple times has no additional side-effects.

let txn = dgraph.new_txn();

Run a mutation

txn.mutate(mu) runs a mutation. It takes in a dgraph::Mutation object. You can set the data using JSON or RDF N-Quad format.

We define a Person struct to represent a Person and marshal an instance of it to use with Mutation object.

#[derive(Serialize, Deserialize, Default, Debug)]
struct Person {
  uid: String,
  name: String,
}

let p = Person {
  uid:  "_:alice".to_string(),
  Name: "Alice".to_string(),
}

let pb = serde_json::to_vec(&p).expect("Invalid json");

let mut mu = dgraph::Mutation {
  json: pb, ..Default::default()
};

let assigned = txn.mutate(mu).expect("failed to create data");

For a more complete example, see the simple example simple (or the same example with secure client).

Sometimes, you only want to commit a mutation, without querying anything further. In such cases, you can use mu.commit_now = true to indicate that the mutation must be immediately committed.

Run a query

You can run a query by calling txn.query(q). You will need to pass in a GraphQL+- query string. If you want to pass an additional map of any variables that you might want to set in the query, call txn.query_with_vars(q, vars) with the variables map as third argument.

Let's run the following query with a variable $a:

let q = r#"query all($a: string) {
    all(func: eq(name, $a)) {
      name
    }
  }"#;

let mut vars = HashMap::new();
vars.insert("$a".to_string(), "Alice".to_string());

let resp = dgraph.new_readonly_txn().query_with_vars(&q, vars).expect("query");
let root: Root = serde_json::from_slice(&resp.json).expect("parsing");
println!("Root: {:#?}", root);

When running a schema query, the schema response is found in the Schema field of dgraph::Response.

let q = r#"schema(pred: [name]) {
  type
  index
  reverse
  tokenizer
  list
  count
  upsert
  lang
}"#;

let resp = txn.query(&q)?;
println!("{:#?}", resp.schema);

Commit a transaction

A transaction can be committed using the txn.commit() method. If your transaction consisted solely of calls to txn.query or txn.query_with_vars, and no calls to txn.mutate, then calling txn.commit is not necessary.

An error will be returned if other transactions running concurrently modify the same data that was modified in this transaction. It is up to the user to retry transactions when they fail.

let txn = dgraph.new_txn();
// Perform some queries and mutations.

let res = txn.commit();
if res.is_err() {
  // Retry or handle error
}

Integration tests

Tests require Dgraph running on localhost:19080. For the convenience there are a couple of docker-compose-*.yaml files - depending on Dgraph you are testing against - prepared in the root directory:

docker-compose -f docker-compose-*.yaml up -d

Since we are working with database, tests also need to be run in a single thread to prevent aborts. Eg.:

cargo test -- --test-threads=1

Contributing

Contributions are welcome. Feel free to raise an issue, for feature requests, bug fixes and improvements.

If you have made changes to one of src/protos/api.proto files, you need to regenerate the source files generated by Protocol Buffer tools. To do that, install the Protocol Buffer Compiler and then run the following command:

cargo run --features="compile-protobufs" --bin protoc

Release checklist

These have to be done with every version we support:

  • Run tests
  • Try examples

Update the version and publish crate:

  • Update tag in Cargo.toml
  • Update tag in README.md
  • git tag v0.X.X
  • git push origin v0.X.X
  • Write release log on GitHub
  • cargo publish
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].