feat(database): add changelog schema, rename access tokens schema

This commit is contained in:
Lars Hampe 2024-10-01 12:39:12 +02:00
parent 18cbf5c0a3
commit 060bb71b0e
5 changed files with 116 additions and 12 deletions

View File

@ -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,
},

View File

@ -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

View File

@ -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),
}),
)

View File

@ -1,2 +1,3 @@
export * from './user'
export * from './accessToken'
export * from './access_token'
export * from './changelog'

View File

@ -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