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

@snotra/hasura-parser

v1.0.3

Published

Parse Hasura actions and events easily.

Downloads

103

Readme

Hasura Parser

npm version Coverage License

An easy utility library for parsing data from Hasura events and actions.

Getting started

First install the package.

yarn add @snotra/hasura-parser
npm install @snotra/hasura-parser

Actions

To use the Action Parser you can either import ActionParser or require the whole package.

import { ActionParser } from '@snotra/hasura-parser'

// Data is from your request body
const actionParser = new ActionParser( data )

Parsing data

To get the data you need, just pass in the keys of the arguments you want:

const data = actionParser.getData( "id", "type", "user" )

This will give you the following response:

{
	"id": <data>,
	"type": <data>,
	"user": null
}

If the value is not found a null will be returned in its place.

If you want to get all the data in its raw form, you can issue the following call:

const data = actionParser.getRawData()

This will give you all the values that were passed in by Hasura.

Getting session variables

Single session variable:

const userId = actionParser.getSessionVariable( "x-hasura-user-id" )

This will either give you the value of the session variable or just null if it is not set.

All session variables:

const sessionVariables = actionParser.getSessionVariables()

Other data

Action name:

const sessionVariables = actionParser.getActionName()

Events

To use the Events Parser you can either import EventParser or require the whole package.

import { EventParser } from '@snotra/hasura-parser'

// Data is from your request body
const eventParser = new EventParser( data )

Parsing data

To get the data you need, just pass in the keys of the arguments you want:

const data = eventParser.getData( "id", "type", "user" )

The response depends on the event type, if it is an INSERT, DELETE or MANUAL operation you will receive the following response:

{
	"id": <data>,
	"type": <data>,
	"user": null
}

If the value is not found a null will be returned in its place.

If it is an UPDATE operation, the object will contain the old and new values:

{
	"old": {
		"id": <data>,
		"type": <data>,
		"user": null
	},
	"new": {
		"id": <data>,
		"type": <data>,
		"user": null	
	}
}

If you want to get all the data in its raw form, you can issue the following calls:

const oldData = eventParser.getOldData()
const newData = eventParser.getNewData()

Depending on the event type, old or new can be null.

This will give you all the values that were passed in by Hasura.

Getting session variables

Single session variable:

const userId = eventParser.getSessionVariable( "x-hasura-user-id" )

This will either give you the value of the session variable or just null if it is not set.

All session variables:

const sessionVariables = eventParser.getSessionVariables()

Other data

Get ID of event:

const eventID = eventParser.getID()

Get trigger name (set in Hasura Console):

const triggerName = eventParser.getTriggerName()

Get schema name (the name of the schema that was affected by the event):

const schemaName = eventParser.getSchemaName()

Get table name (name of affected table by the event):

const tableName = eventParser.getTableName()

Get current retries and max retries (if this is set in the event in Hasura):

const currentRetry = eventParser.getCurrentRetry()
const maxRetries = eventParser.getMaxRetries()

Operation type checking (INSERT, UPDATE, DELETE, MANUAL):

const isInsert = eventParser.isInsertOperation() // The following operations return a boolean value
const isUpdate = eventParser.isUpdateOperation()
const isDelete = eventParser.isDeleteOperation()
const isManual = eventParser.isManualOperation()
const operationType = eventParser.getOperationType() // Returns INSERT, UPDATE, DELETE or MANUAL

Timestamp of operation:

const timestamp = eventParser.getTimestamp()

Trace context:

const traceContextID = eventParser.getTraceContextID()
const traceContextSpanID = eventParser.getTraceContextSpanID()

Contributions

If you would like to make any contribution you are welcome to do so.