From 060bb71b0ecdfc7203c69af8fded8da443d03d1a Mon Sep 17 00:00:00 2001 From: Lars Hampe Date: Tue, 1 Oct 2024 12:39:12 +0200 Subject: [PATCH] feat(database): add changelog schema, rename access tokens schema --- apps/api/src/utils/authentication.ts | 4 +- .../{accessToken.ts => access_token.ts} | 9 +- packages/database/src/schema/changelog.ts | 104 ++++++++++++++++++ packages/database/src/schema/index.ts | 3 +- packages/database/src/schema/user.ts | 8 +- 5 files changed, 116 insertions(+), 12 deletions(-) rename packages/database/src/schema/{accessToken.ts => access_token.ts} (64%) create mode 100644 packages/database/src/schema/changelog.ts diff --git a/apps/api/src/utils/authentication.ts b/apps/api/src/utils/authentication.ts index e59ac85..0cbd6a8 100644 --- a/apps/api/src/utils/authentication.ts +++ b/apps/api/src/utils/authentication.ts @@ -1,4 +1,4 @@ -import { accessToken, db } from '@boring.tools/database' +import { access_token, db } from '@boring.tools/database' import { clerkMiddleware, getAuth } from '@hono/clerk-auth' import { eq } from 'drizzle-orm' import type { Context, Next } from 'hono' @@ -14,7 +14,7 @@ const generatedToken = async (c: Context, next: Next) => { const token = authHeader.replace('Bearer ', '') const accessTokenResult = await db.query.accessToken.findFirst({ - where: eq(accessToken.token, token), + where: eq(access_token.token, token), with: { user: true, }, diff --git a/packages/database/src/schema/accessToken.ts b/packages/database/src/schema/access_token.ts similarity index 64% rename from packages/database/src/schema/accessToken.ts rename to packages/database/src/schema/access_token.ts index f3aa7ca..4a5be11 100644 --- a/packages/database/src/schema/accessToken.ts +++ b/packages/database/src/schema/access_token.ts @@ -2,7 +2,7 @@ import { relations } from 'drizzle-orm' import { pgTable, text, timestamp, uuid, varchar } from 'drizzle-orm/pg-core' import { user } from '.' -export const accessToken = pgTable('accessToken', { +export const access_token = pgTable('access_token', { id: uuid('id').primaryKey().defaultRandom(), userId: varchar('userId', { length: 32 }).references(() => user.id, { onDelete: 'cascade', @@ -13,12 +13,9 @@ export const accessToken = pgTable('accessToken', { lastUsedOn: timestamp('lastUsedOn'), }) -export const accessTokenRelation = relations(accessToken, ({ one }) => ({ +export const access_token_relation = relations(access_token, ({ one }) => ({ user: one(user, { - fields: [accessToken.userId], + fields: [access_token.userId], references: [user.id], }), })) - -export type AccessTokenSelect = typeof accessToken.$inferSelect -export type AccessTokenInsert = typeof accessToken.$inferInsert diff --git a/packages/database/src/schema/changelog.ts b/packages/database/src/schema/changelog.ts new file mode 100644 index 0000000..ae83dd1 --- /dev/null +++ b/packages/database/src/schema/changelog.ts @@ -0,0 +1,104 @@ +import { relations } from 'drizzle-orm' +import { + boolean, + pgEnum, + pgTable, + text, + timestamp, + uuid, + varchar, +} from 'drizzle-orm/pg-core' +import { json } from 'drizzle-orm/pg-core' +import { uniqueIndex } from 'drizzle-orm/pg-core' +import { user } from './user' + +export const changelog = pgTable('changelog', { + id: uuid('id').primaryKey().defaultRandom(), + createdAt: timestamp('createdAt').defaultNow(), + updatedAt: timestamp('updatedAt'), + + userId: text('userId').references(() => user.id, { + onDelete: 'cascade', + }), + + title: varchar('title', { length: 256 }), + description: text('description'), + isSemver: boolean('isSemver').default(true), +}) + +export const changelog_relation = relations(changelog, ({ many }) => ({ + versions: many(changelog_version), + commits: many(changelog_commit), +})) + +export const changelog_version_status = pgEnum('status', [ + 'draft', + 'review', + 'published', +]) + +export const changelog_version = pgTable('changelog_version', { + id: uuid('id').primaryKey().defaultRandom(), + createdAt: timestamp('createdAt').defaultNow(), + updatedAt: timestamp('updatedAt'), + releasedAt: timestamp('releasedAt'), + + changelogId: uuid('changelogId') + .references(() => changelog.id, { + onDelete: 'cascade', + }) + .notNull(), + + version: varchar('version', { length: 32 }).notNull(), + markdown: text('markdown').notNull(), + status: changelog_version_status('status').default('draft').notNull(), + shortHash: varchar('shortHash', { length: 8 }).notNull(), +}) + +export const changelog_commit = pgTable( + 'changelog_commit', + { + id: uuid('id').primaryKey().defaultRandom(), + createdAt: timestamp('createdAt'), + + changelogId: uuid('changelogId').references(() => changelog.id, { + onDelete: 'cascade', + }), + versionId: uuid('versionId').references(() => changelog_version.id, { + onDelete: 'cascade', + }), + + shortHash: varchar('shortHash', { length: 8 }).notNull(), + author: json('author').$type<{ name: string; email: string }>(), + body: text('body'), + message: text('message').notNull(), + }, + (table) => ({ + unique: uniqueIndex('unique').on(table.changelogId, table.shortHash), + }), +) + +export const changelog_commit_relation = relations( + changelog_commit, + ({ one }) => ({ + changelog: one(changelog, { + fields: [changelog_commit.changelogId], + references: [changelog.id], + }), + version: one(changelog_version, { + fields: [changelog_commit.versionId], + references: [changelog_version.id], + }), + }), +) + +export const changelog_version_relation = relations( + changelog_version, + ({ one, many }) => ({ + changelog: one(changelog, { + fields: [changelog_version.changelogId], + references: [changelog.id], + }), + commits: many(changelog_commit), + }), +) diff --git a/packages/database/src/schema/index.ts b/packages/database/src/schema/index.ts index 6782b75..6118e91 100644 --- a/packages/database/src/schema/index.ts +++ b/packages/database/src/schema/index.ts @@ -1,2 +1,3 @@ export * from './user' -export * from './accessToken' +export * from './access_token' +export * from './changelog' diff --git a/packages/database/src/schema/user.ts b/packages/database/src/schema/user.ts index 50d446e..955a762 100644 --- a/packages/database/src/schema/user.ts +++ b/packages/database/src/schema/user.ts @@ -1,6 +1,7 @@ import { relations } from 'drizzle-orm' -import { pgTable, text, timestamp, varchar } from 'drizzle-orm/pg-core' -import { accessToken } from '.' +import { pgTable, text, varchar } from 'drizzle-orm/pg-core' +import { access_token } from '.' +import { changelog } from './changelog' export const user = pgTable('user', { id: varchar('id', { length: 32 }).primaryKey(), // Clerk User Id @@ -9,7 +10,8 @@ export const user = pgTable('user', { }) export const userRelation = relations(user, ({ many }) => ({ - accessTokens: many(accessToken), + access_tokens: many(access_token), + changelogs: many(changelog), })) export type UserSelect = typeof user.$inferSelect