Skip to content

Commit

Permalink
docs: add examples for the article
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Zorkaltsev authored and Alexey Zorkaltsev committed Sep 14, 2023
1 parent 9c66dd7 commit a440f4c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/transaction-control/README.md
Original file line number Diff line number Diff line change
@@ -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 <example name>.ts
```
16 changes: 16 additions & 0 deletions examples/transaction-control/online-read-only.ts
Original file line number Diff line number Diff line change
@@ -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();
}
})();
19 changes: 19 additions & 0 deletions examples/transaction-control/serializable.ts
Original file line number Diff line number Diff line change
@@ -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();
}
})();
19 changes: 19 additions & 0 deletions examples/transaction-control/snapshot-read-only.ts
Original file line number Diff line number Diff line change
@@ -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();
}
})();
16 changes: 16 additions & 0 deletions examples/transaction-control/stale-read-only.ts
Original file line number Diff line number Diff line change
@@ -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();
}
})();

0 comments on commit a440f4c

Please sign in to comment.