-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.js
149 lines (134 loc) · 3.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const dgraph = require("dgraph-js")
// Create a client stub.
function newClientStub() {
return new dgraph.DgraphClientStub("localhost:9080")
}
// Create a client.
function newClient(clientStub) {
return new dgraph.DgraphClient(clientStub)
}
// Drop All - discard all data, schema and start from a clean slate.
async function dropAll(dgraphClient) {
const op = new dgraph.Operation()
op.setDropAll(true)
await dgraphClient.alter(op)
}
// Drop All Data, but keep the schema.
async function dropData(dgraphClient) {
const op = new dgraph.Operation()
op.setDropOp(dgraph.Operation.DropOp.DATA)
await dgraphClient.alter(op)
}
// Set schema.
async function setSchema(dgraphClient) {
const schema = `
name: string @index(exact) .
age: int .
married: bool .
loc: geo .
dob: datetime .
friend: [uid] @reverse .
`
const op = new dgraph.Operation()
op.setSchema(schema)
await dgraphClient.alter(op)
}
// Create data using JSON.
async function createData(dgraphClient) {
// Create a new transaction.
const txn = dgraphClient.newTxn()
try {
// Create data.
const p = {
uid: "_:alice",
name: "Alice",
age: 26,
married: true,
loc: {
type: "Point",
coordinates: [1.1, 2],
},
dob: new Date(1980, 1, 1, 23, 0, 0, 0),
friend: [
{
name: "Bob",
age: 24,
},
{
name: "Charlie",
age: 29,
},
],
school: [
{
name: "Crown Public School",
},
],
}
// Run mutation.
const mu = new dgraph.Mutation()
mu.setSetJson(p)
const response = await txn.mutate(mu)
// Commit transaction.
await txn.commit()
// Get uid of the outermost object (person named "Alice").
// Response#getUidsMap() returns a map from blank node names to uids.
// For a json mutation, blank node label is used for the name of the created nodes.
console.log(`Created person named "Alice" with uid = ${response.getUidsMap().get("alice")}\n`)
console.log("All created nodes (map from blank node names to uids):")
response.getUidsMap().forEach((uid, key) => console.log(`${key} => ${uid}`))
console.log()
} finally {
// Clean up. Calling this after txn.commit() is a no-op
// and hence safe.
await txn.discard()
}
}
// Query for data.
async function queryData(dgraphClient) {
// Run query.
const query = `query all($a: string) {
all(func: eq(name, $a)) {
uid
name
age
married
loc
dob
friend {
name
age
}
school {
name
}
}
}`
const vars = { $a: "Alice" }
const res = await dgraphClient.newTxn({ readOnly: true }).queryWithVars(query, vars)
const ppl = res.getJson()
// Print results.
console.log(`Number of people named "Alice": ${ppl.all.length}`)
ppl.all.forEach((person) => console.log(person))
}
async function main() {
const dgraphClientStub = newClientStub()
const dgraphClient = newClient(dgraphClientStub)
await dropAll(dgraphClient)
await setSchema(dgraphClient)
await createData(dgraphClient)
await queryData(dgraphClient)
await dropData(dgraphClient)
await queryData(dgraphClient)
await createData(dgraphClient)
await queryData(dgraphClient)
// Close the client stub.
dgraphClientStub.close()
}
main()
.then(() => {
console.log("\nDONE!")
})
.catch((e) => {
console.log("ERROR: ", e)
})