Drizzle Store
Setup and schema for the Drizzle store adapter.
Drizzle Store
The DrizzleAdapter
allows you to use mcpauth
with a Drizzle-compatible database.
1. Installation
First, install the necessary packages. You'll need drizzle-orm
, a database driver (like pg
for Postgres), and drizzle-kit
for schema management.
npm install drizzle-orm pg
npm install -D drizzle-kit
2. Usage
Import the adapter and pass your Drizzle instance to it in your mcpAuth
configuration.
import { DrizzleAdapter } from "@mcpauth/auth/stores/drizzle";
// ... your other imports and db setup
export const { auth, GET, POST } = mcpAuth({
// ... other options
adapter: DrizzleAdapter(db),
});
The Drizzle store currently only supports Postgres.
Schema Generation
MCPAuth provides a CLI tool to generate the required Drizzle schema for you. First, add the following script to your package.json
:
{
"scripts": {
"db:generate": "mcpauth-generate drizzle ./db/schema.ts"
}
}
Then, run the command to generate the schema file:
npm run db:generate
This will create a db/schema.ts
file with all the necessary tables for the Drizzle store.
Migrations
Once the schema is generated, you can use drizzle-kit
to push the schema to your database.
First, create a drizzle.config.ts
file:
import type { Config } from 'drizzle-kit';
export default {
schema: './db/schema.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
} satisfies Config;
Finally, run the push command to apply the schema to your database:
npx drizzle-kit push:pg
import { pgTable, varchar, text, timestamp, jsonb } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
export const oauthClient = pgTable("oauth_client", {
id: varchar("id", { length: 255 }).primaryKey(),
clientId: varchar("client_id", { length: 255 }).unique().notNull(),
clientSecret: varchar("client_secret", { length: 255 }).notNull(),
name: varchar("name", { length: 255 }).notNull(),
description: text("description"),
logoUri: text("logo_uri"),
redirectUris: text("redirect_uris").array().notNull(),
grantTypes: text("grant_types").array().notNull(),
scope: text("scope"),
userId: varchar("user_id", { length: 255 }),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
});
export const oauthAuthorizationCode = pgTable("oauth_authorization_code", {
authorizationCode: varchar("authorization_code", { length: 255 }).primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
redirectUri: text("redirect_uri").notNull(),
scope: text("scope"),
authorizationDetails: jsonb("authorization_details"),
codeChallenge: text("code_challenge"),
codeChallengeMethod: text("code_challenge_method"),
clientId: varchar("client_id", { length: 255 })
.notNull()
.references(() => oauthClient.id, { onDelete: "cascade" }),
userId: varchar("user_id", { length: 255 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
export const oauthToken = pgTable("oauth_token", {
accessToken: varchar("access_token", { length: 255 }).primaryKey(),
accessTokenExpiresAt: timestamp("access_token_expires_at").notNull(),
refreshToken: varchar("refresh_token", { length: 255 }).unique(),
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
scope: text("scope"),
authorizationDetails: jsonb("authorization_details"),
clientId: varchar("client_id", { length: 255 })
.notNull()
.references(() => oauthClient.id, { onDelete: "cascade" }),
userId: varchar("user_id", { length: 255 }).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
// Relations ---------------------------------------------------
export const oauthClientRelations = relations(oauthClient, ({ many }) => ({
authorizationCodes: many(oauthAuthorizationCode),
tokens: many(oauthToken),
}));
export const oauthAuthorizationCodeRelations = relations(
oauthAuthorizationCode,
({ one }) => ({
client: one(oauthClient, {
fields: [oauthAuthorizationCode.clientId],
references: [oauthClient.id],
}),
}),
);
export const oauthTokenRelations = relations(oauthToken, ({ one }) => ({
client: one(oauthClient, {
fields: [oauthToken.clientId],
references: [oauthClient.id],
}),
}));
Note: The schema provided is for PostgreSQL. While other SQL databases may be compatible, they are not officially tested or supported at this time.