dh_demo

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

wiki_text.ts (983B)


      1 import { modelBehaviour } from '@/lib/model_helpers'
      2 import { OkPacket, RowDataPacket } from 'mysql2'
      3 
      4 export interface WikiText {
      5   id: number
      6   content: string
      7   encoding: 'utf-8' | 'gzip'
      8 }
      9 
     10 const SQL_GET_WIKI_TEXT = `
     11     select id, content, encoding
     12     from wiki_texts
     13     where id = ?
     14 `
     15 
     16 export const getWikiText = modelBehaviour<
     17   [id: number],
     18   WikiText | null
     19 >(async (conn, args) => {
     20   const [rows] = await conn.query<RowDataPacket[]>({
     21     sql: SQL_GET_WIKI_TEXT,
     22   }, args)
     23 
     24   if (rows.length === 0) {
     25     return null
     26   }
     27 
     28   const row = rows[0]
     29   return {
     30     id: row[0],
     31     content: row[1],
     32     encoding: row[2],
     33   }
     34 })
     35 
     36 const SQL_INSERT_WIKI_TEXT = `
     37     insert into wiki_texts (content, encoding)
     38     values (?, ?)
     39 `
     40 
     41 export const createWikiText = modelBehaviour<
     42   [content: string, encoding: 'utf-8' | 'gzip'],
     43   number
     44 >(async (conn, args) => {
     45   const [rows] = await conn.query<OkPacket>({
     46     sql: SQL_INSERT_WIKI_TEXT,
     47   }, args)
     48 
     49   return rows.insertId
     50 })