Skip to content

Commit 0982a58

Browse files
polRkCopilot
andauthored
feat: update js/ts connection example to use new sdk (#2974)
Signed-off-by: Vladislav Polyakov <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 55a1fc6 commit 0982a58

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/components/ConnectToDB/ConnectToDBDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const connectionTabs: {id: SnippetLanguage; title: string}[] = [
2828
{id: 'csharp', title: 'C# (.NET)'},
2929
{id: 'go', title: 'Go'},
3030
{id: 'java', title: 'Java'},
31-
{id: 'javascript', title: 'Node JS'},
31+
{id: 'javascript', title: 'JavaScript/TypeScript'},
3232
{id: 'php', title: 'PHP'},
3333
{id: 'python', title: 'Python'},
3434
];

src/components/ConnectToDB/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"docs_dotnet": "https://ydb.tech/docs/en/dev/example-app/example-dotnet",
1212
"docs_go": "https://ydb.tech/docs/en/dev/example-app/go",
1313
"docs_java": "https://ydb.tech/docs/en/dev/example-app/java",
14-
"docs_nodejs": "https://ydb.tech/docs/en/dev/example-app/example-nodejs",
14+
"docs_nodejs": "https://github.com/ydb-platform/ydb-js-examples",
1515
"docs_php": "https://ydb.tech/docs/en/dev/example-app/example-php",
1616
"docs_python": "https://ydb.tech/docs/en/dev/example-app/python"
1717
}

src/components/ConnectToDB/snippets.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,26 @@ public class YDBConnect {
123123
}
124124

125125
export function getNodeJSSnippetCode({database, endpoint}: SnippetParams) {
126-
return `const {Driver, getCredentialsFromEnv, getLogger} = require('ydb-sdk');
126+
return `// Requires Node.js 20.19 or higher (also works with Deno/Bun)
127+
// Documentation: https://ydb.js.org
128+
// Source code: https://github.com/ydb-platform/ydb-js-sdk
129+
// Examples: https://github.com/ydb-platform/ydb-js-examples
130+
import {Driver} from '@ydbjs/core';
131+
import {query} from '@ydbjs/query';
132+
import type {ResultSet} from '@ydbjs/query';
127133
128-
const logger = getLogger({level: 'debug'});
129-
const endpoint = '${endpoint ?? '<endpoint>'}';
130-
const database = '${database ?? '/<database>'}';
131-
const authService = getCredentialsFromEnv();
132-
const driver = new Driver({endpoint, database, authService});
134+
const connectionString = '${endpoint ?? '<endpoint>'}${database ?? '/<database>'}';
135+
136+
const driver = new Driver(connectionString);
133137
134138
async function run() {
135-
if (!await driver.ready(100)) {
136-
logger.fatal('Driver has not become ready in 10 seconds!');
137-
process.exit(1);
138-
}
139-
140-
await driver.tableClient.withSession(async (session) => {
141-
res = await session.executeQuery("SELECT 'Hello, world!'")
142-
console.log(res.resultSets[0].rows[0].items[0].bytesValue.toString())
143-
return
144-
});
145-
146-
process.exit(0)
139+
await driver.ready();
140+
const sql = query(driver);
141+
142+
const resultSets: ResultSet[] = await sql\`SELECT 'Hello, world!' AS message;\`;
143+
console.log(resultSets[0].rows[0].message);
144+
145+
await driver.close();
147146
}
148147
149148
run();`;
@@ -169,7 +168,7 @@ $config = [
169168
// 'root_cert_file' => './CA.pem', Root CA file (uncomment for dedicated server only)
170169
],
171170
172-
'credentials' => new AccessTokenAuthentication('<token>') // use from reference/ydb-sdk/auth
171+
'credentials' => new AccessTokenAuthentication('<token>')
173172
];
174173
175174
$ydb = new Ydb($config);`;

0 commit comments

Comments
 (0)