|
| 1 | +import { Router } from 'express' |
| 2 | +import { ident, literal } from 'pg-format' |
| 3 | +import sql = require('../lib/sql') |
| 4 | +import { RunQuery } from '../lib/connectionPool' |
| 5 | + |
| 6 | +const { publications } = sql |
| 7 | +const router = Router() |
| 8 | + |
| 9 | +router.get('/', async (req, res) => { |
| 10 | + try { |
| 11 | + const { data } = await RunQuery(req.headers.pg, publications) |
| 12 | + return res.status(200).json(data) |
| 13 | + } catch (error) { |
| 14 | + res.status(500).json({ error: error.message }) |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +router.post('/', async (req, res) => { |
| 19 | + try { |
| 20 | + const createPublicationSql = createPublicationSqlize(req.body) |
| 21 | + await RunQuery(req.headers.pg, createPublicationSql) |
| 22 | + |
| 23 | + const getPublicationSql = getPublicationByNameSqlize(req.body.name) |
| 24 | + const newPublication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0] |
| 25 | + return res.status(200).json(newPublication) |
| 26 | + } catch (error) { |
| 27 | + res.status(400).json({ error: error.message }) |
| 28 | + } |
| 29 | +}) |
| 30 | + |
| 31 | +router.patch('/:id', async (req, res) => { |
| 32 | + try { |
| 33 | + const id = req.params.id |
| 34 | + const getPublicationSql = getPublicationByIdSqlize(id) |
| 35 | + const oldPublication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0] |
| 36 | + |
| 37 | + const args = req.body |
| 38 | + const alterPublicationSql = alterPublicationSqlize({ oldPublication, ...args }) |
| 39 | + await RunQuery(req.headers.pg, alterPublicationSql) |
| 40 | + |
| 41 | + const updatedPublication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0] |
| 42 | + return res.status(200).json(updatedPublication) |
| 43 | + } catch (error) { |
| 44 | + console.log(error.message) |
| 45 | + if (error instanceof TypeError) { |
| 46 | + res.status(404).json({ error: 'Cannot find a publication with that id' }) |
| 47 | + } else { |
| 48 | + res.status(400).json({ error: error.message }) |
| 49 | + } |
| 50 | + } |
| 51 | +}) |
| 52 | + |
| 53 | +router.delete('/:id', async (req, res) => { |
| 54 | + try { |
| 55 | + const id = req.params.id |
| 56 | + const getPublicationSql = getPublicationByIdSqlize(id) |
| 57 | + const publication = (await RunQuery(req.headers.pg, getPublicationSql)).data[0] |
| 58 | + const { name } = publication |
| 59 | + |
| 60 | + const dropPublicationSql = dropPublicationSqlize(name) |
| 61 | + await RunQuery(req.headers.pg, dropPublicationSql) |
| 62 | + |
| 63 | + return res.status(200).json(publication) |
| 64 | + } catch (error) { |
| 65 | + if (error instanceof TypeError) { |
| 66 | + res.status(404).json({ error: 'Cannot find a publication with that id' }) |
| 67 | + } else { |
| 68 | + res.status(400).json({ error: error.message }) |
| 69 | + } |
| 70 | + } |
| 71 | +}) |
| 72 | + |
| 73 | +const createPublicationSqlize = ({ |
| 74 | + name, |
| 75 | + publish_insert = false, |
| 76 | + publish_update = false, |
| 77 | + publish_delete = false, |
| 78 | + publish_truncate = false, |
| 79 | + tables, |
| 80 | +}: { |
| 81 | + name: string |
| 82 | + publish_insert?: boolean |
| 83 | + publish_update?: boolean |
| 84 | + publish_delete?: boolean |
| 85 | + publish_truncate?: boolean |
| 86 | + tables?: string[] |
| 87 | +}) => { |
| 88 | + let tableClause: string |
| 89 | + if (tables === undefined) { |
| 90 | + tableClause = 'FOR ALL TABLES' |
| 91 | + } else if (tables.length === 0) { |
| 92 | + tableClause = '' |
| 93 | + } else { |
| 94 | + tableClause = `FOR TABLE ${tables.map(ident).join(',')}` |
| 95 | + } |
| 96 | + |
| 97 | + let publishOps = [] |
| 98 | + if (publish_insert) publishOps.push('insert') |
| 99 | + if (publish_update) publishOps.push('update') |
| 100 | + if (publish_delete) publishOps.push('delete') |
| 101 | + if (publish_truncate) publishOps.push('truncate') |
| 102 | + |
| 103 | + return ` |
| 104 | +CREATE PUBLICATION ${ident(name)} ${tableClause} |
| 105 | + WITH (publish = '${publishOps.join(',')}') |
| 106 | +` |
| 107 | +} |
| 108 | + |
| 109 | +const getPublicationByNameSqlize = (name: string) => { |
| 110 | + return `${publications} WHERE p.pubname = ${literal(name)}` |
| 111 | +} |
| 112 | + |
| 113 | +const getPublicationByIdSqlize = (id: string) => { |
| 114 | + return `${publications} WHERE p.oid = ${literal(id)}` |
| 115 | +} |
| 116 | + |
| 117 | +const alterPublicationSqlize = ({ |
| 118 | + oldPublication, |
| 119 | + name, |
| 120 | + owner, |
| 121 | + publish_insert, |
| 122 | + publish_update, |
| 123 | + publish_delete, |
| 124 | + publish_truncate, |
| 125 | + tables, |
| 126 | +}: { |
| 127 | + oldPublication: any |
| 128 | + name?: string |
| 129 | + owner?: string |
| 130 | + publish_insert?: boolean |
| 131 | + publish_update?: boolean |
| 132 | + publish_delete?: boolean |
| 133 | + publish_truncate?: boolean |
| 134 | + tables?: string[] |
| 135 | +}) => { |
| 136 | + // Need to work around the limitations of the SQL. Can't add/drop tables from |
| 137 | + // a publication with FOR ALL TABLES. Can't use the SET TABLE clause without |
| 138 | + // at least one table. |
| 139 | + // |
| 140 | + // new tables |
| 141 | + // |
| 142 | + // | undefined | string[] | |
| 143 | + // ---------|-----------|-----------------| |
| 144 | + // null | '' | 400 Bad Request | |
| 145 | + // old tables ---------|-----------|-----------------| |
| 146 | + // string[] | '' | See below | |
| 147 | + // |
| 148 | + // new tables |
| 149 | + // |
| 150 | + // | [] | [...] | |
| 151 | + // ---------|-----------|-----------------| |
| 152 | + // [] | '' | SET TABLE | |
| 153 | + // old tables ---------|-----------|-----------------| |
| 154 | + // [...] | DROP all | SET TABLE | |
| 155 | + // |
| 156 | + let tableSql: string |
| 157 | + if (tables === undefined) { |
| 158 | + tableSql = '' |
| 159 | + } else if (oldPublication.tables === null) { |
| 160 | + throw Error('Tables cannot be added to or dropped from FOR ALL TABLES publications') |
| 161 | + } else if (tables.length > 0) { |
| 162 | + tableSql = `ALTER PUBLICATION ${ident(oldPublication.name)} SET TABLE ${tables |
| 163 | + .map(ident) |
| 164 | + .join(',')}` |
| 165 | + } else if (oldPublication.tables.length === 0) { |
| 166 | + tableSql = '' |
| 167 | + } else { |
| 168 | + tableSql = `ALTER PUBLICATION ${ident( |
| 169 | + oldPublication.name |
| 170 | + )} DROP TABLE ${oldPublication.tables.map(ident).join(',')}` |
| 171 | + } |
| 172 | + |
| 173 | + let publishOps = [] |
| 174 | + if (publish_insert ?? oldPublication.publish_insert) publishOps.push('insert') |
| 175 | + if (publish_update ?? oldPublication.publish_update) publishOps.push('update') |
| 176 | + if (publish_delete ?? oldPublication.publish_delete) publishOps.push('delete') |
| 177 | + if (publish_truncate ?? oldPublication.publish_truncate) publishOps.push('truncate') |
| 178 | + const publishSql = `ALTER PUBLICATION ${ident( |
| 179 | + oldPublication.name |
| 180 | + )} SET (publish = '${publishOps.join(',')}')` |
| 181 | + |
| 182 | + const ownerSql = |
| 183 | + owner === undefined |
| 184 | + ? '' |
| 185 | + : `ALTER PUBLICATION ${ident(oldPublication.name)} OWNER TO ${ident(owner)}` |
| 186 | + |
| 187 | + const nameSql = |
| 188 | + name === undefined || name === oldPublication.name |
| 189 | + ? '' |
| 190 | + : `ALTER PUBLICATION ${ident(oldPublication.name)} RENAME TO ${ident(name)}` |
| 191 | + |
| 192 | + // nameSql must be last |
| 193 | + return ` |
| 194 | +BEGIN; |
| 195 | + ${tableSql}; |
| 196 | + ${publishSql}; |
| 197 | + ${ownerSql}; |
| 198 | + ${nameSql}; |
| 199 | +COMMIT; |
| 200 | +` |
| 201 | +} |
| 202 | + |
| 203 | +const dropPublicationSqlize = (name: string) => { |
| 204 | + return `DROP PUBLICATION IF EXISTS ${ident(name)}` |
| 205 | +} |
| 206 | + |
| 207 | +export = router |
0 commit comments