wiki_info.ts (3248B)
1 import db 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 WikiInfo { 7 id: number 8 ownerId: number 9 slug: string 10 title: string 11 description?: string 12 acl?: ACL 13 createdAt: Date 14 updatedAt?: Date 15 } 16 17 const SQL_CREATE_WIKI = ` 18 insert into wikis (owner_id, slug, title, description) 19 values (?, ?, ?, ?) 20 ` 21 22 export const createWiki = modelBehaviour< 23 [ ownerId: number, slug: string, title: string, description: string | null ], 24 void 25 >(async (conn, args) => { 26 await conn.query<OkPacket>({ 27 sql: SQL_CREATE_WIKI, 28 }, args) 29 }) 30 31 const SQL_LIST_WIKI_LINKS_BY_OWNER_ID = ` 32 select slug, title 33 from wikis 34 where owner_id = ? 35 ` 36 37 export const listWikiLinksByOwnerId = modelBehaviour< 38 [ ownerId: number ], 39 Array<{ slug: string, title: string }> 40 >(async (conn, args) => { 41 const [rows] = await conn.query<RowDataPacket[]>({ 42 sql: SQL_LIST_WIKI_LINKS_BY_OWNER_ID, 43 }, args) 44 45 return rows.map(row => ({ 46 slug: row[0], 47 title: row[1], 48 })) 49 }) 50 51 const SQL_LIST_WIKI_BY_OWNER_ID = ` 52 select id, owner_id, slug, title, description, acl_data, created_at, updated_at 53 from wikis 54 where owner_id = ? 55 ` 56 57 export const listWikiByOwnerId = modelBehaviour< 58 [ ownerId: number ], 59 WikiInfo[] 60 >(async (conn, args) => { 61 const [rows] = await conn.query<RowDataPacket[]>({ 62 sql: SQL_LIST_WIKI_BY_OWNER_ID, 63 }, [args]) 64 65 return rows.map(row => ({ 66 id: row[0], 67 ownerId: row[1], 68 slug: row[2], 69 title: row[3], 70 description: row[4], 71 acl: row[5], 72 createdAt: row[6], 73 updatedAt: row[7], 74 })) 75 }) 76 77 const SQL_GET_WIKI = ` 78 select id, owner_id, slug, title, description, acl_data, created_at, updated_at 79 from wikis 80 where id = ? 81 ` 82 83 export const getWiki = modelBehaviour< 84 [ id: number ], 85 WikiInfo | null 86 >(async (conn, args) => { 87 const [rows] = await conn.query<RowDataPacket[]>({ 88 sql: SQL_GET_WIKI, 89 }, [args]) 90 91 if (rows.length === 0) { 92 return null 93 } 94 95 const row = rows[0] 96 97 return { 98 id: row[0], 99 ownerId: row[1], 100 slug: row[2], 101 title: row[3], 102 description: row[4], 103 acl: row[5], 104 createdAt: row[6], 105 updatedAt: row[7], 106 } 107 }) 108 109 const SQL_GET_WIKI_VIA_SLUG = ` 110 select id, owner_id, slug, title, description, acl_data, created_at, updated_at 111 from wikis 112 where slug = ? 113 ` 114 115 export const getWikiViaSlug = modelBehaviour< 116 [slug: string], 117 WikiInfo | null 118 >(async (conn, args) => { 119 const [rows] = await conn.query<RowDataPacket[]>({ 120 sql: SQL_GET_WIKI_VIA_SLUG, 121 }, args) 122 123 if (rows.length === 0) { 124 return null 125 } 126 127 const row = rows[0] 128 return { 129 id: row[0], 130 ownerId: row[1], 131 slug: row[2], 132 title: row[3], 133 description: row[4], 134 acl: row[5], 135 createdAt: row[6], 136 updatedAt: row[7], 137 } 138 }) 139 140 const SQL_UPDATE_WIKI = ` 141 update wikis 142 set title = ?, description = ?, acl_data = ?, updated_at = now() 143 where id = ? 144 ` 145 146 export const updateWiki = modelBehaviour< 147 [ title: string, description: string | null, acl: string | null, id: number ], 148 void 149 >(async (conn, args) => { 150 await conn.query<OkPacket>({ 151 sql: SQL_UPDATE_WIKI, 152 }, args) 153 })