flatfile.schema 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. CREATE TABLE IF NOT EXISTS post (
  2. id INTEGER PRIMARY KEY AUTOINCREMENT,
  3. uuid TEXT NOT NULL UNIQUE
  4. );
  5. CREATE TABLE IF NOT EXISTS tag (
  6. id INTEGER PRIMARY KEY AUTOINCREMENT,
  7. name TEXT NOT NULL UNIQUE
  8. );
  9. CREATE TABLE IF NOT EXISTS posts_index (
  10. post_id INTEGER NOT NULL REFERENCES post(id) ON DELETE CASCADE,
  11. post_time INTEGER NOT NULL,
  12. tag_id INTEGER NOT NULL REFERENCES tag(id) ON DELETE RESTRICT
  13. );
  14. CREATE INDEX IF NOT EXISTS tag_idx ON tag(name);
  15. CREATE VIEW IF NOT EXISTS posts AS SELECT pu.uuid AS id, p.post_time AS created, t.name AS tag FROM posts_index AS p JOIN tag AS t ON t.id=p.tag_id JOIN post AS pu ON p.post_id=pu.id;
  16. /* The intention is to read this entirely into memory at app startup */
  17. /* This should not incur significant costs, even with millions of posts. */
  18. CREATE TABLE IF NOT EXISTS routes (
  19. id INTEGER PRIMARY KEY AUTOINCREMENT,
  20. post_id INTEGER NOT NULL REFERENCES post(id) ON DELETE CASCADE,
  21. route TEXT NOT NULL UNIQUE,
  22. method_id TEXT NOT NULL REFERENCES methods(id) ON DELETE RESTRICT,
  23. callback_id TEXT NOT NULL REFERENCES callbacks(id) ON DELETE RESTRICT
  24. );
  25. CREATE TABLE IF NOT EXISTS methods (
  26. id INTEGER PRIMARY KEY AUTOINCREMENT,
  27. method TEXT NOT NULL UNIQUE
  28. );
  29. CREATE TABLE IF NOT EXISTS callbacks (
  30. id INTEGER PRIMARY KEY AUTOINCREMENT,
  31. callback TEXT NOT NULL UNIQUE
  32. );
  33. /* The actual routing table */
  34. CREATE VIEW IF NOT EXISTS all_routes AS SELECT r.id AS id, r.route AS route, m.method AS method, c.callback AS callback FROM routes AS r JOIN methods AS m ON m.id=r.method_id JOIN callbacks AS c ON c.id=r.callback_id;
  35. /* Fill the methods table with the HTTP verbs */
  36. INSERT OR IGNORE INTO methods (method) VALUES ('GET'),('POST'),('DELETE'),('PUT'),('HEAD'),('PATCH'),('CONNECT'),('OPTIONS'),('TRACE');
  37. CREATE TABLE IF NOT EXISTS post_aliases (
  38. route_id INTEGER NOT NULL REFERENCES routes(id) ON DELETE CASCADE,
  39. alias TEXT NOT NULL UNIQUE
  40. );
  41. CREATE VIEW IF NOT EXISTS aliases AS SELECT r.route AS actual, a.alias AS alias FROM routes AS r JOIN post_aliases AS a ON r.id=a.route_id;