first commit
This commit is contained in:
11
src/database/database.module.ts
Normal file
11
src/database/database.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { DrizzleService } from './drizzle.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
providers: [DrizzleService],
|
||||
exports: [DrizzleService],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
27
src/database/drizzle.service.ts
Normal file
27
src/database/drizzle.service.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import * as schema from './schema';
|
||||
|
||||
@Injectable()
|
||||
export class DrizzleService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(DrizzleService.name);
|
||||
private pool: Pool;
|
||||
db: NodePgDatabase<typeof schema>;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
async onModuleInit() {
|
||||
const url = this.configService.get<string>('database.url');
|
||||
this.pool = new Pool({ connectionString: url });
|
||||
this.db = drizzle(this.pool, { schema });
|
||||
await this.pool.connect();
|
||||
this.logger.log('Database connected');
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.pool.end();
|
||||
this.logger.log('Database disconnected');
|
||||
}
|
||||
}
|
||||
268
src/database/migrations/0000_romantic_morg.sql
Normal file
268
src/database/migrations/0000_romantic_morg.sql
Normal file
@@ -0,0 +1,268 @@
|
||||
CREATE TYPE "public"."chat_status" AS ENUM('active', 'closed');--> statement-breakpoint
|
||||
CREATE TYPE "public"."media_type" AS ENUM('photo', 'voice', 'video');--> statement-breakpoint
|
||||
CREATE TYPE "public"."user_status" AS ENUM('active', 'banned', 'pending');--> statement-breakpoint
|
||||
CREATE TYPE "public"."like_type" AS ENUM('like', 'dislike');--> statement-breakpoint
|
||||
CREATE TYPE "public"."report_entity_type" AS ENUM('profile', 'message');--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "permission" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"role_id" uuid NOT NULL,
|
||||
"name" varchar(100) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "role" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"name" varchar(50) NOT NULL,
|
||||
CONSTRAINT "role_name_unique" UNIQUE("name")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "tariff" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"name" varchar(100) NOT NULL,
|
||||
"price_per_month" numeric(10, 2) NOT NULL,
|
||||
"price_per_year" numeric(10, 2) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "city" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"name" varchar(200) NOT NULL,
|
||||
"lat" numeric(10, 7) NOT NULL,
|
||||
"lng" numeric(10, 7) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "city_district" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"city_id" uuid NOT NULL,
|
||||
"name" varchar(200) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "chat" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"profile1_id" uuid NOT NULL,
|
||||
"profile2_id" uuid NOT NULL,
|
||||
"status" "chat_status" DEFAULT 'active' NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "greetings" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"text" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "message" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"chat_id" uuid NOT NULL,
|
||||
"user_id" uuid NOT NULL,
|
||||
"text" text,
|
||||
"media_url" text,
|
||||
"media_type" "media_type",
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "payment" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"user_id" uuid NOT NULL,
|
||||
"provider" varchar(100) NOT NULL,
|
||||
"credentials" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "user" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"phone" varchar(20) NOT NULL,
|
||||
"password" text NOT NULL,
|
||||
"status" "user_status" DEFAULT 'pending' NOT NULL,
|
||||
"role_id" uuid,
|
||||
"tariff_id" uuid,
|
||||
"payment_id" uuid,
|
||||
"active_chat_id" uuid,
|
||||
"fcm_token" text,
|
||||
CONSTRAINT "user_phone_unique" UNIQUE("phone")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "media" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"user_id" uuid NOT NULL,
|
||||
"path" text NOT NULL,
|
||||
"type" varchar(10) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "profile" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"user_id" uuid NOT NULL,
|
||||
"name" varchar(100) NOT NULL,
|
||||
"birth_date" date NOT NULL,
|
||||
"city_id" uuid,
|
||||
"district_id" uuid,
|
||||
"description" text,
|
||||
"nation" varchar(100),
|
||||
"height" double precision,
|
||||
"weight" double precision,
|
||||
CONSTRAINT "profile_user_id_unique" UNIQUE("user_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "profile_tag" (
|
||||
"profile_id" uuid NOT NULL,
|
||||
"tag_id" uuid NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "tag" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"value" varchar(100) NOT NULL,
|
||||
CONSTRAINT "tag_value_unique" UNIQUE("value")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "like" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"source_user" uuid NOT NULL,
|
||||
"target_user" uuid NOT NULL,
|
||||
"type" "like_type" NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "match" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"user1_id" uuid NOT NULL,
|
||||
"user2_id" uuid NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "date" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"user1_id" uuid NOT NULL,
|
||||
"user2_id" uuid NOT NULL,
|
||||
"lat" numeric(10, 7) NOT NULL,
|
||||
"lng" numeric(10, 7) NOT NULL,
|
||||
"time" timestamp with time zone NOT NULL,
|
||||
"status_id" uuid
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "date_status" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"text" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "report" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"source_user" uuid NOT NULL,
|
||||
"entity_id" uuid NOT NULL,
|
||||
"entity_type" "report_entity_type" NOT NULL,
|
||||
"description" text
|
||||
);
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "permission" ADD CONSTRAINT "permission_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "city_district" ADD CONSTRAINT "city_district_city_id_city_id_fk" FOREIGN KEY ("city_id") REFERENCES "public"."city"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "message" ADD CONSTRAINT "message_chat_id_chat_id_fk" FOREIGN KEY ("chat_id") REFERENCES "public"."chat"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "payment" ADD CONSTRAINT "payment_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "user" ADD CONSTRAINT "user_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "user" ADD CONSTRAINT "user_tariff_id_tariff_id_fk" FOREIGN KEY ("tariff_id") REFERENCES "public"."tariff"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "media" ADD CONSTRAINT "media_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "profile" ADD CONSTRAINT "profile_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "profile" ADD CONSTRAINT "profile_city_id_city_id_fk" FOREIGN KEY ("city_id") REFERENCES "public"."city"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "profile" ADD CONSTRAINT "profile_district_id_city_district_id_fk" FOREIGN KEY ("district_id") REFERENCES "public"."city_district"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "profile_tag" ADD CONSTRAINT "profile_tag_profile_id_profile_id_fk" FOREIGN KEY ("profile_id") REFERENCES "public"."profile"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "profile_tag" ADD CONSTRAINT "profile_tag_tag_id_tag_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."tag"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "like" ADD CONSTRAINT "like_source_user_user_id_fk" FOREIGN KEY ("source_user") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "like" ADD CONSTRAINT "like_target_user_user_id_fk" FOREIGN KEY ("target_user") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "match" ADD CONSTRAINT "match_user1_id_user_id_fk" FOREIGN KEY ("user1_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "match" ADD CONSTRAINT "match_user2_id_user_id_fk" FOREIGN KEY ("user2_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "date" ADD CONSTRAINT "date_user1_id_user_id_fk" FOREIGN KEY ("user1_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "date" ADD CONSTRAINT "date_user2_id_user_id_fk" FOREIGN KEY ("user2_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "date" ADD CONSTRAINT "date_status_id_date_status_id_fk" FOREIGN KEY ("status_id") REFERENCES "public"."date_status"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "report" ADD CONSTRAINT "report_source_user_user_id_fk" FOREIGN KEY ("source_user") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
3
src/database/migrations/0001_brown_marrow.sql
Normal file
3
src/database/migrations/0001_brown_marrow.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
CREATE TYPE "public"."gender" AS ENUM('male', 'female');--> statement-breakpoint
|
||||
ALTER TABLE "profile" ADD COLUMN "gender" "gender" NOT NULL DEFAULT 'male';--> statement-breakpoint
|
||||
ALTER TABLE "profile" ALTER COLUMN "gender" DROP DEFAULT;
|
||||
1139
src/database/migrations/meta/0000_snapshot.json
Normal file
1139
src/database/migrations/meta/0000_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
1154
src/database/migrations/meta/0001_snapshot.json
Normal file
1154
src/database/migrations/meta/0001_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
20
src/database/migrations/meta/_journal.json
Normal file
20
src/database/migrations/meta/_journal.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"entries": [
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1780401435523,
|
||||
"tag": "0000_romantic_morg",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 1,
|
||||
"version": "7",
|
||||
"when": 1780403477744,
|
||||
"tag": "0001_brown_marrow",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
28
src/database/schema/chat.schema.ts
Normal file
28
src/database/schema/chat.schema.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { pgEnum, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core';
|
||||
|
||||
export const chatStatusEnum = pgEnum('chat_status', ['active', 'closed']);
|
||||
export const mediaTypeEnum = pgEnum('media_type', ['photo', 'voice', 'video']);
|
||||
|
||||
export const chat = pgTable('chat', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
profile1Id: uuid('profile1_id').notNull(),
|
||||
profile2Id: uuid('profile2_id').notNull(),
|
||||
status: chatStatusEnum('status').notNull().default('active'),
|
||||
});
|
||||
|
||||
export const message = pgTable('message', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
chatId: uuid('chat_id')
|
||||
.notNull()
|
||||
.references(() => chat.id, { onDelete: 'cascade' }),
|
||||
userId: uuid('user_id').notNull(),
|
||||
text: text('text'),
|
||||
mediaUrl: text('media_url'),
|
||||
mediaType: mediaTypeEnum('media_type'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const greetings = pgTable('greetings', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
text: text('text').notNull(),
|
||||
});
|
||||
16
src/database/schema/city.schema.ts
Normal file
16
src/database/schema/city.schema.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { decimal, pgTable, uuid, varchar } from 'drizzle-orm/pg-core';
|
||||
|
||||
export const city = pgTable('city', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
name: varchar('name', { length: 200 }).notNull(),
|
||||
lat: decimal('lat', { precision: 10, scale: 7 }).notNull(),
|
||||
lng: decimal('lng', { precision: 10, scale: 7 }).notNull(),
|
||||
});
|
||||
|
||||
export const cityDistrict = pgTable('city_district', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
cityId: uuid('city_id')
|
||||
.notNull()
|
||||
.references(() => city.id, { onDelete: 'cascade' }),
|
||||
name: varchar('name', { length: 200 }).notNull(),
|
||||
});
|
||||
21
src/database/schema/date.schema.ts
Normal file
21
src/database/schema/date.schema.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { decimal, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core';
|
||||
import { user } from './user.schema';
|
||||
|
||||
export const dateStatus = pgTable('date_status', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
text: text('text').notNull(),
|
||||
});
|
||||
|
||||
export const date = pgTable('date', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
user1Id: uuid('user1_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
user2Id: uuid('user2_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
lat: decimal('lat', { precision: 10, scale: 7 }).notNull(),
|
||||
lng: decimal('lng', { precision: 10, scale: 7 }).notNull(),
|
||||
time: timestamp('time', { withTimezone: true }).notNull(),
|
||||
statusId: uuid('status_id').references(() => dateStatus.id, { onDelete: 'set null' }),
|
||||
});
|
||||
9
src/database/schema/index.ts
Normal file
9
src/database/schema/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export * from './role.schema';
|
||||
export * from './tariff.schema';
|
||||
export * from './city.schema';
|
||||
export * from './chat.schema';
|
||||
export * from './user.schema';
|
||||
export * from './profile.schema';
|
||||
export * from './social.schema';
|
||||
export * from './date.schema';
|
||||
export * from './report.schema';
|
||||
45
src/database/schema/profile.schema.ts
Normal file
45
src/database/schema/profile.schema.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { date, doublePrecision, pgEnum, pgTable, text, uuid, varchar } from 'drizzle-orm/pg-core';
|
||||
import { user } from './user.schema';
|
||||
import { city, cityDistrict } from './city.schema';
|
||||
|
||||
export const genderEnum = pgEnum('gender', ['male', 'female']);
|
||||
|
||||
export const profile = pgTable('profile', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
userId: uuid('user_id')
|
||||
.notNull()
|
||||
.unique()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
birthDate: date('birth_date').notNull(),
|
||||
gender: genderEnum('gender').notNull(),
|
||||
cityId: uuid('city_id').references(() => city.id, { onDelete: 'set null' }),
|
||||
districtId: uuid('district_id').references(() => cityDistrict.id, { onDelete: 'set null' }),
|
||||
description: text('description'),
|
||||
nation: varchar('nation', { length: 100 }),
|
||||
height: doublePrecision('height'),
|
||||
weight: doublePrecision('weight'),
|
||||
});
|
||||
|
||||
export const tag = pgTable('tag', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
value: varchar('value', { length: 100 }).notNull().unique(),
|
||||
});
|
||||
|
||||
export const profileTag = pgTable('profile_tag', {
|
||||
profileId: uuid('profile_id')
|
||||
.notNull()
|
||||
.references(() => profile.id, { onDelete: 'cascade' }),
|
||||
tagId: uuid('tag_id')
|
||||
.notNull()
|
||||
.references(() => tag.id, { onDelete: 'cascade' }),
|
||||
});
|
||||
|
||||
export const media = pgTable('media', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
userId: uuid('user_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
path: text('path').notNull(),
|
||||
type: varchar('type', { length: 10 }).notNull(),
|
||||
});
|
||||
14
src/database/schema/report.schema.ts
Normal file
14
src/database/schema/report.schema.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { pgEnum, pgTable, text, uuid } from 'drizzle-orm/pg-core';
|
||||
import { user } from './user.schema';
|
||||
|
||||
export const reportEntityTypeEnum = pgEnum('report_entity_type', ['profile', 'message']);
|
||||
|
||||
export const report = pgTable('report', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
sourceUser: uuid('source_user')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
entityId: uuid('entity_id').notNull(),
|
||||
entityType: reportEntityTypeEnum('entity_type').notNull(),
|
||||
description: text('description'),
|
||||
});
|
||||
14
src/database/schema/role.schema.ts
Normal file
14
src/database/schema/role.schema.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { pgTable, uuid, varchar } from 'drizzle-orm/pg-core';
|
||||
|
||||
export const role = pgTable('role', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
name: varchar('name', { length: 50 }).notNull().unique(),
|
||||
});
|
||||
|
||||
export const permission = pgTable('permission', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
roleId: uuid('role_id')
|
||||
.notNull()
|
||||
.references(() => role.id, { onDelete: 'cascade' }),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
});
|
||||
27
src/database/schema/social.schema.ts
Normal file
27
src/database/schema/social.schema.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { pgEnum, pgTable, timestamp, uuid } from 'drizzle-orm/pg-core';
|
||||
import { user } from './user.schema';
|
||||
|
||||
export const likeTypeEnum = pgEnum('like_type', ['like', 'dislike']);
|
||||
|
||||
export const like = pgTable('like', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
sourceUser: uuid('source_user')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
targetUser: uuid('target_user')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
type: likeTypeEnum('type').notNull(),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
export const match = pgTable('match', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
user1Id: uuid('user1_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
user2Id: uuid('user2_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
8
src/database/schema/tariff.schema.ts
Normal file
8
src/database/schema/tariff.schema.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { decimal, pgTable, uuid, varchar } from 'drizzle-orm/pg-core';
|
||||
|
||||
export const tariff = pgTable('tariff', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
name: varchar('name', { length: 100 }).notNull(),
|
||||
pricePerMonth: decimal('price_per_month', { precision: 10, scale: 2 }).notNull(),
|
||||
pricePerYear: decimal('price_per_year', { precision: 10, scale: 2 }).notNull(),
|
||||
});
|
||||
26
src/database/schema/user.schema.ts
Normal file
26
src/database/schema/user.schema.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { pgEnum, pgTable, text, uuid, varchar } from 'drizzle-orm/pg-core';
|
||||
import { role } from './role.schema';
|
||||
import { tariff } from './tariff.schema';
|
||||
|
||||
export const userStatusEnum = pgEnum('user_status', ['active', 'banned', 'pending']);
|
||||
|
||||
export const user = pgTable('user', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
phone: varchar('phone', { length: 20 }).notNull().unique(),
|
||||
password: text('password').notNull(),
|
||||
status: userStatusEnum('status').notNull().default('pending'),
|
||||
roleId: uuid('role_id').references(() => role.id, { onDelete: 'set null' }),
|
||||
tariffId: uuid('tariff_id').references(() => tariff.id, { onDelete: 'set null' }),
|
||||
paymentId: uuid('payment_id'),
|
||||
activeChatId: uuid('active_chat_id'),
|
||||
fcmToken: text('fcm_token'),
|
||||
});
|
||||
|
||||
export const payment = pgTable('payment', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
userId: uuid('user_id')
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: 'cascade' }),
|
||||
provider: varchar('provider', { length: 100 }).notNull(),
|
||||
credentials: text('credentials').notNull(),
|
||||
});
|
||||
57
src/database/seed.ts
Normal file
57
src/database/seed.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as schema from './schema';
|
||||
import { role, tariff, dateStatus, greetings } from './schema';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
async function seed() {
|
||||
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
||||
const db = drizzle(pool, { schema });
|
||||
|
||||
console.log('Seeding roles...');
|
||||
await db
|
||||
.insert(role)
|
||||
.values([
|
||||
{ name: 'user' },
|
||||
{ name: 'moderator' },
|
||||
{ name: 'admin' },
|
||||
])
|
||||
.onConflictDoNothing();
|
||||
|
||||
console.log('Seeding tariffs...');
|
||||
await db
|
||||
.insert(tariff)
|
||||
.values([
|
||||
{ name: 'Standard', pricePerMonth: '9.99', pricePerYear: '89.99' },
|
||||
])
|
||||
.onConflictDoNothing();
|
||||
|
||||
console.log('Seeding date statuses...');
|
||||
await db
|
||||
.insert(dateStatus)
|
||||
.values([
|
||||
{ text: 'pending' },
|
||||
{ text: 'confirmed' },
|
||||
{ text: 'cancelled' },
|
||||
{ text: 'rescheduled' },
|
||||
])
|
||||
.onConflictDoNothing();
|
||||
|
||||
console.log('Seeding greetings...');
|
||||
await db
|
||||
.insert(greetings)
|
||||
.values([
|
||||
{ text: 'Hi! I noticed your profile and would love to chat 😊' },
|
||||
{ text: 'Hey there! Want to grab a coffee sometime?' },
|
||||
{ text: 'Hello! Your interests caught my eye. Let\'s talk!' },
|
||||
{ text: 'Hi! I think we might have a lot in common 🙂' },
|
||||
])
|
||||
.onConflictDoNothing();
|
||||
|
||||
console.log('Seed complete!');
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
seed().catch(console.error);
|
||||
Reference in New Issue
Block a user