11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
33
4- const dgraph = require ( "dgraph-js" ) ;
4+ import * as dgraph from "dgraph-js" ;
55
66// Create a client stub.
77function newClientStub ( ) {
8- // First create the appropriate TLS certs with dgraph cert:
9- // $ dgraph cert
10- // $ dgraph cert -n localhost
11- // $ dgraph cert -c user
12- console . log ( path . join ( __dirname , "tls" , "ca.crt" ) ) ;
13- const rootCaCert = fs . readFileSync ( path . join ( __dirname , "tls" , "ca.crt" ) ) ;
14- const clientCertKey = fs . readFileSync (
15- path . join ( __dirname , "tls" , "client.user.key" )
16- ) ;
17- const clientCert = fs . readFileSync (
18- path . join ( __dirname , "tls" , "client.user.crt" )
19- ) ;
20- return new dgraph . DgraphClientStub (
21- "localhost:9080" ,
22- dgraph . grpc . credentials . createSsl ( rootCaCert , clientCertKey , clientCert )
23- ) ;
8+ // First create the appropriate TLS certs with dgraph cert:
9+ // $ dgraph cert
10+ // $ dgraph cert -n localhost
11+ // $ dgraph cert -c user
12+ console . log ( path . join ( __dirname , "tls" , "ca.crt" ) ) ;
13+ const rootCaCert = fs . readFileSync ( path . join ( __dirname , "tls" , "ca.crt" ) ) ;
14+ const clientCertKey = fs . readFileSync (
15+ path . join ( __dirname , "tls" , "client.user.key" )
16+ ) ;
17+ const clientCert = fs . readFileSync (
18+ path . join ( __dirname , "tls" , "client.user.crt" )
19+ ) ;
20+ return new dgraph . DgraphClientStub (
21+ "localhost:9080" ,
22+ dgraph . grpc . credentials . createSsl ( rootCaCert , clientCertKey , clientCert )
23+ ) ;
2424}
2525
2626// Create a client.
2727function newClient ( clientStub ) {
28- return new dgraph . DgraphClient ( clientStub ) ;
28+ return new dgraph . DgraphClient ( clientStub ) ;
2929}
3030
3131// Drop All - discard all data and start from a clean slate.
3232async function dropAll ( dgraphClient ) {
33- const op = new dgraph . Operation ( ) ;
34- op . setDropAll ( true ) ;
35- await dgraphClient . alter ( op ) ;
33+ const op = new dgraph . Operation ( ) ;
34+ op . setDropAll ( true ) ;
35+ await dgraphClient . alter ( op ) ;
3636}
3737
3838// Set schema.
3939async function setSchema ( dgraphClient ) {
40- const schema = `
40+ const schema = `
4141 name: string @index(exact) .
4242 age: int .
4343 married: bool .
4444 loc: geo .
4545 dob: datetime .
4646 friend: [uid] @reverse .
4747 ` ;
48- const op = new dgraph . Operation ( ) ;
49- op . setSchema ( schema ) ;
50- await dgraphClient . alter ( op ) ;
48+ const op = new dgraph . Operation ( ) ;
49+ op . setSchema ( schema ) ;
50+ await dgraphClient . alter ( op ) ;
5151}
5252
5353// Create data using JSON.
5454async function createData ( dgraphClient ) {
55- // Create a new transaction.
56- const txn = dgraphClient . newTxn ( ) ;
57- try {
58- // Create data.
59- const p = {
60- uid : "_:alice" ,
61- name : "Alice" ,
62- age : 26 ,
63- married : true ,
64- loc : {
65- type : "Point" ,
66- coordinates : [ 1.1 , 2 ] ,
67- } ,
68- dob : new Date ( 1980 , 1 , 1 , 23 , 0 , 0 , 0 ) ,
69- friend : [
70- {
71- name : "Bob" ,
72- age : 24 ,
73- } ,
74- {
75- name : "Charlie" ,
76- age : 29 ,
77- } ,
78- ] ,
79- school : [
80- {
81- name : "Crown Public School" ,
82- } ,
83- ] ,
84- } ;
55+ // Create a new transaction.
56+ const txn = dgraphClient . newTxn ( ) ;
57+ try {
58+ // Create data.
59+ const p = {
60+ uid : "_:alice" ,
61+ name : "Alice" ,
62+ age : 26 ,
63+ married : true ,
64+ loc : {
65+ type : "Point" ,
66+ coordinates : [ 1.1 , 2 ] ,
67+ } ,
68+ dob : new Date ( 1980 , 1 , 1 , 23 , 0 , 0 , 0 ) ,
69+ friend : [
70+ {
71+ name : "Bob" ,
72+ age : 24 ,
73+ } ,
74+ {
75+ name : "Charlie" ,
76+ age : 29 ,
77+ } ,
78+ ] ,
79+ school : [
80+ {
81+ name : "Crown Public School" ,
82+ } ,
83+ ] ,
84+ } ;
8585
86- // Run mutation.
87- const mu = new dgraph . Mutation ( ) ;
88- mu . setSetJson ( p ) ;
89- const response = await txn . mutate ( mu ) ;
86+ // Run mutation.
87+ const mu = new dgraph . Mutation ( ) ;
88+ mu . setSetJson ( p ) ;
89+ const response = await txn . mutate ( mu ) ;
9090
91- // Commit transaction.
92- await txn . commit ( ) ;
91+ // Commit transaction.
92+ await txn . commit ( ) ;
9393
94- // Get uid of the outermost object (person named "Alice").
95- // Response#getUidsMap() returns a map from blank node names to uids.
96- // For a json mutation, blank node label is used for the name of the created nodes.
97- console . log (
98- `Created person named "Alice" with uid = ${ response
99- . getUidsMap ( )
100- . get ( "alice" ) } \n`
101- ) ;
94+ // Get uid of the outermost object (person named "Alice").
95+ // Response#getUidsMap() returns a map from blank node names to uids.
96+ // For a json mutation, blank node label is used for the name of the created nodes.
97+ console . log (
98+ `Created person named "Alice" with uid = ${ response
99+ . getUidsMap ( )
100+ . get ( "alice" ) } \n`
101+ ) ;
102102
103- console . log ( "All created nodes (map from blank node names to uids):" ) ;
104- response
105- . getUidsMap ( )
106- . forEach ( ( uid , key ) => console . log ( `${ key } => ${ uid } ` ) ) ;
107- console . log ( ) ;
108- } finally {
109- // Clean up. Calling this after txn.commit() is a no-op
110- // and hence safe.
111- await txn . discard ( ) ;
112- }
103+ console . log ( "All created nodes (map from blank node names to uids):" ) ;
104+ response
105+ . getUidsMap ( )
106+ . forEach ( ( uid , key ) => console . log ( `${ key } => ${ uid } ` ) ) ;
107+ console . log ( ) ;
108+ } finally {
109+ // Clean up. Calling this after txn.commit() is a no-op
110+ // and hence safe.
111+ await txn . discard ( ) ;
112+ }
113113}
114114
115115// Query for data.
116116async function queryData ( dgraphClient ) {
117- // Run query.
118- const query = `query all($a: string) {
117+ // Run query.
118+ const query = `query all($a: string) {
119119 all(func: eq(name, $a)) {
120120 uid
121121 name
@@ -132,33 +132,33 @@ async function queryData(dgraphClient) {
132132 }
133133 }
134134 }` ;
135- const vars = { $a : "Alice" } ;
136- const res = await dgraphClient
137- . newTxn ( { readOnly : true } )
138- . queryWithVars ( query , vars ) ;
139- const ppl = res . getJson ( ) ;
135+ const vars = { $a : "Alice" } ;
136+ const res = await dgraphClient
137+ . newTxn ( { readOnly : true } )
138+ . queryWithVars ( query , vars ) ;
139+ const ppl = res . getJson ( ) ;
140140
141- // Print results.
142- console . log ( `Number of people named "Alice": ${ ppl . all . length } ` ) ;
143- ppl . all . forEach ( ( person ) => console . log ( person ) ) ;
141+ // Print results.
142+ console . log ( `Number of people named "Alice": ${ ppl . all . length } ` ) ;
143+ ppl . all . forEach ( ( person ) => console . log ( person ) ) ;
144144}
145145
146146async function main ( ) {
147- const dgraphClientStub = newClientStub ( ) ;
148- const dgraphClient = newClient ( dgraphClientStub ) ;
149- await dropAll ( dgraphClient ) ;
150- await setSchema ( dgraphClient ) ;
151- await createData ( dgraphClient ) ;
152- await queryData ( dgraphClient ) ;
147+ const dgraphClientStub = newClientStub ( ) ;
148+ const dgraphClient = newClient ( dgraphClientStub ) ;
149+ await dropAll ( dgraphClient ) ;
150+ await setSchema ( dgraphClient ) ;
151+ await createData ( dgraphClient ) ;
152+ await queryData ( dgraphClient ) ;
153153
154- // Close the client stub.
155- dgraphClientStub . close ( ) ;
154+ // Close the client stub.
155+ dgraphClientStub . close ( ) ;
156156}
157157
158158main ( )
159- . then ( ( ) => {
160- console . log ( "\nDONE!" ) ;
161- } )
162- . catch ( ( e ) => {
163- console . log ( "ERROR: " , e ) ;
164- } ) ;
159+ . then ( ( ) => {
160+ console . log ( "\nDONE!" ) ;
161+ } )
162+ . catch ( ( e ) => {
163+ console . log ( "ERROR: " , e ) ;
164+ } ) ;
0 commit comments