Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.

Commit a730822

Browse files
committed
Moved ReadOnly/BestEffort to Txn, Created TxnOptions
1 parent b89ddfc commit a730822

File tree

8 files changed

+45
-29
lines changed

8 files changed

+45
-29
lines changed

lib/client.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DgraphClientStub } from "./clientStub";
22
import { Txn } from "./txn";
3-
import { Operation, Payload, UiKeywords } from "./types";
3+
import { Operation, Payload, UiKeywords, TxnOptions } from "./types";
44
export declare class DgraphClient {
55
private readonly clients;
66
private debugMode;
@@ -11,7 +11,7 @@ export declare class DgraphClient {
1111
alter(op: Operation): Promise<Payload>;
1212
login(userid: string, password: string): Promise<boolean>;
1313
logout(): void;
14-
newTxn(): Txn;
14+
newTxn(options?: TxnOptions): Txn;
1515
setDebugMode(mode?: boolean): void;
1616
fetchUiKeywords(): Promise<UiKeywords>;
1717
debug(msg: string): void;

lib/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ var DgraphClient = (function () {
8383
this.debug("Logout");
8484
this.clients.forEach(function (c) { return c.logout(); });
8585
};
86-
DgraphClient.prototype.newTxn = function () {
87-
return new txn_1.Txn(this);
86+
DgraphClient.prototype.newTxn = function (options) {
87+
return new txn_1.Txn(this, options);
8888
};
8989
DgraphClient.prototype.setDebugMode = function (mode) {
9090
if (mode === void 0) { mode = true; }

lib/txn.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { DgraphClient } from "./client";
2-
import { Assigned, Mutation, Response } from "./types";
2+
import { Assigned, Mutation, Response, TxnOptions } from "./types";
33
export declare class Txn {
44
private readonly dc;
55
private readonly ctx;
66
private finished;
77
private mutated;
8-
constructor(dc: DgraphClient);
8+
constructor(dc: DgraphClient, options?: TxnOptions);
99
query(q: string, options?: {
1010
debug?: boolean;
1111
}): Promise<Response>;
1212
queryWithVars(q: string, vars?: {
1313
[k: string]: any;
1414
}, options?: {
1515
debug?: boolean;
16-
readOnly?: boolean;
17-
bestEffort?: boolean;
1816
}): Promise<Response>;
1917
mutate(mu: Mutation): Promise<Assigned>;
2018
commit(): Promise<void>;

lib/txn.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3939
var errors_1 = require("./errors");
4040
var util_1 = require("./util");
4141
var Txn = (function () {
42-
function Txn(dc) {
42+
function Txn(dc, options) {
4343
this.finished = false;
4444
this.mutated = false;
4545
this.dc = dc;
46+
if (options.bestEffort && !options.readOnly) {
47+
this.dc.debug('Best effort only works with read-only queries.');
48+
throw errors_1.ERR_ABORTED;
49+
}
4650
this.ctx = {
4751
start_ts: 0,
4852
keys: [],
4953
preds: [],
54+
readOnly: options.readOnly || false,
55+
bestEffort: options.bestEffort || false
5056
};
5157
}
5258
Txn.prototype.query = function (q, options) {
@@ -63,17 +69,13 @@ var Txn = (function () {
6369
this.dc.debug("Query request (ERR_FINISHED):\nquery = " + q + "\nvars = " + vars);
6470
throw errors_1.ERR_FINISHED;
6571
}
66-
if (options.bestEffort && !options.readOnly) {
67-
this.dc.debug('Best effort only works with read-only queries.');
68-
throw errors_1.ERR_FINISHED;
69-
}
7072
req = {
7173
query: q,
7274
startTs: this.ctx.start_ts,
7375
timeout: this.dc.getQueryTimeout(),
7476
debug: options.debug,
75-
readOnly: options.readOnly,
76-
bestEffort: options.bestEffort
77+
readOnly: this.ctx.readOnly,
78+
bestEffort: this.ctx.bestEffort
7779
};
7880
if (vars !== undefined) {
7981
varsObj_1 = {};

lib/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ export interface TxnContext {
6161
aborted?: boolean | null;
6262
keys?: string[] | null;
6363
preds?: string[] | null;
64+
readOnly: boolean;
65+
bestEffort: boolean;
6466
}
6567
export interface Latency {
6668
parsing_ns?: number | null;
6769
processing_ns?: number | null;
6870
encoding_ns?: number | null;
6971
}
72+
export interface TxnOptions {
73+
readOnly?: boolean;
74+
bestEffort?: boolean;
75+
}

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DgraphClientStub } from "./clientStub";
22
import { ERR_NO_CLIENTS } from "./errors";
33
import { Txn } from "./txn";
4-
import { Operation, Payload, UiKeywords } from "./types";
4+
import { Operation, Payload, UiKeywords, TxnOptions } from "./types";
55
import { stringifyMessage } from "./util";
66

77
/**
@@ -76,8 +76,8 @@ export class DgraphClient {
7676
/**
7777
* newTxn creates a new transaction.
7878
*/
79-
public newTxn(): Txn {
80-
return new Txn(this);
79+
public newTxn(options?: TxnOptions): Txn {
80+
return new Txn(this, options);
8181
}
8282

8383
/**

src/txn.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DgraphClient } from "./client";
22
import { 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";
44
import {
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 } = {};

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,17 @@ export interface TxnContext {
6666
aborted?: boolean | null;
6767
keys?: string[] | null;
6868
preds?: string[] | null;
69+
readOnly: boolean;
70+
bestEffort: boolean;
6971
}
7072

7173
export interface Latency {
7274
parsing_ns?: number | null;
7375
processing_ns?: number | null;
7476
encoding_ns?: number | null;
7577
}
78+
79+
export interface TxnOptions {
80+
readOnly?: boolean,
81+
bestEffort?: boolean
82+
}

0 commit comments

Comments
 (0)