Nachos Preventing Motivation
Miss any of our Open RFC calls?Watch the recordings here! »

@thi.ng/oquery

0.1.7 • Public • Published

oquery

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Datalog-inspired, optimized pattern/predicate query engine for JS objects.

Currently, there're 125 possible query approaches, which can be collapsed into 27 unique query implementations. Each query is based on RDF-style Subject-Predicate-Object patterns (only without requiring query terms to be URIs), with each term one of:

  • null - wildcard, any non-null value in that position will be selected
  • Predicate function - called with all possible terms in that position
  • Literal value - for subjects and predicates, this can only be a string or number. For "object" position any value type is allowed
  • Array or Set - multiple choices (literals) for given query term

See basic query examples below...

Status

ALPHA - bleeding edge / work-in-progress

Related packages

Installation

yarn add @thi.ng/oquery
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/oquery?module" crossorigin></script>
 
// UMD
<script src="https://unpkg.com/@thi.ng/oquery/lib/index.umd.js" crossorigin></script>

Package sizes (gzipped, pre-treeshake): ESM: 823 bytes / CJS: 883 bytes / UMD: 957 bytes

Dependencies

API

Generated API docs

TODO - Please see extensive tests for now...

import { defQuery } from "@thi.ng/oquery";
 
// object to query
const DB = {
    alice: {
        age: 33,
        knows: ["bob", "charlie", "dori"],
        type: "person",
    },
    bob: {
        age: 32,
        knows: ["alice"],
        type: "person",
        spouse: "alice",
    },
    charlie: {
        parent: "alice",
        knows: ["alice", "bob", "dori"],
    },
};
 
// init w/ default opts
// (uses @thi.ng/equiv for equality checks)
const query = defQuery();
 
// find all subjects with `type = "person"` relationship
query(DB, null, "type", "person");
// { alice: { type: 'person' }, bob: { type: 'person' } }
 
// find all who know bob or charlie
query(DB, null, "knows", ["bob", "charlie"])
// { alice: { knows: [ 'bob', 'charlie' ] }, charlie: { knows: [ 'bob' ] } }
 
// everyone w/ given min age
query(DB, null, "age", (age) => age >= 33)
// { alice: { age: 33 } }
 
// select only subjects with A/B initials
query(DB, (id) => id > "a" && id < "c", null, null)
// {
//   alice: { age: 33, knows: [ 'bob', 'charlie', 'dori' ], type: 'person' },
//   bob: { age: 32, knows: [ 'alice' ], type: 'person', spouse: 'alice' }
// }

Authors

Karsten Schmidt

License

© 2020 Karsten Schmidt // Apache Software License 2.0

Install

npm i @thi.ng/oquery

DownloadsWeekly Downloads

25

Version

0.1.7

License

Apache-2.0

Unpacked Size

51.5 kB

Total Files

14

Last publish

Collaborators

  • avatar