DgraphClient
object can be initialized by passing it a list of
DgraphClientStub
clients as arguments. Connecting to multiple Dgraph servers
in the same cluster allows for better distribution of workload.
The following code snippet shows just one connection.
dgraph-js
provides a new method loginIntoNamespace()
, which allows the users to login to
a specific namespace.
In order to create a JavaScript client, and make the client login into namespace
123
:
123
using username groot
and
password password
. Once logged in, the client can perform all the operations
allowed to the groot
user of namespace 123
.
Operation
object, set the schema and pass it to
DgraphClient#alter(Operation)
method.
setRunInBackground
field of the Operation
object to true
before
passing it to the DgraphClient#alter(Operation)
method. You can find more
details here.
NOTE: Many of the examples here use theawait
keyword which requiresasync/await
support which is available on Node.js >= v7.6.0. For prior versions, the expressions followingawait
can be used just like normalPromise
:
Operation
contains other fields as well, including drop predicate and drop
all. Drop all is useful if you wish to discard all the data, and start from a
clean slate, without bringing the instance down.
DgraphClient#newTxn()
method, which returns a
new Txn
object. This operation incurs no network overhead.
It is good practice to call Txn#discard()
in a finally
block after running
the transaction. Calling Txn#discard()
after Txn#commit()
is a no-op and you
can call Txn#discard()
multiple times with no additional side-effects.
readOnly
boolean to true
while
calling DgraphClient#newTxn()
method. Read-only transactions canβt contain
mutations and trying to call Txn#mutate()
or Txn#commit()
results in an
error. Calling Txn.Discard()
is a no-op.
You can optionally set the bestEffort
boolean to true
. This may yield
improved latencies in read-bound workloads where the guaranteed latest value is
not strictly needed.
Txn#mutate(Mutation)
runs a mutation. It takes in a Mutation
object, which
provides two main ways to set data, JSON and RDF N-Quad. You can choose
whichever way is convenient.
We define a person object to represent a person and use it in a Mutation
object.
examples
folder.
Sometimes, you only want to commit a mutation, without querying anything
further. In such cases, you can use Mutation#setCommitNow(true)
to indicate
that the mutation must be immediately committed.
Mutation#setIgnoreIndexConflict(true)
can be applied on a Mutation
object to
not run conflict detection over the index, which would decrease the number of
transaction conflicts and aborts. However, this would come at the cost of
potentially inconsistent upsert operations.
Mutation can be run using txn.doRequest
as well.
Txn#query(string)
. You need to pass in a DQL
query string. If you want to pass an additional map of any variables that you
might want to set in the query, call Txn#queryWithVars(string, object)
with
the variables object as the second argument.
The response would contain the method Response#getJSON()
, which returns the
response JSON.
Letβs run the following query with a variable $a:
txn.doRequest
function to run the query.
txn.doRequest
function allows you to run upserts consisting of one query
and one mutation. Query variables could be defined and can then be used in the
mutation. You can also use the txn.doRequest
function to perform just a query
or a mutation.
@if
directive. The mutation is executed only when the specified condition is true.
If the condition is false, the mutation is silently ignored.
See more about Conditional Upsert
Here.
Txn#commit()
method. If your
transaction consisted solely of calls to Txn#query
or Txn#queryWithVars
, and
no calls to Txn#mutate
, then calling Txn#commit()
isnβt necessary.
An error is returned if other transactions running concurrently modify the same
data that was modified in this transaction. It is up to the user to retry
transactions when they fail.
DgraphClientStub#close()
individually
for all the instances of DgraphClientStub
.
DgraphClient#setDebugMode(boolean?)
method.
auth-token
.
fetch
requests.