Typical (Common) Example
The following code is a complete working example of how to specify a small “governed” schema (follows certain rules).
The code above will generate the following equivalent SQL:
Here’s how to use SQLa:
Install Deno (single binary)
If you haven’t already, install Deno by following the instructions specific to your operating system. Visit the Deno website (https://deno.land/) for installation guidelines.
Import SQLa
Replace vX.Y.Z
with the version number or just main
if you want the latest
version (pinning versions is highly recommended, though).
- The
#!/usr/bin/env -S deno run --allow-all
line is the shebang descriptor that allows running the script as a binary on Linux. - The
import
statements bring in the necessary modules from the SQLa Typical Pattern library (tp), which is specifically designed for small to medium-sized databases with common business domains and table schemas. - The SQLa symbol provides access to the SQLa functionality, while
ws
provides support for whitespace management. EmitContext
is not too useful here but it’s an example of how the template can have a complete type-safe contextual environment for passing around variables or other dynamic content.
Prepare domains and table types
SQLa prefers governed schemas meaning standard naming conventions and rules should be followed.
gts
is the governed template state which controls SQL emit functionalitygm
is the governed model which has the domains (column types) keys, table types, etc.
Define your schema
execCtx
is an enumeration table which helps create reference or static lookup rows (with foreign key support)jobGrade
is a simple tablejobPosition
is a more complex table with foreign key tojobGrade
(seegrade_id
column)