dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

wiki_change.ts (1941B)


      1 import { modelBehaviour } from '@/lib/model_helpers'
      2 import { OkPacket, RowDataPacket } from 'mysql2'
      3 
      4 export interface WikiChange {
      5   id: number
      6   pageId: number
      7   authorId?: number
      8   authorIp?: string
      9   textId: number
     10   createdAt: Date
     11 }
     12 
     13 export interface WikiChangeListItem extends WikiChange {
     14   nickname?: string
     15   size: number
     16 }
     17 
     18 const SQL_CREATE_WIKI_CHANGE = `
     19     insert into wiki_changes (page_id, author_id, author_ip, text_id)
     20     values (?, ?, ?, ?)
     21 `
     22 
     23 export const createWikiChange = modelBehaviour<
     24   [pageId: number, authorId: number | null, authorIp: string | null, textId: number],
     25   number
     26 >(async (conn, args) => {
     27   const [rows] = await conn.query<OkPacket>({
     28     sql: SQL_CREATE_WIKI_CHANGE,
     29   }, args)
     30 
     31   return rows.insertId
     32 })
     33 
     34 const SQL_COUNT_WIKI_CHANGES = `
     35     select count(*)
     36     from wiki_changes
     37     where page_id = ?
     38 `
     39 
     40 export const countWikiChanges = modelBehaviour<
     41   [pageId: number],
     42   number
     43 >(async (conn, args) => {
     44   const [rows] = await conn.query<RowDataPacket[]>({
     45     sql: SQL_COUNT_WIKI_CHANGES,
     46   }, args)
     47 
     48   return rows[0][0]
     49 })
     50 
     51 const SQL_GET_WIKI_CHANGES = `
     52     select 
     53         wc.id,
     54         page_id,
     55         author_id,
     56         author_ip,
     57         text_id,
     58         created_at,
     59         up.nickname,
     60         length(wt.content)
     61     from wiki_changes wc
     62          inner join wiki_texts wt on wc.text_id = wt.id
     63          left join user_profiles up on wc.author_id = up.login_id
     64     where page_id = ?
     65     order by created_at desc
     66     limit ? offset ?
     67 `
     68 
     69 export const getWikiChanges = modelBehaviour<
     70   [pageId: number, limit: number, offset: number],
     71   WikiChangeListItem[]
     72 >(async (conn, args) => {
     73   const [rows] = await conn.query<RowDataPacket[]>({
     74     sql: SQL_GET_WIKI_CHANGES,
     75   }, args)
     76 
     77   return rows.map(row => ({
     78     id: row[0],
     79     pageId: row[1],
     80     authorId: row[2],
     81     authorIp: row[3],
     82     textId: row[4],
     83     createdAt: row[5],
     84 
     85     nickname: row[6],
     86     size: row[7],
     87   }))
     88 })