Skip to content

Commit b659954

Browse files
wasabigeeksoedirgo
authored andcommitted
feat(lib): add naive constraint definition
on column creation
1 parent 2e9b4cd commit b659954

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default class PostgresMetaColumns {
8989
is_primary_key = false,
9090
is_unique = false,
9191
comment,
92+
constraint,
9293
}: {
9394
table_id: number
9495
name: string
@@ -101,6 +102,7 @@ export default class PostgresMetaColumns {
101102
is_primary_key?: boolean
102103
is_unique?: boolean
103104
comment?: string
105+
constraint?: string
104106
}): Promise<PostgresMetaResult<PostgresColumn>> {
105107
const { data, error } = await this.metaTables.retrieve({ id: table_id })
106108
if (error) {
@@ -120,6 +122,10 @@ export default class PostgresMetaColumns {
120122
const isNullableClause = is_nullable ? 'NULL' : 'NOT NULL'
121123
const isPrimaryKeyClause = is_primary_key ? 'PRIMARY KEY' : ''
122124
const isUniqueClause = is_unique ? 'UNIQUE' : ''
125+
const constraintSql =
126+
constraint === undefined
127+
? ''
128+
: constraint
123129
const commentSql =
124130
comment === undefined
125131
? ''
@@ -132,7 +138,8 @@ BEGIN;
132138
${isIdentityClause}
133139
${isNullableClause}
134140
${isPrimaryKeyClause}
135-
${isUniqueClause};
141+
${isUniqueClause}
142+
${constraintSql};
136143
${commentSql};
137144
COMMIT;`
138145
{

test/integration/index.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,21 @@ describe('/tables', async () => {
414414
await axios.delete(`${URL}/columns/${newTable.id}.1`)
415415
await axios.delete(`${URL}/tables/${newTable.id}`)
416416
})
417+
it('POST /columns with constraint definition', async () => {
418+
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
419+
const { error } = await axios.post(`${URL}/columns`, {
420+
table_id: newTable.id,
421+
name: 'description',
422+
type: 'text',
423+
constraint: "CHECK (description <> '')",
424+
})
425+
426+
// TODO: some way to check constraints?
427+
assert.equal(error, undefined)
428+
429+
await axios.delete(`${URL}/columns/${newTable.id}.1`)
430+
await axios.delete(`${URL}/tables/${newTable.id}`)
431+
})
417432
it('PATCH /columns', async () => {
418433
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo bar' })
419434
await axios.post(`${URL}/columns`, {

0 commit comments

Comments
 (0)