Dgraph JavaScript 客户端使用教程

Dgraph JavaScript 客户端使用教程

dgraph-jsOfficial Dgraph JavaScript client项目地址:https://gitcode.com/gh_mirrors/dg/dgraph-js

1. 项目的目录结构及介绍

dgraph-js/
├── examples/
│   ├── simple/
│   └── tls/
├── lib/
│   ├── client.js
│   ├── clientStub.js
│   ├── txn.js
│   └── util.js
├── proto/
│   └── api.proto
├── .gitignore
├── .npmignore
├── .travis.yml
├── LICENSE
├── README.md
├── package.json
└── webpack.config.js
  • examples/: 包含使用 Dgraph JavaScript 客户端的示例项目。
    • simple/: 简单的快速入门示例。
    • tls/: 使用 TLS 安全连接的示例。
  • lib/: 包含客户端的核心实现文件。
    • client.js: Dgraph 客户端的主要实现。
    • clientStub.js: 客户端存根的实现。
    • txn.js: 事务处理的实现。
    • util.js: 工具函数。
  • proto/: 包含 Protocol Buffer 定义文件。
    • api.proto: Dgraph API 的 Protocol Buffer 定义。
  • .gitignore: Git 忽略文件配置。
  • .npmignore: npm 忽略文件配置。
  • .travis.yml: Travis CI 配置文件。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • package.json: npm 包配置文件。
  • webpack.config.js: Webpack 配置文件。

2. 项目的启动文件介绍

项目的启动文件通常位于 examples/ 目录下。以 examples/simple/ 为例,主要的启动文件是 index.js

const dgraph = require("dgraph-js");
const grpc = require("grpc");

async function main() {
  // 创建客户端存根
  const clientStub = new dgraph.DgraphClientStub(
    "localhost:9080",
    grpc.credentials.createInsecure()
  );

  // 创建 Dgraph 客户端
  const dgraphClient = new dgraph.DgraphClient(clientStub);

  // 设置请求的 JSON 数据
  const txn = dgraphClient.newTxn();
  try {
    const mu = new dgraph.Mutation();
    mu.setSetJson({
      name: "Alice",
      age: 26,
    });

    const response = await txn.mutate(mu);
    await txn.commit();

    console.log("Response: ", response);
  } finally {
    await txn.discard();
  }
}

main().catch(console.error);

3. 项目的配置文件介绍

项目的配置文件主要是 package.json,其中包含了项目的依赖、脚本命令等信息。

{
  "name": "dgraph-js",
  "version": "2.0.3",
  "description": "Official Dgraph JavaScript client",
  "main": "lib/client.js",
  "scripts": {
    "build": "webpack",
    "build:protos": "protoc --js_out=import_style=commonjs,binary:lib proto/api.proto",
    "test": "mocha --exit"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/dgraph-io/dgraph-js.git"
  },
  "keywords": [
    "dgraph",
    "graphql",
    "graph",
    "database",
    "client"
  ],
  "author": "Dgraph Labs, Inc.",
  "license": "Apache-2.0",
  "bugs": {
    "url": "https://github.com/dgraph-io/dgraph-js/issues"
  },
  "homepage": "https://github.com/dgraph-io/dgraph-js#readme",
  "dependencies": {
    "@grpc/grpc-js": "^1.0.5",
    "google-protobuf": "^3.13.0",
    "protobufjs": "^6.10.1"
  },
  "devDependencies": {
    "@types/

dgraph-jsOfficial Dgraph JavaScript client项目地址:https://gitcode.com/gh_mirrors/dg/dgraph-js

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏赢安Simona

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值