Daniel Cruz
Daniel Cruz
KKysely
Created by Daniel Cruz on 5/19/2023 in #help
Coalesce return empty array
Hey is there a way to return an empty array rather than null when an array column is empty? I already tried
.select(coalesce("categories",sql`ARRAY[]::"CollectionCategory"[]`).as("categories"))
.select(coalesce("categories",sql`ARRAY[]::"CollectionCategory"[]`).as("categories"))
but it's still returning null and not an empty array
14 replies
KKysely
Created by Daniel Cruz on 5/18/2023 in #help
Extract OrderBy TS Keys
Hey I have the following query
const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuery) =>
db
.selectFrom((eb) =>
eb
.selectFrom(...)
.innerJoin(...)
.where(...)
.select([
...
])
.as(...)
)
.select([
...
])
.offset(offset)
.limit(pageSize);
const getBaseQuery = ({ offset, pageSize, slug }: GetBaseQuery) =>
db
.selectFrom((eb) =>
eb
.selectFrom(...)
.innerJoin(...)
.where(...)
.select([
...
])
.as(...)
)
.select([
...
])
.offset(offset)
.limit(pageSize);
And I'm trying to create a function that accepts that query and adds and orderBy expression based in some conditions
type BaseQuery = ReturnType<typeof getBaseQuery>;

type AddOrderBy = {
query: BaseQuery;
key?: 'what do I put here';
dir?: "asc" | "desc";
};

const addOrderBy = ({ query, key, dir }: AddOrderBy) => {
...
};
type BaseQuery = ReturnType<typeof getBaseQuery>;

type AddOrderBy = {
query: BaseQuery;
key?: 'what do I put here';
dir?: "asc" | "desc";
};

const addOrderBy = ({ query, key, dir }: AddOrderBy) => {
...
};
How can I get the accepted key value types so I can put them in my type declaration?
11 replies
KKysely
Created by Daniel Cruz on 5/10/2023 in #help
Correct type definitions for function receiving builder
5 replies
KKysely
Created by Daniel Cruz on 5/8/2023 in #help
Transform Postgres array into JS array
Hey, I was wondering if there's a helper or something to transform Postgres arrays {one, two} into JS arrays ['one', 'two']. Right I know the Postgres array is turned into a string when executing queries
25 replies
KKysely
Created by Daniel Cruz on 5/4/2023 in #help
Argument of type 'string' is not assignable to parameter of type 'DynamicReferenceBuilder<never>'.
The following statement, is giving me the error on the title
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"),
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"),
I have other fn calls that don't fail, so Idk what could be the issue
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"), // TS does complain ❌
fn("round", [fn.sum("s.volume"), sql.lit(2)]).as("volume"), // TS does not complain ✅
fn("round", [fn.avg("s.avg_price"), sql.lit(5)]).as("avg_price"),// TS does not complain✅
fn("round", [fn.min("ld.price_btc"), sql.lit(5)]).as("floor_price"), // TS does complain ❌
fn("round", [fn.sum("s.volume"), sql.lit(2)]).as("volume"), // TS does not complain ✅
fn("round", [fn.avg("s.avg_price"), sql.lit(5)]).as("avg_price"),// TS does not complain✅
15 replies
KKysely
Created by Daniel Cruz on 4/20/2023 in #help
Join DATE_TRUNC
Hey, sorry if this is already answered somewhere. I couldn't find anything 😦 I want to translate this part of a SQL query into Kysely
LEFT JOIN currency cu ON DATE_TRUNC('day', s.timestamp)
LEFT JOIN currency cu ON DATE_TRUNC('day', s.timestamp)
I tried this, but doesn't seem to be working
.leftJoin(
"currency as cu",
sql`DATE_TRUNC('day', s.timestamp)`.as("s.timestamp"),
"cu.timestamp"
);
.leftJoin(
"currency as cu",
sql`DATE_TRUNC('day', s.timestamp)`.as("s.timestamp"),
"cu.timestamp"
);
16 replies