I want to create a function or stored procedure that will create the table in PostgreSQL with its columns based from the parameter passed through the function.
The example of the parameter will be in jsonb
type, like this:
{
"columns": [
{
"name": "name",
"dataType": "Text",
"isRequired": true
},
{
"name": "type",
"dataType": "Text",
"isRequired": false
},
{
"name": "id",
"dataType": "Text",
"isRequired": true
}
]
}
What I would like to achieve is create the column based on the name
field along with their dataType
and isRequired
will represents the NULL
or NOT NULL
.
Currently I have my function look like this:
CREATE OR REPLACE FUNCTION create_table(tableName text, metadata json) RETURNS VOID AS
$$
BEGIN
EXECUTE (SELECT format('CREATE TABLE IF NOT EXISTS %s (%s);', $1, y.cols)
FROM (SELECT string_agg((s.details ->> 'name')::text || ' ' || (s.details->>'dataType')::text || ' ' || CASE WHEN (s.details->>'isRequired') = 'true'
THEN 'NOT NULL'
ELSE 'NULL'
END, '', '') AS cols
FROM (SELECT json_array_elements(metadata) as details) s ) y);
END
$$
LANGUAGE plpgsql VOLATILE;
But this gave me error:
function string_agg(text, unknown, unknown) does not exist
.
Ultimately I wanted a query look like this:
CREATE TABLE type_1 (
name TEXT NOT NULL,
type TEXT NULL,
id TEXT NOT NULL
);
If anyone knows how to do this I deeply appreciated it, thank you!