|
| 1 | +import {Driver as YDB} from '../../src'; |
| 2 | +import {AnonymousAuthService} from "../../src/credentials/anonymous-auth-service"; |
| 3 | +import {Ydb} from "ydb-sdk-proto"; |
| 4 | +import {SimpleLogger} from "../../src/logger/simple-logger"; |
| 5 | +import {Context} from "../../src/context"; |
| 6 | + |
| 7 | +require('dotenv').config(); |
| 8 | + |
| 9 | +const DATABASE = '/local'; |
| 10 | +const ENDPOINT = process.env.YDB_ENDPOINT || 'grpc://localhost:2136'; |
| 11 | + |
| 12 | +async function main() { |
| 13 | + const db = new YDB({ |
| 14 | + endpoint: ENDPOINT, |
| 15 | + database: DATABASE, |
| 16 | + authService: new AnonymousAuthService(), |
| 17 | + logger: new SimpleLogger({envKey: 'YDB_TEST_LOG_LEVEL'}), |
| 18 | + }); |
| 19 | + if (!(await db.ready(3000))) throw new Error('Driver is not ready!'); |
| 20 | + await db.topic.createTopic({ |
| 21 | + path: 'demoTopic', |
| 22 | + consumers: [{ |
| 23 | + name: 'demo', |
| 24 | + }], |
| 25 | + }); |
| 26 | + const writer = await db.topic.createWriter({ |
| 27 | + path: 'demoTopic', |
| 28 | + // producerId: '...', // will be genereted automatically |
| 29 | + // messageGroupId: '...' // will be the same as producerId |
| 30 | + getLastSeqNo: true, // seqNo will be assigned automatically |
| 31 | + }); |
| 32 | + await writer.sendMessages({ |
| 33 | + codec: Ydb.Topic.Codec.CODEC_RAW, |
| 34 | + messages: [{ |
| 35 | + data: Buffer.from('Hello, world'), |
| 36 | + uncompressedSize: 'Hello, world'.length, |
| 37 | + }], |
| 38 | + }); |
| 39 | + const promises = []; |
| 40 | + for (let n = 0; n < 4; n++) { |
| 41 | + // ((writer as any).innerWriteStream as TopicWriteStreamWithEvents).close(Context.createNew().ctx, new Error('Fake error')); |
| 42 | + |
| 43 | + // await sleep(3000); // TODO: |
| 44 | + |
| 45 | + promises.push(writer.sendMessages({ |
| 46 | + codec: Ydb.Topic.Codec.CODEC_RAW, |
| 47 | + messages: [{ |
| 48 | + data: Buffer.from(`Message N${n}`), |
| 49 | + uncompressedSize: `Message N${n}`.length, |
| 50 | + }], |
| 51 | + })); |
| 52 | + } |
| 53 | + await Promise.all(promises); |
| 54 | + const reader = await db.topic.createReader(Context.createNew({ |
| 55 | + timeout: 3000, |
| 56 | + }).ctx, { |
| 57 | + topicsReadSettings: [{ |
| 58 | + path: 'demoTopic', |
| 59 | + }], |
| 60 | + consumer: 'demo', |
| 61 | + receiveBufferSizeInBytes: 10_000_000, |
| 62 | + }); |
| 63 | + for await (const message of reader.messages) { |
| 64 | + console.info(`Message: ${message.data!.toString()}`); |
| 65 | + await message.commit(); |
| 66 | + } |
| 67 | + await reader.close(); // graceful close() - complete when all messages are commited |
| 68 | +} |
| 69 | + |
| 70 | +main(); |
0 commit comments