1- const dgraph = require ( "dgraph-js" )
1+ const dgraph = require ( "dgraph-js" ) ;
22// Drop All - discard all data, schema and start from a clean slate.
33async 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 ) ;
77}
88
99// Drop All Data, but keep the schema.
1010async 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 ) ;
1414}
1515
1616// Set schema.
1717async function setSchema ( dgraphClient ) {
18- const schema = `
18+ const schema = `
1919 name: string @index(exact) .
2020 age: int .
2121 married: bool .
2222 loc: geo .
2323 dob: datetime .
2424 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 ) ;
2929}
3030
3131// Create data using JSON.
3232async 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+ } ;
6363
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 ) ;
6868
69- // Commit transaction.
70- await txn . commit ( )
69+ // Commit transaction.
70+ await txn . commit ( ) ;
7171
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+ ) ;
7678
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+ }
8589}
8690
8791// Query for data.
8892async function queryData ( dgraphClient ) {
89- // Run query.
90- const query = `query all($a: string) {
93+ // Run query.
94+ const query = `query all($a: string) {
9195 all(func: eq(name, $a)) {
9296 uid
9397 name
@@ -103,35 +107,39 @@ async function queryData(dgraphClient) {
103107 name
104108 }
105109 }
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 ( ) ;
110116
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 ) ) ;
114120}
115121
116122async 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 ) ;
126134
127- // Close the client stub.
128- dgraphClient . close ( )
135+ // Close the client stub.
136+ dgraphClient . close ( ) ;
129137}
130138
131139main ( )
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