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

flora-exception

v0.1.9

Published

An exception system for FQL.

Downloads

3

Readme

flora-exception  ·

const FloraAdd = mFx(
    [$Number, $Number], $Number,
    (a, b)=>Add(a, b) as number
)

// OR

const FloraAdd = (a : number, b : number) : number =>{
    return Fx(
        [ [a, $Number], [b, $Number] ], $Number,
        (a, b)=> Add(a, b) as number
    )
}

flora-exception is an exception system for Fauna Query Language built on top of the faunadb-js driver. It provides methods for:

  • exception raising,
  • typing,
  • and exception catching.

Quick start

Install

yarn add flora-exception

Import

import {Flora, mFx, $Number} from "flora-exception";

Compose

const FloraAdd = mFx(
    [$Number, $Number], $Number,
    (a, b)=>Add(a, b) as number
)

Query

const result = await client.query(Flora(
    FloraAdd(2, 2)
));

Flora()

Wrap your query logic with Flora():

  • Returns value of query, unless a FloraException was raised.
const result = await db.client.query(Flora(
  Add(2, 2)
));
expect(result).toBe(4);

Raise()

Raise() a FloraException:

  • The exception along with a stack trace will be returned.
  • In the event of an exception, even without Fx consumers, the FloraExceptionStack will be returned so long as the query is not Aborted.
const result = await db.client.query(Flora(
    If(
      IsString(2),
      2,
      Raise(FloraException())
    )
));
expect(isFloraException(result)).toBe(true);

Fx()

Use Fx() to execute logic with type and exception safety:

  • Fx will handle mismatched arg or return types.
  • In the event that an arg or a return is a FloraException it will be reraised.
/**
 * Adds two numbers.
 * @param a 
 * @param b 
 * @returns 
 */
const FloraAdd = (a : number, b : number) : number=>{
      return Fx(
          [ [a, $Number], [b, $Number] ], // args with type predicates
          $Number, // return type predicate
          (a, b)=>{ // logic
              return Add(a, b) as number
          }
      ) as number
  }

  const result = await db.client.query(Flora(
      FloraAdd(2, 2)
  ));

 expect(result).toBe(4);

mFx()

Use mFx() to create type and exception safe queries with an abbreviated syntax:

  • mFx produces a function that will call Fx to compose your query.
  • mFxinfers TypeScript types from your type predicates.
/**
 * Adds two numbers.
 * @param a 
 * @param b 
 * @returns 
 */
const FloraAdd = mFx(
    [$Number, $Number], // arg type predicates
    $Number,  // return type predicate
    (a, b)=>Add(a, b) as number
)

const result = await db.client.query(Flora(
     FloraAdd(2, 2)
));

expect(result).toBe(4);

Types

Types are implemented as predicates which are evaluated against a query expression. Fx provides the most robust method of handling types and type exceptions.

Core types are denoted with $. We recommend composed types follow this standard.

const $Numbers = $Array($Number);

Currently, the following primitive types are available:

  • $Number
  • $Int
  • $UInt8
  • $Double
  • $String
  • $Boolean

Several container types have also been implemented:

  • $Array
  • $Object
  • $Tuple

Container types can be used by wrapping another type predicate:

const $Numbers = $Array($Number);

Members of $Object and $Tuple can be made optional:

const $Player = $Object({
  name : $String,
  wins : $Optional($Number)
});

The $Or type predicate can be used to support an arbitrary number of type alternatives. While the $Any type predicate accepts all types.

const $StringsAndNumbers = $Array($Or($String, $Number));  

How it works

  • Flora creates or uses an exisiting FloraCollection to store FloraExceptions raised during a query.
  • At the start of a query, Flora creates a document on which the FloraExcpetionStack will be stored.
  • During the query, all Raises are pushed to the stack.
  • Special consumers Fx and Yield, will handle FloraExceptions in the stack without Aborting the query.
  • At the end of the query, a StackError will be compiled from the ExceptionStack reporting on the earliest source of error as well as all errant branches.
  • In StrictFlora, an errant query will also be aborted to prevent any changes made from being applied.