Skip to content

Commit cf00b87

Browse files
committed
fix: Adds the enums key to the column
Fixes #33
1 parent 2b2ce14 commit cf00b87

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

src/lib/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export namespace Tables {
9393
grants: Roles.Grant[]
9494
primary_keys: PrimaryKey[]
9595
relationships: Relationship[]
96+
enums: any[]
9697
}
9798

9899
export interface Column {

src/lib/sql/columns.sql

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ SELECT
88
column_default AS default_value,
99
data_type,
1010
udt_name AS format,
11-
col_description(
12-
c.oid,
13-
ordinal_position
14-
) AS description,
15-
is_identity::boolean,
11+
col_description(c.oid, ordinal_position) AS description,
12+
is_identity :: boolean,
1613
identity_generation,
17-
is_nullable::boolean,
18-
is_updatable::boolean
14+
is_nullable :: boolean,
15+
is_updatable :: boolean,
16+
array_to_json(
17+
array(
18+
SELECT
19+
enumlabel
20+
FROM
21+
pg_catalog.pg_enum enums
22+
WHERE
23+
udt_name = pg_catalog.Format_type(enums.enumtypid :: regclass, NULL)
24+
ORDER BY
25+
enums.enumsortorder
26+
)
27+
) AS enums
1928
FROM
2029
information_schema.columns
21-
JOIN pg_class c ON quote_ident(table_schema)::regnamespace = c.relnamespace
22-
AND c.relname = table_name
30+
JOIN pg_class c ON quote_ident(table_schema) :: regnamespace = c.relnamespace
31+
AND c.relname = table_name

test/integration/index.spec.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,24 @@ describe('/tables', async () => {
168168
it('GET', async () => {
169169
const tables = await axios.get(`${URL}/tables`)
170170
const datum = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.users')
171+
const memes = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.memes')
171172
const notIncluded = tables.data.find((x) => `${x.schema}.${x.name}` === 'pg_catalog.pg_type')
173+
const idColumn = datum.columns.find((x) => x.name === 'id')
174+
const nameColumn = datum.columns.find((x) => x.name === 'name')
175+
const statusColumn = memes.columns.find((x) => x.name ==='status')
172176
assert.equal(tables.status, STATUS.SUCCESS)
173177
assert.equal(true, !!datum)
174178
assert.equal(true, !notIncluded)
175179
assert.equal(datum['rls_enabled'], false)
176180
assert.equal(datum['rls_forced'], false)
177-
})
178-
it('should return the columns, grants, and policies', async () => {
179-
const tables = await axios.get(`${URL}/tables`)
180-
const datum = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.users')
181-
const idColumn = datum.columns.find((x) => x.name === 'id')
182-
const nameColumn = datum.columns.find((x) => x.name === 'name')
183-
assert.equal(true, !!datum)
184181
assert.equal(datum.columns.length > 0, true)
185182
assert.equal(datum.primary_keys.length > 0, true)
186183
assert.equal(idColumn.is_updatable, true)
187184
assert.equal(idColumn.is_identity, true)
188185
assert.equal(nameColumn.is_identity, false)
189186
assert.equal(datum.grants.length > 0, true)
190187
assert.equal(datum.policies.length == 0, true)
188+
assert.equal(statusColumn.enums[0], 'new')
191189
})
192190
it('/tables should return the relationships', async () => {
193191
const tables = await axios.get(`${URL}/tables`)

test/postgres/mnt/01-memes.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ INSERT INTO public.category (id, name) VALUES
2222
(4, 'Cute'),
2323
(5, 'Interesting');
2424

25+
CREATE TYPE meme_status AS ENUM ('new', 'old', 'retired');
26+
2527
CREATE TABLE public.memes (
2628
id serial NOT NULL PRIMARY KEY,
2729
name text NOT NULL,
2830
category INTEGER REFERENCES category(id),
2931
metadata jsonb,
30-
created_at TIMESTAMP NOT NULL
32+
created_at TIMESTAMP NOT NULL,
33+
status meme_status DEFAULT 'old'
3134
);
3235

3336
INSERT INTO public.memes (name, category, created_at) VALUES

0 commit comments

Comments
 (0)