Skip to content

Commit 81bc9cd

Browse files
Merge pull request #43 from dgraph-io/apoorv/txn-robust
chore: Txn & deprecate Slash endpoint method
2 parents c1a93dd + 4d69723 commit 81bc9cd

8 files changed

+79
-3
lines changed

lib/clientStub.js

+25
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ var DgraphClientStub = (function () {
108108
return this.callAPI("alter", __assign(__assign({}, this.options), { method: "POST", body: body }));
109109
};
110110
DgraphClientStub.prototype.query = function (req) {
111+
var _a;
111112
var headers = this.options.headers !== undefined
112113
? __assign({}, this.options.headers) : {};
113114
if (req.vars !== undefined) {
@@ -166,6 +167,12 @@ var DgraphClientStub = (function () {
166167
value: "true",
167168
});
168169
}
170+
if (((_a = req === null || req === void 0 ? void 0 : req.hash) === null || _a === void 0 ? void 0 : _a.length) > 0) {
171+
params.push({
172+
key: "hash",
173+
value: "" + req.hash,
174+
});
175+
}
169176
if (params.length > 0) {
170177
url += "?";
171178
url += params
@@ -179,6 +186,7 @@ var DgraphClientStub = (function () {
179186
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST", body: req.query, headers: headers }));
180187
};
181188
DgraphClientStub.prototype.mutate = function (mu) {
189+
var _a;
182190
var body;
183191
var usingJSON = false;
184192
if (mu.setJson !== undefined || mu.deleteJson !== undefined) {
@@ -229,6 +237,11 @@ var DgraphClientStub = (function () {
229237
(!this.legacyApi ? "?startTs=" : "/") + mu.startTs.toString();
230238
nextDelim = "&";
231239
}
240+
if (((_a = mu === null || mu === void 0 ? void 0 : mu.hash) === null || _a === void 0 ? void 0 : _a.length) > 0) {
241+
if (!this.legacyApi) {
242+
url += nextDelim + "hash=" + mu.hash;
243+
}
244+
}
232245
if (mu.commitNow) {
233246
if (!this.legacyApi) {
234247
url += nextDelim + "commitNow=true";
@@ -241,6 +254,7 @@ var DgraphClientStub = (function () {
241254
headers: headers }));
242255
};
243256
DgraphClientStub.prototype.commit = function (ctx) {
257+
var _a;
244258
var body;
245259
if (ctx.keys === undefined) {
246260
body = "[]";
@@ -251,12 +265,23 @@ var DgraphClientStub = (function () {
251265
var url = !this.legacyApi
252266
? "commit?startTs=" + ctx.start_ts
253267
: "commit/" + ctx.start_ts;
268+
if (((_a = ctx === null || ctx === void 0 ? void 0 : ctx.hash) === null || _a === void 0 ? void 0 : _a.length) > 0) {
269+
if (!this.legacyApi) {
270+
url += "&hash=" + ctx.hash;
271+
}
272+
}
254273
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST", body: body }));
255274
};
256275
DgraphClientStub.prototype.abort = function (ctx) {
276+
var _a;
257277
var url = !this.legacyApi
258278
? "commit?startTs=" + ctx.start_ts + "&abort=true"
259279
: "abort/" + ctx.start_ts;
280+
if (((_a = ctx === null || ctx === void 0 ? void 0 : ctx.hash) === null || _a === void 0 ? void 0 : _a.length) > 0) {
281+
if (!this.legacyApi) {
282+
url += "&hash=" + ctx.hash;
283+
}
284+
}
260285
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST" }));
261286
};
262287
DgraphClientStub.prototype.login = function (userid, password, refreshToken) {

lib/txn.js

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var Txn = (function () {
5555
preds: [],
5656
readOnly: options.readOnly,
5757
bestEffort: options.bestEffort,
58+
hash: "",
5859
};
5960
}
6061
Txn.prototype.query = function (q, options) {
@@ -78,6 +79,7 @@ var Txn = (function () {
7879
debug: options.debug,
7980
readOnly: this.ctx.readOnly,
8081
bestEffort: this.ctx.bestEffort,
82+
hash: this.ctx.hash,
8183
};
8284
if (vars !== undefined) {
8385
varsObj_1 = {};
@@ -113,6 +115,7 @@ var Txn = (function () {
113115
}
114116
this.mutated = true;
115117
mu.startTs = this.ctx.start_ts;
118+
mu.hash = this.ctx.hash;
116119
this.dc.debug("Mutate request:\n" + util_1.stringifyMessage(mu));
117120
c = this.dc.anyClient();
118121
_a.label = 1;
@@ -203,9 +206,11 @@ var Txn = (function () {
203206
return res.filter(function (item, idx, arr) { return idx === 0 || arr[idx - 1] !== item; });
204207
};
205208
Txn.prototype.mergeContext = function (src) {
209+
var _a;
206210
if (src === undefined) {
207211
return;
208212
}
213+
this.ctx.hash = (_a = src.hash) !== null && _a !== void 0 ? _a : "";
209214
if (this.ctx.start_ts === 0) {
210215
this.ctx.start_ts = src.start_ts;
211216
}

lib/types.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface Request {
1818
debug?: boolean;
1919
readOnly?: boolean;
2020
bestEffort?: boolean;
21+
hash?: string;
2122
}
2223
export interface Response {
2324
data: {};
@@ -44,6 +45,7 @@ export interface Mutation {
4445
commitNow?: boolean;
4546
mutation?: string;
4647
isJsonString?: boolean;
48+
hash?: string;
4749
}
4850
export interface Assigned {
4951
data: AssignedData;
@@ -65,6 +67,7 @@ export interface TxnContext {
6567
preds?: string[];
6668
readOnly: boolean;
6769
bestEffort: boolean;
70+
hash?: string;
6871
}
6972
export interface Latency {
7073
parsing_ns?: number;

src/client.ts

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export class DgraphClient {
5959
);
6060
}
6161

62+
/**
63+
* @deprecated since v21.3 and will be removed in v21.07 release. For more details, see:
64+
* https://discuss.dgraph.io/t/regarding-slash-cloud-dgraph-endpoints-in-the-clients/13492
65+
*/
66+
6267
public setSlashApiKey(apiKey: string) {
6368
this.clients.forEach((c: DgraphClientStub) => c.setSlashApiKey(apiKey));
6469
}

src/clientStub.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ export class DgraphClientStub {
155155
value: "true",
156156
});
157157
}
158+
if (req?.hash?.length > 0) {
159+
params.push({
160+
key: "hash",
161+
value: `${req.hash}`,
162+
});
163+
}
158164
if (params.length > 0) {
159165
url += "?";
160166
url += params
@@ -248,6 +254,12 @@ export class DgraphClientStub {
248254
nextDelim = "&";
249255
}
250256

257+
if (mu?.hash?.length > 0) {
258+
if (!this.legacyApi) {
259+
url += `${nextDelim}hash=${mu.hash}`;
260+
}
261+
}
262+
251263
if (mu.commitNow) {
252264
if (!this.legacyApi) {
253265
url += `${nextDelim}commitNow=true`;
@@ -272,10 +284,16 @@ export class DgraphClientStub {
272284
body = JSON.stringify(ctx.keys);
273285
}
274286

275-
const url = !this.legacyApi
287+
let url = !this.legacyApi
276288
? `commit?startTs=${ctx.start_ts}`
277289
: `commit/${ctx.start_ts}`;
278290

291+
if (ctx?.hash?.length > 0) {
292+
if (!this.legacyApi) {
293+
url += `&hash=${ctx.hash}`;
294+
}
295+
}
296+
279297
return this.callAPI(url, {
280298
...this.options,
281299
method: "POST",
@@ -284,11 +302,17 @@ export class DgraphClientStub {
284302
}
285303

286304
public abort(ctx: TxnContext): Promise<TxnContext> {
287-
const url = !this.legacyApi
305+
let url = !this.legacyApi
288306
? `commit?startTs=${ctx.start_ts}&abort=true`
289307
: `abort/${ctx.start_ts}`;
290308

291-
return this.callAPI(url, { ...this.options, method: "POST" });
309+
if (ctx?.hash?.length > 0) {
310+
if (!this.legacyApi) {
311+
url += `&hash=${ctx.hash}`;
312+
}
313+
}
314+
315+
return this.callAPI(url, { ...this.options, method: "POST" });
292316
}
293317

294318
public async login(
@@ -419,6 +443,11 @@ export class DgraphClientStub {
419443
this.options.headers[ALPHA_AUTH_TOKEN_HEADER] = authToken;
420444
}
421445

446+
/**
447+
* @deprecated since v21.3 and will be removed in v21.07 release. For more details, see:
448+
* https://discuss.dgraph.io/t/regarding-slash-cloud-dgraph-endpoints-in-the-clients/13492
449+
*/
450+
422451
public setSlashApiKey(apiKey: string) {
423452
if (this.options.headers === undefined) {
424453
this.options.headers = {};

src/txn.ts

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class Txn {
4141
preds: [],
4242
readOnly: options.readOnly,
4343
bestEffort: options.bestEffort,
44+
hash: "",
4445
};
4546
}
4647

@@ -74,6 +75,7 @@ export class Txn {
7475
debug: options.debug,
7576
readOnly: this.ctx.readOnly,
7677
bestEffort: this.ctx.bestEffort,
78+
hash: this.ctx.hash,
7779
};
7880
if (vars !== undefined) {
7981
const varsObj: { [k: string]: string } = {};
@@ -117,6 +119,7 @@ export class Txn {
117119

118120
this.mutated = true;
119121
mu.startTs = this.ctx.start_ts;
122+
mu.hash = this.ctx.hash;
120123
this.dc.debug(`Mutate request:\n${stringifyMessage(mu)}`);
121124

122125
const c = this.dc.anyClient();
@@ -213,6 +216,8 @@ export class Txn {
213216
return;
214217
}
215218

219+
this.ctx.hash = src.hash ?? "" ;
220+
216221
if (this.ctx.start_ts === 0) {
217222
this.ctx.start_ts = src.start_ts;
218223
} else if (this.ctx.start_ts !== src.start_ts) {

src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface Request {
1818
debug?: boolean;
1919
readOnly?: boolean;
2020
bestEffort?: boolean;
21+
hash?: string;
2122
}
2223

2324
export interface Response {
@@ -48,6 +49,7 @@ export interface Mutation {
4849
mutation?: string;
4950
// Set to true if `mutation` field (above) contains a JSON mutation.
5051
isJsonString?: boolean;
52+
hash?: string;
5153
}
5254

5355
export interface Assigned {
@@ -71,6 +73,7 @@ export interface TxnContext {
7173
preds?: string[];
7274
readOnly: boolean;
7375
bestEffort: boolean;
76+
hash?: string;
7477
}
7578

7679
export interface Latency {

tslint.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"no-relative-imports": false,
1010
"import-name": false,
1111
"export-name": false,
12+
"cyclomatic-complexity": false,
1213
"promise-function-async": false,
1314
"no-void-expression": false,
1415
"newline-before-return": false,

0 commit comments

Comments
 (0)