djMax
djMax
KKysely
Created by djMax on 3/11/2024 in #help
Multiple calls to values in insert statement?
I just had a production bug where I was calling .values multiple times on a query object. I've fixed it by passing an array to a single values call instead, but this seems like a bug no? Either the types should reflect that values can no longer be called after it has been, or it should collect the values calls into one array?
7 replies
KKysely
Created by djMax on 2/15/2024 in #help
Is there a way to keep full date precision?
It appears that selecting dates from Postgresql loses some precision, which wreaks havoc with my paging logic. Is there a way to get dates to have full precision on the JS side?
3 replies
KKysely
Created by djMax on 2/14/2024 in #help
How can I get total row count without blowing up types?
I have a simple query function that is in charge of generating a list of ids so that it can be passed into a function which does some other fancy stuff with those ids as a CTE. Like so:
export async function getByExternalId(
db: Kysely<DB>,
partner: string,
externalId: string,
) {
return getWithDataByIds(db, db => db.selectFrom('some_table')
.innerJoin('some_other_table', 'some_table.provider_id', 'some_other_table.provider_id')
.select('some_id')
.where('external_id', '=', externalId)
.where('name', '=', partner));
}
export async function getByExternalId(
db: Kysely<DB>,
partner: string,
externalId: string,
) {
return getWithDataByIds(db, db => db.selectFrom('some_table')
.innerJoin('some_other_table', 'some_table.provider_id', 'some_other_table.provider_id')
.select('some_id')
.where('external_id', '=', externalId)
.where('name', '=', partner));
}
Now, the problem is I also want to get the total row count with COUNT(*) OVER() so that I don't have to make two trips. But if I try to add sql to the select list to get that value, the type system loses its mind... Can I somehow cast the sqlCOUNT(*) OVER() to behave as if the query is going to return a column with the name total and make it not worry about how?
4 replies
KKysely
Created by djMax on 1/6/2024 in #help
Generic function to stream a table
I am trying to write a generic function that will take a kysely connection, a table name, a column name and a value for that column and return a streaming select. I can't figure out the "foward type declarations" to make it all work, and I think it's because of some of the fancy bits that Kysely does for selectFrom("table as alias") that I can't untangle. Playground link here: https://kyse.link/?p=s&i=n4OdlKPOjCkNULp6niLU . As you can see, it does WORK, but the red squigglies mean TS thinks it's wrong. Any idea how to declare this properly? Thanks!
5 replies
KKysely
Created by djMax on 9/16/2023 in #help
How do you formulate a custom type argument?
I would like to pass a record type to a postgres function with kysely. How do I do that? I get "malformed record literal" when I try the obvious things. I'm sure I can raw-sql it together but I'd like to do it as properly as possible.
4 replies
KKysely
Created by djMax on 9/15/2023 in #help
How do I type the arguments to a function?
I am calling a stored proc with parameters (we have very few, but some make the client's life a lot easier and make transactions shorter). I get an error that it can't find the function given the types:
function my_favorite_function(unknown, unknown, unknown)
function my_favorite_function(unknown, unknown, unknown)
I'm calling it like:
select my_favorite_function(someString, someNumber, someJsonCodableObject)
select my_favorite_function(someString, someNumber, someJsonCodableObject)
Do I need to just cast each one in the SQL?
3 replies
KKysely
Created by djMax on 9/15/2023 in #help
InsertInto with all default values?
How do I run the equivalent of
INSERT INTO test DEFAULT VALUES returning
INSERT INTO test DEFAULT VALUES returning
in kysely?
5 replies
KKysely
Created by djMax on 5/24/2023 in #help
create role in migration
Is it possible to create a role in a migration? And to do the grants and things as part of that?
4 replies
KKysely
Created by djMax on 5/17/2023 in #help
insert into with mix of static and table values
How might I execute an insert into that combines JS-side values with a select, like:
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
(Where, 16, 32 and 'hello world' are JS variables, not true literals)
7 replies