@@ -3,7 +3,7 @@ import * as grpc from "grpc";
33import * as messages from "../generated/api_pb" ;
44
55import { DgraphClient } from "./client" ;
6- import { ERR_ABORTED , ERR_FINISHED } from "./errors" ;
6+ import { ERR_ABORTED , ERR_BEST_EFFORT_REQUIRED_READ_ONLY , ERR_FINISHED } from "./errors" ;
77import * as types from "./types" ;
88import {
99 isAbortedError ,
@@ -12,6 +12,11 @@ import {
1212 stringifyMessage ,
1313} from "./util" ;
1414
15+ export type TxnOptions = {
16+ readOnly ?: boolean ;
17+ bestEffort ?: boolean ;
18+ } ;
19+
1520/**
1621 * Txn is a single atomic transaction.
1722 *
@@ -31,13 +36,22 @@ export class Txn {
3136 private readonly ctx : messages . TxnContext ;
3237 private finished : boolean = false ;
3338 private mutated : boolean = false ;
39+ private readonly useReadOnly : boolean = false ;
40+ private readonly useBestEffort : boolean = false ;
3441 private sequencingProp : messages . LinRead . SequencingMap [ keyof messages . LinRead . SequencingMap ] ;
3542
36- constructor ( dc : DgraphClient ) {
43+ constructor ( dc : DgraphClient , txnOpts ?: TxnOptions ) {
3744 this . dc = dc ;
3845 this . ctx = new messages . TxnContext ( ) ;
3946 this . ctx . setLinRead ( this . dc . getLinRead ( ) ) ;
4047 this . sequencingProp = messages . LinRead . Sequencing . CLIENT_SIDE ;
48+ const defaultedTxnOpts = { readOnly : false , bestEffort : false , ...txnOpts } ;
49+ this . useReadOnly = defaultedTxnOpts . readOnly ;
50+ this . useBestEffort = defaultedTxnOpts . bestEffort ;
51+ if ( this . useBestEffort && ! this . useReadOnly ) {
52+ this . dc . debug ( `Client attempted to query using best-effort without setting the transaction to read-only` ) ;
53+ throw ERR_BEST_EFFORT_REQUIRED_READ_ONLY ;
54+ }
4155 }
4256
4357 public sequencing ( sequencing : messages . LinRead . SequencingMap [ keyof messages . LinRead . SequencingMap ] ) : void {
@@ -71,6 +85,8 @@ export class Txn {
7185 const req = new messages . Request ( ) ;
7286 req . setQuery ( q ) ;
7387 req . setStartTs ( this . ctx . getStartTs ( ) ) ;
88+ req . setReadOnly ( this . useReadOnly ) ;
89+ req . setBestEffort ( this . useBestEffort ) ;
7490
7591 const linRead = this . ctx . getLinRead ( ) ;
7692 // tslint:disable-next-line no-unsafe-any
0 commit comments