K
Join ServerKysely
help
What is the suggested way of adding/removing methods to expression builders?
Solution
Context:
I am building a custom dialect for YDB https://github.com/Gaspero/kysely-ydb
YDB is slightly different from standart dialects. E.g. create/drop index expressions are a part of alter table expression (ALTER TABLE ... ADD/DROP INDEX) https://ydb.tech/en/docs/yql/reference/syntax/alter_table#secondary-index
There are also other differences that are not as critical, but I am thinking of the ways how I can protect library users from writing broken SQL queries. E.g. Insert/Replace/Update/Upsert do not support returning values and thus do not support RETURNING expression; CREATE TABLE expression only supports primary key constraint.
I've read extending Kysely section of docs https://kysely.dev/docs/recipes/extending-kysely and it looks like both options (Extending using inheritance
and Extending using module augmentation) are not advised.
I was wondering, may be there is some recommended way of modifying existing expression builders?
Regarding CREATE INDEX issue - i've managed to solve the problem not in a very elegant way but it might be non-intuitive for users familiar with YDB syntax. So I still keep looking for the idiomatic way of adding addIndex/dropIndex methods to CreateTableBuilder/AlterTableBuilder and removing .reateIndex/dropIndex from SchemaModule.
I've modified Query Compiler visitCreateIndex method
then running
results to expected sql statement
I am building a custom dialect for YDB https://github.com/Gaspero/kysely-ydb
YDB is slightly different from standart dialects. E.g. create/drop index expressions are a part of alter table expression (ALTER TABLE ... ADD/DROP INDEX) https://ydb.tech/en/docs/yql/reference/syntax/alter_table#secondary-index
There are also other differences that are not as critical, but I am thinking of the ways how I can protect library users from writing broken SQL queries. E.g. Insert/Replace/Update/Upsert do not support returning values and thus do not support RETURNING expression; CREATE TABLE expression only supports primary key constraint.
I've read extending Kysely section of docs https://kysely.dev/docs/recipes/extending-kysely and it looks like both options (Extending using inheritance
and Extending using module augmentation) are not advised.
I was wondering, may be there is some recommended way of modifying existing expression builders?
Regarding CREATE INDEX issue - i've managed to solve the problem not in a very elegant way but it might be non-intuitive for users familiar with YDB syntax. So I still keep looking for the idiomatic way of adding addIndex/dropIndex methods to CreateTableBuilder/AlterTableBuilder and removing .reateIndex/dropIndex from SchemaModule.
I've modified Query Compiler visitCreateIndex method
protected override visitCreateIndex(node: CreateIndexNode): void {
if (node.table) {
this.append('alter table ')
this.visitNode(node.table)
}
this.append(' add ')
this.append('index ')
this.visitNode(node.name)
if (node.expression) {
this.append(' global on (')
this.visitNode(node.expression)
this.append(')')
}
}
then running
await db.schema
.createIndex('user_email_index')
.on('users_test')
.column('email')
.execute()
results to expected sql statement
alter table
users_testadd index
user_email_indexglobal on (
email)
Hey π
Kysely's API is dialect agnostic by design. This means we don't omit/add methods to builders based on current dialect.
Kysely's API is dialect agnostic by design. This means we don't omit/add methods to builders based on current dialect.
adding/dropping indexes is also supported in MySQL's alter table. Wanna submit an issue?
Got it. Thank you!
its actually 2 separate issues, one for alter table add/drop index, one for create table add index.
Ok. I will submit two issues and I am ready to contribute if this issues will get βgreenlitβ label
I actually meant an issue for create table, and an issue for alter table haha π
thanks!
also, looks like they're greenlit