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