Skip to content

Commit be4a7cb

Browse files
fix example
1 parent 3638e05 commit be4a7cb

21 files changed

+122
-98
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Using the `open` function with a connection string:
8080

8181
```js
8282
// open a connection to an ACL-enabled, non-TLS cluster and login as groot
83-
const client = dgraph.open("dgraph://groot:password@localhost:8090")
83+
const client = await dgraph.open("dgraph://groot:password@localhost:8090")
8484
// Use the client
8585

8686
// this will close all the client stubs
@@ -381,7 +381,7 @@ const SERVER_ADDR = "localhost:9080"
381381
const SERVER_CREDENTIALS = grpc.credentials.createInsecure()
382382

383383
// Create instances of DgraphClient.
384-
const client = dgraph.open("dgraph://groot:password@${SERVER_ADDR}")
384+
const client = await dgraph.open("dgraph://groot:password@${SERVER_ADDR}")
385385

386386
// ...
387387
// Use dgraphClient

examples/simple/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function queryData(dgraphClient) {
115115
}
116116

117117
async function main() {
118-
const dgraphClient = dgraph.open()
118+
const dgraphClient = await dgraph.open()
119119
await dropAll(dgraphClient)
120120
await setSchema(dgraphClient)
121121
await createData(dgraphClient)

lib/client.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
import * as grpc from "@grpc/grpc-js"
72
import * as messages from "../generated/api_pb"
83
import { DgraphClientStub } from "./clientStub"
@@ -21,6 +16,8 @@ export declare class DgraphClient {
2116
setDebugMode(mode?: boolean): void
2217
debug(msg: string): void
2318
anyClient(): DgraphClientStub
19+
close(): void
2420
}
2521
export declare function isJwtExpired(err: any): boolean
2622
export declare function deleteEdges(mu: types.Mutation, uid: string, ...predicates: string[]): void
23+
export declare function open(connStr: string): Promise<DgraphClient>

lib/client.js

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
"use strict"
72
var __awaiter =
83
(this && this.__awaiter) ||
@@ -141,11 +136,18 @@ Object.defineProperty(exports, "__esModule", { value: true })
141136
exports.DgraphClient = void 0
142137
exports.isJwtExpired = isJwtExpired
143138
exports.deleteEdges = deleteEdges
139+
exports.open = open
140+
var grpc = require("@grpc/grpc-js")
144141
var messages = require("../generated/api_pb")
142+
var clientStub_1 = require("./clientStub")
145143
var errors_1 = require("./errors")
146144
var txn_1 = require("./txn")
147145
var types = require("./types")
148146
var util_1 = require("./util")
147+
var dgraphScheme = "dgraph:"
148+
var sslModeDisable = "disable"
149+
var sslModeRequire = "require"
150+
var sslModeVerifyCA = "verify-ca"
149151
var DgraphClient = (function () {
150152
function DgraphClient() {
151153
var clients = []
@@ -220,6 +222,16 @@ var DgraphClient = (function () {
220222
DgraphClient.prototype.anyClient = function () {
221223
return this.clients[Math.floor(Math.random() * this.clients.length)]
222224
}
225+
DgraphClient.prototype.close = function () {
226+
this.clients.forEach(function (clientStub) {
227+
try {
228+
clientStub.close()
229+
console.log("Closed client stub successfully")
230+
} catch (error) {
231+
console.error("Failed to close client stub:", error)
232+
}
233+
})
234+
}
223235
return DgraphClient
224236
})()
225237
exports.DgraphClient = DgraphClient
@@ -245,3 +257,99 @@ function deleteEdges(mu, uid) {
245257
mu.addDel(nquad)
246258
}
247259
}
260+
function addApiKeyToCredentials(baseCreds, apiKey) {
261+
var metaCreds = grpc.credentials.createFromMetadataGenerator(function (_, callback) {
262+
var metadata = new grpc.Metadata()
263+
metadata.add("authorization", apiKey)
264+
callback(null, metadata)
265+
})
266+
return grpc.credentials.combineChannelCredentials(baseCreds, metaCreds)
267+
}
268+
function addBearerTokenToCredentials(baseCreds, bearerToken) {
269+
var metaCreds = grpc.credentials.createFromMetadataGenerator(function (_, callback) {
270+
var metadata = new grpc.Metadata()
271+
metadata.add("Authorization", "Bearer ".concat(bearerToken))
272+
callback(null, metadata)
273+
})
274+
return grpc.credentials.combineChannelCredentials(baseCreds, metaCreds)
275+
}
276+
function open(connStr) {
277+
return __awaiter(this, void 0, void 0, function () {
278+
var parsedUrl, host, port, queryParams, sslMode, credentials, clientStub, err_1
279+
return __generator(this, function (_a) {
280+
switch (_a.label) {
281+
case 0:
282+
parsedUrl = new URL(connStr)
283+
if (parsedUrl.protocol !== dgraphScheme) {
284+
throw new Error("Invalid scheme: must start with dgraph://")
285+
}
286+
host = parsedUrl.hostname
287+
port = parsedUrl.port
288+
if (!host) {
289+
throw new Error("Invalid connection string: hostname required")
290+
}
291+
if (!port) {
292+
throw new Error("Invalid connection string: port required")
293+
}
294+
queryParams = {}
295+
if (parsedUrl.searchParams) {
296+
parsedUrl.searchParams.forEach(function (value, key) {
297+
queryParams[key] = value
298+
})
299+
}
300+
if (queryParams.apikey && queryParams.bearertoken) {
301+
throw new Error("Both apikey and bearertoken cannot be provided")
302+
}
303+
sslMode = queryParams.sslmode
304+
if (sslMode === undefined) {
305+
sslMode = sslModeDisable
306+
}
307+
switch (sslMode) {
308+
case sslModeDisable:
309+
credentials = grpc.credentials.createInsecure()
310+
break
311+
case sslModeRequire:
312+
credentials = grpc.credentials.createSsl(null, null, null, {
313+
checkServerIdentity: function () {
314+
return undefined
315+
},
316+
})
317+
break
318+
case sslModeVerifyCA:
319+
credentials = grpc.credentials.createSsl()
320+
break
321+
default:
322+
throw new Error(
323+
"Invalid SSL mode: ".concat(
324+
sslMode,
325+
" (must be one of disable, require, verify-ca)",
326+
),
327+
)
328+
}
329+
if (queryParams.apikey) {
330+
credentials = addApiKeyToCredentials(credentials, queryParams.apikey)
331+
} else if (queryParams.bearertoken) {
332+
credentials = addBearerTokenToCredentials(credentials, queryParams.bearertoken)
333+
}
334+
clientStub = new clientStub_1.DgraphClientStub(
335+
"".concat(host, ":").concat(port),
336+
credentials,
337+
)
338+
if (!(parsedUrl.username != "")) return [3, 4]
339+
if (!(parsedUrl.password === "")) return [3, 1]
340+
throw new Error("Invalid connection string: password required when username is provided")
341+
case 1:
342+
_a.trys.push([1, 3, , 4])
343+
return [4, clientStub.login(parsedUrl.username, parsedUrl.password)]
344+
case 2:
345+
_a.sent()
346+
return [3, 4]
347+
case 3:
348+
err_1 = _a.sent()
349+
throw new Error("Failed to sign in user: ".concat(err_1.message))
350+
case 4:
351+
return [2, new DgraphClient(clientStub)]
352+
}
353+
})
354+
})
355+
}

lib/clientStub.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
import * as grpc from "@grpc/grpc-js"
72
import * as messages from "../generated/api_pb"
83
export declare class DgraphClientStub {

lib/clientStub.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
"use strict"
72
var __awaiter =
83
(this && this.__awaiter) ||

lib/clientStubFromSlash.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
import { DgraphClientStub } from "./clientStub"
72
export declare function clientStubFromSlashGraphQLEndpoint(
83
graphqlEndpoint: string,

lib/clientStubFromSlash.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
"use strict"
72
Object.defineProperty(exports, "__esModule", { value: true })
83
exports.clientStubFromSlashGraphQLEndpoint = clientStubFromSlashGraphQLEndpoint

lib/dgraph.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
export * from "./types"
72
export {
83
Operation,

lib/dgraph.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
61
"use strict"
72
var __createBinding =
83
(this && this.__createBinding) ||

0 commit comments

Comments
 (0)