Dgraph JavaScript 客户端使用教程

随笔1天前发布 零雄强
3 0 0

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

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...