feat: add users and auth tables schema
Made-with: Cursor
This commit is contained in:
@@ -1 +1,4 @@
|
||||
export * from './enums.js';
|
||||
export * from './users.js';
|
||||
export * from './sessions.js';
|
||||
export * from './subscriptions.js';
|
||||
|
||||
18
src/db/schema/sessions.ts
Normal file
18
src/db/schema/sessions.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { pgTable, uuid, varchar, timestamp } from 'drizzle-orm/pg-core';
|
||||
import { users } from './users.js';
|
||||
|
||||
export const sessions = pgTable('sessions', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
userId: uuid('user_id')
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: 'cascade' }),
|
||||
refreshTokenHash: varchar('refresh_token_hash', { length: 255 }).notNull(),
|
||||
userAgent: varchar('user_agent', { length: 500 }),
|
||||
ipAddress: varchar('ip_address', { length: 45 }),
|
||||
lastActiveAt: timestamp('last_active_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export type Session = typeof sessions.$inferSelect;
|
||||
export type NewSession = typeof sessions.$inferInsert;
|
||||
21
src/db/schema/subscriptions.ts
Normal file
21
src/db/schema/subscriptions.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { pgTable, uuid, varchar, timestamp } from 'drizzle-orm/pg-core';
|
||||
import { planEnum, subscriptionStatusEnum } from './enums.js';
|
||||
import { users } from './users.js';
|
||||
|
||||
export const subscriptions = pgTable('subscriptions', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
userId: uuid('user_id')
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: 'cascade' })
|
||||
.unique(),
|
||||
plan: planEnum('plan').notNull(),
|
||||
status: subscriptionStatusEnum('status').notNull(),
|
||||
startedAt: timestamp('started_at', { withTimezone: true }).notNull(),
|
||||
expiresAt: timestamp('expires_at', { withTimezone: true }),
|
||||
cancelledAt: timestamp('cancelled_at', { withTimezone: true }),
|
||||
paymentProvider: varchar('payment_provider', { length: 50 }),
|
||||
externalId: varchar('external_id', { length: 255 }),
|
||||
});
|
||||
|
||||
export type Subscription = typeof subscriptions.$inferSelect;
|
||||
export type NewSubscription = typeof subscriptions.$inferInsert;
|
||||
21
src/db/schema/users.ts
Normal file
21
src/db/schema/users.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { pgTable, uuid, varchar, timestamp, boolean } from 'drizzle-orm/pg-core';
|
||||
import { userRoleEnum, selfLevelEnum } from './enums.js';
|
||||
|
||||
export const users = pgTable('users', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
email: varchar('email', { length: 255 }).notNull().unique(),
|
||||
passwordHash: varchar('password_hash', { length: 255 }).notNull(),
|
||||
nickname: varchar('nickname', { length: 30 }).notNull(),
|
||||
avatarUrl: varchar('avatar_url', { length: 500 }),
|
||||
country: varchar('country', { length: 100 }),
|
||||
city: varchar('city', { length: 100 }),
|
||||
selfLevel: selfLevelEnum('self_level'),
|
||||
isPublic: boolean('is_public').notNull().default(true),
|
||||
role: userRoleEnum('role').notNull().default('free'),
|
||||
emailVerifiedAt: timestamp('email_verified_at', { withTimezone: true }),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export type User = typeof users.$inferSelect;
|
||||
export type NewUser = typeof users.$inferInsert;
|
||||
Reference in New Issue
Block a user