Skip to content

Dynamic Business Rules in JSON

Decouple business logic from application code. Store rules in your database, update them without deployments, evaluate in memory, or compile directly to SQL and MongoDB queries.
1. Store in Database
Rule as JSON AST
{ "where": "cart.total", "gte": 100 }

Stored in Postgres, Mongo, or Redis. Updated at runtime without redeploying servers.

2. Core Engine
Qarar Runtime
Zero eval()
Pre-compiled closures
Sandboxed & deterministic

High-throughput tree traversal with monotonic nanosecond execution.

3. Evaluate Anywhere
In-Memorytrue / false
SQL / MongoWHERE cart_total >= 100
Audit Log"Total must be at least 100"

One rule definition powers memory checks, DB queries, and natural-language triage.


Why Qarar?

Hardcoded if/else chains tie business rule updates to software release cycles. A routine change to discount tiers, fraud score thresholds, or access policies requires a code commit, pull request review, and server redeployment.

Qarar treats business rules as portable, technology-agnostic JSON trees. Store rules in PostgreSQL, MongoDB, or Redis, and evaluate them dynamically at runtime:

  • No deployments for logic changes: Update rule definitions in your database or admin UI; production services apply them immediately.
  • Run in-memory or in database: Check incoming payloads in Node.js or push the rule down into a SQL WHERE clause or MongoDB query filter.
  • Built-in explanations: When a rule evaluation fails, Qarar returns the exact condition that failed in plain language for audit logs and customer support.

How It Works

Every Qarar rule is a portable JSON AST that you can store in any database (PostgreSQL jsonb, MongoDB, Redis) and evaluate anywhere — or build in TypeScript with rule():

{
"id": "loyalty-discount-v1",
"name": "Loyalty Checkout Discount",
"version": "1",
"root": {
"type": "and",
"conditions": [
{
"type": "in",
"left": { "type": "field", "path": "user.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 }
}
]
}
}

Load the JSON rule from your database (or pass the built rule) and evaluate it against incoming data:

import { checkRule } from "@qarar/sdk";
// Load JSON rule from DB / API / cache, then evaluate
const { result, reasons } = checkRule(loyaltyDiscount, {
user: { tier: "bronze" },
cart: { total: 150 }
});
console.log(result); // false
console.log(reasons); // ['user.tier: "bronze" is not in [gold, vip]']

Capabilities

Zero-Deploy Logic

Store rules as standard JSON in PostgreSQL, MongoDB, or Redis. Update business logic live without rebuilding or redeploying code.

Database Pushdown

Compile the same rule AST into parameterized SQL WHERE clauses or MongoDB filter objects to filter data directly in the database.

Audit Explanations

Generate human-readable failure explanations automatically. Know exactly why an order was flagged or a user was denied without log grepping.

Sandboxed Evaluation

Safe evaluation with zero eval() and guarded field lookups that prevent prototype pollution. Guaranteed deterministic termination.


Next Steps