1
1
import { Router } from 'express'
2
-
3
- import sql = require( '../lib/sql' )
4
- const { schemas } = sql
2
+ import SQL from 'sql-template-strings'
3
+ import sqlTemplates = require( '../lib/sql' )
5
4
import { RunQuery } from '../lib/connectionPool'
6
5
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
7
6
import { Schemas } from '../lib/interfaces'
8
7
8
+ const { schemas } = sqlTemplates
9
+
9
10
/**
10
11
* @param {boolean } [includeSystemSchemas=false] - Return system schemas as well as user schemas
11
12
*/
@@ -14,6 +15,7 @@ interface GetSchemasQueryParams {
14
15
}
15
16
16
17
const router = Router ( )
18
+
17
19
router . get ( '/' , async ( req , res ) => {
18
20
try {
19
21
const { data } = await RunQuery ( req . headers . pg , schemas )
@@ -23,11 +25,81 @@ router.get('/', async (req, res) => {
23
25
24
26
return res . status ( 200 ) . json ( payload )
25
27
} catch ( error ) {
26
- console . log ( 'throwing error' )
28
+ console . log ( 'throwing error' , error )
29
+ res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
30
+ }
31
+ } )
32
+ router . post ( '/' , async ( req , res ) => {
33
+ try {
34
+ const name : string = req . body . name
35
+ const owner : string = req . body . owner
36
+
37
+ // Create the schema
38
+ const schemqQuery = createSchema ( name , owner )
39
+ await RunQuery ( req . headers . pg , schemqQuery )
40
+
41
+ // Return fresh details
42
+ const getSchema = selectSingleByName ( name )
43
+ const { data } = await RunQuery ( req . headers . pg , getSchema )
44
+ let schema : Schemas . Schema = data [ 0 ]
45
+ return res . status ( 200 ) . json ( schema )
46
+ } catch ( error ) {
47
+ console . log ( 'throwing error' , error )
48
+ res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
49
+ }
50
+ } )
51
+ router . patch ( '/:id' , async ( req , res ) => {
52
+ try {
53
+ const id : number = parseInt ( req . params . id )
54
+ const name : string = req . body . name
55
+ const owner : string = req . body . owner
56
+
57
+ // Get schema name
58
+ const getSchema = selectSingleSql ( id )
59
+ const { data : getSchemaResults } = await RunQuery ( req . headers . pg , getSchema )
60
+ let previousSchema : Schemas . Schema = getSchemaResults [ 0 ]
61
+
62
+ // Update fields
63
+ if ( owner ) {
64
+ const updateOwner = alterSchemaOwner ( previousSchema . name , owner )
65
+ await RunQuery ( req . headers . pg , updateOwner )
66
+ }
67
+ if ( name ) {
68
+ const updateName = alterSchemaName ( previousSchema . name , name )
69
+ await RunQuery ( req . headers . pg , updateName )
70
+ }
71
+
72
+ // Return fresh details
73
+ const { data : updatedSchemaResults } = await RunQuery ( req . headers . pg , getSchema )
74
+ let updatedSchema : Schemas . Schema = updatedSchemaResults [ 0 ]
75
+ return res . status ( 200 ) . json ( updatedSchema )
76
+ } catch ( error ) {
77
+ console . log ( 'throwing error' , error )
27
78
res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
28
79
}
29
80
} )
30
81
82
+ // Helpers
83
+ const selectSingleSql = ( id : number ) => {
84
+ const query = SQL `` . append ( schemas ) . append ( SQL ` where nsp.oid = ${ id } ` )
85
+ return query
86
+ }
87
+ const selectSingleByName = ( name : string ) => {
88
+ const query = SQL `` . append ( schemas ) . append ( SQL ` where schema_name = ${ name } ` )
89
+ return query
90
+ }
91
+ const createSchema = ( name : string , owner : string = 'postgres' ) => {
92
+ const query = SQL `` . append ( `CREATE SCHEMA IF NOT EXISTS ${ name } AUTHORIZATION ${ owner } ` )
93
+ return query
94
+ }
95
+ const alterSchemaName = ( previousName : string , newName : string ) => {
96
+ const query = SQL `` . append ( `ALTER SCHEMA ${ previousName } RENAME TO ${ newName } ` )
97
+ return query
98
+ }
99
+ const alterSchemaOwner = ( schemaName : string , newOwner : string ) => {
100
+ const query = SQL `` . append ( `ALTER SCHEMA ${ schemaName } OWNER TO ${ newOwner } ` )
101
+ return query
102
+ }
31
103
const removeSystemSchemas = ( data : Schemas . Schema [ ] ) => {
32
104
return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . name ) )
33
105
}
0 commit comments