summaryrefslogtreecommitdiff
path: root/src/server/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/db')
-rw-r--r--src/server/db/index.ts13
-rw-r--r--src/server/db/schema/auth.ts55
-rw-r--r--src/server/db/schema/comments.ts43
-rw-r--r--src/server/db/schema/index.ts2
4 files changed, 113 insertions, 0 deletions
diff --git a/src/server/db/index.ts b/src/server/db/index.ts
new file mode 100644
index 0000000..8579424
--- /dev/null
+++ b/src/server/db/index.ts
@@ -0,0 +1,13 @@
+import { neon } from '@neondatabase/serverless';
+import { drizzle } from 'drizzle-orm/neon-http';
+
+import { env } from '@/env';
+import * as schema from './schema';
+
+const sql = neon(env.DATABASE_URL);
+
+export const db = drizzle({
+ client: sql,
+ schema,
+ casing: 'snake_case',
+});
diff --git a/src/server/db/schema/auth.ts b/src/server/db/schema/auth.ts
new file mode 100644
index 0000000..5843fdb
--- /dev/null
+++ b/src/server/db/schema/auth.ts
@@ -0,0 +1,55 @@
+import { boolean, text, timestamp } from 'drizzle-orm/pg-core';
+import { pgTableCreator } from 'drizzle-orm/pg-core';
+
+const createTable = pgTableCreator((name) => `blog_${name}`);
+
+export const users = createTable('users', {
+ id: text('id').primaryKey(),
+ name: text('name').notNull(),
+ email: text('email').notNull().unique(),
+ emailVerified: boolean('email_verified').notNull(),
+ image: text('image'),
+ createdAt: timestamp('created_at').notNull(),
+ updatedAt: timestamp('updated_at').notNull(),
+ role: text('role').notNull(),
+});
+
+export const sessions = createTable('sessions', {
+ id: text('id').primaryKey(),
+ expiresAt: timestamp('expires_at').notNull(),
+ token: text('token').notNull().unique(),
+ createdAt: timestamp('created_at').notNull(),
+ updatedAt: timestamp('updated_at').notNull(),
+ ipAddress: text('ip_address'),
+ userAgent: text('user_agent'),
+ userId: text('user_id')
+ .notNull()
+ .references(() => users.id, { onDelete: 'cascade' }),
+});
+
+export const accounts = createTable('accounts', {
+ id: text('id').primaryKey(),
+ accountId: text('account_id').notNull(),
+ providerId: text('provider_id').notNull(),
+ userId: text('user_id')
+ .notNull()
+ .references(() => users.id, { onDelete: 'cascade' }),
+ accessToken: text('access_token'),
+ refreshToken: text('refresh_token'),
+ idToken: text('id_token'),
+ accessTokenExpiresAt: timestamp('access_token_expires_at'),
+ refreshTokenExpiresAt: timestamp('refresh_token_expires_at'),
+ scope: text('scope'),
+ password: text('password'),
+ createdAt: timestamp('created_at').notNull(),
+ updatedAt: timestamp('updated_at').notNull(),
+});
+
+export const verifications = createTable('verifications', {
+ id: text('id').primaryKey(),
+ identifier: text('identifier').notNull(),
+ value: text('value').notNull(),
+ expiresAt: timestamp('expires_at').notNull(),
+ createdAt: timestamp('created_at'),
+ updatedAt: timestamp('updated_at'),
+});
diff --git a/src/server/db/schema/comments.ts b/src/server/db/schema/comments.ts
new file mode 100644
index 0000000..9c0626b
--- /dev/null
+++ b/src/server/db/schema/comments.ts
@@ -0,0 +1,43 @@
+import {
+ boolean,
+ index,
+ integer,
+ json,
+ primaryKey,
+ serial,
+ timestamp,
+ varchar,
+} from 'drizzle-orm/pg-core';
+import { pgTableCreator } from 'drizzle-orm/pg-core';
+
+const createTable = pgTableCreator((name) => `blog_${name}`);
+
+export const roles = createTable('roles', {
+ userId: varchar('userId', { length: 256 }).primaryKey(),
+ name: varchar('name', { length: 256 }).notNull(),
+ canDelete: boolean('canDelete').notNull(),
+});
+
+export const comments = createTable('comments', {
+ id: serial('id').primaryKey().notNull(),
+ page: varchar('page', { length: 256 }).notNull(),
+ thread: integer('thread'),
+ author: varchar('author', { length: 256 }).notNull(),
+ content: json('content').notNull(),
+ timestamp: timestamp('timestamp', { withTimezone: true })
+ .defaultNow()
+ .notNull(),
+});
+
+export const rates = createTable(
+ 'rates',
+ {
+ userId: varchar('userId', { length: 256 }).notNull(),
+ commentId: integer('commentId').notNull(),
+ like: boolean('like').notNull(),
+ },
+ (table) => [
+ primaryKey({ columns: [table.userId, table.commentId] }),
+ index('comment_idx').on(table.commentId),
+ ],
+);
diff --git a/src/server/db/schema/index.ts b/src/server/db/schema/index.ts
new file mode 100644
index 0000000..def18aa
--- /dev/null
+++ b/src/server/db/schema/index.ts
@@ -0,0 +1,2 @@
+export * from './auth';
+export * from './comments';