kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.82k stars 275 forks source link

I need help to create FULLTEXT index #1261

Closed iceleo-com closed 5 days ago

iceleo-com commented 5 days ago

Hi,

I want to create FULLTEXT index full_name on table tbl_user with 3 columns first_name, middle_name, last_name to MySQL database

But I cannot figure out a good way to do that, except using raw SQL

This is my current code

await db.schema
    .createIndex('full_name')
    .ifNotExists()
    .on('tbl_user')
    .columns(['first_name', 'middle_name', 'last_name'])
    .$call((builder) => {
        const rawSql = builder
            .compile()
            .sql
            .replace(
                /CREATE((?!(INDEX)).)*INDEX/i,
                'CREATE FULLTEXT INDEX',
            );

        console.log(`${rawSql}\n`);

        return sql.raw(rawSql);
    })
    .execute(db);

I guess the FULLTEXT index creating syntax has not been supported yet.

If you can add a new method to the library, it would be nice. Because there is no guarantee that my code will work in the future. Or any better solutions are helpful too.

Thank you so much for your help!

koskimas commented 5 days ago

It's much cleaner and easier to just write the whole thing as raw SQL.