From d3cbcc77f6c53ebe5baf7ab8aed166c840fd0cea Mon Sep 17 00:00:00 2001 From: Alexey Zorkaltsev Date: Thu, 14 Sep 2023 18:59:41 +0300 Subject: [PATCH] docs: add examples for the article --- examples/transaction-control/README.md | 13 +++++++++++++ .../transaction-control/online-read-only.ts | 16 ++++++++++++++++ examples/transaction-control/serializable.ts | 19 +++++++++++++++++++ .../transaction-control/snapshot-read-only.ts | 19 +++++++++++++++++++ .../transaction-control/stale-read-only.ts | 16 ++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 examples/transaction-control/README.md create mode 100644 examples/transaction-control/online-read-only.ts create mode 100644 examples/transaction-control/serializable.ts create mode 100644 examples/transaction-control/snapshot-read-only.ts create mode 100644 examples/transaction-control/stale-read-only.ts diff --git a/examples/transaction-control/README.md b/examples/transaction-control/README.md new file mode 100644 index 00000000..d43eb443 --- /dev/null +++ b/examples/transaction-control/README.md @@ -0,0 +1,13 @@ +Examples in the folder are for the [article](https://ydb.tech/ru/docs/reference/ydb-sdk/recipes/tx-control) *Setting the transaction execution mode* + +To run examples + +Define *YDB_CONNECTION_STRING* and *YDB_TOKEN* environment variables + +Make new project by *npm i ydb-sdk* in clear folder + +And copy example code to this project + +``` +npx ts-node .ts +``` diff --git a/examples/transaction-control/online-read-only.ts b/examples/transaction-control/online-read-only.ts new file mode 100644 index 00000000..15d83e7a --- /dev/null +++ b/examples/transaction-control/online-read-only.ts @@ -0,0 +1,16 @@ +import {Driver, TokenAuthService} from 'ydb-sdk'; + +(async function () { + const driver = new Driver({ + connectionString: process.env.YDB_CONNECTION_STRING, + authService: new TokenAuthService(process.env.YDB_TOKEN as string), + }); + try { + await driver.tableClient.withSession(async (session) => { + const preparedQuery = await session.prepareQuery("SELECT 1"); + await session.executeQuery(preparedQuery, {}, {beginTx: {onlineReadOnly: {allowInconsistentReads: false}}, commitTx: true}); + }); + } finally { + await driver.destroy(); + } +})(); diff --git a/examples/transaction-control/serializable.ts b/examples/transaction-control/serializable.ts new file mode 100644 index 00000000..43635b69 --- /dev/null +++ b/examples/transaction-control/serializable.ts @@ -0,0 +1,19 @@ +import {Driver, TokenAuthService} from 'ydb-sdk'; + +(async function () { + const driver = new Driver({ + connectionString: process.env.YDB_CONNECTION_STRING, + authService: new TokenAuthService(process.env.YDB_TOKEN as string), + }); + try { + await driver.tableClient.withSession(async (session) => { + const preparedQuery = await session.prepareQuery("SELECT 1"); + const txMeta = await session.beginTransaction({serializableReadWrite: {}}); + const txId = txMeta.id as string; + await session.executeQuery(preparedQuery, {}, {txId}); + await session.commitTransaction({txId}); + }); + } finally { + await driver.destroy(); + } +})(); diff --git a/examples/transaction-control/snapshot-read-only.ts b/examples/transaction-control/snapshot-read-only.ts new file mode 100644 index 00000000..bb1537b8 --- /dev/null +++ b/examples/transaction-control/snapshot-read-only.ts @@ -0,0 +1,19 @@ +import {Driver, TokenAuthService} from 'ydb-sdk'; + +(async function () { + const driver = new Driver({ + connectionString: process.env.YDB_CONNECTION_STRING, + authService: new TokenAuthService(process.env.YDB_TOKEN as string), + }); + try { + await driver.tableClient.withSession(async (session) => { + const preparedQuery = await session.prepareQuery("SELECT 1"); + const txMeta = await session.beginTransaction({snapshotReadOnly: {}}); + const txId = txMeta.id as string; + await session.executeQuery(preparedQuery, {}, {txId}); + await session.commitTransaction({txId}); + }); + } finally { + await driver.destroy(); + } +})(); diff --git a/examples/transaction-control/stale-read-only.ts b/examples/transaction-control/stale-read-only.ts new file mode 100644 index 00000000..67660162 --- /dev/null +++ b/examples/transaction-control/stale-read-only.ts @@ -0,0 +1,16 @@ +import {Driver, TokenAuthService} from 'ydb-sdk'; + +(async function () { + const driver = new Driver({ + connectionString: process.env.YDB_CONNECTION_STRING, + authService: new TokenAuthService(process.env.YDB_TOKEN as string), + }); + try { + await driver.tableClient.withSession(async (session) => { + const preparedQuery = await session.prepareQuery("SELECT 1"); + await session.executeQuery(preparedQuery, {}, {beginTx: {staleReadOnly: {}}, commitTx: true}); + }); + } finally { + await driver.destroy(); + } +})();