|
| 1 | +const grpc = require("grpc"); |
| 2 | +const dgraph = require("dgraph-js"); |
| 3 | + |
| 4 | +function newClient() { |
| 5 | + const clientStub = new dgraph.DgraphClientStub("localhost:9080", grpc.credentials.createInsecure()); |
| 6 | + return new dgraph.DgraphClient(clientStub); |
| 7 | +} |
| 8 | + |
| 9 | +// Drop All - discard all data and start from a clean slate. |
| 10 | +function dropAll(dgraphClient) { |
| 11 | + const op = new dgraph.Operation(); |
| 12 | + op.setDropAll(true); |
| 13 | + return dgraphClient.alter(op); |
| 14 | +} |
| 15 | + |
| 16 | +// Set schema. |
| 17 | +function setSchema(dgraphClient) { |
| 18 | + const schema = "name: string @index(exact) ."; |
| 19 | + const op = new dgraph.Operation(); |
| 20 | + op.setSchema(schema); |
| 21 | + return dgraphClient.alter(op); |
| 22 | +} |
| 23 | + |
| 24 | +// Create a node for a person with name Alice. |
| 25 | +function createAlice(dgraphClient) { |
| 26 | + // Create a new transaction. |
| 27 | + const txn = dgraphClient.newTxn(); |
| 28 | + |
| 29 | + // Create data. |
| 30 | + const p = { name: "Alice" }; |
| 31 | + |
| 32 | + // Serialize it. |
| 33 | + const json = JSON.stringify(p); |
| 34 | + |
| 35 | + const serialized = new Uint8Array(new Buffer(json)); |
| 36 | + // OR if you want to use base64 |
| 37 | + // const serialized = new Buffer(json).toString("base64"); |
| 38 | + |
| 39 | + let err; |
| 40 | + |
| 41 | + // Run mutation. |
| 42 | + const mu = new dgraph.Mutation(); |
| 43 | + mu.setSetJson(serialized); |
| 44 | + return txn.mutate(mu).then(() => { |
| 45 | + // Commit transaction. |
| 46 | + return txn.commit(); |
| 47 | + }).catch((e) => { |
| 48 | + err = e; |
| 49 | + }).then(() => { |
| 50 | + return txn.discard(); |
| 51 | + }).then(() => { |
| 52 | + if (err != null) { |
| 53 | + throw err; |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +// Query for a person with name Alice. |
| 59 | +function queryAlice(dgraphClient) { |
| 60 | + // Run query. |
| 61 | + const query = `query all($a: string) { |
| 62 | + all(func: eq(name, $a)) |
| 63 | + { |
| 64 | + name |
| 65 | + } |
| 66 | + }`; |
| 67 | + const vars = { $a: "Alice" }; |
| 68 | + |
| 69 | + return dgraphClient.newTxn().queryWithVars(query, vars).then((res) => { |
| 70 | + // Deserialize result. |
| 71 | + const jsonStr = new Buffer(res.getJson_asU8()).toString(); |
| 72 | + // OR if you want to use base64 |
| 73 | + // const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString(); |
| 74 | + |
| 75 | + const ppl = JSON.parse(jsonStr); |
| 76 | + |
| 77 | + // Print results. |
| 78 | + console.log(`people found: ${ppl.all.length}`); |
| 79 | + for (let i = 0; i < ppl.all.length; i += 1) { |
| 80 | + console.log(ppl.all[i].name); |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function main() { |
| 86 | + const dgraphClient = newClient(); |
| 87 | + return dropAll(dgraphClient).then(() => { |
| 88 | + return setSchema(dgraphClient); |
| 89 | + }).then(() => { |
| 90 | + return createAlice(dgraphClient); |
| 91 | + }).then(() => { |
| 92 | + return queryAlice(dgraphClient); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +main().then(() => { |
| 97 | + console.log("\nDONE!"); |
| 98 | +}).catch((e) => { |
| 99 | + console.log("ERROR: ", e); |
| 100 | +}); |
0 commit comments