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