@@ -51,24 +51,38 @@ of using the Dgraph JavaScript client. Follow the instructions in the README of
5151
5252### Creating a Client
5353
54- A ` DgraphClient ` object can be initialised by passing it a list of ` DgraphClientStub ` clients as
55- variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better
56- distribution of workload.
54+ #### Connection Strings
5755
58- The following code snippet shows just one connection.
56+ The dgraph-js supports connecting to a Dgraph cluster using connection strings. Dgraph connections
57+ strings take the form ` dgraph://{username:password@}host:port?args ` .
5958
60- ``` js
61- const dgraph = require (" dgraph-js" )
62- const grpc = require (" @grpc/grpc-js" )
63-
64- const clientStub = new dgraph.DgraphClientStub (
65- // addr: optional, default: "localhost:9080"
66- " localhost:9080" ,
67- // credentials: optional, default: grpc.credentials.createInsecure()
68- grpc .credentials .createInsecure (),
69- )
70- const dgraphClient = new dgraph.DgraphClient (clientStub)
71- ```
59+ ` username ` and ` password ` are optional. If username is provided, a password must also be present. If
60+ supplied, these credentials are used to log into a Dgraph cluster through the ACL mechanism.
61+
62+ Valid connection string args:
63+
64+ | Arg | Value | Description |
65+ | ----------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
66+ | apikey | \< key\> | a Dgraph Cloud API Key |
67+ | bearertoken | \< token\> | an access token |
68+ | sslmode | disable \| require \| verify-ca | TLS option, the default is ` disable ` . If ` verify-ca ` is set, the TLS certificate configured in the Dgraph cluster must be from a valid certificate authority. |
69+
70+ ## Some example connection strings: | Value | Explanation | |
71+
72+ | ----------------------------------------------------------------------------------- | |
73+ dgraph://localhost:9080 | Connect to localhost, no ACL, no TLS | |
74+ dgraph://sally: supersecret @dg.example.com:443?sslmode=verify-ca | Connect to remote server, use ACL
75+ and require TLS and a valid certificate from a CA | |
76+ dgraph://foo-bar.grpc.us-west-2.aws.cloud.dgraph.io:443?sslmode=verify-ca&apikey=\< your-api-connection-key\>
77+ | Connect to a Dgraph Cloud cluster | |
78+ dgraph://foo-bar.grpc.hypermode.com?sslmode=verify-ca&bearertoken=\< some access token\> | Connect to
79+ a Dgraph cluster protected by a secure gateway |
80+
81+ Using the ` Open ` function with a connection string: // open a connection to an ACL-enabled, non-TLS
82+ cluster and login as groot const {client,closeStub} =
83+ dgraph.Open("dgraph://groot: password @localhost:8090")
84+
85+ ````
7286
7387To facilitate debugging, [debug mode](#debug-mode) can be enabled for a client.
7488
@@ -83,31 +97,12 @@ In order to create a JavaScript client, and make the client login into namespace
8397```js
8498const dgraphClientStub = new dgraph.DgraphClientStub("localhost:9080")
8599await dgraphClientStub.loginIntoNamespace("groot", "password", 123) // where 123 is the namespaceId
86- ```
100+ ````
87101
88102In the example above, the client logs into namespace ` 123 ` using username ` groot ` and password
89103` password ` . Once logged in, the client can perform all the operations allowed to the ` groot ` user of
90104namespace ` 123 ` .
91105
92- ### Creating a Client for Dgraph Cloud Endpoint
93-
94- If you want to connect to Dgraph running on your [ Dgraph Cloud] ( https://cloud.dgraph.io ) instance,
95- then all you need is the URL of your Dgraph Cloud endpoint and the API key. You can get a client
96- using them as follows:
97-
98- ``` js
99- const dgraph = require (" dgraph-js" )
100-
101- const clientStub = dgraph .clientStubFromCloudEndpoint (
102- " https://frozen-mango.eu-central-1.aws.cloud.dgraph.io/graphql" ,
103- " <api-key>" ,
104- )
105- const dgraphClient = new dgraph.DgraphClient (clientStub)
106- ```
107-
108- ** Note:** the ` clientStubFromSlashGraphQLEndpoint ` method is deprecated and will be removed in the
109- next release. Instead use ` clientStubFromCloudEndpoint ` method.
110-
111106### Altering the Database
112107
113108To set the schema, create an ` Operation ` object, set the schema and pass it to
@@ -376,27 +371,21 @@ try {
376371
377372### Cleanup Resources
378373
379- To cleanup resources, you have to call ` DgraphClientStub#close() ` individually for all the instances
380- of ` DgraphClientStub ` .
374+ To cleanup resources, you have to call ` close() ` .
381375
382376``` js
383377const SERVER_ADDR = " localhost:9080"
384378const SERVER_CREDENTIALS = grpc .credentials .createInsecure ()
385379
386- // Create instances of DgraphClientStub.
387- const stub1 = new dgraph.DgraphClientStub (SERVER_ADDR , SERVER_CREDENTIALS )
388- const stub2 = new dgraph.DgraphClientStub (SERVER_ADDR , SERVER_CREDENTIALS )
389-
390- // Create an instance of DgraphClient.
391- const dgraphClient = new dgraph.DgraphClient (stub1, stub2)
380+ // Create instances of DgraphClient.
381+ const { client , closeStub } = dgraph .Open (" dgraph://groot:password@${SERVER_ADDR}" )
392382
393383// ...
394384// Use dgraphClient
395385// ...
396386
397- // Cleanup resources by closing all client stubs.
398- stub1 .close ()
399- stub2 .close ()
387+ // Cleanup resources by closing client stubs.
388+ closeStub ()
400389```
401390
402391### Debug mode
0 commit comments