K
Kysely•15mo ago
Gaspero

What is the suggested way of adding/removing methods to expression builders?

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 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_test add index user_email_index global on (email)
GitHub
GitHub - Gaspero/kysely-ydb
Contribute to Gaspero/kysely-ydb development by creating an account on GitHub.
Extending kysely | Kysely
In many cases Kysely doesn't provide a built-in type-safe method for a feature. It's often because adding
ALTER TABLE
Using the ALTER TABLE command, you can change the composition of columns and additional table parameters. You can specify several actions in one command. In general, the ALTER TABLE command looks like this:
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. ...
GitHub
GitHub - Gaspero/kysely-ydb
Contribute to Gaspero/kysely-ydb development by creating an account on GitHub.
Extending kysely | Kysely
In many cases Kysely doesn't provide a built-in type-safe method for a feature. It's often because adding
ALTER TABLE
Using the ALTER TABLE command, you can change the composition of columns and additional table parameters. You can specify several actions in one command. In general, the ALTER TABLE command looks like this:
Jump to solution
6 Replies
Igal
Igal•15mo ago
Hey 👋 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?
Gaspero
Gaspero•15mo ago
Got it. Thank you!
Igal
Igal•15mo ago
its actually 2 separate issues, one for alter table add/drop index, one for create table add index.
Gaspero
Gaspero•15mo ago
Ok. I will submit two issues and I am ready to contribute if this issues will get ‘greenlit’ label
Gaspero
Gaspero•15mo ago
GitHub
[Feature] Allow creating indexes using ALTER TABLE ... ADD INDEX · ...
Currently the only way of creating indexes using Kysely is using createIndex method e.g.: await db.schema .createIndex('user_email_index') .on('users_test') .column('email')...
GitHub
[Feature] Allow removing indexes using ALTER TABLE ... DROP INDEX ·...
Currently the only way of removing indexes using Kysely is using dropIndex method e.g.: await db.schema .dropIndex('user_email_index') .on('users_test') .execute() But MySQL and som...
Igal
Igal•15mo ago
I actually meant an issue for create table, and an issue for alter table haha 😅 thanks! also, looks like they're greenlit