feat: create and delete api keys

This commit is contained in:
Henry
2025-05-31 23:59:48 +01:00
parent e61dd4e27e
commit 1d00aa78b4
11 changed files with 153 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
import { eq } from "drizzle-orm";
import { desc, eq } from "drizzle-orm";
import { v4 as uuidv4 } from "uuid";
import type { dbClient } from "@kan/db/client";
import { users } from "@kan/db/schema";
import { apiKey, users } from "@kan/db/schema";
export const getById = async (db: dbClient, userId: string) => {
return await db.query.users.findFirst({
@@ -13,6 +13,17 @@ export const getById = async (db: dbClient, userId: string) => {
image: true,
stripeCustomerId: true,
},
with: {
apiKeys: {
columns: {
id: true,
prefix: true,
key: true,
},
orderBy: desc(apiKey.createdAt),
limit: 1,
},
},
where: eq(users.id, userId),
});
};

View File

@@ -1,3 +1,4 @@
import { relations } from "drizzle-orm";
import {
bigserial,
boolean,
@@ -50,7 +51,7 @@ export const verification = pgTable("verification", {
updatedAt: timestamp("updatedAt"),
}).enableRLS();
export const apiKey = pgTable("apiKey", {
export const apiKey = pgTable("apikey", {
id: bigserial("id", { mode: "number" }).primaryKey(),
name: text("name"),
start: text("start"),
@@ -75,3 +76,10 @@ export const apiKey = pgTable("apiKey", {
permissions: text("permissions"),
metadata: text("metadata"),
}).enableRLS();
export const apiKeyRelations = relations(apiKey, ({ one }) => ({
user: one(users, {
fields: [apiKey.userId],
references: [users.id],
}),
}));

View File

@@ -8,3 +8,5 @@ export * from "./labels";
export * from "./lists";
export * from "./users";
export * from "./workspaces";
export { apiKey as apikey } from "./auth";

View File

@@ -7,6 +7,7 @@ import {
varchar,
} from "drizzle-orm/pg-core";
import { apiKey } from "./auth";
import { boards } from "./boards";
import { cards } from "./cards";
import { imports } from "./imports";
@@ -33,6 +34,7 @@ export const usersRelations = relations(users, ({ many }) => ({
imports: many(imports),
lists: many(lists),
workspaces: many(workspaces),
apiKeys: many(apiKey),
}));
export const usersToWorkspacesRelations = relations(