05-database.agi 681 B

123456789101112131415161718192021
  1. /*---
  2. title: Key-value database
  3. group: Data
  4. summary: Store a visit counter in the system database. Run it twice.
  5. ---*/
  6. /*
  7. The database outlives a run, as on a server: run this script a few
  8. times and watch the counter grow. The Database tab of the output shows
  9. every table; clear it from the menu at the top right.
  10. */
  11. newDBTableIfNotExists("playground_visits");
  12. var key = USERNAME;
  13. var count = parseInt(readDBItem("playground_visits", key) || "0", 10) + 1;
  14. writeDBItem("playground_visits", key, String(count));
  15. writeDBItem("playground_visits", key + "_last", new Date().toISOString());
  16. sendJSONResp({
  17. visits: count,
  18. table: listDBTable("playground_visits")
  19. });