$OpenBSD: patch-db_txt,v 1.1.1.1 2018/03/14 01:49:50 abieber Exp $

Index: db.txt
--- db.txt.orig
+++ db.txt
@@ -0,0 +1,90 @@
+# This is a top-level structure.
+# It's output as a struct in C, a table in SQL, and an object in
+# JavaScript.
+
+struct company {
+  # The "limit" clause is for input validation.
+  field name text limit gt 0 comment
+    "Name of the organisation.";
+  # SQL primary keys.
+  field id int rowid;
+  field somenum int null comment
+    "Simply a check for null values.";
+  # Operations: a "list" function produces an in-memory  
+  # queue of responses.
+  # "Insert" allows us to insert into the table.
+  list somenum isnull;
+  insert;
+  comment "Controlling organisation.";
+};
+
+# This is an enumeration.
+# It lets us validate input fields and use
+# better type-safety in the C API.
+# They're also export to JavaScript.
+
+enum sex {
+  item male comment "Male";
+  item female comment "Femmale";
+  item other comment "Other";
+  comment "Birthsex of individual";
+};
+
+struct user {
+  # Foreign key support.
+  # This will produce a nested "struct company" filled
+  # in with the join on "cid" (see below).
+  field company struct cid comment
+    "This struct will be filled in from an inner join
+    on the \"cid\" variable.";
+  # The foreign key itself.
+  # We also stipulate an action on delete.
+  field cid:company.id int actdel cascade comment 
+    "A foreign key reference.";
+  field sex enum sex comment
+    "User's birth sex.";
+  # Passwords are important and often screwed up.
+  # This automatically handles the logic of accepting
+  # passwords and hashing them on insertion.
+  # When we "search" on password fields, the system
+  # will do the hashing for us.
+  field hash password limit gt 0 comment
+    "Password hash.
+    This is passed to inserts and updates as a password,
+    then hashed within the implementation and extracted
+    (in listings and searches) as the hash value.";
+  field email email unique comment
+    "Unique e-mail address.";
+  field image blob null comment 
+    "A PNG image or something.";
+  field name text comment 
+    "User's full name.";
+  field uid int rowid;
+  iterate name: limit 5 comment
+    "Create a function that searches for users by a given
+    name; and, when found, invokes a callback function
+    provided the user structure.";
+  search email,hash: name creds comment
+    "Search for a unique user with their e-mail and
+    password.
+    This is a quick way to verify that a user has entered
+    the correct password for logging in.";
+  search uid: comment "Lookup by unique identifier.";
+  update hash: uid;
+  update email: uid;
+  insert;
+  comment "A regular user.";
+};
+
+struct session { 
+  field user struct userid;
+  field userid:user.uid int comment "Associated user.";
+  field token int comment "Random cookie.";
+  field mtime epoch;
+  field id int rowid;
+  iterate user.company.name,mtime: name foo comment 
+    "Search for company's logged-in users.";
+  insert;
+  delete id;
+  comment "Authenticated session.";
+};
