Sqlite JSON

Hello, I'm a new user to Kysely and I'm having trouble using the JSON interface. I would normally just stringify and parse JSON myself when using Kysely, but it looks like I can addColumn("myColumn", "json"). However, when I try to insert values as JSON, I get TypeError: SQLite3 can only bind numbers, strings, bigints, buffers, and null. If I strigify my JSON first, it adds to the database properly, but the Kysely gives me type errors. Should I have my Database table column of type string when I am passing type "json" to Kysely?
Solution:
Hey 👋🏻 Try using: ```ts...
Jump to solution
4 Replies
marshymallow
marshymallowOP12mo ago
Right now, I have
interface MyTable {
myColumn: Array<string>, // or this could just be "object" for JSON
}
interface MyTable {
myColumn: Array<string>, // or this could just be "object" for JSON
}
Also, having a similar issue with dates... I am telling Kysely that the column is to be of type "date", so I expect it to parse my dates before and after database retreival. But, it appears that I must stringify and parse them myself.
Solution
Igal
Igal12mo ago
Hey 👋🏻 Try using:
import { JSONColumnType } from 'kysely'

interface MyTable {
myColumn: JSONColumnType<Array<string>>,
}
import { JSONColumnType } from 'kysely'

interface MyTable {
myColumn: JSONColumnType<Array<string>>,
}
https://kyse.link/?p=s&i=yHizR2sNwRixkL6M2Fxc --- Kysely can support different types for select, insert and update queries. JSONColumnType<T> is a shorthand for ColumnType<T, string, string>
marshymallow
marshymallowOP12mo ago
Thanks! That works. Is there a similar date column type or must I use ColumnType<Date, string, never>?
Igal
Igal12mo ago
There isn't one, you should use what's available and make sure it's aligned with the underlying driver output.
Want results from more Discord servers?
Add your server