2.2.40 • Published 10 days ago

@thi.ng/oquery v2.2.40

Weekly downloads
26
License
Apache-2.0
Repository
github
Last release
10 days ago

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 & arrays.

This package provides a single higher-order function defQuery(), which takes a number of options to configure query behavior and returns an actual query function. This returned function can then be used for pattern matching of objects and arrays of objects.

Status

STABLE - used in production

Search or submit any issues for this package

Planned features

Some of the below features are already partially addressed by other thi.ng/umbrella packages, but would benefit from a more unified approach.

  • query joins (AND queries)
  • optional queries (OR queries)
  • result projection
  • result aggregation/grouping

Related packages

Installation

yarn add @thi.ng/oquery

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/oquery"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const oquery = await import("@thi.ng/oquery");

Package sizes (gzipped, pre-treeshake): ESM: 1.39 KB

Dependencies

API

Generated API docs

Currently, there are 27 unique & optimized query implementations, each based on RDF-style Subject-Predicate-Object triple patterns (only without any similar restrictions on query terms data types), with each of the three terms one of:

  • null - wildcard, any non-null (or undefined) 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

Intersection vs. union queries

By default, arrays or sets in O(bject) position are matched in an elementwise manner using OR-semantics, i.e. a match succeeds with the first matched element. Since v0.3.0 intersection queries are supported too, i.e. all elements of the given array/set must match for the query to succeed (see examples/differences further below).

The behavior can be chosen via the intersect query option.

Query patterns

Object queries expect an object of the following structure:

{ subj1: { pred1: "obj1", pred2: 2, pred3: ["a", "b"] }, ... }

A concrete example:

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"],
    },
    dori: {
        knows: ["bob"]
    }
};

To find all answers for the question: Who knows Bob?

// create query w/ custom options
// (options explained further below...)
const query = defQuery({ partial: true });

query(DB, null, "knows", "bob");
// {
//   alice: { knows: ["bob"] },
//   charlie: { knows: ["bob"] }
// }

For each of the 3 query terms, the following IDs are used:

  • * = null (wildcard)
  • l = literal value (or array/set of literals)
  • f = predicate function
SPO patternMatches...
* * *everything
* * lall objects with ANY property matching given literal
* * fall objects with ANY property matching given predicate
* l *objects with a property matching given literal
* l lobjects with a property AND its value matching given literals
* l fobjects with given property AND its value matching predicate
* f *objects with properties matching given predicate
* f lobjects with properties matching given predicate AND their literal values
* f fobjects with properties matching given predicate AND their literal values

Further variations:

(1) If the "subject" term is a literal (or array), then only object(s) for given key value(s) are matched, using the same logic for the other two terms as in the table above.

// Who does Alice know?
query(DB, "alice", "knows", null)
// { alice: { knows: [ 'bob', 'charlie', 'dori' ] } }

(2) If the subject is a predicate, then any top-level keys matching the given predicate will be matched (again using same rules as above for the other query terms).

// Anyone with initial "A" knows Charlie?
query(DB, (s) => s[0] === "a", "knows", "charlie")
// { alice: { knows: [ 'charlie' ] } }

(3) Instead of a root object (like DB), an array of objects can be queried. In this case, only predicate-object patterns are used (no subject terms, aka array indices in this case).

const DBALT = [
  { id: "alice", knows: ["bob", "charlie"] },
  { id: "bob", knows: ["alice"] },
  { id: "charlie", knows: ["alice","bob","dori"] },
];

defQuery()(DBALT, "knows", "alice")
// [
//   { id: 'bob', knows: [ 'alice' ] },
//   { id: 'charlie', knows: [ 'alice', 'bob', 'dori' ] }
// ]

Querying objects

The following example is using the DB object defined further above...

import { defQuery } from "@thi.ng/oquery";

// using partial result objects option for brevity here
const query = defQuery({ partial: true });

// find all subjects with `type = "person"` relationship
query(DB, null, "type", "person");
// { alice: { type: 'person' }, bob: { type: 'person' } }

// 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' }
// }

Union vs. intersection queries:

const union = defQuery();

// who knows bob OR charlie?
union(DB, null, "knows", ["bob", "charlie"]);
// {
//   alice: { age: 33, knows: [ 'bob', 'charlie', 'dori' ], type: 'person' },
//   charlie: { parent: 'alice', knows: [ 'alice', 'bob', 'dori' ] },
//   dori: { knows: [ 'bob' ] }
// }

const isec = defQuery({ intersect: true });

// who knows bob AND charlie?
isec(DB, null, "knows", ["bob", "charlie"]);
// {
//   alice: { age: 33, knows: [ 'bob', 'charlie', 'dori' ], type: 'person' }
// }

More query examples in tests...

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-oquery,
  title = "@thi.ng/oquery",
  author = "Karsten Schmidt",
  note = "https://thi.ng/oquery",
  year = 2020
}

License

© 2020 - 2021 Karsten Schmidt // Apache Software License 2.0

2.2.40

10 days ago

2.2.39

13 days ago

2.2.38

22 days ago

2.2.37

25 days ago

2.2.36

1 month ago

2.2.35

1 month ago

2.2.34

2 months ago

2.2.33

2 months ago

2.2.32

2 months ago

2.2.31

2 months ago

2.2.30

2 months ago

2.2.29

2 months ago

2.2.28

2 months ago

2.2.26

2 months ago

2.2.27

2 months ago

2.2.25

2 months ago

2.2.24

2 months ago

2.2.23

3 months ago

2.2.22

3 months ago

2.2.21

3 months ago

2.2.18

3 months ago

2.2.19

3 months ago

2.2.20

3 months ago

2.2.16

3 months ago

2.2.15

3 months ago

2.2.13

4 months ago

2.2.14

4 months ago

2.2.11

5 months ago

2.2.12

5 months ago

2.2.10

5 months ago

2.2.9

5 months ago

2.2.1

6 months ago

2.2.0

8 months ago

2.2.3

6 months ago

2.1.47

8 months ago

2.2.2

6 months ago

2.1.45

8 months ago

2.2.4

6 months ago

2.1.46

8 months ago

2.2.7

5 months ago

2.1.43

9 months ago

2.2.6

6 months ago

2.1.44

9 months ago

2.1.41

9 months ago

2.2.8

5 months ago

2.1.40

11 months ago

2.1.39

12 months ago

2.1.38

1 year ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.34

1 year ago

2.1.35

1 year ago

2.1.33

1 year ago

2.1.32

1 year ago

2.1.30

1 year ago

2.1.27

1 year ago

2.1.28

1 year ago

2.1.26

1 year ago

2.1.29

1 year ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.25

1 year ago

2.1.23

1 year ago

2.1.24

1 year ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.20

2 years ago

2.1.15

2 years ago

2.1.14

2 years ago

2.1.9

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.6

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.5

2 years ago

2.1.4

2 years ago

2.0.8

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.9

3 years ago

0.2.7

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago