Skip to content

Latest commit

 

History

History

oquery

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

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: 825 bytes / CJS: 881 bytes / UMD: 943 bytes

Dependencies

API

Generated API docs

TODO - Please see extensive tests for now...

There're 64 possible query approaches, each based on RDF-style Subject-Predicate-Object patterns (only without requiring query terms to be URIs). Each term can be 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
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