kysely-org / kysely

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

What should I type binary data as? #912

Closed Qrokqt closed 5 months ago

Qrokqt commented 5 months ago

We use mysql and are trying to convert from knex but we have a number of columns in binary formats. For ids when selecting we use BIN_TO_UUID and inserting/updating we use UUID_TO_BIN. For spatial columns we similarly use ST_AsGeoJSON and ST_GeomFromGeoJSON. What should we tell kysely the type of those columns are?

Also, what does the O in the raw builder represent? If I put string for everything it seems to all work, but it would be nice to know why

let query = db.selectFrom('users');
query = query.select([
    sql<string>`BIN_TO_UUID(users.id)`.as('id'),
]);
query = query.where('users.id', '=', sql<string>`UUID_TO_BIN(${id})`);

let query = db.insertInto('users');
query = query.values({
    id: sql<string>`UUID_TO_BIN(${user.id})`,
});
koskimas commented 5 months ago

O is the expression's type. You should use the type the db returns. Kysely doesn't touch the data types. It's left to the underlying driver (mysql2 in this case). Figure out what it returns for those and update your table interface types accordingly.

koskimas commented 5 months ago

https://kysely.dev/docs/recipes/data-types