How to add table/column comments when creating tables?

Hey, we're approaching close to 100 tables in our project. New people will be joining our founding team and we thought documenting the tables using the Postgres table/column commenting feature would make it easier to understand the DB schema. After searching the docs, discord, and google, I couldn't find any info on how to do this in Kysely. Should we resort to raw SQL? Thanks!
Solution:
Hey 👋 Kysely supports column comments in introspectors. This should allow external codegen tools (e.g. kysely-codegen) to generate types with jsdocs. Builders don't have built-in support for adding comments on tables or columns. You can use modifyEnd where possible to add comments with sql template tag....
Jump to solution
3 Replies
Solution
Igal
Igal•8mo ago
Hey 👋 Kysely supports column comments in introspectors. This should allow external codegen tools (e.g. kysely-codegen) to generate types with jsdocs. Builders don't have built-in support for adding comments on tables or columns. You can use modifyEnd where possible to add comments with sql template tag.
import { sql } from 'kysely'

await db.schema
.createTable('awesome_stuff')
.addColumn('price', 'double precision', (col) =>
col.notNull().modifyEnd(sql`comment ${sql.lit('Price in USD')}`),
)
.modifyEnd(sql`comment ${sql.lit('This table is awesome!')}`)
.execute()
import { sql } from 'kysely'

await db.schema
.createTable('awesome_stuff')
.addColumn('price', 'double precision', (col) =>
col.notNull().modifyEnd(sql`comment ${sql.lit('Price in USD')}`),
)
.modifyEnd(sql`comment ${sql.lit('This table is awesome!')}`)
.execute()
lukas.slezevicius
lukas.slezeviciusOP•8mo ago
Thank you!!
Igal
Igal•8mo ago
We might have missed a few spots with modifyEnd , so if you see any that miss it, let us know.
Want results from more Discord servers?
Add your server