App in Node.js

This page contains a detailed description of the code of a test app that is available as part of the YDB Node.js SDK.

Initializing a database connection

To interact with YDB, create an instance of the driver, client, and session:

  • The YDB driver lets the app and YDB interact at the transport layer. The driver must exist throughout the YDB access lifecycle and be initialized before creating a client or session.
  • The YDB client runs on top of the YDB driver and enables the handling of entities and transactions.
  • The YDB session contains information about executed transactions and prepared queries, and is part of the YDB client context.

App code snippet for driver initialization:

  1. const authService = getCredentialsFromEnv();
  2. logger.debug('Driver initializing...');
  3. const driver = new Driver({endpoint, database, authService});
  4. const timeout = 10000;
  5. if (!await driver.ready(timeout)) {
  6. logger.fatal(`Driver has not become ready in ${timeout}ms!`);
  7. process.exit(1);
  8. }

Node.js - 图1

App code snippet for creating a session:

  1. await driver.tableClient.withSession(async (session) => {
  2. ...
  3. });

Node.js - 图2

Creating tables

Creating tables to be used in operations on a test app. This step results in the creation of DB tables of the series directory data model:

  • Series
  • Seasons
  • Episodes

Once the tables are created, the method for getting information about data schema objects is called and the result of its execution is output.

To create tables, use the Session.CreateTable() method:

  1. async function createTables(session: Session, logger: Logger) {
  2. logger.info('Creating tables...');
  3. await session.createTable(
  4. 'series',
  5. new TableDescription()
  6. .withColumn(new Column(
  7. 'series_id',
  8. Types.optional(Types.UINT64),
  9. ))
  10. .withColumn(new Column(
  11. 'title',
  12. Types.optional(Types.UTF8),
  13. ))
  14. .withColumn(new Column(
  15. 'series_info',
  16. Types.optional(Types.UTF8),
  17. ))
  18. .withColumn(new Column(
  19. 'release_date',
  20. Types.optional(Types.DATE),
  21. ))
  22. .withPrimaryKey('series_id')
  23. );
  24. await session.createTable(
  25. 'seasons',
  26. new TableDescription()
  27. .withColumn(new Column(
  28. 'series_id',
  29. Types.optional(Types.UINT64),
  30. ))
  31. .withColumn(new Column(
  32. 'season_id',
  33. Types.optional(Types.UINT64),
  34. ))
  35. .withColumn(new Column(
  36. 'title',
  37. Types.optional(Types.UTF8),
  38. ))
  39. .withColumn(new Column(
  40. 'first_aired',
  41. Types.optional(Types.DATE),
  42. ))
  43. .withColumn(new Column(
  44. 'last_aired',
  45. Types.optional(Types.DATE),
  46. ))
  47. .withPrimaryKeys('series_id', 'season_id')
  48. );
  49. await session.createTable(
  50. 'episodes',
  51. new TableDescription()
  52. .withColumn(new Column(
  53. 'series_id',
  54. Types.optional(Types.UINT64),
  55. ))
  56. .withColumn(new Column(
  57. 'season_id',
  58. Types.optional(Types.UINT64),
  59. ))
  60. .withColumn(new Column(
  61. 'episode_id',
  62. Types.optional(Types.UINT64),
  63. ))
  64. .withColumn(new Column(
  65. 'title',
  66. Types.optional(Types.UTF8),
  67. ))
  68. .withColumn(new Column(
  69. 'air_date',
  70. Types.optional(Types.DATE),
  71. ))
  72. .withPrimaryKeys('series_id', 'season_id', 'episode_id')
  73. );
  74. }

Node.js - 图3

You can use the Session.DescribeTable() method to output information about the table structure and make sure that it was properly created:

  1. async function describeTable(session: Session, tableName: string, logger: Logger) {
  2. const result = await session.describeTable(tableName);
  3. for (const column of result.columns) {
  4. logger.info(`Column name '${column.name}' has type ${JSON.stringify(column.type)}`);
  5. }
  6. }
  7. await describeTable(session, 'series', logger);
  8. await describeTable(session, 'seasons', logger);
  9. await describeTable(session, 'episodes', logger);

Node.js - 图4

Adding data

Adding data to the created tables using an UPSERT statement of YQL. A data update request is sent within a single request to the server with transaction auto-commit mode enabled.

Code snippet for inserting and updating data:

  1. async function upsertSimple(session: Session, logger: Logger): Promise<void> {
  2. const query = `
  3. ${SYNTAX_V1}
  4. UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES
  5. (2, 6, 1, "TBD");`;
  6. logger.info('Making an upsert...');
  7. await session.executeQuery(query);
  8. logger.info('Upsert completed');
  9. }

Node.js - 图5

Retrieving data with a Select

Retrieving data using a SELECT statement in YQL. Handling the retrieved data selection in the app.

To execute YQL queries, use the Session.executeQuery() method.

  1. async function selectSimple(session: Session, logger: Logger): Promise<void> {
  2. const query = `
  3. ${SYNTAX_V1}
  4. SELECT series_id,
  5. title,
  6. release_date
  7. FROM series
  8. WHERE series_id = 1;`;
  9. logger.info('Making a simple select...');
  10. const {resultSets} = await session.executeQuery(query);
  11. const result = Series.createNativeObjects(resultSets[0]);
  12. logger.info(`selectSimple result: ${JSON.stringify(result, null, 2)}`);
  13. }

Node.js - 图6

Parameterized queries

Querying data using parameters. This query execution option is preferable as it allows the server to reuse the query execution plan for subsequent calls and also protects from such vulnerabilities as SQL Injection.

The code snippet below shows the use of queries prepared with Session.prepareQuery() and parameters in the Session.executeQuery() method.

  1. async function selectPrepared(session: Session, data: ThreeIds[], logger: Logger): Promise<void> {
  2. const query = `
  3. ${SYNTAX_V1}
  4. DECLARE $seriesId AS Uint64;
  5. DECLARE $seasonId AS Uint64;
  6. DECLARE $episodeId AS Uint64;
  7. SELECT title,
  8. air_date
  9. FROM episodes
  10. WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;`;
  11. async function select() {
  12. logger.info('Preparing query...');
  13. const preparedQuery = await session.prepareQuery(query);
  14. logger.info('Selecting prepared query...');
  15. for (const [seriesId, seasonId, episodeId] of data) {
  16. const episode = new Episode({seriesId, seasonId, episodeId, title: '', airDate: new Date()});
  17. const {resultSets} = await session.executeQuery(preparedQuery, {
  18. '$seriesId': episode.getTypedValue('seriesId'),
  19. '$seasonId': episode.getTypedValue('seasonId'),
  20. '$episodeId': episode.getTypedValue('episodeId')
  21. });
  22. const result = Series.createNativeObjects(resultSets[0]);
  23. logger.info(`Select prepared query ${JSON.stringify(result, null, 2)}`);
  24. }
  25. }
  26. await withRetries(select);
  27. }

Node.js - 图7

Scan queries

Making a scan query that results in a data stream. Streaming lets you read an unlimited number of rows and amount of data.

  1. async function executeScanQueryWithParams(session: Session, logger: Logger): Promise<void> {
  2. const query = `
  3. ${SYNTAX_V1}
  4. DECLARE $value AS Utf8;
  5. SELECT key
  6. FROM ${TABLE}
  7. WHERE value = $value;`;
  8. logger.info('Making a stream execute scan query...');
  9. const params = {
  10. '$value': TypedValues.utf8('odd'),
  11. };
  12. let count = 0;
  13. await session.streamExecuteScanQuery(query, (result) => {
  14. logger.info(`Stream scan query partial result #${++count}: ${formatPartialResult(result)}`);
  15. }, params);
  16. logger.info(`Stream scan query completed, partial result count: ${count}`);
  17. }

Node.js - 图8

Managing transactions

Transactions are managed through TCL Begin and Commit calls.

In most cases, instead of explicitly using Begin and Commit calls, it’s better to use transaction control parameters in execute calls. This helps you avoid unnecessary requests to YDB and run your queries more efficiently.

Code snippet for Session.beginTransaction() and Session.commitTransaction() calls for beginning and ending a transaction:

  1. async function explicitTcl(session: Session, ids: ThreeIds, logger: Logger) {
  2. const query = `
  3. ${SYNTAX_V1}
  4. DECLARE $seriesId AS Uint64;
  5. DECLARE $seasonId AS Uint64;
  6. DECLARE $episodeId AS Uint64;
  7. UPDATE episodes
  8. SET air_date = CurrentUtcDate()
  9. WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;`;
  10. async function update() {
  11. logger.info('Running prepared query with explicit transaction control...');
  12. const preparedQuery = await session.prepareQuery(query);
  13. const txMeta = await session.beginTransaction({serializableReadWrite: {}});
  14. const [seriesId, seasonId, episodeId] = ids;
  15. const episode = new Episode({seriesId, seasonId, episodeId, title: '', airDate: new Date()});
  16. const params = {
  17. '$seriesId': episode.getTypedValue('seriesId'),
  18. '$seasonId': episode.getTypedValue('seasonId'),
  19. '$episodeId': episode.getTypedValue('episodeId')
  20. };
  21. const txId = txMeta.id as string;
  22. logger.info(`Executing query with txId ${txId}.`);
  23. await session.executeQuery(preparedQuery, params, {txId});
  24. await session.commitTransaction({txId});
  25. logger.info(`TxId ${txId} committed.`);
  26. }
  27. await withRetries(update);
  28. }

Node.js - 图9

Handling errors

For more information about error handling, see Error handling in the API.