-- JSON 배열 태그를 펼쳐 빈도순으로frequencies :: [[Tag]] -> [(Tag, Int)]frequencies = sortOn (\(t, n) -> (Down n, t)) -- ORDER BY frequency DESC, tag ASC . map (\g -> (head g, length g)) . group . sort . concat -- FROM example, json_each(example.tags)-- 다시 JSON 배열로 모을 때: 바깥 ORDER BY 는 집계 안 순서를 보장하지 않는다-- json_group_array(tag ORDER BY frequency DESC, tag ASC) -- 집계 안 ORDER BY, SQLite 3.44+
-- 게시물 수정 내역: 바꾸기 전에 지금 행을 history 로 복사한다 (posts : post_history = 1 : N)update :: Post -> Changes -> [History] -> (Post, [History])update post new history = (post `apply` new { updatedAt = now }, history ++ [snapshot post]) where snapshot p = History (postId p) (title p) (content p) (updatedAt p)-- INSERT INTO post_history (post_id, title, content, updated_at) SELECT … FROM posts WHERE id = 1;-- UPDATE posts SET title = '새 제목', updated_at = CURRENT_TIMESTAMP WHERE id = 1;-- 게시물이 지워지면 ON DELETE CASCADE 로 내역도