dh_demo

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

wiki_page.ts (3148B)


      1 import { Connection } from '@/lib/db'
      2 import { modelBehaviour } from '@/lib/model_helpers'
      3 import { ACL } from '@/lib/security/acl'
      4 import { OkPacket, RowDataPacket } from 'mysql2'
      5 
      6 export interface WikiPage {
      7   wikiId: number
      8   path: string
      9   textId: number
     10   acl?: ACL
     11   createdAt: Date
     12   updatedAt?: Date
     13 }
     14 
     15 const SQL_PUT_WIKI_PAGE = `
     16     insert into wiki_pages (wiki_id, path, text_id)
     17     values (?, ?, ?)
     18     on duplicate key update text_id = values(text_id)
     19 `
     20 
     21 export const putWikiPage = modelBehaviour<
     22   [wikiId: number, path: string, textId: number],
     23   number
     24 >(async (conn, args) => {
     25   const [result] = await conn.query<OkPacket>({
     26     sql: SQL_PUT_WIKI_PAGE,
     27   }, args)
     28 
     29   return result.insertId
     30 })
     31 
     32 const SQL_UPDATE_WIKI_PAGE_ACL = `
     33     update wiki_pages
     34     set acl_data = ?
     35     where id = ?
     36 `
     37 
     38 export const updateWikiPageAcl = modelBehaviour<
     39   [id: number, acl: ACL | string],
     40   number
     41 >(async (conn, args) => {
     42   const [result] = await conn.query<OkPacket>({
     43     sql: SQL_UPDATE_WIKI_PAGE_ACL,
     44   }, [args[1], args[0]])
     45 
     46   return result.affectedRows
     47 })
     48 
     49 const SQL_GET_WIKI_PAGE = `
     50     select wiki_id, path, text_id, acl_data, created_at, updated_at
     51     from wiki_pages
     52     where wiki_id = ?
     53       and path = ?
     54 `
     55 
     56 export const getWikiPage = modelBehaviour<
     57   [wikiId: number, path: string],
     58   WikiPage | null
     59 >(async (conn, args) => {
     60   const [rows] = await conn.query<RowDataPacket[]>({
     61     sql: SQL_GET_WIKI_PAGE,
     62   }, args)
     63 
     64   if (rows.length === 0) {
     65     return null
     66   }
     67 
     68   const row = rows[0]
     69   return {
     70     wikiId: row[0],
     71     path: row[1],
     72     textId: row[2],
     73     acl: row[3],
     74     createdAt: row[4],
     75     updatedAt: row[5],
     76   }
     77 })
     78 
     79 export interface WikiRawPage extends WikiPage {
     80   content: string
     81 }
     82 
     83 const SQL_GET_WIKI_RAW_PAGE = `
     84     select wiki_id, path, text_id, wt.content, acl_data, created_at, updated_at
     85     from wiki_pages wp
     86          inner join wiki_texts wt on wt.id = wp.text_id
     87     where wiki_id = ?
     88       and path = ?
     89 `
     90 
     91 export const getWikiRawPage = modelBehaviour<
     92   [wikiId: number, path: string],
     93   WikiRawPage | null
     94 >(async (conn, args) => {
     95   const [rows] = await conn.query<RowDataPacket[]>({
     96     sql: SQL_GET_WIKI_RAW_PAGE,
     97   }, args)
     98 
     99   if (rows.length === 0) {
    100     return null
    101   }
    102 
    103   const row = rows[0]
    104   return {
    105     wikiId: row[0],
    106     path: row[1],
    107     textId: row[2],
    108     content: row[3],
    109     acl: row[4],
    110     createdAt: row[5],
    111     updatedAt: row[6],
    112   }
    113 })
    114 
    115 const SQL_GET_WIKI_PAGE_REVISION = `
    116     select wp.wiki_id, wp.path, wc.text_id, wp.acl_data, wp.created_at, wc.created_at
    117     from wiki_changes wc
    118          inner join wiki_pages wp on wc.page_id = wp.id
    119     where wp.wiki_id = ?
    120       and wp.path = ?
    121       and wc.id = ?
    122 `
    123 
    124 export const getWikiPageRevision = modelBehaviour<
    125   [wikiId: number, path: string, changeId: number],
    126   WikiPage | null
    127 >(async (conn, args) => {
    128   const [rows] = await conn.query<RowDataPacket[]>({
    129     sql: SQL_GET_WIKI_PAGE_REVISION,
    130   }, args)
    131 
    132   if (rows.length === 0) {
    133     return null
    134   }
    135 
    136   const row = rows[0]
    137   return {
    138     wikiId: row[0],
    139     path: row[1],
    140     textId: row[2],
    141     acl: row[3],
    142     createdAt: row[4],
    143     updatedAt: row[5],
    144   }
    145 })