Skip to content

Instantly share code, notes, and snippets.

@appcypher
Last active December 28, 2023 08:02
Show Gist options
  • Save appcypher/6032834df3f835b611139c56d2b5a8d3 to your computer and use it in GitHub Desktop.
Save appcypher/6032834df3f835b611139c56d2b5a8d3 to your computer and use it in GitHub Desktop.
ZQL Ideas

Database Operations

Create

zql! {
    dbs::create(#app_db, {
        namespace: "bf91cccb-81ca-4ebe-9687-a79d7d3debb2"
    })
}

Destroy

zql! {
    destroy dbs.#app_db
}

Table Operations

Create

zql! {
    let persons = table::create(#persons, {
        name: string validate [is_uppercase],
        age: int,
    })
}

Destroy

zql! {
    destroy table.#persons
}

Enum Operations

Create

zql! {
    let color = enums::create(#color, [
        #red,
        #green,
        #blue,
    ])
}

Destroy

zql! {
    destroy enums.#color
}

Type Operations

Create

zql! {
    let person = types::create(#person, {
        name: string validate [is_uppercase],
        age: int,
    })
}

Destroy

zql! {
    destroy types.#person
}

Document Operations

Add

zql! {
    persons::add(#john, {
        name: "John Doe",
        age: 42,
    })
}

Update

zql! {
    persons.#john.update({
        age: 43,
    })
}

Remove

zql! {
    remove persons.#john
}

Let Bindings

zql! {
    let red = color.#red
}

Querying

Select All

zql! {
    [ { name } in persons ]
}

Conditional

zql! {
    [ p in persons, p.age > 18 ]
}

Expanded Form

zql! {
    [ { name: p.name, age: p.age } : p in persons, p.age > 18 ]
}

Operators

Uniform Function Call Syntax

zql! {
    [ { name: uppercase(p.name) } : p in persons ]

    [ { name: p.name.uppercase() } : p in persons ]
}

Prefix Notation

zql! {
    [ { name: uppercase(p.name) } : p in persons ]

    [ { name: uppercase p.name } : p in persons ]
}

Infix Notation

zql! {
    [ { age: mod(p.age, 18) } : p in persons ]

    [ { name: p.age mod 18 } : p in persons ]
}

The Dot Notation

zql! {
    [ { name: .name.uppercase() } in persons, 18..20 contains .age ]
}

The Pipe Operator

zql! {
    [ { name: .name.uppercase() } in persons, 18..20 contains .age ] |> order asc |> group_by #age |> limit 10
}

Transactions

zql! {
    transaction {
        persons::add(#john, {
            name: "John Doe",
            age: 42,
        })

        persons.#john.update({
            age: 43,
        })

        remove persons.#john
    }
}

Math Operations

Addition

zql! {
    [ { age: .age + 1 } in persons ]
}

Subtraction

zql! {
    [ { age: .age - 1 } in persons ]
}

Multiplication

zql! {
    [ { age: .age * 2 } in persons ]
}

Division

zql! {
    [ { age: .age / 2 } in persons ]
}

Modulo

zql! {
    [ { age: .age % 2 } in persons ]
}

Power

zql! {
    [ { age: .age ^ 2 } in persons ]
}

Graph Relations

Establishing a Relation

zql! {
    persons.#john -> friend -> persons.#jane
}

Removing a Relation

zql! {
    persons.#john -/> friend -/> persons.#jane
}

Querying a Relation

zql! {
    [ { name } in persons.#john -> friend -> * ]
    [ { name } in * -> friend -> persons.#jane ]
    [ { name } in * -> friend -> * ]
    [ { name } in * -> friend ]
    [ { name } in friend -> * ]
}

Literals

Numeric

123
+123
-123
123_u32
123.0
123.0e-2
123.0_f64
123.0e-2_f64
123.
.123
0b11001
0b11001_u32
0xabcf
0xabcf_f64
0o1274
0o1274_u16

String

"Hello World"
'Hello World'

Boolean

true
false

Regex

!/John/;

Stream

[1, 2, 3]

Object

{
    name: "John Doe",
    age: 42,
}

Tuple

(1, 2, 3)

Range

1..10
1..=10

Symbols

#james
#0xf356bc
#`😏j37386vSG)=`

Block

zql! {
    {
        let x = 1;
        let y = 2;
        x + y
    }
}

Pure Functions

zql! {
    function average(s: [int]) -> [int] {
        // ...
    }
}

Closure

zql! {
    |p| p.age > 18

    |p| {
        p.age > 18
    }
}

Match

Numeric Matching

zql! {
    let age = [ { age } in persons ] |> first
    match age {
        18 => "adult",
        0..=17 => "child",
    }
}

Stream Matching

zql! {
    function sum(s: [int]) -> [int] {
        match s {
            [] => 0,
            [x] => x,
            [x, ..xs] => x + sum(xs),
        }
    }
}

Importing

#[zql::import]
fn average(s: IntStream) -> Int {
    // ...
}

zql! {
    import average

    [ { age } in persons ] |> average
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment