8000 refactor: add some small changes · wasabigeek/postgres-meta@330dead · GitHub
[go: up one dir, main page]

Skip to content

Commit 330dead

Browse files
ftonatosoedirgo
authored andcommitted
refactor: add some small changes
1 parent 9889293 commit 330dead

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

src/api/columns.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,13 @@ const addColumnSqlize = ({
128128
is_unique?: boolean
129129
comment?: string
130130
}) => {
131-
let defaultValueSql: string
131+
let defaultValueSql: string = `DEFAULT ${literal(default_value)}`
132132
if (default_value === undefined) {
133133
defaultValueSql = ''
134134
} else if (default_value_format === 'expression') {
135135
defaultValueSql = `DEFAULT ${default_value}`
136-
} else {
137-
defaultValueSql = `DEFAULT ${literal(default_value)}`
138136
}
137+
139138
const isIdentitySql = is_identity ? `GENERATED ${identity_generation} AS IDENTITY` : ''
140139
const isNullableSql = is_nullable ? 'NULL' : 'NOT NULL'
141140
const isPrimaryKeySql = is_primary_key ? 'PRIMARY KEY' : ''
@@ -215,7 +214,7 @@ const alterColumnSqlize = (
215214
} else if (default_value === undefined) {
216215
defaultValueSql = ''
217216
} else {
218-
let defaultValue =
217+
const defaultValue =
219218
default_value_format === 'expression' ? default_value : literal(default_value)
220219
defaultValueSql = format(
221220
`ALTER TABLE %I.%I ALTER COLUMN %I SET DEFAULT ${defaultValue};`,

src/api/policies.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Router } from 'express'
22
import format from 'pg-format'
3-
import { coalesceRowsToArray, toTransaction } from '../lib/helpers'
3+
import { toTransaction } from '../lib/helpers'
44
import { RunQuery } from '../lib/connectionPool'
55
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
66
import { Tables } from '../lib/interfaces'
@@ -61,7 +61,7 @@ router.patch('/:id', async (req, res) => {
6161
const payload: Tables.Policy = { ...req.body }
6262
const previousPolicy: Tables.Policy = await getPolicyById(pcConnection, id)
6363
const nameChange = !!payload.name && payload.name != previousPolicy.name
64-
let updates = { ...payload }
64+
const updates = { ...payload }
6565
if (!updates.name) updates.name = previousPolicy.name
6666
if (!updates.schema) updates.schema = previousPolicy.schema
6767
if (!updates.table) updates.table = previousPolicy.table
@@ -115,7 +115,7 @@ const getAllSql = (sqlTemplates) => {
115115
}
116116
const getPolicyById = async (connection: string, id: number) => {
117117
const { policies } = sqlTemplates
118-
let sql = `
118+
const sql = `
119119
with policies as (${policies})
120120
select * from policies
121121
where policies.id = ${id}
@@ -191,10 +191,10 @@ const alterSql = ({
191191
check?: string
192192
roles?: string[]
193193
}) => {
194-
let alter = format(`ALTER POLICY %I ON %I.%I`, name, schema, table)
195-
let newDefinition = definition !== undefined ? `${alter} USING (${definition});` : ''
196-
let newCheck = check !== undefined ? `${alter} WITH CHECK (${check});` : ''
197-
let newRoles = roles !== undefined ? `${alter} TO (${roles.join(',')});` : ''
194+
const alter = format(`ALTER POLICY %I ON %I.%I`, name, schema, table)
195+
const newDefinition = definition !== undefined ? `${alter} USING (${definition});` : ''
196+
const newCheck = check !== undefined ? `${alter} WITH CHECK (${check});` : ''
197+
const newRoles = roles !== undefined ? `${alter} TO (${roles.join(',')});` : ''
198198

199199
return `
200200
${newDefinition}

src/api/publications.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,14 @@ const createPublicationSqlize = ({
8989
publish_truncate?: boolean
9090
tables?: string[]
9191
}) => {
92-
let tableClause: string
92+
let tableClause: string = `FOR TABLE ${tables.map(ident).join(',')}`
9393
if (tables === undefined) {
9494
tableClause = 'FOR ALL TABLES'
9595
} else if (tables.length === 0) {
9696
tableClause = ''
97-
} else {
98-
tableClause = `FOR TABLE ${tables.map(ident).join(',')}`
9997
}
10098

101-
let publishOps = []
99+
const publishOps = []
102100
if (publish_insert) publishOps.push('insert')
103101
if (publish_update) publishOps.push('update')
104102
if (publish_delete) publishOps.push('delete')

src/api/tables.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ router.post('/', async (req, res) => {
6262
// Return fresh details
6363
const getTable = selectSingleByName(sqlTemplates, schema, name)
6464
const { data: newTableResults } = await RunQuery(pcConnection, getTable)
65-
let newTable: Tables.Table = newTableResults[0]
65+
const newTable: Tables.Table = newTableResults[0]
6666
return res.status(200).json(newTable)
6767
} catch (error) {
6868
logger.error({ error, req: req.body })
@@ -82,7 +82,7 @@ router.patch('/:id', async (req, res) => {
8282
// Get table
8383
const getTableSql = selectSingleSql(sqlTemplates, id)
8484
const { data: getTableResults } = await RunQuery(pcConnection, getTableSql)
85-
let previousTable: Tables.Table = getTableResults[0]
85+
const previousTable: Tables.Table = getTableResults[0]
8686

8787
// Update fields and name
8888
const nameSql =
@@ -243,16 +243,18 @@ const createTableSqlize = ({
243243
comment?: string
244244
}) => {
245245
const tableSql = format('CREATE TABLE IF NOT EXISTS %I.%I ();', schema, name)
246-
let replicaSql: string
246+
let replicaSql: string = `ALTER TABLE ${ident(schema)}.${ident(
247+
name
248+
)} REPLICA IDENTITY ${replica_identity};`
249+
247250
if (replica_identity === undefined) {
248251
replicaSql = ''
249252
} else if (replica_identity === 'INDEX') {
250253
replicaSql = `ALTER TABLE ${ident(schema)}.${ident(
251254
name
252255
)} REPLICA IDENTITY USING INDEX ${replica_identity_index};`
253-
} else {
254-
replicaSql = `ALTER TABLE ${ident(schema)}.${ident(name)} REPLICA IDENTITY ${replica_identity};`
255256
}
257+
256258
const commentSql =
257259
comment === undefined ? '' : format('COMMENT ON TABLE %I.%I IS %L;', schema, name, comment)
258260
return `

src/lib/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export const toTransaction = (statements: string[]) => {
2121
if (sql.length > 0 && x.slice(-1) !== ';') sql += ';'
2222
return sql
2323
})
24-
let allStatements = cleansed.join('')
24+
const allStatements = cleansed.join('')
2525
return `BEGIN; ${allStatements} COMMIT;`
2626
}

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ app.get('/', (_req, res) =>
1818
)
1919
app.get('/health', (_req, res) => res.status(200).json({ date: new Date() }))
2020

21-
let Server = {
21+
const Server = {
2222
start(port: number) {
2323
this.server = app.listen(port, () => {
2424
logger.info(`App started on port ${port}`)

0 commit comments

Comments
 (0)
0