Help, Partial select with drizzle-zod

// schema
const usersTable = pgTable("users", {
id: varchar("id", { length: 256 })
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
name: varchar("name", { length: 256 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull()
});
const usersTable = pgTable("users", {
id: varchar("id", { length: 256 })
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
name: varchar("name", { length: 256 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull()
});
// drizzle-zod
const SelectUser = createSelectSchema(usersTable);
const SelectUserById = SelectUser.pick({ id: true });

export type TSelectUserById = z.infer<typeof SelectUserById>;

function getUserById (input: TSelectUserById) {
const result = await db.select({ id: input.id }).from(usersTable);
return result;
}
const SelectUser = createSelectSchema(usersTable);
const SelectUserById = SelectUser.pick({ id: true });

export type TSelectUserById = z.infer<typeof SelectUserById>;

function getUserById (input: TSelectUserById) {
const result = await db.select({ id: input.id }).from(usersTable);
return result;
}
Type 'string' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.ts(2322)
Type 'string' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.ts(2322)
No description
2 Replies
glennvoid
glennvoidOP11mo ago
by adding customType the error was gone,
const customText = customType<{ data: string }>({
dataType() {
return "text";
}
});

export const usersTable = pgTable("users", {
id: customText("id")
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
email: varchar("email", { length: 256 }).notNull(),
password: varchar("password", { length: 256 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull()
});
const customText = customType<{ data: string }>({
dataType() {
return "text";
}
});

export const usersTable = pgTable("users", {
id: customText("id")
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
email: varchar("email", { length: 256 }).notNull(),
password: varchar("password", { length: 256 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull()
});
export async function getUserById(data: TSelectUserById) {
const parseId = SelectUserById.parse({ _id: data._id });

const result = await db.select(parseId).from(usersTable);

return result;
}
export async function getUserById(data: TSelectUserById) {
const parseId = SelectUserById.parse({ _id: data._id });

const result = await db.select(parseId).from(usersTable);

return result;
}
but when I query the user id
GET : http//localhost:3000/user?getUserId="someid"
GET : http//localhost:3000/user?getUserId="someid"
response
error: "Maximum call stack size exceeded
error: "Maximum call stack size exceeded
and when I query it with empty string,
GET : http//localhost:3000/user?getUserId=""
GET : http//localhost:3000/user?getUserId=""
response
{
"result": {
"data": [
{},
{},
{},
{},
{}
]
}
}
{
"result": {
"data": [
{},
{},
{},
{},
{}
]
}
}
No description
glennvoid
glennvoidOP11mo ago
I fix it. nvm
Want results from more Discord servers?
Add your server