flatfile.schema 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. CREATE TABLE IF NOT EXISTS tag (
  2. id INTEGER PRIMARY KEY AUTOINCREMENT,
  3. name TEXT NOT NULL UNIQUE
  4. );
  5. CREATE TABLE IF NOT EXISTS posts_index (
  6. post_id TEXT NOT NULL,
  7. post_time INTEGER NOT NULL,
  8. tag_id INTEGER NOT NULL REFERENCES tag(id) ON DELETE CASCADE
  9. );
  10. CREATE INDEX IF NOT EXISTS tag_idx ON tag(name);
  11. CREATE VIEW IF NOT EXISTS posts AS SELECT p.post_id 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;
  12. /* The intention is to read this entirely into memory at app startup */
  13. /* This should not incur significant costs, even with millions of posts. */
  14. CREATE TABLE IF NOT EXISTS routes (
  15. route TEXT NOT NULL UNIQUE,
  16. method_id TEXT NOT NULL REFERENCES methods(id) ON DELETE RESTRICT,
  17. callback_id TEXT NOT NULL REFERENCES callbacks(id) ON DELETE RESTRICT
  18. );
  19. /* Enum tables like this always require cleanup when there are no more references. */
  20. /* TODO ^^^ */
  21. CREATE TABLE IF NOT EXISTS methods (
  22. id INTEGER PRIMARY KEY AUTOINCREMENT,
  23. method TEXT NOT NULL UNIQUE
  24. );
  25. CREATE TABLE IF NOT EXISTS callbacks (
  26. id INTEGER PRIMARY KEY AUTOINCREMENT,
  27. callback TEXT NOT NULL UNIQUE
  28. );
  29. CREATE VIEW IF NOT EXISTS all_routes AS SELECT 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;
  30. /* Fill the methods table with the HTTP verbs */
  31. INSERT OR IGNORE INTO methods (method) VALUES ('GET'),('POST'),('DELETE'),('PUT'),('HEAD'),('PATCH'),('CONNECT'),('OPTIONS'),('TRACE');