Skip to content

Commit 8263326

Browse files
committed
Update simple example for Node.js < v7.6
1 parent 56eb307 commit 8263326

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

examples/simple/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ npm install
4545
```
4646

4747
## Run the sample code
48+
For Node.js >= v7.6, run:
4849

4950
```sh
5051
node index.js
5152
```
5253

54+
For Node.js < v7.6 or if you don't want to use the async/await syntax, run:
55+
56+
```sh
57+
node index-pre-v7.6.js
58+
```
59+
5360
Your output should look something like this:
5461

5562
```
@@ -59,4 +66,4 @@ Alice
5966
DONE!
6067
```
6168

62-
You can explore the source code in the `index.js` file.
69+
You can explore the source code in the `index.js` and `index-pre-v7.6.js` files.

examples/simple/index-pre-v7.6.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)