Quickstart & Setup
Qarar lets you decouple business rules from application code. Store rules as portable JSON in your database, evaluate incoming payloads in Node.js, and compile directly to SQL or MongoDB pushdown queries.
-
Installation
Install the official SDK package:
Terminal window pnpm add @qarar/sdkTerminal window npm install @qarar/sdkTerminal window yarn add @qarar/sdk -
Define or Store Your Rule as JSON
Qarar rules are portable JSON Abstract Syntax Trees (AST) designed to be stored in PostgreSQL
jsonb, MongoDB, or Redis — or constructed in TypeScript using the fluentrule()builder:{"id": "promo_summer_2026","name": "Summer VIP Promo","description": "Grants 20% discount to VIP members with carts over $100","version": "1","root": {"type": "and","conditions": [{"type": "gte","left": { "type": "field", "path": "customer.age" },"right": { "type": "number", "value": 18 }},{"type": "in","left": { "type": "field", "path": "customer.tier" },"right": {"type": "array","values": [{ "type": "string", "value": "gold" },{ "type": "string", "value": "vip" }]}},{"type": "gte","left": { "type": "field", "path": "cart.total" },"right": { "type": "number", "value": 100 }}]}}import { rule } from "@qarar/sdk";export const promoRule = rule("Summer VIP Promo").id("promo_summer_2026").description("Grants 20% discount to VIP members with carts over $100").where("customer.age").gte(18).and("customer.tier").in(["gold", "vip"]).and("cart.total").gte(100).build(); // ➔ produces the exact JSON AST shown in the first tab -
Evaluate and Inspect Human Explanations
Pass the rule and any JavaScript object to
checkRule. It returns both a booleanresultand human-readablereasonsexplaining why conditions failed:import { checkRule } from "@qarar/sdk";const incomingCart = {customer: { age: 24, tier: "bronze" },cart: { total: 150 },};const check = checkRule(promoRule, incomingCart);console.log("Passed:", check.result);// ➔ Passed: falseconsole.log("Failure Reasons:", check.reasons);// ➔ Failure Reasons: ['customer.tier: "bronze" is not in [gold, vip]'] -
Push Rule to Database Queries
Instead of pulling thousands of records into Node.js to filter them in memory, compile the rule into a database query:
import { compileToSQL } from "@qarar/sdk";const { sql } = compileToSQL(promoRule, { columnStyle: "snake" });console.log(sql);// ➔ customer_age >= 18 AND customer_tier IN ('gold', 'vip') AND cart_total >= 100// Query database directlyconst eligibleUsers = await db.query(`SELECT id, email FROM customers WHERE ${sql}`);import { compileToMongo } from "@qarar/sdk";const { filter } = compileToMongo(promoRule);// Query MongoDB collection directlyconst eligibleUsers = await db.collection("customers").find(filter).toArray(); -
Enable Zero-Deploy Updates
- Save Rule as JSON in a
rulestable in PostgreSQL (jsonb) or MongoDB. - Fetch dynamically at application startup and cache in memory.
- Invalidate Cache via webhook or Pub/Sub when a rule is modified in your admin UI.
- Result: Business rules change live in production without server restarts or CI/CD pipelines.
- Save Rule as JSON in a
Next Steps
Rule Engine & Evaluation Deep dive into operators, nested logic groups, and execution traces.
Decision Tables & Switch Cases Replace switch-case statements with dynamic multi-branch decision matrices.