Skip to content

Commit aa991a2

Browse files
committed
refactor: exclude default roles by default
1 parent 06c65e4 commit aa991a2

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

src/api/roles.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import sql = require('../lib/sql')
44
const { grants, roles } = sql
55
import { coalesceRowsToArray } from '../lib/helpers'
66
import { RunQuery } from '../lib/connectionPool'
7-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
7+
import { DEFAULT_ROLES, DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
88
import { Roles } from '../lib/interfaces'
99

1010
/**
1111
* @param {boolean} [includeSystemSchemas=false] - Return system schemas as well as user schemas
1212
*/
1313
interface GetRolesQueryParams {
14+
includeDefaultRoles?: boolean
1415
includeSystemSchemas?: boolean
1516
}
1617

@@ -29,6 +30,7 @@ FROM
2930
const query: GetRolesQueryParams = req.query
3031
let payload: Roles.Role[] = data
3132
if (!query?.includeSystemSchemas) payload = removeSystemSchemas(data)
33+
if (!query?.includeDefaultRoles) payload = removeDefaultRoles(payload)
3234

3335
return res.status(200).json(payload)
3436
} catch (error) {
@@ -104,4 +106,8 @@ const removeSystemSchemas = (data: Roles.Role[]) => {
104106
})
105107
}
106108

109+
const removeDefaultRoles = (data: Roles.Role[]) => {
110+
return data.filter((role) => !DEFAULT_ROLES.includes(role.name))
111+
}
112+
107113
export = router

src/api/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Router } from 'express'
33
import sql = require('../lib/sql')
44
const { schemas } = sql
55
import { RunQuery } from '../lib/connectionPool'
6-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
6+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Schemas } from '../lib/interfaces'
88

99
/**

src/api/tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sql = require('../lib/sql')
44
const { columns, grants, primary_keys, relationships, tables } = sql
55
import { coalesceRowsToArray, formatColumns } from '../lib/helpers'
66
import { RunQuery } from '../lib/connectionPool'
7-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
7+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
88
import { Tables } from '../lib/interfaces'
99

1010
const router = Router()

src/api/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Router } from 'express'
33
import sql = require('../lib/sql')
44
const { types } = sql
55
import { RunQuery } from '../lib/connectionPool'
6-
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
6+
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
77
import { Types } from '../lib/interfaces'
88

99
const router = Router()

src/lib/constants.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@ export const PG_API_URL = process.env.PG_API_URL || 'http://localhost'
22
export const PG_API_PORT = Number(process.env.PG_API_PORT || 1337)
33
export const CRYPTO_KEY = process.env.CRYPTO_KEY || 'SAMPLE_KEY'
44
export const PG_CONNECTION = 'postgres://postgres:postgres@localhost:5432/postgres'
5+
6+
export const DEFAULT_ROLES: string[] = [
7+
'pg_read_all_settings',
8+
'pg_read_all_stats',
9+
'pg_stat_scan_tables',
10+
'pg_monitor',
11+
'pg_signal_backend',
12+
'pg_read_server_files',
13+
'pg_write_server_files',
14+
'pg_execute_server_program',
15+
]
16+
17+
export const DEFAULT_SYSTEM_SCHEMAS: string[] = [
18+
'information_schema',
19+
'pg_catalog',
20+
'pg_toast_temp_1',
21+
'pg_temp_1',
22+
'pg_toast',
23+
]

src/lib/constants/schemas.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)