npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

topic-validator

v1.1.2

Published

Used to filter topic messages in the past to build a state object to use when validating incoming messages.

Downloads

13

Readme

Background

This package was created the help keep track of messages in an existing topic in kafka for use with validation when creating, updating, or deleting new messages. Instead of having a completely different service to store and check records I created this package to maintain a local version of the state so far. In some instances you might still need to create a separate service for tracking state in your topics get to be too big. In my app I create a consumer and consume all messages from the beginning of a topic. We use domain topics for better replay-ability.

I would love to hear input from others around the current problem this package is trying to solve and how I can make it better for your use case. Hope you like!

Getting Started

Install

npm install topic-validator --save

API

Table of Contents

buildTopicState

Builds the most current state of a topic from looping over messages and replaying state.

Parameters

  • topicMessages array Array of the current topic messages.
  • primaryKey string The primary key used to identify each message.
  • topicActionKey string The key for your command action in the topic message. IMPORTANT your actions must use the keywords: created, updated, or deleted. (example: topicAction: 'created')

Examples

const topicMessages = [
  {
    deviceId: 'testDeviceId',
    externalHardwareId: 'testExternalHardwareId',
    type: 'testType',
    vendorId: 'testVendorId',
    topicAction: 'created',
    created: 1511215962473,
    updated: 1511215962473
  }
];

buildTopicState(
  topicMessages,
  'deviceId',
'topicAction'
);

Returns array Returns a new state array of topic messages from replay.

create

Adds the new message to the current state.

Parameters

  • state array Array of the current topic messages.
  • record object The new message being created.

Examples

const topicMessages = [{
  thing: 'thing'
}]

create(topicMessages, { test: 'test' });

Returns array Returns the new state with the passed in item.

update

Updates found message to the current state.

Parameters

  • state array Array of the current topic messages.
  • record object The new message being created.
  • primaryKeyIdentifier string The primary key used to keep records unique.

Examples

const existingObject = {
  id: 'testId',
  test: 'test'
};

const newRecord = {
  id: 'testId',
  test: 'updatedContent'
};

const existingState = [existingObject];

update(existingState, newRecord, 'id');

Returns array Updates record in the state and returns the new state array.

remove

Removes found message from the current state.

Parameters

  • state array Array of the current topic messages.
  • record object The new message being created.
  • primaryKeyIdentifier string The primary key used to keep records unique.

Examples

const existingObject = {
  id: 'testId',
  test: 'test'
};

const newRecord = {
  id: 'testId',
  test: 'deletedContent'
};

const existingState = [existingObject];

remove(existingState, newRecord, 'id');

Returns array Removes the record from state and returns the new state array.

isCreatable

Searches the current state to see if the message has already been created.

Parameters

  • record object The new message being created.
  • state array Array of the current topic messages.
  • primaryKeyIdentifier string The primary key used to keep records unique.

Examples

const existingObject = {
  id: 'testId',
  test: 'test'
};

const record = {
  id: 'otherId',
  test: 'deletedContent'
};

const topicMessages = [existingObject];

// Returns true
isCreatable(record, topicMessages, 'id');

Returns boolean Returns a boolean value.

isUpdatable

Searches the current state to see if the message can be updated.

Parameters

  • record object The new message being created.
  • state array Array of the current topic messages.
  • primaryKeyIdentifier string The primary key used to keep records unique.

Examples

const existingObject = {
  id: 'testId',
  test: 'test'
};

const record = {
  id: 'testId',
  test: 'deletedContent'
};

const topicMessages = [existingObject];

// Returns true
isUpdatable(record, topicMessages, 'id');

Returns boolean Returns a boolean value.

isRemovable

Searches the current state to see if the message can be deleted.

Parameters

  • record object The new message being created.
  • state array Array of the current topic messages.
  • primaryKeyIdentifier string The primary key used to keep records unique.

Examples

const existingObject = {
  id: 'testId',
  test: 'test'
};

const record = {
  id: 'testId',
  test: 'deletedContent'
};

const topicMessages = [existingObject];

// Returns true
isRemovable(record, topicMessages, 'id');

Returns boolean Returns a boolean value.