Skip to content

Commit fe8ff30

Browse files
authored
Merge pull request #19 from dgraph-io/aelbannan/cluster
Cluster actions, Direct Connection to Zero instance (Needed for PR #150 on Ratel)
2 parents c6aa5fd + 1f70640 commit fe8ff30

17 files changed

+5472
-35
lines changed

lib/client.d.ts

Lines changed: 3 additions & 1 deletion
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, TxnOptions } from "./types";
3+
import { Operation, Payload, UiKeywords, TxnOptions, Response } from "./types";
44
export declare class DgraphClient {
55
private readonly clients;
66
private debugMode;
@@ -14,6 +14,8 @@ export declare class DgraphClient {
1414
newTxn(options?: TxnOptions): Txn;
1515
setDebugMode(mode?: boolean): void;
1616
fetchUiKeywords(): Promise<UiKeywords>;
17+
getHealth(all?: boolean): Promise<Response>;
18+
getState(): Promise<Response>;
1719
debug(msg: string): void;
1820
anyClient(): DgraphClientStub;
1921
}

lib/client.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ var DgraphClient = (function () {
9393
DgraphClient.prototype.fetchUiKeywords = function () {
9494
return this.anyClient().fetchUiKeywords();
9595
};
96+
DgraphClient.prototype.getHealth = function (all) {
97+
if (all === void 0) { all = true; }
98+
return __awaiter(this, void 0, void 0, function () {
99+
return __generator(this, function (_a) {
100+
switch (_a.label) {
101+
case 0: return [4, this.anyClient().getHealth(all)];
102+
case 1: return [2, _a.sent()];
103+
}
104+
});
105+
});
106+
};
107+
DgraphClient.prototype.getState = function () {
108+
return __awaiter(this, void 0, void 0, function () {
109+
return __generator(this, function (_a) {
110+
switch (_a.label) {
111+
case 0: return [4, this.anyClient().getState()];
112+
case 1: return [2, _a.sent()];
113+
}
114+
});
115+
});
116+
};
96117
DgraphClient.prototype.debug = function (msg) {
97118
if (this.debugMode) {
98119
console.log(msg);

lib/clientStub.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export declare class DgraphClientStub {
2727
refreshToken?: string;
2828
};
2929
fetchUiKeywords(): Promise<UiKeywords>;
30+
getHealth(all?: boolean): Promise<Response>;
31+
getState(): Promise<Response>;
3032
setAutoRefresh(val: boolean): void;
3133
private cancelRefreshTimer;
3234
private maybeStartRefreshTimer;

lib/clientStub.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ var DgraphClientStub = (function () {
327327
});
328328
});
329329
};
330+
DgraphClientStub.prototype.getHealth = function (all) {
331+
if (all === void 0) { all = false; }
332+
return __awaiter(this, void 0, void 0, function () {
333+
var url;
334+
return __generator(this, function (_a) {
335+
switch (_a.label) {
336+
case 0:
337+
url = "health" + (all ? "?all" : "");
338+
return [4, this.callAPI(url, {
339+
method: "GET",
340+
})];
341+
case 1: return [2, _a.sent()];
342+
}
343+
});
344+
});
345+
};
346+
DgraphClientStub.prototype.getState = function () {
347+
return __awaiter(this, void 0, void 0, function () {
348+
return __generator(this, function (_a) {
349+
switch (_a.label) {
350+
case 0: return [4, this.callAPI("state", {
351+
method: "GET",
352+
})];
353+
case 1: return [2, _a.sent()];
354+
}
355+
});
356+
});
357+
};
330358
DgraphClientStub.prototype.setAutoRefresh = function (val) {
331359
if (!val) {
332360
this.cancelRefreshTimer();
@@ -352,27 +380,30 @@ var DgraphClientStub = (function () {
352380
};
353381
DgraphClientStub.prototype.callAPI = function (path, config) {
354382
return __awaiter(this, void 0, void 0, function () {
355-
var url;
383+
var url, response, json, errors;
356384
return __generator(this, function (_a) {
357-
url = this.getURL(path);
358-
if (this.accessToken !== undefined && path !== "login") {
359-
config.headers = config.headers !== undefined ? config.headers : {};
360-
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
361-
}
362-
return [2, fetch(url, config)
363-
.then(function (response) {
385+
switch (_a.label) {
386+
case 0:
387+
url = this.getURL(path);
388+
if (this.accessToken !== undefined && path !== "login") {
389+
config.headers = config.headers !== undefined ? config.headers : {};
390+
config.headers["X-Dgraph-AccessToken"] = this.accessToken;
391+
}
392+
return [4, fetch(url, config)];
393+
case 1:
394+
response = _a.sent();
364395
if (response.status >= 300 || response.status < 200) {
365396
throw new Error("Invalid status code = " + response.status);
366397
}
367-
return response.json();
368-
})
369-
.then(function (json) {
370-
var errors = json.errors;
398+
return [4, response.json()];
399+
case 2:
400+
json = _a.sent();
401+
errors = json.errors;
371402
if (errors !== undefined) {
372403
throw new errors_1.APIError(url, errors);
373404
}
374-
return json;
375-
})];
405+
return [2, json];
406+
}
376407
});
377408
});
378409
};

lib/errors.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export declare const ERR_NO_CLIENTS: Error;
22
export declare const ERR_FINISHED: Error;
33
export declare const ERR_ABORTED: Error;
4+
export declare const ERR_BEST_EFFORT_REQUIRED_READ_ONLY: Error;
45
export declare class CustomError extends Error {
56
readonly name: string;
67
constructor(message?: string);

lib/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
1616
exports.ERR_NO_CLIENTS = new Error("No clients provided in DgraphClient constructor");
1717
exports.ERR_FINISHED = new Error("Transaction has already been committed or discarded");
1818
exports.ERR_ABORTED = new Error("Transaction has been aborted. Please retry");
19+
exports.ERR_BEST_EFFORT_REQUIRED_READ_ONLY = new Error("Best effort only works for read-only queries");
1920
var CustomError = (function (_super) {
2021
__extends(CustomError, _super);
2122
function CustomError(message) {

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./clientStub";
33
export * from "./client";
44
export * from "./txn";
55
export * from "./errors";
6+
export * from "./zero";

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ __export(require("./clientStub"));
77
__export(require("./client"));
88
__export(require("./txn"));
99
__export(require("./errors"));
10+
__export(require("./zero"));

lib/txn.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ var Txn = (function () {
4444
this.mutated = false;
4545
this.dc = dc;
4646
if (options && options.bestEffort && !options.readOnly) {
47-
this.dc.debug('Best effort only works with read-only queries.');
48-
throw errors_1.ERR_ABORTED;
47+
this.dc.debug('Client attempted to query using best-effort without setting the transaction to read-only');
48+
throw errors_1.ERR_BEST_EFFORT_REQUIRED_READ_ONLY;
4949
}
5050
this.ctx = {
5151
start_ts: 0,
5252
keys: [],
5353
preds: [],
5454
readOnly: options && options.readOnly,
55-
bestEffort: options && options.bestEffort
55+
bestEffort: options && options.bestEffort,
5656
};
5757
}
5858
Txn.prototype.query = function (q, options) {

lib/zero.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Response } from "./types";
2+
export declare class DgraphZero {
3+
private readonly addr;
4+
constructor(addr?: string);
5+
getState(): Promise<Response>;
6+
getHealth(all?: boolean): Promise<Response>;
7+
private callAPI;
8+
private getURL;
9+
}

0 commit comments

Comments
 (0)