Skip to content

Commit 0d4585a

Browse files
committed
Prepare for release and update documentation and examples
1 parent 11552a5 commit 0d4585a

File tree

15 files changed

+375
-151
lines changed

15 files changed

+375
-151
lines changed

README.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,17 @@ try {
133133
provides two main ways to set data: JSON and RDF N-Quad. You can choose whichever
134134
way is convenient.
135135

136-
We're going to use JSON. We define a person object to represent a person, serialize
137-
it as `Uint8Array` (or `base64`) and use it in `Mutation` object.
136+
We define a person object to represent a person and use it in a `Mutation` object.
138137

139138
```js
140139
// Create data.
141140
const p = {
142141
name: "Alice",
143142
};
144143

145-
// Serialize it.
146-
const json = JSON.stringify(p);
147-
148-
const serialized = new Uint8Array(new Buffer(json));
149-
// OR if you want to use base64
150-
// const serialized = new Buffer(json).toString("base64");
151-
152144
// Run mutation.
153145
const mu = new dgraph.Mutation();
154-
mu.setSetJson(serialized);
146+
mu.setSetJson(p);
155147
await txn.mutate(mu);
156148
```
157149

@@ -174,10 +166,8 @@ GraphQL+- query string. If you want to pass an additional map of any variables t
174166
you might want to set in the query, call `Txn#queryWithVars(string, object)` with
175167
the variables object as the second argument.
176168

177-
The response would contain `Response#getJSON_asU8()` and `Response#getJSON_asB64()`
178-
methods, which return the response JSON serialized as `Uint8Array` and `base64`
179-
respectively. You will need to deserialize it before you can do anything useful
180-
with it.
169+
The response would contain the method `Response#getJSON()`, which returns the response
170+
JSON.
181171

182172
Let’s run the following query with a variable $a:
183173

@@ -203,23 +193,17 @@ const query = `query all($a: string) {
203193
}`;
204194
const vars = { $a: "Alice" };
205195
const res = await dgraphClient.newTxn().queryWithVars(query, vars);
206-
207-
// Deserialize result.
208-
const jsonStr = new Buffer(res.getJson_asU8()).toString();
209-
// OR if you want to use base64
210-
// const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString();
211-
212-
const ppl = JSON.parse(jsonStr);
196+
const ppl = res.getJson();
213197

214198
// Print results.
215-
console.log(`people found: ${ppl.all.length}`);
199+
console.log(`Number of people named "Alice": ${ppl.all.length}`);
216200
ppl.all.forEach((person) => console.log(person.name));
217201
```
218202

219203
This should print:
220204

221205
```console
222-
people found: 1
206+
Number of people named "Alice": 1
223207
Alice
224208
```
225209

examples/simple/index-pre-v7.6.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,12 @@ function createData(dgraphClient) {
5959
]
6060
};
6161

62-
// Serialize it.
63-
const json = JSON.stringify(p);
64-
65-
const serialized = new Uint8Array(new Buffer(json));
66-
// OR if you want to use base64
67-
// const serialized = new Buffer(json).toString("base64");
68-
6962
let assigned;
7063
let err;
7164

7265
// Run mutation.
7366
const mu = new dgraph.Mutation();
74-
mu.setSetJson(serialized);
67+
mu.setSetJson(p);
7568
return txn.mutate(mu).then((res) => {
7669
assigned = res;
7770

@@ -121,12 +114,7 @@ function queryData(dgraphClient) {
121114
const vars = { $a: "Alice" };
122115

123116
return dgraphClient.newTxn().queryWithVars(query, vars).then((res) => {
124-
// Deserialize result.
125-
const jsonStr = new Buffer(res.getJson_asU8()).toString();
126-
// OR if you want to use base64
127-
// const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString();
128-
129-
const ppl = JSON.parse(jsonStr);
117+
const ppl = res.getJson();
130118

131119
// Print results.
132120
console.log(`Number of people named "Alice": ${ppl.all.length}`);

examples/simple/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,9 @@ async function createData(dgraphClient) {
6060
]
6161
};
6262

63-
// Serialize it.
64-
const json = JSON.stringify(p);
65-
66-
const serialized = new Uint8Array(new Buffer(json));
67-
// OR if you want to use base64
68-
// const serialized = new Buffer(json).toString("base64");
69-
7063
// Run mutation.
7164
const mu = new dgraph.Mutation();
72-
mu.setSetJson(serialized);
65+
mu.setSetJson(p);
7366
const assigned = await txn.mutate(mu);
7467

7568
// Commit transaction.
@@ -113,13 +106,7 @@ async function queryData(dgraphClient) {
113106
}`;
114107
const vars = { $a: "Alice" };
115108
const res = await dgraphClient.newTxn().queryWithVars(query, vars);
116-
117-
// Deserialize result.
118-
const jsonStr = new Buffer(res.getJson_asU8()).toString();
119-
// OR if you want to use base64
120-
// const jsonStr = new Buffer(res.getJson_asB64(), "base64").toString();
121-
122-
const ppl = JSON.parse(jsonStr);
109+
const ppl = res.getJson();
123110

124111
// Print results.
125112
console.log(`Number of people named "Alice": ${ppl.all.length}`);

examples/simple/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "simple",
33
"dependencies": {
4-
"dgraph-js": "^1.0.0",
4+
"dgraph-js": "^1.0.1",
55
"grpc": "^1.7.2"
66
}
77
}

lib/client.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as messages from "../generated/api_pb";
22
import { DgraphClientStub } from "./clientStub";
33
import { Txn } from "./txn";
4+
import * as types from "./types";
45
export declare class DgraphClient {
56
private clients;
67
private linRead;
78
private debugMode;
89
constructor(...clients: DgraphClientStub[]);
9-
alter(op: messages.Operation): Promise<messages.Payload>;
10+
alter(op: messages.Operation): Promise<types.Payload>;
1011
newTxn(): Txn;
1112
setDebugMode(mode?: boolean): void;
1213
debug(msg: string): void;
1314
getLinRead(): messages.LinRead;
1415
mergeLinReads(src?: messages.LinRead | null): void;
1516
anyClient(): DgraphClientStub;
1617
}
17-
export declare function deleteEdges(mu: messages.Mutation, uid: string, ...predicates: string[]): void;
18+
export declare function deleteEdges(mu: types.Mutation, uid: string, ...predicates: string[]): void;

lib/client.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3838
var messages = require("../generated/api_pb");
3939
var errors_1 = require("./errors");
4040
var txn_1 = require("./txn");
41+
var types = require("./types");
4142
var util_1 = require("./util");
4243
var DgraphClient = (function () {
4344
function DgraphClient() {
@@ -54,15 +55,16 @@ var DgraphClient = (function () {
5455
}
5556
DgraphClient.prototype.alter = function (op) {
5657
return __awaiter(this, void 0, void 0, function () {
57-
var c, pl;
58-
return __generator(this, function (_a) {
59-
switch (_a.label) {
58+
var c, pl, _a, _b;
59+
return __generator(this, function (_c) {
60+
switch (_c.label) {
6061
case 0:
6162
this.debug("Alter request:\n" + util_1.stringifyMessage(op));
6263
c = this.anyClient();
64+
_b = (_a = types).createPayload;
6365
return [4, c.alter(op)];
6466
case 1:
65-
pl = _a.sent();
67+
pl = _b.apply(_a, [_c.sent()]);
6668
this.debug("Alter response:\n" + util_1.stringifyMessage(pl));
6769
return [2, pl];
6870
}

lib/clientStub.js

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
11
"use strict";
2-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3-
return new (P || (P = Promise))(function (resolve, reject) {
4-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7-
step((generator = generator.apply(thisArg, _arguments || [])).next());
8-
});
9-
};
10-
var __generator = (this && this.__generator) || function (thisArg, body) {
11-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13-
function verb(n) { return function (v) { return step([n, v]); }; }
14-
function step(op) {
15-
if (f) throw new TypeError("Generator is already executing.");
16-
while (_) try {
17-
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18-
if (y = 0, t) op = [0, t.value];
19-
switch (op[0]) {
20-
case 0: case 1: t = op; break;
21-
case 4: _.label++; return { value: op[1], done: false };
22-
case 5: _.label++; y = op[1]; op = [0]; continue;
23-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
24-
default:
25-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29-
if (t[2]) _.ops.pop();
30-
_.trys.pop(); continue;
31-
}
32-
op = body.call(thisArg, _);
33-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35-
}
36-
};
372
Object.defineProperty(exports, "__esModule", { value: true });
383
var grpc = require("grpc");
394
var services = require("../generated/api_grpc_pb");
@@ -56,54 +21,19 @@ var DgraphClientStub = (function () {
5621
};
5722
}
5823
DgraphClientStub.prototype.alter = function (op) {
59-
return __awaiter(this, void 0, void 0, function () {
60-
return __generator(this, function (_a) {
61-
switch (_a.label) {
62-
case 0: return [4, this.promisified.alter(op)];
63-
case 1: return [2, _a.sent()];
64-
}
65-
});
66-
});
24+
return this.promisified.alter(op);
6725
};
6826
DgraphClientStub.prototype.query = function (req) {
69-
return __awaiter(this, void 0, void 0, function () {
70-
return __generator(this, function (_a) {
71-
switch (_a.label) {
72-
case 0: return [4, this.promisified.query(req)];
73-
case 1: return [2, _a.sent()];
74-
}
75-
});
76-
});
27+
return this.promisified.query(req);
7728
};
7829
DgraphClientStub.prototype.mutate = function (mu) {
79-
return __awaiter(this, void 0, void 0, function () {
80-
return __generator(this, function (_a) {
81-
switch (_a.label) {
82-
case 0: return [4, this.promisified.mutate(mu)];
83-
case 1: return [2, _a.sent()];
84-
}
85-
});
86-
});
30+
return this.promisified.mutate(mu);
8731
};
8832
DgraphClientStub.prototype.commitOrAbort = function (ctx) {
89-
return __awaiter(this, void 0, void 0, function () {
90-
return __generator(this, function (_a) {
91-
switch (_a.label) {
92-
case 0: return [4, this.promisified.commitOrAbort(ctx)];
93-
case 1: return [2, _a.sent()];
94-
}
95-
});
96-
});
33+
return this.promisified.commitOrAbort(ctx);
9734
};
9835
DgraphClientStub.prototype.checkVersion = function (check) {
99-
return __awaiter(this, void 0, void 0, function () {
100-
return __generator(this, function (_a) {
101-
switch (_a.label) {
102-
case 0: return [4, this.promisified.checkVersion(check)];
103-
case 1: return [2, _a.sent()];
104-
}
105-
});
106-
});
36+
return this.promisified.checkVersion(check);
10737
};
10838
return DgraphClientStub;
10939
}());

lib/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export * from "../generated/api_pb";
1+
export * from "./types";
2+
export { Operation, Request, Assigned, TxnContext, Check, Version, NQuad, Value, Facet, SchemaNode, LinRead, Latency } from "../generated/api_pb";
23
export * from "./client";
34
export * from "./clientStub";
45
export * from "./errors";

lib/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ function __export(m) {
33
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
44
}
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
__export(require("../generated/api_pb"));
6+
__export(require("./types"));
7+
var api_pb_1 = require("../generated/api_pb");
8+
exports.Operation = api_pb_1.Operation;
9+
exports.Request = api_pb_1.Request;
10+
exports.Assigned = api_pb_1.Assigned;
11+
exports.TxnContext = api_pb_1.TxnContext;
12+
exports.Check = api_pb_1.Check;
13+
exports.Version = api_pb_1.Version;
14+
exports.NQuad = api_pb_1.NQuad;
15+
exports.Value = api_pb_1.Value;
16+
exports.Facet = api_pb_1.Facet;
17+
exports.SchemaNode = api_pb_1.SchemaNode;
18+
exports.LinRead = api_pb_1.LinRead;
19+
exports.Latency = api_pb_1.Latency;
720
__export(require("./client"));
821
__export(require("./clientStub"));
922
__export(require("./errors"));

lib/txn.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import * as messages from "../generated/api_pb";
22
import { DgraphClient } from "./client";
3+
import * as types from "./types";
34
export declare class Txn {
45
private dc;
56
private ctx;
67
private finished;
78
private mutated;
89
constructor(dc: DgraphClient);
9-
query(q: string): Promise<messages.Response>;
10+
query(q: string): Promise<types.Response>;
1011
queryWithVars(q: string, vars?: {
1112
[k: string]: any;
12-
} | null): Promise<messages.Response>;
13-
mutate(mu: messages.Mutation): Promise<messages.Assigned>;
13+
} | null): Promise<types.Response>;
14+
mutate(mu: types.Mutation): Promise<messages.Assigned>;
1415
commit(): Promise<void>;
1516
discard(): Promise<void>;
1617
private mergeContext(src?);

0 commit comments

Comments
 (0)