Skip to content

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.


  1. Installation

    Install the official SDK package:

    Terminal window
    pnpm add @qarar/sdk
  2. 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 fluent rule() 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 }
    }
    ]
    }
    }
  3. Evaluate and Inspect Human Explanations

    Pass the rule and any JavaScript object to checkRule. It returns both a boolean result and human-readable reasons explaining 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: false
    console.log("Failure Reasons:", check.reasons);
    // ➔ Failure Reasons: ['customer.tier: "bronze" is not in [gold, vip]']
  4. 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 directly
    const eligibleUsers = await db.query(
    `SELECT id, email FROM customers WHERE ${sql}`
    );
  5. Enable Zero-Deploy Updates

    1. Save Rule as JSON in a rules table in PostgreSQL (jsonb) or MongoDB.
    2. Fetch dynamically at application startup and cache in memory.
    3. Invalidate Cache via webhook or Pub/Sub when a rule is modified in your admin UI.
    4. Result: Business rules change live in production without server restarts or CI/CD pipelines.

Next Steps