enalmada
enalmada
DTDrizzle Team
Created by enalmada on 8/24/2023 in #help
parameterized insert and update
@micaww the value.toISOString is not a function error is actually happening on page load when the statement is being parameterized before any data. I do believe I am ultimately passing in a Date object on execution but I need to confim. FYI I created this bug to track the { mode: 'Date' } issue with timestamp parameters https://github.com/drizzle-team/drizzle-orm/issues/1113 With that out of the way I think I am finding that parameterized insert doesn't respect the new default id logic (not too surprising as it is very new https://github.com/drizzle-team/drizzle-orm/releases/tag/0.28.3). I believe I can work around that by just passing my own id in.
id: varchar('id').$defaultFn(() => `usr_${nanoid()}`).primaryKey();
id: varchar('id').$defaultFn(() => `usr_${nanoid()}`).primaryKey();
11 replies
DTDrizzle Team
Created by enalmada on 8/24/2023 in #help
parameterized insert and update
I was able to get around my value.toISOString is not a function problem with insert paramaterized timestamps by switching schema of timestamp to { mode: 'string' }

dueDate: timestamp('due_date', { mode: 'string' }).defaultNow().notNull(),

dueDate: timestamp('due_date', { mode: 'string' }).defaultNow().notNull(),
11 replies
DTDrizzle Team
Created by enalmada on 8/24/2023 in #help
parameterized insert and update
Thanks @micaww for your help. I do believe the placeholder is necessary for prepared statements. When I remove sql.placeholder from the where of a prepared statement it doesn't seem to work. I was trying to follow this as close as possible. https://orm.drizzle.team/docs/performance#placeholder I assume this should be the right syntax for Insert:
const preparedInsert = db
.insert(TaskTable)
.values({
dueDate: sql.placeholder('dueDate'),
})
.returning()
.prepare('Task.create');
const preparedInsert = db
.insert(TaskTable)
.values({
dueDate: sql.placeholder('dueDate'),
})
.returning()
.prepare('Task.create');
But I get
- error Error [TypeError]: value.toISOString is not a function
at PgTimestamp.mapToDriverValue (webpack-internal:///(rsc)/./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/drizzle-orm/alias-a0c6a0a1.mjs:1363:68)

- error Error [TypeError]: value.toISOString is not a function
at PgTimestamp.mapToDriverValue (webpack-internal:///(rsc)/./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/drizzle-orm/alias-a0c6a0a1.mjs:1363:68)

And when doing update I get a typescript error on the set key dueDate.
const preparedUpdate = db
.update(TaskTable)
.set({
dueDate: sql.placeholder('dueDate'),
})
.where(eq(TaskTable.id, sql.placeholder('id')))
.returning()
.prepare('Task.update');
const preparedUpdate = db
.update(TaskTable)
.set({
dueDate: sql.placeholder('dueDate'),
})
.where(eq(TaskTable.id, sql.placeholder('id')))
.returning()
.prepare('Task.update');
TS2322: Type  Placeholder<"dueDate", any>  is not assignable to type  SQL<unknown> | Date | null | undefined  Type  Placeholder<"dueDate", any>  is missing the following properties from type  SQL<unknown> :  queryChunks, _, shouldInlineParams, append , and  6  more. So I think I am having two separate problems with insert and update parameterization that the following just happens to get around
sql`${sql.placeholder('dueDate')}`
sql`${sql.placeholder('dueDate')}`
11 replies
DTDrizzle Team
Created by enalmada on 8/24/2023 in #help
parameterized insert and update
It actually looks like this might be working:
const preparedUpdate = db
.update(TaskTable)
.set({
title: sql`${sql.placeholder('title')}`,
})
.where(eq(TaskTable.id, sql.placeholder('id')))
.returning()
.prepare('Task.update');
const preparedUpdate = db
.update(TaskTable)
.set({
title: sql`${sql.placeholder('title')}`,
})
.where(eq(TaskTable.id, sql.placeholder('id')))
.returning()
.prepare('Task.update');
Is that the recommended syntax or is there a more official way to avoid wrapping the placeholder in sql...?
11 replies