11import { DgraphClient } from "./client" ;
22import { ERR_ABORTED , ERR_FINISHED } from "./errors" ;
3- import { Assigned , Mutation , Request , Response , TxnContext } from "./types" ;
3+ import { Assigned , Mutation , Request , Response , TxnContext , TxnOptions } from "./types" ;
44import {
55 isAbortedError ,
66 isConflictError ,
@@ -27,12 +27,20 @@ export class Txn {
2727 private finished : boolean = false ;
2828 private mutated : boolean = false ;
2929
30- constructor ( dc : DgraphClient ) {
30+ constructor ( dc : DgraphClient , options ?: TxnOptions ) {
3131 this . dc = dc ;
32+
33+ if ( options . bestEffort && ! options . readOnly ) {
34+ this . dc . debug ( 'Best effort only works with read-only queries.' ) ;
35+ throw ERR_ABORTED ;
36+ }
37+
3238 this . ctx = {
3339 start_ts : 0 ,
3440 keys : [ ] ,
3541 preds : [ ] ,
42+ readOnly : options . readOnly || false ,
43+ bestEffort : options . bestEffort || false
3644 } ;
3745 }
3846
@@ -41,7 +49,7 @@ export class Txn {
4149 * need to be made in the same transaction, it's convenient to chain the method,
4250 * e.g. client.newTxn().query("...").
4351 */
44- public query ( q : string , options ?: { debug ?: boolean , readOnly ?: boolean , bestEffort ?: boolean } ) : Promise < Response > {
52+ public query ( q : string , options ?: { debug ?: boolean } ) : Promise < Response > {
4553 return this . queryWithVars ( q , undefined , options ) ;
4654 }
4755
@@ -52,25 +60,20 @@ export class Txn {
5260 public async queryWithVars (
5361 q : string ,
5462 vars ?: { [ k : string ] : any } , // tslint:disable-line no-any
55- options : { debug ?: boolean , readOnly ?: boolean , bestEffort ?: boolean } = { } ,
63+ options : { debug ?: boolean } = { } ,
5664 ) : Promise < Response > {
5765 if ( this . finished ) {
5866 this . dc . debug ( `Query request (ERR_FINISHED):\nquery = ${ q } \nvars = ${ vars } ` ) ;
5967 throw ERR_FINISHED ;
6068 }
6169
62- if ( options . bestEffort && ! options . readOnly ) {
63- this . dc . debug ( 'Best effort only works with read-only queries.' ) ;
64- throw ERR_FINISHED ;
65- }
66-
6770 const req : Request = {
6871 query : q ,
6972 startTs : this . ctx . start_ts ,
7073 timeout : this . dc . getQueryTimeout ( ) ,
7174 debug : options . debug ,
72- readOnly : options . readOnly ,
73- bestEffort : options . bestEffort
75+ readOnly : this . ctx . readOnly ,
76+ bestEffort : this . ctx . bestEffort
7477 } ;
7578 if ( vars !== undefined ) {
7679 const varsObj : { [ k : string ] : string } = { } ;
0 commit comments