11import { 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' )
54import { RunQuery } from '../lib/connectionPool'
65import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants'
76import { Schemas } from '../lib/interfaces'
87
8+ const { schemas } = sqlTemplates
9+
910/**
1011 * @param {boolean } [includeSystemSchemas=false] - Return system schemas as well as user schemas
1112 */
@@ -14,6 +15,7 @@ interface GetSchemasQueryParams {
1415}
1516
1617const router = Router ( )
18+
1719router . get ( '/' , async ( req , res ) => {
1820 try {
1921 const { data } = await RunQuery ( req . headers . pg , schemas )
@@ -23,11 +25,81 @@ router.get('/', async (req, res) => {
2325
2426 return res . status ( 200 ) . json ( payload )
2527 } 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 )
2778 res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
2879 }
2980} )
3081
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+ }
31103const removeSystemSchemas = ( data : Schemas . Schema [ ] ) => {
32104 return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . name ) )
33105}
0 commit comments