samson
samson
Explore posts from servers
DTDrizzle Team
Created by samson on 6/2/2024 in #help
drizzle-zod not working with column of type vector (as introduced in latest release v.0.31.0)
Tried using createSelectSchema() on a table that has a column of type vector, e.g.
...
embedding: vector("embedding", { dimensions: 384 }),
...
embedding: vector("embedding", { dimensions: 384 }),
Getting a strange error: Error: Cannot use 'in' operator to search for 'enumValues' in undefined The error message is kind of weird (why is it talking about enumValues?), but without the vector column, everything works out... Any pointers to how I could fix this and keep using drizzle-zod's helper functions? I use them extensively in my project so it would be a bummer if I had to ditch them
2 replies
DTDrizzle Team
Created by samson on 5/6/2024 in #help
Having placeholders "survive" dynamic query building
If the query I'm trying to modify with a dynamic query (....$dynamic()) contains any placeholders, I'm getting an error that says No value for placeholder.... provided. That seems counterintuitive because I should only be required to provide placeholder values when I actually execute the query, not when I'm still "building" it. Is this a limitation of Drizzle, or am I thinking the wrong way about this? EDIT: Funny enough, the query seems to still work despite the error.. Simple example to illustrate:
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
) {
return qb.limit(pageSize).offset((page - 1) * pageSize);
}

const query = db.select().from(users).where(eq(users.id, sql.placeholder("myPlaceholder"))

const dynamicQuery = query.$dynamic()

withPagination(dynamicQuery, 2, 20) // Error: No value for placeholder "myPlaceholder" was provided
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
) {
return qb.limit(pageSize).offset((page - 1) * pageSize);
}

const query = db.select().from(users).where(eq(users.id, sql.placeholder("myPlaceholder"))

const dynamicQuery = query.$dynamic()

withPagination(dynamicQuery, 2, 20) // Error: No value for placeholder "myPlaceholder" was provided
Error: No value for placeholder "myPlaceholder" was provided
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/sql/sql.js:358:15)
at Array.map (<anonymous>)
at fillPlaceholders (webpack-internal:///(rsc)/./node_modules/drizzle-orm/sql/sql.js:355:17)
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/postgres-js/session.js:34:83)
at Object.startActiveSpan (webpack-internal:///(rsc)/./node_modules/drizzle-orm/tracing.js:14:14)
at PostgresJsPreparedQuery.execute (webpack-internal:///(rsc)/./node_modules/drizzle-orm/postgres-js/session.js:33:60)
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/pg-core/query-builders/select.js:713:30)
at Object.startActiveSpan (webpack-internal:///(rsc)/./node_modules/drizzle-orm/tracing.js:14:14)
at PgSelectBase.execute (webpack-internal:///(rsc)/./node_modules/drizzle-orm/pg-core/query-builders/select.js:712:60)
at PgSelectBase.then (webpack-internal:///(rsc)/./node_modules/drizzle-orm/query-promise.js:26:17)
at process.processTicksAndRejections..
Error: No value for placeholder "myPlaceholder" was provided
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/sql/sql.js:358:15)
at Array.map (<anonymous>)
at fillPlaceholders (webpack-internal:///(rsc)/./node_modules/drizzle-orm/sql/sql.js:355:17)
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/postgres-js/session.js:34:83)
at Object.startActiveSpan (webpack-internal:///(rsc)/./node_modules/drizzle-orm/tracing.js:14:14)
at PostgresJsPreparedQuery.execute (webpack-internal:///(rsc)/./node_modules/drizzle-orm/postgres-js/session.js:33:60)
at eval (webpack-internal:///(rsc)/./node_modules/drizzle-orm/pg-core/query-builders/select.js:713:30)
at Object.startActiveSpan (webpack-internal:///(rsc)/./node_modules/drizzle-orm/tracing.js:14:14)
at PgSelectBase.execute (webpack-internal:///(rsc)/./node_modules/drizzle-orm/pg-core/query-builders/select.js:712:60)
at PgSelectBase.then (webpack-internal:///(rsc)/./node_modules/drizzle-orm/query-promise.js:26:17)
at process.processTicksAndRejections..
1 replies
DTDrizzle Team
Created by samson on 2/21/2024 in #help
Is there really no way to construct a valid orderBy statement dynamically without $.dynamicQuery?
I realize I can append multiple orderBy statements with .$dynamic() But I'm wondering why a more direct approach doesn't work?
//Let's define a schema first
export const sortSchema = z.array(
z.object({
columnId: createSelectSchema(myTable).keyof(),
desc: z.boolean(),
})
);

//parse an object against that schema
const parsedSortSchema = sortSchema.safeParse(sortObject);
const sortToUse = parsedSortSchema.success
? parsedSortSchema.data
: undefined;

//create orderBy
const orderBy = sortToUse?.map((s) => {
return s.desc ? desc(myTable[s.columnId]) : asc(myTable[s.columnId]);
});

//so far so good (?), `orderBy` is of type `SQL<unknown>[] | undefined`.

//now, here comes the issue:
const { data, count } = await db.transaction(async (tx) => {
const data = await tx
.select()
.from(myTable)
.orderBy(orderBy ? {...orderBy} : desc(myTable.createdAt)); //T

const count = await tx
.select({
count: sql<number>`count(${myTable.id})`.mapWith(Number),
})
.from(myTable)
.execute()
.then((res) => res[0]?.count ?? 0);

return {
data,
count,
};
});
//Let's define a schema first
export const sortSchema = z.array(
z.object({
columnId: createSelectSchema(myTable).keyof(),
desc: z.boolean(),
})
);

//parse an object against that schema
const parsedSortSchema = sortSchema.safeParse(sortObject);
const sortToUse = parsedSortSchema.success
? parsedSortSchema.data
: undefined;

//create orderBy
const orderBy = sortToUse?.map((s) => {
return s.desc ? desc(myTable[s.columnId]) : asc(myTable[s.columnId]);
});

//so far so good (?), `orderBy` is of type `SQL<unknown>[] | undefined`.

//now, here comes the issue:
const { data, count } = await db.transaction(async (tx) => {
const data = await tx
.select()
.from(myTable)
.orderBy(orderBy ? {...orderBy} : desc(myTable.createdAt)); //T

const count = await tx
.select({
count: sql<number>`count(${myTable.id})`.mapWith(Number),
})
.from(myTable)
.execute()
.then((res) => res[0]?.count ?? 0);

return {
data,
count,
};
});
Error message is
2 replies
PD🧩 Plasmo Developers
Created by samson on 12/22/2023 in #🔰newbie
Receiving end does not exist.
Hey there, new Plasmo user here. I'm trying to pull some information from the DOM (of the currently active tab) and display it in my extension. Should be simple... I'm getting Error: Could not establish connection. Receiving end does not exist. when calling sendToContentScript. I couldn't find any documentation for this function, so hoping you can point me to the right direction. I refreshed all tabs/ reloaded the extension, so I think the error is due to me not understanding how messaging in Plasmo works.. popup.tsx
const handleClick = async () => {
//call function inside content script
const res = await sendToContentScript({ name: "getFields" })
console.log("result is", res)
}
const handleClick = async () => {
//call function inside content script
const res = await sendToContentScript({ name: "getFields" })
console.log("result is", res)
}
contents/getFields.ts
import { useMessage } from "@plasmohq/messaging/hook"

export default function getFields() {
useMessage<string, string[]>(async (req, res) => {
const { name } = req
if (name === "getFields") {
const fields = Array.from(document.body.querySelectorAll("input")).map(
(input) => {
return input.name
}
)
res.send({ ...fields })
}
})
}
import { useMessage } from "@plasmohq/messaging/hook"

export default function getFields() {
useMessage<string, string[]>(async (req, res) => {
const { name } = req
if (name === "getFields") {
const fields = Array.from(document.body.querySelectorAll("input")).map(
(input) => {
return input.name
}
)
res.send({ ...fields })
}
})
}
18 replies