/ Readme.md
Readme.md
 1  # @dialog-db/query
 2  
 3  [Datalog] query engine for the dialog database.
 4  
 5  ## Example
 6  
 7  ```js
 8  import { fact } from "@dialog-db/query"
 9  
10  export const demo = async (db) => {
11    // We will be trying to find movie titles and director names for movies
12    // where Arnold Schwarzenegger casted. We do not need a database schema
13    // for writes but we do need schema for queries meaning we want to define
14    // relations between entities and attributes.
15  
16    // We well be looking for actors and directors that are entities with
17    // "person/name" attribute.
18    const Person = fact({
19      the: 'person',
20      name: String
21    })
22  
23    // We also define `Moive` entity with attributes for the director, cast
24    // and a title.
25    const Movie = fact({
26      the: 'movie',
27      title: String,
28      director: Object,
29      cast: Object
30    })
31  
32    const Query = fact({
33      title: String,
34      director: String,
35      actor: String,
36    })
37      .with({ cast: Object, directedBy: Object })
38      .where(({ title, cast, directedBy, director, actor }) => [
39        Movie({ cast, director: directedBy, title }),
40        Person({ this: directedBy, name: director }),
41        Person({ this: cast, name: actor }),
42        Cast.claim({ title, actor, director }),
43      ])
44  
45  
46    await Query.match({ actor: 'Arnold Schwarzenegger' }).query({ from: db })
47    // [
48    //   {
49    //      title: 'The Terminator',
50    //      director: 'James Cameron',
51    //      actor: 'Arnold Schwarzenegger'
52    //   },
53    //   {
54    //      title: 'Predator',
55    //      director: 'John McTiernan',
56    //      actor: 'Arnold Schwarzenegger'
57    //   },
58    //   {
59    //      title: 'Commando',
60    //      director: 'Mark L. Lester',
61    //      actor: 'Arnold Schwarzenegger'
62    //   },
63    //   {
64    //      title: 'Terminator 2: Judgment Day',
65    //      director: 'James Cameron',
66    //      actor: 'Arnold Schwarzenegger'
67    //   },
68    //   {
69    //     title: 'Terminator 3: Rise of the Machines',
70    //     director: 'Jonathan Mostow',
71    //     actor: 'Arnold Schwarzenegger'
72    //   },
73    // ]
74  }
75  ```
76  
77  ## Setup
78  
79  After cloning this repository, run the setup script to configure the Git remotes:
80  
81  ```bash
82  . ./scripts/setup.sh
83  ```
84  
85  Prerequisites:
86  
87  - You must have the [Radicle CLI](https://radicle.xyz/download) installed (`rad` command)
88  
89  This script will:
90  
91  1. ✅ Check if the `rad` command is available
92  2. 👾 Set up Git aliases for with radicle patches.
93  3. ⛓️ Configure Git remotes for CI jobs.
94  
95  [datalog]: https://en.wikipedia.org/wiki/Datalog