Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Created November 12, 2019 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thosakwe/4415e6d7770ec2a345d253b09433e526 to your computer and use it in GitHub Desktop.
Save thosakwe/4415e6d7770ec2a345d253b09433e526 to your computer and use it in GitHub Desktop.
Functional database query language
# create table users
schema users =
{
id: serial;
name: string;
age: int;
}
# select * from users
users
# select id, name from users
users.{id, name}
# select * from users where id > 2
users where id > 2
# select name from users where id = 3
let selected = users where id = 3 in
selected.{name}
### relationships...
# When a user is created, so is a profile.
# `profile` becomes a field on `user`.
schema profiles : belongs_to(profile) user =
{
avatar_url: string?;
}
# For example, fetch Bob's profile picture:
let bob = one (users where name = "Bob") in
{pic: bob.profile.avatar_url or "none"}
# Has-many
# In this case, a "has_many_profiles_id" is added to the profiles schema,
# and deleting a has_many_profiles will wipe all related profiles.
schema has_many_profiles =
{
profiles: many profiles
}
# Many-to-many
schema profile_types =
{
name: string;
}
# Creates an associative table between the two, which depends on both.
schema profile_type_to_profile = pivot(profiles, profile_types)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment