nowifi4u
nowifi4u
Explore posts from servers
DTDrizzle Team
Created by nowifi4u on 6/10/2024 in #help
Prepared insert statement with customType
When using prepared insert statement with customType the Placeholder object gets passed to the toDriver 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 toDriver 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]]:
Object
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]]:
Object
1 replies
DTDrizzle Team
Created by nowifi4u on 5/9/2024 in #help
Cannot prepare a statement for table update
Trying to make a prepared statement for updating user's subscription but getting errors:
export const preparedSetSubscriptionStatement = db
.update(userSchema)
.set({
subscriptionTierId: sql.placeholder('subscriptionTierId'),
subscriptionTierPrice: sql.placeholder('subscriptionTierPrice'),
subscriptionTierStartedAt: sql`CURRENT_TIMESTAMP`,
})
.where(eq(userSchema.id, sql.placeholder('id')))
.prepare('user_set_subscription');
export const preparedSetSubscriptionStatement = db
.update(userSchema)
.set({
subscriptionTierId: sql.placeholder('subscriptionTierId'),
subscriptionTierPrice: sql.placeholder('subscriptionTierPrice'),
subscriptionTierStartedAt: sql`CURRENT_TIMESTAMP`,
})
.where(eq(userSchema.id, sql.placeholder('id')))
.prepare('user_set_subscription');
Errors for subscriptionTierId and subscriptionTierPrice set fields:
Type 'Placeholder<"subscriptionTierId", any>' is not assignable to type 'number | SQL<unknown> | null | undefined'.
Type 'Placeholder<"subscriptionTierId", any>' is missing the following properties from type 'SQL<unknown>': queryChunks, _, shouldInlineParams, append, and 7 more.ts(2322)
schema.ts(29, 3): The expected type comes from property 'subscriptionTierId' which is declared here on type '<redacted, fields from userSchema>'
Type 'Placeholder<"subscriptionTierId", any>' is not assignable to type 'number | SQL<unknown> | null | undefined'.
Type 'Placeholder<"subscriptionTierId", any>' is missing the following properties from type 'SQL<unknown>': queryChunks, _, shouldInlineParams, append, and 7 more.ts(2322)
schema.ts(29, 3): The expected type comes from property 'subscriptionTierId' which is declared here on type '<redacted, fields from userSchema>'
Type 'Placeholder<"subscriptionTierPrice", any>' is not assignable to type 'string | SQL<unknown> | null | undefined'.
Type 'Placeholder<"subscriptionTierPrice", any>' is missing the following properties from type 'SQL<unknown>': queryChunks, _, shouldInlineParams, append, and 7 more.ts(2322)
schema.ts(30, 3): The expected type comes from property 'subscriptionTierPrice' which is declared here on type '<redacted, fields from userSchema>'
Type 'Placeholder<"subscriptionTierPrice", any>' is not assignable to type 'string | SQL<unknown> | null | undefined'.
Type 'Placeholder<"subscriptionTierPrice", any>' is missing the following properties from type 'SQL<unknown>': queryChunks, _, shouldInlineParams, append, and 7 more.ts(2322)
schema.ts(30, 3): The expected type comes from property 'subscriptionTierPrice' which is declared here on type '<redacted, fields from userSchema>'
2 replies