binajmen
binajmen
Explore posts from servers
DTDrizzle Team
Created by binajmen on 3/19/2024 in #help
Store percentage value as integer
up 😇
3 replies
DTDrizzle Team
Created by binajmen on 11/29/2023 in #help
`.orderBy` influence `sql` output
Should I open an issue on Drizzle's Github for the sake of tracking this?
9 replies
DTDrizzle Team
Created by binajmen on 11/29/2023 in #help
`.orderBy` influence `sql` output
ikr 🙈
9 replies
DTDrizzle Team
Created by binajmen on 11/29/2023 in #help
`.orderBy` influence `sql` output
But now, it's even weirder. Why would the orderBy cast it "normally"?
9 replies
DTDrizzle Team
Created by binajmen on 11/29/2023 in #help
`.orderBy` influence `sql` output
Yesterday, I already mentioned I was surprised the case when .. then 1 else 0 end would be casted a string. https://discord.com/channels/1043890932593987624/1179094189615628348
9 replies
DTDrizzle Team
Created by binajmen on 11/29/2023 in #help
`.orderBy` influence `sql` output
If I remove the .mapWith, things get more funny:
const exchanges = await db
.select({
...getTableColumns(stockExchanges),
isUsed: sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`,
})
.from(stockExchanges)
.leftJoin(usedExchanges, eq(usedExchanges.data, stockExchanges.code));

console.log(exchanges.find((exch) => exch.code === "BR"));

const orderedExchanges = await db
.select({
...getTableColumns(stockExchanges),
isUsed: sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`,
})
.from(stockExchanges)
.leftJoin(usedExchanges, eq(usedExchanges.data, stockExchanges.code))
.orderBy(asc(stockExchanges.name));

console.log(orderedExchanges.find((exch) => exch.code === "BR"));
const exchanges = await db
.select({
...getTableColumns(stockExchanges),
isUsed: sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`,
})
.from(stockExchanges)
.leftJoin(usedExchanges, eq(usedExchanges.data, stockExchanges.code));

console.log(exchanges.find((exch) => exch.code === "BR"));

const orderedExchanges = await db
.select({
...getTableColumns(stockExchanges),
isUsed: sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`,
})
.from(stockExchanges)
.leftJoin(usedExchanges, eq(usedExchanges.data, stockExchanges.code))
.orderBy(asc(stockExchanges.name));

console.log(orderedExchanges.find((exch) => exch.code === "BR"));
The output:
{
name: 'Euronext Brussels',
code: 'BR',
operatingMIC: 'XBRU',
country: 'Belgium',
currency: 'EUR',
countryISO2: 'BE',
countryISO3: 'BEL',
isUsed: '1'
}
{
name: 'Euronext Brussels',
code: 'BR',
operatingMIC: 'XBRU',
country: 'Belgium',
currency: 'EUR',
countryISO2: 'BE',
countryISO3: 'BEL',
isUsed: 1
}
{
name: 'Euronext Brussels',
code: 'BR',
operatingMIC: 'XBRU',
country: 'Belgium',
currency: 'EUR',
countryISO2: 'BE',
countryISO3: 'BEL',
isUsed: '1'
}
{
name: 'Euronext Brussels',
code: 'BR',
operatingMIC: 'XBRU',
country: 'Belgium',
currency: 'EUR',
countryISO2: 'BE',
countryISO3: 'BEL',
isUsed: 1
}
Is used is casted as a string in the first query, and as a number in the second query.
9 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
I think .mapWith is the right trade-off here 🙂
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
At least it seems to support your suggestion that it is a deliberate choice
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
thanks for the sparring session 🙏
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
mmh ok, I'll move on with .mapWith then. I'll dig in parallel the reason, cause I feel this is wrong, if not a bug 🙂
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
still the same 🙈
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
let me try
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
anyhow, I wonder if this could be considered as a bug, since it's not a 1 to 1 result compared to the pure SQL
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
I probably mispoke, maybe it is as the mysql2 driver
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
I already tried cast(1 as signed) and still get a string
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
Yup that's what I would expect. In this case the db returns integer. Yet drizzle provides me string. It seems there's an inconvenient conversion made by Drizzle. At least, that's how it looks like
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
If I run the equivalent query in a MySQL client, I get a number 1 or 0.
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
What I don't understand is why the end result is a string, whereas it should be at least a number Oo
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
Because Boolean will always return true on a non empty string
33 replies
DTDrizzle Team
Created by binajmen on 11/28/2023 in #help
Why is 1 and 0 are returned as string?
I did find out mapWith and I'm using as follow:
sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`.mapWith(
(value) => value === "1",
),
sql<boolean>`case when ${usedExchanges.data} is not null then 1 else 0 end`.mapWith(
(value) => value === "1",
),
33 replies