🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

notion-query-builder

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

notion-query-builder

A simple and easy to write filter queries for the Notion API

0.3.0
latest
Source
npm
Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Notion-query-builder

Notion-query-builder is an opinionated filter query builder written in Typescript for use with the official Notion javascript client.

Note: This software is not affliated with Notion Labs, Inc. Issues and/or support should relating to this library be directed to the project's issue tracker.

Prerequisites

CompatibilityVersion Id
Notion API2022-06-28
Notion JS SDKv2.2.3+
JavascriptES2019+

Installation

NodeJS

npm i notion-query-builder

// or alternatively, [yarn|pnpm] add notion-query-builder

Deno

import nob from 'https://deno.land/x/notion-query-builder/mod.ts'

Usage

This library serves as a utility to write compact yet manageable queries for the Notion API - it is not a replacement for the client. It does this by doing the following:

  • You do not write JSON directly. This avoids large JSON objects clogging up your code, needing to remember structure and repetitive work.
  • You instead use 3 components - builders, queries and conditions - to construct your query and only provide the values.
  • You can use the components separately or mix with raw JSON if you prefer.

Once you have your query built, you execute the builder to output the JSON which defines your filter. This JSON is then used with the official client.

import { Client } from '@notionhq/client';
import nob from 'notion-query-builder';

const notion = new Client({ auth: process.env.NOTION_API_KEY });

const myFilter = nob.filterQuery([
  nob.checkboxFilter('Published', nob.equal(true)),
  nob.multiSelectFilter('Tags', nob.contains(['A', 'B']))
]);

const response = await notion.databases.query({
  database_id: databaseId,
  filter: myFilter.toJson()
});
console.log(myFilter.toJson())
{
  "and": [
    {
      "property": "Published",
      "checkbox": {
        "equals": true
      }
    },
    {
      "property": "Tags",
      "multi_select": {
        "contains": "A"
      }
    },
    {
      "property": "Tags",
      "multi_select": {
        "contains": "B"
      }
    }
  ]
}

API Quick Reference

This library's method namings aims to match closely with the official Notion API. As such, please also refer to the official Notion API documentation.

Full documentation can be found here.

Conditions

These conditions take native data types as arguments.

methodaliasexample
equaleq,equal,equals,is,benob.equal('hello world')
notEqualneq,notEqual,notEquals,isnt,isNotnob.notEqual('hello world')
containcontain,contains,has,include,includesnob.contain('hello world')
notContainnotContain,notContains,doesNotContain,hasNot,exclude,excludes,nob.notContain('hello world')
startsWithstartsWithnob.startsWith('hello')
endsWithendsWithnob.endsWith('world')
greaterThangt,greaterThan,moreThannob.greaterThan(9999)
greaterThanOrEqualTogte,greaterThanOrEqualTo,moreThanOrEqualTonob.greaterThanOrEqualTo(9999)
lessThanlt,lessThannob.lessThan(9999)
lessThanOrEqualTolte,lessThanOrEqualTonob.lessThanOrEqualTo(9999)
emptyempty,isEmpty,notExistnob.empty()
NotEmptynotEmpty,isNotEmpty,existsnob.notEmpty()
beforebeforenob.before('2023-01-01), nob.before(new Date(2023,0,1))
onOrBeforeonOrBeforenob.onOrBefore('2023-01-01)
afterafternob.after('2023-01-01), nob.after(new Date(2023,0,1))
onOrAfteronOrAfternob.onOrAfter('2023-01-01)
pastWeekpastWeeknob.pastWeek()
pastMonthpastMonthnob.pastMonth()
pastYearpastYearnob.pastYear()
thisWeekthisWeeknob.thisWeek()
nextWeeknextWeeknob.nextWeek()
nextMonthnextMonthnob.nextMonth()
nextYearnextYearnob.nextYear()

Term Level Filters

These filters take conditions as arguments.

methodexample
textFilternob.textFilter('name', nob.contains('notion'), 'title')
numberFilternob.numberFilter('age', nob.gte(12))
checkboxFilternob.checkboxFilter('name', nob.eq(true))
selectFilternob.selectFilter('name', nob.eq('notion'))
multiSelectFilternob.multiSelectFilter('name', nob.eq('notion'))
statusFilternob.statusFilter('status', nob.eq(['todo', 'in progress']))
dateFilternob.dateFilter('name', nob.before('2023-01-01))
peopleFilternob.peopleFilter('name', nob.eq('notion'))
filesFilternob.filesFilter('name', nob.eq('notion'))
relationFilternob.relationFilter('name', nob.eq('notion'))

Queries and Sepcial Filters

These queries and special filters take term level filters as arguments.

methodexample
filterQuerynob.filterQuery(nob.textQuery('name', nob.eq('notion'))
rollupFilternob.rollupFilter('every', nob.numberFilter('age', nob.gte(10)))
formulaFilternob.formulaFilter('number', nob.numberFilter('age', nob.gte(10))
compoundFilternob.compoundFilter().and(nob.textFilter('name', nob.eq('notion'))).or(nob.numberFilter('age', nob.gte(10)))

Sort

Only one function but will switch between propertySort and TimestampSort depending on the field given. Direction is optional and defaults to descending.

methodsexamples
sortnob.sort('age'), nob.sort('age', 'ascending), nob.sort('created_time', 'ascending)

Tests

Run unit tests

npm test

Credits

notion-query-builder is inspired by elastic-builder and the countless hours it has saved writing ElasticSearch queries.

Licence

MIT

Donate

A random charity appears!

Young Women's Trust

Young Women’s Trust offers support to young women aged 18 to 30, who are living on low or no pay and want to build a better future, through Work It Out. Work It Out is a free service that offers coaching and personalised feedback on CV and job applications.

Org: Young Women's Trust, reg. charity no. 217868

Donate: https://www.youngwomenstrust.org/donate/

Keywords

notion

FAQs

Package last updated on 04 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts