|
1 | | -import { Router } from 'express' |
2 | | -import format from 'pg-format' |
3 | 1 | import SQL from 'sql-template-strings' |
4 | 2 | import sqlTemplates = require('../lib/sql') |
5 | 3 | import { RunQuery } from '../lib/connectionPool' |
6 | 4 | import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants' |
7 | | -import { Schemas } from '../lib/interfaces' |
| 5 | +import { Schemas as Interfaces } from '../lib/interfaces' |
8 | 6 |
|
9 | | -const { schemas } = sqlTemplates |
| 7 | +const { schemas: allSchemasSql } = sqlTemplates |
| 8 | +const defaultSchemasList = DEFAULT_SYSTEM_SCHEMAS.map((x) => `'${x}'`).join(', ') |
10 | 9 |
|
11 | 10 | /** |
12 | | - * @param {boolean} [include_system_schemas=false] - Return system schemas as well as user schemas |
| 11 | + * Get a list of schemas in the database |
13 | 12 | */ |
14 | | -interface QueryParams { |
15 | | - include_system_schemas?: string |
16 | | -} |
17 | | - |
18 | | -const router = Router() |
19 | | - |
20 | | -router.get('/', async (req, res) => { |
21 | | - try { |
22 | | - const { data } = await RunQuery(req.headers.pg, schemas) |
23 | | - const query: QueryParams = req.query |
24 | | - const include_system_schemas = query?.include_system_schemas === 'true' |
25 | | - let payload: Schemas.Schema[] = data |
26 | | - if (!include_system_schemas) payload = removeSystemSchemas(data) |
27 | | - |
28 | | - return res.status(200).json(payload) |
29 | | - } catch (error) { |
30 | | - console.log('throwing error', error) |
31 | | - res.status(500).json({ error: 'Database error', status: 500 }) |
| 13 | +export async function list( |
| 14 | + /** A Postgres connection string */ |
| 15 | + connection: string, |
| 16 | + { |
| 17 | + /** If true, will include the system schemas */ |
| 18 | + include_system_schemas = false, |
| 19 | + }: { |
| 20 | + include_system_schemas?: boolean |
32 | 21 | } |
33 | | -}) |
34 | | - |
35 | | -router.post('/', async (req, res) => { |
| 22 | +): /** Returns a list of schemas */ |
| 23 | +Promise<{ data: Interfaces.Schema[]; error: null | Error }> { |
36 | 24 | try { |
37 | | - const name: string = req.body.name |
38 | | - const owner: string = req.body.owner |
39 | | - |
40 | | - // Create the schema |
41 | | - const schemqQuery = createSchema(name, owner) |
42 | | - await RunQuery(req.headers.pg, schemqQuery) |
43 | | - |
44 | | - // Return fresh details |
45 | | - const getSchema = selectSingleByName(name) |
46 | | - const { data } = await RunQuery(req.headers.pg, getSchema) |
47 | | - let schema: Schemas.Schema = data[0] |
48 | | - return res.status(200).json(schema) |
| 25 | + let query = SQL``.append(allSchemasSql) |
| 26 | + if (!include_system_schemas) { |
| 27 | + query.append(`where schema_name not in (${defaultSchemasList})`) |
| 28 | + } |
| 29 | + const { data } = await RunQuery<Interfaces.Schema>(connection, query) |
| 30 | + return { data, error: null } |
49 | 31 | } catch (error) { |
50 | | - console.log('throwing error', error) |
51 | | - res.status(500).json({ error: 'Database error', status: 500 }) |
| 32 | + return { data: null, error } |
52 | 33 | } |
53 | | -}) |
| 34 | +} |
54 | 35 |
|
55 | | -router.patch('/:id', async (req, res) => { |
| 36 | +/** |
| 37 | + * Get a single schema by its `oid` |
| 38 | + */ |
| 39 | +export async function byId( |
| 40 | + /** A Postgres connection string */ |
| 41 | + connection: string, |
| 42 | + /** The schema `oid` */ |
| 43 | + id: number |
| 44 | +): /** Returns a single schemas */ |
| 45 | +Promise<{ data: Interfaces.Schema; error: null | Error }> { |
56 | 46 | try { |
57 | | - const id: number = parseInt(req.params.id) |
58 | | - const name: string = req.body.name |
59 | | - const owner: string = req.body.owner |
60 | | - |
61 | | - // Get schema name |
62 | | - const getSchema = selectSingleSql(id) |
63 | | - const { data: getSchemaResults } = await RunQuery(req.headers.pg, getSchema) |
64 | | - let previousSchema: Schemas.Schema = getSchemaResults[0] |
65 | | - |
66 | | - // Update fields |
67 | | - if (owner) { |
68 | | - const updateOwner = alterSchemaOwner(previousSchema.name, owner) |
69 | | - await RunQuery(req.headers.pg, updateOwner) |
70 | | - } |
71 | | - // NB: Run name updates last |
72 | | - if (name) { |
73 | | - const updateName = alterSchemaName(previousSchema.name, name) |
74 | | - await RunQuery(req.headers.pg, updateName) |
75 | | - } |
76 | | - |
77 | | - // Return fresh details |
78 | | - const { data: updatedResults } = await RunQuery(req.headers.pg, getSchema) |
79 | | - let updated: Schemas.Schema = updatedResults[0] |
80 | | - return res.status(200).json(updated) |
| 47 | + const query = SQL``.append(allSchemasSql).append(SQL` where nsp.oid = ${id}`) |
| 48 | + const { data } = await RunQuery<Interfaces.Schema>(connection, query) |
| 49 | + return { data: data[0], error: null } |
81 | 50 | } catch (error) { |
82 | | - console.log('throwing error', error) |
83 | | - res.status(500).json({ error: 'Database error', status: 500 }) |
| 51 | + return { data: null, error } |
84 | 52 | } |
85 | | -}) |
| 53 | +} |
86 | 54 |
|
87 | | -router.delete('/:id', async (req, res) => { |
| 55 | +/** |
| 56 | + * Get a single schema by its name |
| 57 | + */ |
| 58 | +export async function byName ( |
| 59 | + /** A Postgres connection string */ |
| 60 | + connection: string, |
| 61 | + /** The schema name */ |
| 62 | + name: string |
| 63 | +): /** Returns a single schemas */ |
| 64 | +Promise<{ data: Interfaces.Schema; error: null | Error }> { |
88 | 65 | try { |
89 | | - const id = parseInt(req.params.id) |
90 | | - const getNameQuery = selectSingleSql(id) |
91 | | - const schema = (await RunQuery(req.headers.pg, getNameQuery)).data[0] |
92 | | - |
93 | | - const cascade = req.query.cascade === 'true' |
94 | | - const query = dropSchemaSqlize(schema.name, cascade) |
95 | | - await RunQuery(req.headers.pg, query) |
96 | | - |
97 | | - return res.status(200).json(schema) |
| 66 | + const query = SQL``.append(allSchemasSql).append(SQL` where schema_name = ${name}`) |
| 67 | + const { data } = await RunQuery<Interfaces.Schema>(connection, query) |
| 68 | + return { data: data[0], error: null } |
98 | 69 | } catch (error) { |
99 | | - console.log('throwing error', error) |
100 | | - res.status(500).json({ error: 'Database error', status: 500 }) |
| 70 | + return { data: null, error } |
101 | 71 | } |
102 | | -}) |
103 | | - |
104 | | -// Helpers |
105 | | -const selectSingleSql = (id: number) => { |
106 | | - const query = SQL``.append(schemas).append(SQL` where nsp.oid = ${id}`) |
107 | | - return query |
108 | 72 | } |
109 | | -const selectSingleByName = (name: string) => { |
110 | | - const query = SQL``.append(schemas).append(SQL` where schema_name = ${name}`) |
111 | | - return query |
112 | | -} |
113 | | -const createSchema = (name: string, owner: string = 'postgres') => { |
114 | | - const query = SQL``.append(`CREATE SCHEMA IF NOT EXISTS ${name} AUTHORIZATION ${owner}`) |
115 | | - return query |
116 | | -} |
117 | | -const alterSchemaName = (previousName: string, newName: string) => { |
118 | | - const query = SQL``.append(`ALTER SCHEMA ${previousName} RENAME TO ${newName}`) |
119 | | - return query |
120 | | -} |
121 | | -const alterSchemaOwner = (schemaName: string, newOwner: string) => { |
122 | | - const query = SQL``.append(`ALTER SCHEMA ${schemaName} OWNER TO ${newOwner}`) |
123 | | - return query |
124 | | -} |
125 | | -const dropSchemaSqlize = (name: string, cascade: boolean) => { |
126 | | - const query = `DROP SCHEMA ${format.ident(name)} ${cascade ? 'CASCADE' : 'RESTRICT'}` |
127 | | - return query |
128 | | -} |
129 | | -const removeSystemSchemas = (data: Schemas.Schema[]) => { |
130 | | - return data.filter((x) => !DEFAULT_SYSTEM_SCHEMAS.includes(x.name)) |
131 | | -} |
132 | | - |
133 | | -export = router |
0 commit comments