tcurdt
tcurdt
Explore posts from servers
DTDrizzle Team
Created by tcurdt on 10/16/2024 in #help
inArray not working as expected
This works but looks like a (sql injection) const tagsArray = selectedTags.map((tag) => '${tag}').join(', '); where.push(sql${tagData.tags} @> ARRAY[${sql.raw(tagsArray)}]::text[]); Why doesn't this just work (tm)? where.push(inArray(tagData.tags, selectedTags));
39 replies
DTDrizzle Team
Created by tcurdt on 10/12/2024 in #help
custom datatypes
Let's say I have
features: text().array().notNull().default(sql`ARRAY[]::text[]`),
features: text().array().notNull().default(sql`ARRAY[]::text[]`),
but I want to shorten this to textArray(). I'd assume this should be as easy as
extern function textArray() {
return text().array().notNull().default(sql`ARRAY[]::text[]`)
}
extern function textArray() {
return text().array().notNull().default(sql`ARRAY[]::text[]`)
}
but what is the correct type? Especially when looking at more complicated shorthands like
export function ref(
id: AnyPgColumn,
options?: { onDelete: 'cascade' | 'set null' | 'restrict' }
): PgBigInt53BuilderInitial<''> {
return bigint({ mode: 'number' }).references((): AnyPgColumn => id, options);
}
export function ref(
id: AnyPgColumn,
options?: { onDelete: 'cascade' | 'set null' | 'restrict' }
): PgBigInt53BuilderInitial<''> {
return bigint({ mode: 'number' }).references((): AnyPgColumn => id, options);
}
I can't get it to be a transparent pass through as ref() does not allow for adding notNull(). Any pointers for reducing the verbosity with some helper functions?
2 replies
DTDrizzle Team
Created by tcurdt on 10/9/2024 in #help
PgColumnBuilderBase and notNull()
holy mother of types ... could someone help me out here?
export function ref(id: AnyPgColumn): PgColumnBuilderBase {
return bigint({ mode: 'number' })
.references((): AnyPgColumn => id)
}
export function ref(id: AnyPgColumn): PgColumnBuilderBase {
return bigint({ mode: 'number' })
.references((): AnyPgColumn => id)
}
OK: id: ref(sometable.id) KO: id: ref(sometable.id).notNull() Is there a base type or interface I can use that does include notNull() and friends?
2 replies
DTDrizzle Team
Created by tcurdt on 10/8/2024 in #help
Unclear docs on bigserial mode
Unfortunately the docs are not clear here:
id: bigserial('id', { mode: 'number' }).primaryKey(),
questionId: bigint('question_id', { mode: 'number' })
id: bigserial('id', { mode: 'number' }).primaryKey(),
questionId: bigint('question_id', { mode: 'number' })
Is there a way to always use mode 'number'?
3 replies
DTDrizzle Team
Created by tcurdt on 10/8/2024 in #help
Wrong docs on casing
The docs chat bot suggests this but that seems wrong
const db = drizzle(client, { casing: 'snake_case' })
const db = drizzle(client, { casing: 'snake_case' })
Where can I specify the rule for the column aliases? Right now it's a lot of noise responseId: bigint('response_id')
21 replies
DTDrizzle Team
Created by tcurdt on 9/24/2024 in #help
Generate drizzle model from database
I have a postgres database and I am wondering if there is a way to generate the typescript table declarations from the database. I didn't find anything in the drizzlekit docs about this.
2 replies
DTDrizzle Team
Created by tcurdt on 5/29/2024 in #help
schema validation enforcement
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
The above schema is nice and well - but how can I enforce that email actually only allows to store strings that are emails? So I looked at drizzle-zod but it looks like I would have to define the schema twice?
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
There is no way to attach this to the table schema itself? What about using postgres constraints? https://www.postgresql.org/docs/current/ddl-constraints.html
1 replies
HHono
Created by tcurdt on 5/24/2024 in #help
[solved] HtmlEscapedString not rendering?
When I have this function
import { HtmlEscapedString } from 'hono/utils/html'

async function render (c): Promise<HtmlEscapedString> {
return c.html(<button>FOO</button>)
}
import { HtmlEscapedString } from 'hono/utils/html'

async function render (c): Promise<HtmlEscapedString> {
return c.html(<button>FOO</button>)
}
Why would this not render?
.get('/', async (c) => {
return c.html(
<BaseLayout>
button: {await render(c)}
</BaseLayout>
)
.get('/', async (c) => {
return c.html(
<BaseLayout>
button: {await render(c)}
</BaseLayout>
)
3 replies
HHono
Created by tcurdt on 5/24/2024 in #help
validating arrays coming from forms
The following code works
<form
action='?'
hx-delete='foo'
hx-target='#list'
>
<ul>
{state.foo.map(item => (
<li
id={`foo-${item.id}`}
key={item.id}>

{item.name} ({item.id})

<input type='checkbox' name="foo[]" value={item.id} />
</li>
))}
</ul>

<button type='submit' name='delete'>Delete Selected</button>
</form>


.delete('/foos', zValidator('form', z.object({
'foos[]': z.coerce.number().array(),
delete: z.string().optional()
})), async (c) => {
const { 'foos[]': foos } = c.req.valid('form')
<form
action='?'
hx-delete='foo'
hx-target='#list'
>
<ul>
{state.foo.map(item => (
<li
id={`foo-${item.id}`}
key={item.id}>

{item.name} ({item.id})

<input type='checkbox' name="foo[]" value={item.id} />
</li>
))}
</ul>

<button type='submit' name='delete'>Delete Selected</button>
</form>


.delete('/foos', zValidator('form', z.object({
'foos[]': z.coerce.number().array(),
delete: z.string().optional()
})), async (c) => {
const { 'foos[]': foos } = c.req.valid('form')
But it feels a little clunky. Is there a nicer way of doing this?
1 replies
HHono
Created by tcurdt on 5/24/2024 in #help
[solved] return type of request handler
Why is 1) is fine and 2) has a type problem? 1)
.get('/foo', async (c) => {
return await c.html(<body/>)
})
.get('/foo', async (c) => {
return await c.html(<body/>)
})
2)
.get('/bar', async (c) => {
return await render(c)
})

async function render(c) : Promise<HtlEscapedString> {
return await c.html(<body/>)
}
.get('/bar', async (c) => {
return await render(c)
})

async function render(c) : Promise<HtlEscapedString> {
return await c.html(<body/>)
}
I don't see it
8 replies
HHono
Created by tcurdt on 5/24/2024 in #help
[solved] route param validation with zod
.delete('/foo/:id', zValidator('param', z.object({
id: z.number()
})), async (c) => {
const { id } = c.req.valid('param')
.delete('/foo/:id', zValidator('param', z.object({
id: z.number()
})), async (c) => {
const { id } = c.req.valid('param')
This is rejecting /foo/1 - shouldn't this work?
8 replies
CDCloudflare Developers
Created by tcurdt on 2/27/2024 in #pages-help
Cloudflare pages very slow
Cloudflare has become very(!) slow for me. Before I was just proxying my VPS. To remove this from the equation I moved the site(s) directly to Cloudflare pages. Unfortunately this did not help much. Sometimes I am still looking at only 40-100Kbytes/sec for some images. It takes so long that Lighthouse times out. I tried from various locations. What is going on? How can I track down this down?
ab -n 10 https://torstencurdt.com/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking torstencurdt.com (be patient).....done


Server Software: cloudflare
Server Hostname: torstencurdt.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: torstencurdt.com

Document Path: /
Document Length: 18823 bytes

Concurrency Level: 1
Time taken for tests: 5.435 seconds
Complete requests: 10
Failed requests: 0
Total transferred: 195598 bytes
HTML transferred: 188230 bytes
Requests per second: 1.84 [#/sec] (mean)
Time per request: 543.523 [ms] (mean)
Time per request: 543.523 [ms] (mean, across all concurrent requests)
Transfer rate: 35.14 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 332 339 12.8 336 375
Processing: 173 204 38.4 194 284
Waiting: 163 194 39.1 181 276
Total: 508 543 39.9 531 618

Percentage of the requests served within a certain time (ms)
50% 531
66% 534
75% 569
80% 603
90% 618
95% 618
98% 618
99% 618
100% 618 (longest request)
ab -n 10 https://torstencurdt.com/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking torstencurdt.com (be patient).....done


Server Software: cloudflare
Server Hostname: torstencurdt.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
Server Temp Key: ECDH X25519 253 bits
TLS Server Name: torstencurdt.com

Document Path: /
Document Length: 18823 bytes

Concurrency Level: 1
Time taken for tests: 5.435 seconds
Complete requests: 10
Failed requests: 0
Total transferred: 195598 bytes
HTML transferred: 188230 bytes
Requests per second: 1.84 [#/sec] (mean)
Time per request: 543.523 [ms] (mean)
Time per request: 543.523 [ms] (mean, across all concurrent requests)
Transfer rate: 35.14 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 332 339 12.8 336 375
Processing: 173 204 38.4 194 284
Waiting: 163 194 39.1 181 276
Total: 508 543 39.9 531 618

Percentage of the requests served within a certain time (ms)
50% 531
66% 534
75% 569
80% 603
90% 618
95% 618
98% 618
99% 618
100% 618 (longest request)
7 replies
DTDrizzle Team
Created by tcurdt on 7/19/2023 in #help
accessing results of a select
I am confused. Based on this docs https://orm.drizzle.team/docs/crud this here
const todos = await db
.select({
id: todosTable.id,
completed: todosTable.completed,
description: todosTable.description,
title: todosTable.title,
})
.from(todosTable);
console.log(todos);
const todos = await db
.select({
id: todosTable.id,
completed: todosTable.completed,
description: todosTable.description,
title: todosTable.title,
})
.from(todosTable);
console.log(todos);
todos should be an array of objects. But it seems to be
SQLiteSelect {
_: {
selectedFields: {
id: [SQLiteInteger],
completed: [SQLiteInteger],
description: [SQLiteText],
title: [SQLiteText]
}
},
config: {
...
SQLiteSelect {
_: {
selectedFields: {
id: [SQLiteInteger],
completed: [SQLiteInteger],
description: [SQLiteText],
title: [SQLiteText]
}
},
config: {
...
What am I missing?
9 replies