1.0.0 • Published 4 years ago

kinesis-core v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Kinesis Core

A base library for all kinesis event producers & consumers to use. It enforces some basic standards (ie. JSON serialization) for kinesis event payloads and provides some utility functions. It is built around AWS-SDK v3, and will not support previous versions.

Basic Usage

Full examples can be found in the examples/ directory.

Producer

// A record of sensor data
export type TemperatureData = { // Must conform to JSONObject type
    temperature: number
    source: string
}

export class TemperatureRecord extends TopicKinesisDataRecord<TemperatureData> {
    static topic = "sensor/temperature"
    override topic = TemperatureRecord.topic // Setting topic as a constant value
    // TODO('Current limitation where constructor can't be extended at the moment, the default one provided by TopicKinesisDataRecord is required.')
}

// Create new record
const tempRecord = new TemperatureRecord({ 
    topic: TemperatureRecord.topic, 
    data: { temperature: 1, source: 'code' }
});

// Serialize and put record into Kinesis
await new KinesisClient({})
    .send(new PutRecordCommand({
        Data: tempRecord.serialize(), // Serialize the record into binary data for Kinesis
        PartitionKey: 'some_partition_key',
        StreamName: 'your_stream_name'
    }));

Consumer

class SensorRecordConsumer {
    handleTempRecord(event: AWSLambdaKinesisRecord) { // Assume the TemperatureRecord has been filtered out
        const temperatureRecord = TemperatureRecord.deserialize(
            event.kinesis.data,
            TemperatureRecord
        )
        console.log(`Print out record to console.
            temperature: ${temperatureRecord.data.temperature},
            source: ${temperatureRecord.data.source},
        `);
    }
}

tsconfig.json

Some notes about the typescript configuration.

  • rootDir - set to ./src. All source code needs to be placed in the ./src folder.
  • outDir - the output directory of compiled files is set to the ./dist folder.
  • exclude - set to exclude all files in ./tests from compilation. ts-jest will transpile them for jest.
1.0.0

4 years ago