Help, Partial select with drizzle-zod
// schema
// drizzle-zod
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()
});
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)
2 Replies
by adding customType the error was gone,
but when I query the user id
response
and when I query it with empty string,
response
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;
}
GET : http//localhost:3000/user?getUserId="someid"
GET : http//localhost:3000/user?getUserId="someid"
error: "Maximum call stack size exceeded
error: "Maximum call stack size exceeded
GET : http//localhost:3000/user?getUserId=""
GET : http//localhost:3000/user?getUserId=""
{
"result": {
"data": [
{},
{},
{},
{},
{}
]
}
}
{
"result": {
"data": [
{},
{},
{},
{},
{}
]
}
}
I fix it. nvm