Skip to content

Commit 8917877

Browse files
committed
Add debug mode
1 parent 25ae876 commit 8917877

File tree

11 files changed

+117
-32
lines changed

11 files changed

+117
-32
lines changed

lib/client.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { Txn } from "./txn";
44
export declare class DgraphClient {
55
private clients;
66
private linRead;
7+
private debugMode;
78
constructor(...clients: DgraphClientStub[]);
89
alter(op: messages.Operation): Promise<messages.Payload>;
910
newTxn(): Txn;
11+
setDebugMode(mode?: boolean): void;
12+
debug(msg: string): void;
1013
getLinRead(): messages.LinRead;
1114
mergeLinReads(src?: messages.LinRead | null): void;
1215
anyClient(): DgraphClientStub;

lib/client.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var DgraphClient = (function () {
4545
for (var _i = 0; _i < arguments.length; _i++) {
4646
clients[_i] = arguments[_i];
4747
}
48+
this.debugMode = false;
4849
if (clients.length === 0) {
4950
throw errors_1.ERR_NO_CLIENTS;
5051
}
@@ -53,20 +54,33 @@ var DgraphClient = (function () {
5354
}
5455
DgraphClient.prototype.alter = function (op) {
5556
return __awaiter(this, void 0, void 0, function () {
56-
var c;
57+
var c, pl;
5758
return __generator(this, function (_a) {
5859
switch (_a.label) {
5960
case 0:
61+
this.debug("Alter request:\n" + util_1.stringifyMessage(op));
6062
c = this.anyClient();
6163
return [4, c.alter(op)];
62-
case 1: return [2, _a.sent()];
64+
case 1:
65+
pl = _a.sent();
66+
this.debug("Alter response:\n" + util_1.stringifyMessage(pl));
67+
return [2, pl];
6368
}
6469
});
6570
});
6671
};
6772
DgraphClient.prototype.newTxn = function () {
6873
return new txn_1.Txn(this);
6974
};
75+
DgraphClient.prototype.setDebugMode = function (mode) {
76+
if (mode === void 0) { mode = true; }
77+
this.debugMode = mode;
78+
};
79+
DgraphClient.prototype.debug = function (msg) {
80+
if (this.debugMode) {
81+
console.log(msg);
82+
}
83+
};
7084
DgraphClient.prototype.getLinRead = function () {
7185
var lr = new messages.LinRead();
7286
var idsMap = lr.getIdsMap();

lib/txn.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var Txn = (function () {
6363
switch (_a.label) {
6464
case 0:
6565
if (this.finished) {
66+
this.dc.debug("Query request (ERR_FINISHED):\nquery = " + q + "\nvars = " + vars);
6667
throw errors_1.ERR_FINISHED;
6768
}
6869
req = new messages.Request();
@@ -78,11 +79,13 @@ var Txn = (function () {
7879
}
7980
});
8081
}
82+
this.dc.debug("Query request:\n" + util_1.stringifyMessage(req));
8183
c = this.dc.anyClient();
8284
return [4, c.query(req)];
8385
case 1:
8486
res = _a.sent();
8587
this.mergeContext(res.getTxn());
88+
this.dc.debug("Query response:\n" + util_1.stringifyMessage(res));
8689
return [2, res];
8790
}
8891
});
@@ -95,10 +98,12 @@ var Txn = (function () {
9598
switch (_a.label) {
9699
case 0:
97100
if (this.finished) {
101+
this.dc.debug("Mutate request (ERR_FINISHED):\nmutation = " + util_1.stringifyMessage(mu));
98102
throw errors_1.ERR_FINISHED;
99103
}
100104
this.mutated = true;
101105
mu.setStartTs(this.ctx.getStartTs());
106+
this.dc.debug("Mutate request:\n" + util_1.stringifyMessage(mu));
102107
c = this.dc.anyClient();
103108
_a.label = 1;
104109
case 1:
@@ -125,6 +130,7 @@ var Txn = (function () {
125130
this.finished = true;
126131
}
127132
this.mergeContext(ag.getContext());
133+
this.dc.debug("Mutate response:\n" + util_1.stringifyMessage(ag));
128134
return [2, ag];
129135
}
130136
});

lib/util.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import * as jspb from "google-protobuf";
12
import * as messages from "../generated/api_pb";
23
export declare function mergeLinReads(target: messages.LinRead, src?: messages.LinRead | null): messages.LinRead;
3-
export declare function promisify<A, T>(f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void, thisContext?: any): (arg: A) => Promise<T>;
44
export declare function errorCode(err: any): {
55
valid: boolean;
66
code: number;
77
};
88
export declare function isAbortedError(err: any): boolean;
99
export declare function isConflictError(err: any): boolean;
10+
export declare function promisify<A, T>(f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void, thisContext?: any): (arg: A) => Promise<T>;
11+
export declare function stringifyMessage(msg: jspb.Message): string;

lib/util.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ function mergeLinReads(target, src) {
1616
return target;
1717
}
1818
exports.mergeLinReads = mergeLinReads;
19-
function promisify(f, thisContext) {
20-
return function (arg) {
21-
return new Promise(function (resolve, reject) {
22-
f.call(thisContext, arg, function (err, result) { return (err != null) ? reject(err) : resolve(result); });
23-
});
24-
};
25-
}
26-
exports.promisify = promisify;
2719
function errorCode(err) {
2820
if (err == null ||
2921
typeof err !== "object" ||
@@ -50,3 +42,15 @@ function isConflictError(err) {
5042
return ec.valid && (ec.code === grpc.status.ABORTED || ec.code === grpc.status.FAILED_PRECONDITION);
5143
}
5244
exports.isConflictError = isConflictError;
45+
function promisify(f, thisContext) {
46+
return function (arg) {
47+
return new Promise(function (resolve, reject) {
48+
f.call(thisContext, arg, function (err, result) { return (err != null) ? reject(err) : resolve(result); });
49+
});
50+
};
51+
}
52+
exports.promisify = promisify;
53+
function stringifyMessage(msg) {
54+
return JSON.stringify(msg.toObject());
55+
}
56+
exports.stringifyMessage = stringifyMessage;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dgraph-js",
3-
"version": "0.9.4-beta.1",
3+
"version": "0.9.4-beta.2",
44
"description": "Official javascript client for Dgraph",
55
"license": "Apache-2.0",
66
"repository": {

src/client.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import * as messages from "../generated/api_pb";
55
import { DgraphClientStub } from "./clientStub";
66
import { ERR_NO_CLIENTS } from "./errors";
77
import { Txn } from "./txn";
8-
import { mergeLinReads } from "./util";
8+
import { mergeLinReads, stringifyMessage } from "./util";
99

1010
/**
1111
* Client is a transaction aware client to a set of Dgraph server instances.
1212
*/
1313
export class DgraphClient {
1414
private clients: DgraphClientStub[];
1515
private linRead: messages.LinRead;
16+
private debugMode: boolean = false;
1617

1718
/**
1819
* Creates a new Client for interacting with the Dgraph store.
@@ -40,8 +41,13 @@ export class DgraphClient {
4041
* 3. Drop the database.
4142
*/
4243
public async alter(op: messages.Operation): Promise<messages.Payload> {
44+
this.debug(`Alter request:\n${stringifyMessage(op)}`);
45+
4346
const c = this.anyClient();
44-
return await c.alter(op);
47+
const pl = await c.alter(op);
48+
this.debug(`Alter response:\n${stringifyMessage(pl)}`);
49+
50+
return pl;
4551
}
4652

4753
/**
@@ -51,6 +57,24 @@ export class DgraphClient {
5157
return new Txn(this);
5258
}
5359

60+
/**
61+
* setDebugMode switches on/off the debug mode which prints helpful debug messages
62+
* while performing alters, queries and mutations.
63+
*/
64+
public setDebugMode(mode: boolean = true): void {
65+
this.debugMode = mode;
66+
}
67+
68+
/**
69+
* debug prints a message on the console if debug mode is switched on.
70+
*/
71+
public debug(msg: string): void {
72+
if (this.debugMode) {
73+
// tslint:disable-next-line no-console
74+
console.log(msg);
75+
}
76+
}
77+
5478
public getLinRead(): messages.LinRead {
5579
const lr = new messages.LinRead();
5680
const idsMap = lr.getIdsMap();

src/txn.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as messages from "../generated/api_pb";
44

55
import { DgraphClient } from "./client";
66
import { ERR_ABORTED, ERR_FINISHED } from "./errors";
7-
import { isAbortedError, isConflictError, mergeLinReads } from "./util";
7+
import { isAbortedError, isConflictError, mergeLinReads, stringifyMessage } from "./util";
88

99
/**
1010
* Txn is a single atomic transaction.
@@ -50,6 +50,7 @@ export class Txn {
5050
vars?: { [k: string]: any } | null, // tslint:disable-line no-any
5151
): Promise<messages.Response> {
5252
if (this.finished) {
53+
this.dc.debug(`Query request (ERR_FINISHED):\nquery = ${q}\nvars = ${vars}`);
5354
throw ERR_FINISHED;
5455
}
5556

@@ -66,10 +67,13 @@ export class Txn {
6667
}
6768
});
6869
}
70+
this.dc.debug(`Query request:\n${stringifyMessage(req)}`);
6971

7072
const c = this.dc.anyClient();
7173
const res = await c.query(req);
7274
this.mergeContext(res.getTxn());
75+
this.dc.debug(`Query response:\n${stringifyMessage(res)}`);
76+
7377
return res;
7478
}
7579

@@ -87,11 +91,13 @@ export class Txn {
8791
*/
8892
public async mutate(mu: messages.Mutation): Promise<messages.Assigned> {
8993
if (this.finished) {
94+
this.dc.debug(`Mutate request (ERR_FINISHED):\nmutation = ${stringifyMessage(mu)}`);
9095
throw ERR_FINISHED;
9196
}
9297

9398
this.mutated = true;
9499
mu.setStartTs(this.ctx.getStartTs());
100+
this.dc.debug(`Mutate request:\n${stringifyMessage(mu)}`);
95101

96102
let ag: messages.Assigned;
97103
const c = this.dc.anyClient();
@@ -118,6 +124,8 @@ export class Txn {
118124
}
119125

120126
this.mergeContext(ag.getContext());
127+
this.dc.debug(`Mutate response:\n${stringifyMessage(ag)}`);
128+
121129
return ag;
122130
}
123131

src/util.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ export function mergeLinReads(target: messages.LinRead, src?: messages.LinRead |
2020
return target;
2121
}
2222

23-
export function promisify<A, T>(
24-
f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void,
25-
thisContext?: any, // tslint:disable-line no-any
26-
): (arg: A) => Promise<T> {
27-
return (arg: A) => {
28-
// tslint:disable-next-line no-any
29-
return new Promise((resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void): void => {
30-
f.call(
31-
thisContext,
32-
arg,
33-
(err?: Error | null, result?: T): void => (err != null) ? reject(err) : resolve(result),
34-
);
35-
});
36-
};
37-
}
38-
3923
export function errorCode(err: any): { valid: boolean, code: number } { // tslint:disable-line no-any
4024
if (
4125
err == null ||
@@ -64,3 +48,23 @@ export function isConflictError(err: any): boolean { // tslint:disable-line no-a
6448
const ec = errorCode(err);
6549
return ec.valid && (ec.code === grpc.status.ABORTED || ec.code === grpc.status.FAILED_PRECONDITION);
6650
}
51+
52+
export function promisify<A, T>(
53+
f: (arg: A, cb: (err?: Error | null, res?: T) => void) => void,
54+
thisContext?: any, // tslint:disable-line no-any
55+
): (arg: A) => Promise<T> {
56+
return (arg: A) => {
57+
// tslint:disable-next-line no-any
58+
return new Promise((resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void): void => {
59+
f.call(
60+
thisContext,
61+
arg,
62+
(err?: Error | null, result?: T): void => (err != null) ? reject(err) : resolve(result),
63+
);
64+
});
65+
};
66+
}
67+
68+
export function stringifyMessage(msg: jspb.Message): string {
69+
return JSON.stringify(msg.toObject());
70+
}

0 commit comments

Comments
 (0)