flora-exception v0.1.9
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
FloraExceptionwas 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
Fxconsumers, theFloraExceptionStackwill be returned so long as the query is notAborted.
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:
Fxwill handle mismatched arg or return types.- In the event that an arg or a return is a
FloraExceptionit 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:
mFxproduces a function that will callFxto 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
Floracreates or uses an exisitingFloraCollectionto storeFloraExceptionsraised during a query.- At the start of a query,
Floracreates a document on which theFloraExcpetionStackwill be stored. - During the query, all
Raisesare pushed to the stack. - Special consumers
FxandYield, will handleFloraExceptionsin the stack withoutAbortingthe query. - At the end of the query, a
StackErrorwill be compiled from theExceptionStackreporting 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.
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago