Prepared insert statement with customType
When using prepared insert statement with customType the
Schema:
The custom type:
Prepared statement:
What gets passed to the
PlaceholderPlaceholder object gets passed to the toDrivertoDriver of the custom type. How do I resolve it?Schema:
export const sessionSchema = pgTable('session', {
id: bigintString('id', { mode: 'bigint' }).primaryKey().$default(() => generateSnowflake()),
// More fields
});export const sessionSchema = pgTable('session', {
id: bigintString('id', { mode: 'bigint' }).primaryKey().$default(() => generateSnowflake()),
// More fields
});The custom type:
export const bigintString = customType<{
data: string;
driverData: bigint;
}>({
dataType: () => 'bigint',
toDriver: (value) => {
return BigInt(value);
},
fromDriver: (value) => {
return String(value);
},
});export const bigintString = customType<{
data: string;
driverData: bigint;
}>({
dataType: () => 'bigint',
toDriver: (value) => {
return BigInt(value);
},
fromDriver: (value) => {
return String(value);
},
});Prepared statement:
public static readonly preparedUpsertStatement = db
.insert(sessionSchema)
.values({
id: sql.placeholder('id'),
// More fields
})
.onConflictDoUpdate({
target: sessionSchema.id,
set: {
// More fields
},
})
.returning()
.prepare('session_upsert');public static readonly preparedUpsertStatement = db
.insert(sessionSchema)
.values({
id: sql.placeholder('id'),
// More fields
})
.onConflictDoUpdate({
target: sessionSchema.id,
set: {
// More fields
},
})
.returning()
.prepare('session_upsert');What gets passed to the
toDrivertoDriver of the custom type:Placeholder {name: 'id'}
name:
'id'
[[Prototype]]:
Object
constructor:
class Placeholder {\n constructor(name2) {\n this.name = name2;\n }\n static [entityKind] = "Placeholder";\n getSQL() {\n return new SQL([this]);\n }\n}
getSQL:
Æ getSQL() {\n return new SQL([this]);\n }
[[Prototype]]:
ObjectPlaceholder {name: 'id'}
name:
'id'
[[Prototype]]:
Object
constructor:
class Placeholder {\n constructor(name2) {\n this.name = name2;\n }\n static [entityKind] = "Placeholder";\n getSQL() {\n return new SQL([this]);\n }\n}
getSQL:
Æ getSQL() {\n return new SQL([this]);\n }
[[Prototype]]:
Object