1 # This is a top-level structure.
    2 # It's output as a struct in C, a table in SQL, and an object in
    3 # JavaScript.
    4 
    5 struct company {
    6   # The "limit" clause is for input validation.
    7   field name text limit gt 0 comment
    8     "Name of the organisation.";
    9   # SQL primary keys.
   10   field id int rowid;
   11   field somenum int null comment
   12     "Simply a check for null values.";
   13   # Operations: a "list" function produces an in-memory  
   14   # queue of responses.
   15   # "Insert" allows us to insert into the table.
   16   list somenum isnull;
   17   insert;
   18   comment "Controlling organisation.";
   19 };
   20 
   21 # This is an enumeration.
   22 # It lets us validate input fields and use
   23 # better type-safety in the C API.
   24 # They're also export to JavaScript.
   25 
   26 enum sex {
   27   item male comment "Male" jslabel "male";
   28   item female comment "Female" jslabel "female";
   29   item other comment "Other" jslabel "other";
   30   comment "Birthsex of individual";
   31 };
   32 
   33 struct user {
   34   # Foreign key support.
   35   # This will produce a nested "struct company" filled
   36   # in with the join on "cid" (see below).
   37   field company struct cid comment
   38     "This struct will be filled in from an inner join
   39     on the \"cid\" variable.";
   40   # The foreign key itself.
   41   # We also stipulate an action on delete.
   42   field cid:company.id int actdel cascade comment 
   43     "A foreign key reference.";
   44   field sex enum sex comment
   45     "User's birth sex.";
   46   # Passwords are important and often screwed up.
   47   # This automatically handles the logic of accepting
   48   # passwords and hashing them on insertion.
   49   # When we "search" on password fields, the system
   50   # will do the hashing for us.
   51   field hash password limit gt 0 comment
   52     "Password hash.
   53     This is passed to inserts and updates as a password,
   54     then hashed within the implementation and extracted
   55     (in listings and searches) as the hash value.";
   56   field email email unique comment
   57     "Unique e-mail address.";
   58   field image blob null comment 
   59     "A PNG image or something.";
   60   field name text comment 
   61     "User's full name.";
   62   field uid int rowid;
   63   iterate name: limit 5 comment
   64     "Create a function that searches for users by a given
   65     name; and, when found, invokes a callback function
   66     provided the user structure.";
   67   search email,hash: name creds comment
   68     "Search for a unique user with their e-mail and
   69     password.
   70     This is a quick way to verify that a user has entered
   71     the correct password for logging in.";
   72   search uid: comment "Lookup by unique identifier." order company.name;
   73   update hash: uid;
   74   update email: uid;
   75   insert;
   76   comment "A regular user.";
   77 };
   78 
   79 struct session { 
   80   field user struct userid;
   81   field userid:user.uid int comment "Associated user.";
   82   field token int comment "Random cookie.";
   83   field mtime epoch;
   84   field id int rowid;
   85   iterate user.company.name,mtime: name foo comment 
   86     "Search for company's logged-in users.";
   87   insert;
   88   delete id;
   89   comment "Authenticated session.";
   90 };