Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
9 replies
barry

Drizzle type question

I swapped over to Drizzle, and I've got this schema right

export const User = mysqlTable('users', {
  id: serial('id').primaryKey(),
  username: text('username').unique(),
  passwordHash: text('password_hash'),
  passwordSalt: text('password_salt'),
  role: mysqlEnum('role', ['user', 'admin']),
  createdAt: timestamp('created_at').notNull().default(new Date()),
  updatedAt: timestamp('updated_at').notNull().default(new Date()),
});
export const Session = mysqlTable('sessions', {
  id: serial('id').primaryKey(),
  token: text('token').unique(),
  csrfToken: text('csrf_token'),
  expiresAt: timestamp('expires_at').notNull(),
  userid: int('user_id').references(() => User.id),
});


And I'm trying to get a session based on an id coming from the client
const session = await db
    .select()
    .from(Session)
    .where(eq(Session.token, sessionToken))
    .innerJoin(User, eq(Session.userid, User.id)).limit(1)
    .execute();


This seems right, right?
But why in the world is the type of session an array consisting of an object with a users and sessions prop?
session: {
    sessions: {
        id: number;
        token: string | null;
        csrfToken: string | null;
        expiresAt: Date;
        userid: number | null;
    };
    users: {
        id: number;
        username: string | null;
        passwordHash: string | null;
        passwordSalt: string | null;
        role: "user" | ... 1 more ... | null;
        createdAt: Date;
        updatedAt: Date;
    };
}[]
Solution
And limit 1 still returns an array btw. So you have to do data[0].sessions
Was this page helpful?