import 'posts.drift';

CREATE VIRTUAL TABLE search_in_posts USING fts5 (
    author,
    content,
    content=posts,
    content_rowid=id
);

search: WITH relevant_ports AS (SELECT rowid FROM search_in_posts WHERE search_in_posts MATCH ?)
  SELECT posts.* FROM relevant_ports results
    INNER JOIN posts ON id = results.rowid;

-- Keep fts5 table and posts synchronized

CREATE TRIGGER posts_insert AFTER INSERT ON posts BEGIN
  INSERT INTO search_in_posts (rowid, author, content) VALUES (new.id, new.author, new.content);
END;

CREATE TRIGGER posts_update AFTER UPDATE ON posts BEGIN
  INSERT INTO search_in_posts (search_in_posts, rowid, author, content) VALUES ('delete', old.id, old.author, old.content);
  INSERT INTO search_in_posts (rowid, author, content) VALUES (new.id, new.author, new.content);
END;

CREATE TRIGGER posts_delete AFTER DELETE ON posts BEGIN
  INSERT INTO search_in_posts (search_in_posts, rowid, author, content) VALUES ('delete', old.id, old.author, old.content);
END;
