Skip to content

Instantly share code, notes, and snippets.

@filmaj
Last active April 6, 2024 03:20
Show Gist options
  • Select an option

  • Save filmaj/e4965efbc1aae013d611b00231325301 to your computer and use it in GitHub Desktop.

Select an option

Save filmaj/e4965efbc1aae013d611b00231325301 to your computer and use it in GitHub Desktop.

Revisions

  1. filmaj revised this gist Apr 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion example.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ const DocumentClientQueryReadable = require('./documentclientqueryreadable');
    const JSONStream = require('JSONStream');
    awsLite({ plugins: [import('@aws-lite/dynamodb')]}).then((aws) => {
    new DocumentClientQueryReadable(aws, {
    TableName: 'ArcOssContributorsStaging-UsersTable-HMR07QFCV7F9-m3',
    TableName: 'whatevers',
    IndexName: 'PartitionedByYear',
    KeyConditionExpression: 'yearUpdated = :yearUpdated AND startdate BETWEEN :start AND :end',
    ExpressionAttributeValues: {
  2. filmaj created this gist Apr 6, 2024.
    26 changes: 26 additions & 0 deletions documentclientqueryreadable.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    const { Readable } = require('node:stream');
    const DocumentSource = require('./documentsource');

    module.exports = class DocumentClientQueryReadable extends Readable {
    constructor (client, params) {
    super({ objectMode: true });
    this.source = new DocumentSource(client, params);
    this.source.on('data', this.onData.bind(this));
    this.source.on('end', this.onEnd.bind(this));
    }

    _read () {
    this.source.readStart();
    }

    onData (chunk) {
    this.push(chunk);
    }

    onEnd () {
    this.push(null);
    this.source.removeListener('data', this.onData);
    this.source.removeListener('end', this.onEnd);
    }

    };
    28 changes: 28 additions & 0 deletions documentsource.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    const { EventEmitter } = require('node:events');

    module.exports = class DocumentSource extends EventEmitter {
    constructor (client, params) {
    super();
    this.client = client;
    this.params = params;
    }

    readStart () {
    this.read().then((res) => {
    if (res.LastEvaluatedKey) {
    this.params = {
    ...this.params,
    ExclusiveStartKey: res.LastEvaluatedKey
    };
    this.emit('data', res);
    } else {
    this.emit('data', res);
    this.emit('end');
    }
    });
    }

    read () {
    return this.client.DynamoDB.Query(this.params);
    }
    }
    16 changes: 16 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    const awsLite = require('@aws-lite/client');
    const DocumentClientQueryReadable = require('./documentclientqueryreadable');
    const JSONStream = require('JSONStream');
    awsLite({ plugins: [import('@aws-lite/dynamodb')]}).then((aws) => {
    new DocumentClientQueryReadable(aws, {
    TableName: 'ArcOssContributorsStaging-UsersTable-HMR07QFCV7F9-m3',
    IndexName: 'PartitionedByYear',
    KeyConditionExpression: 'yearUpdated = :yearUpdated AND startdate BETWEEN :start AND :end',
    ExpressionAttributeValues: {
    ':yearUpdated': `2024`,
    ':start': `2024-03-01`,
    ':end': `2024-03-31`
    },
    ScanIndexForward: false // sort descending to get most recent records
    }).pipe(JSONStream.stringify(false)).pipe(process.stdout);
    });