K
Kysely•12mo ago
mike

Single source of truth (defaultAlias-schema-tableName)

Hi, is there any way how to set default alias to schema.table mapping to have single source of truth for kysely as well as for existing ORM implementation? We have existing project with huge sequelize implementation and want to take advantage of kysely simplicity for some specific queries. We have existing database architecture mapping in the following object: type ModelName = string; interface TableLocation { tableName: string; schema: string; } const modelTableMapping: Record<ModelName, TableLocation> = { user: {tableName: 'users', schema: 'public'}, post: {tableName: 'posts', schema: 'schema1'}, article: {tableName: 'articles', schema: 'schema2'}, } is it somehow possible to use this object and do mapping to kysely database? as typescript is static typing it's not so straighforward. 😦 any suggestion appreciated.
4 Replies
Unknown User
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
mike
mike•12mo ago
thanks, I'll take a look on that
Igal
Igal•12mo ago
Another option is to maintain a const object and use it like an enum.
const tables = {
user: 'public.users',
post: 'schema1.posts',
article: 'schema2.articles'
} as const satisfies Record<string, keyof Database>

interface Database {
'public.users': UserTable,
'schema1.posts': PostTable,
'schema2.articles': ArticleTable
}

db.selectFrom(tables.user).select('id').execute()
const tables = {
user: 'public.users',
post: 'schema1.posts',
article: 'schema2.articles'
} as const satisfies Record<string, keyof Database>

interface Database {
'public.users': UserTable,
'schema1.posts': PostTable,
'schema2.articles': ArticleTable
}

db.selectFrom(tables.user).select('id').execute()
be careful you don't start rolling your own ORM. also, this is not aligned with Kysely's core design principle - what you see is what you get. we strive to make the code read as close to SQL as possible - predictable. remember you've got autocompletion, co-pilot, and type-safety to help you not make mistakes, so DRY'ing things up is not that beneficial
mike
mike•12mo ago
totally agree with that. based on the actual state of the app's architecture, I have to be careful not to trigger a butterfly effect.