-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexey Zorkaltsev
committed
Mar 24, 2024
1 parent
8d433b9
commit f7a5958
Showing
41 changed files
with
6,220 additions
and
3,493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ build/ | |
|
||
# secrets | ||
secrets/* | ||
|
||
# jest | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {declareType, TypedData, Types} from "../../src"; | ||
|
||
interface IRow { | ||
id: number; | ||
rowTitle: string; | ||
time: Date; | ||
} | ||
|
||
export class Row extends TypedData { | ||
@declareType(Types.UINT64) | ||
public id: number; | ||
|
||
@declareType(Types.UTF8) | ||
public rowTitle: string; | ||
|
||
@declareType(Types.DATETIME) | ||
public time: Date; | ||
|
||
constructor(data: IRow) { | ||
super(data); | ||
this.id = data.id; | ||
this.rowTitle = data.rowTitle; | ||
this.time = data.time; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
process.env.YDB_SDK_PRETTY_LOGS = '1'; | ||
|
||
import { | ||
Driver, | ||
getCredentialsFromEnv, | ||
Logger, | ||
TypedValues, | ||
} from 'ydb-sdk'; | ||
import {Row} from './data-helpers'; | ||
import {main} from '../utils'; | ||
|
||
const TABLE = 'table'; | ||
|
||
async function createTestTable(driver: Driver) { | ||
await driver.queryClient.do({ | ||
fn: async (session) => { | ||
await session.execute({ | ||
text: ` | ||
DROP TABLE IF EXISTS ${TABLE}; | ||
CREATE TABLE ${TABLE} | ||
( | ||
id UInt64, | ||
title Utf8, | ||
time Timestamp, | ||
PRIMARY KEY (id) | ||
);`, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
async function insert(driver: Driver) { | ||
await driver.queryClient.do({ | ||
fn: async (session) => { | ||
await session.execute({ | ||
parameters: { | ||
'$id1': TypedValues.uint64(1), | ||
'$title1': TypedValues.text('Some title1'), | ||
'$id2': TypedValues.uint64(2), | ||
'$title2': TypedValues.text('Some title2'), | ||
'$timestamp': TypedValues.timestamp(new Date()), | ||
}, | ||
text: ` | ||
INSERT INTO ${TABLE} (id, title, time) | ||
VALUES ($id1, $title1, $timestamp); | ||
INSERT INTO ${TABLE} (id, title, time) | ||
VALUES ($id2, $title2, $timestamp);`, | ||
}); | ||
} | ||
}); | ||
return 2; | ||
} | ||
|
||
async function select(driver: Driver) { | ||
const res = await driver.queryClient.do({ | ||
fn: async (session) => { | ||
const res = await session.execute({ | ||
text: ` | ||
SELECT * | ||
FROM ${TABLE}; | ||
SELECT * -- double | ||
FROM ${TABLE}; ` | ||
}); | ||
let rowCount = 0; | ||
for await (const resultSet of res.resultSets) { | ||
console.info(`ResultSet index: ${resultSet.index}`) | ||
for await (const row of resultSet.rows) { | ||
rowCount++; | ||
console.info(`row: ${row}`); | ||
} | ||
} | ||
return rowCount; | ||
} | ||
}); | ||
console.info(`rowCount: ${res}`); | ||
} | ||
|
||
async function typedSelect(driver: Driver) { | ||
const res = await driver.queryClient.do({ | ||
fn: async (session) => { | ||
const res = await session.execute({ | ||
text: ` | ||
SELECT * | ||
FROM ${TABLE}; | ||
SELECT * -- double | ||
FROM ${TABLE}; ` | ||
}); | ||
let rowCount = 0; | ||
for await (const resultSet of res.resultSets) { | ||
console.info(`ResultSet index: ${resultSet.index}`) | ||
for await (const row of resultSet.typedRows(Row)) { | ||
rowCount++; | ||
console.info(`row: ${row}`); | ||
} | ||
} | ||
return rowCount; | ||
} | ||
}); | ||
console.info(`rowCount: ${res}`); | ||
} | ||
|
||
async function bulkUpsert(driver: Driver) { | ||
await driver.queryClient.do({ | ||
fn: async (session) => { | ||
function* dataGenerator(rowsCount: number) { | ||
for (let id = 1; id <= rowsCount; id++) | ||
yield new Row({ | ||
id, | ||
rowTitle: `title_${id}`, | ||
time: new Date(), | ||
}) | ||
} | ||
|
||
await session.execute({ | ||
text: ` | ||
UPSERT INTO ${TABLE} (id, title, time) | ||
SELECT id, title, time FROM AS_TABLE($table)`, | ||
parameters: { | ||
'$table': Row.asTypedCollection([...dataGenerator(20)]), | ||
} | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
async function run(logger: Logger, endpoint: string, database: string) { | ||
const authService = getCredentialsFromEnv(); | ||
logger.debug('Driver initializing...'); | ||
const driver = new Driver({endpoint, database, authService}); | ||
const timeout = 10000; | ||
if (!await driver.ready(timeout)) { | ||
logger.fatal(`Driver has not become ready in ${timeout}ms!`); | ||
process.exit(1); | ||
} | ||
|
||
createTestTable(driver); | ||
insert(driver); | ||
select(driver); | ||
bulkUpsert(driver); | ||
typedSelect(driver); | ||
|
||
// transactions | ||
|
||
await driver.destroy(); | ||
} | ||
|
||
main(run); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const config = require('./jest.config.development'); | ||
|
||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
module.exports = { | ||
...config, | ||
collectCoverage: true, | ||
collectCoverageFrom: [ | ||
'**/*.{js,ts}', | ||
], | ||
coverageDirectory: './coverage', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.