TugQL

Get started with TugQL

Read a TugQL query, learn its source, filter, order and projection stages, and find the Go and TypeScript implementations.

Read a query

This query reads invoices, keeps IDs above 10, orders by ID, and returns one field. Invoice is a source name; the application must bind it to a database.

from Invoice as i
where i.InvoiceId > 10
order by i.InvoiceId
select i.InvoiceId

TugQL puts the projection at the end, after the source and filters. It is SQL-like text, not SQL sent directly to PostgreSQL or Firestore.

Parse and resolve in Go

Install the Go library, then provide the fields this caller is allowed to query. Parsing checks syntax; resolution checks names and permissions against the supplied schema and lowers the query to a DALgo structured query. This example resolves one output column; it does not connect to a database.

go get github.com/dal-go/dalgo/tugql
package main

import (
  "fmt"
  "github.com/dal-go/dalgo/tugql"
)

func main() {
  source := "from Invoice as i\nwhere i.InvoiceId > 10\norder by i.InvoiceId\nselect i.InvoiceId"
  doc, diagnostics := tugql.ParseTugQL(source)
  if len(diagnostics) != 0 {
    fmt.Println(diagnostics)
    return
  }
  context := tugql.TugQLResolveContext{
    AuthorizedSchemas: []tugql.TugQLAuthorizedSchema{{
      Version: "schema-r1",
      Tables: []tugql.TugQLTable{{
        Name: "Invoice",
        Fields: []tugql.TugQLField{{Name: "InvoiceId", Type: "integer", Authorized: true}},
      }},
    }},
  }
  resolved, diagnostics := tugql.ResolveTugQL(doc, context)
  if len(diagnostics) != 0 {
    fmt.Println(diagnostics)
    return
  }
  fmt.Printf("resolved %d output column(s)\n", len(resolved.Columns))
}

Expected output: resolved 1 output column(s). The TypeScript core has separate parsing and resolution APIs; check its README and adapter inventory for supported execution paths.

Try the connected example

The homepage example declares Chinook and geo-ingitdb, joins their sources, and opens in the DataTug browser. It is a separate executable example with two demo references. The executing tool still applies its own access rules.

Try the connected query in DataTug

Read the cross-database reference for the databases block and alias:: source syntax.

Save the query as data

TugQT is the structured YAML/JSON tree of a TugQL query, useful for reviewable saved queries. The published JSON Schema validates the Go library's YAML serialization of a structured query. The versioned text parser tree is a separate representation. The specification repository contains current references; the implementation repositories contain executable behavior and tests.

All documentation