Skip to content

faablecloud/faableql

Repository files navigation

FaableQL is a query syntax with MongoDB support

Usage

Convert the following FaableQL query status:published order:created to a valid mongodb query.

const config = {...}
const fql = create_faableql(config)
const query = fql(`status:published type:article`)

results in.

{
  "$and": [
    { "status": { "$eq": "published" } },
    { "type": { "$eq": "article" } }
  ]
}

Use cases:

  • As a cli flag. ie: mycli --filter status:published
  • To filter results in a single query param. ie: GET /publications?q=status:published

Install

With NPM:

 npm install @faable/faableql

With Yarn:

 yarn add @faable/faableql

Getting Started

Configure fields that can be queried in MongoDB:

const fields = [{ name: "label", db: "labels" }];
param description
name field name in FaableQL string
db MongoDB field name

Create fql instance configured with defined fields.

import { create_faableql } from "@faable/faableql";

const fql = create_faableql(fields);

Convert a FaableQL query to MongoDB:

const query = "label:optimus label:prime";

// Process FaableQL query
const mongodb_query = fql(query);

mongodb_query will be converted to:

{
  "$and": [{ "labels": { "$eq": "optimus" } }, { "labels": { "$eq": "prime" } }]
}

use mongoose to get results filtered by your query

const docs = await Model.find(mongodb_query);

Syntax

Query = FieldTerm { whitespace FieldTerm }
FieldTerm = name Operator value

Operators

Avaliable operators

Operator Description MongoDB
: Equal {<db_field>:{$eq:<value>}}
!: Not equal {<db_field>:{$ne:<value>}}