Skip to main content

Simple Join

Datalog allows you to express joins clearly and simply. You specify a join by referencing the same variable in multiple places in a query. Let's look at the hello world join as an example.

Live Editor

The examples here use React, but they would work equally well with most other UI frameworks.

A full working example above is nice, but let's zoom into the actual query part.

datalog.query(({ greeting, noun }) => {
Greetings({ language: "en", greeting })
Nouns({ language: "en", noun })

In this query we are joining the datums from Greetings and Nouns when their language are both "en". In case you're more familiar with SQL, this is analogous to joining with a where clause. For reference, here's The equivalent SQL query.

We can take this a step forward and have it return every greeting-noun pair in the same language just by removing the given constrain of "en".

const Query = datalog.query(({ language, greeting, noun }) => {
Greetings({ language, greeting })
Nouns({ language, noun })

// =>
"language": "en",
"greeting": "Hello",
"noun": "world"
"language": "es",
"greeting": "Hola",
"noun": "todos"

Notice we had to introduce a new language variable into the query function. This is how Datalog knows which variable is a free variable whose value will be determined by the query and which one is already known.

That's all there is to joins! Pretty straightforward if I do say so myself.