Skip to content

Commit

Permalink
feat(database): init database define
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 11, 2024
1 parent 2c0e7cf commit 7f5ebf1
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/database/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Config } from 'drizzle-kit';

import 'dotenv/config';

export default {
schema: './src/schema/',
out: './drizzle',
driver: 'pg',
dbCredentials: {
host: process.env.DB_HOST!,
database: process.env.DB_NAME!,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
}
} satisfies Config;
54 changes: 54 additions & 0 deletions packages/database/drizzle/0000_groovy_the_watchers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
DO $$ BEGIN
CREATE TYPE "resources_provider" AS ENUM('dmhy', 'moe');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "resources" (
"id" serial PRIMARY KEY NOT NULL,
"provider_type" "resources_provider" NOT NULL,
"provider_id" varchar(256) NOT NULL,
"href" varchar(256) NOT NULL,
"title" varchar(256) NOT NULL,
"title_alt" varchar(256) NOT NULL,
"type" varchar(256) NOT NULL,
"size" varchar(256) NOT NULL,
"magnet" varchar(256) NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"fetched_at" timestamp with time zone DEFAULT now(),
"anitomy" json,
"fansub_id" integer NOT NULL,
"publisher_id" integer,
"is_deleted" boolean DEFAULT false,
"is_duplicated" boolean DEFAULT false
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "teams" (
"id" serial PRIMARY KEY NOT NULL,
"provider_type" "resources_provider" NOT NULL,
"provider_id" varchar(256) NOT NULL,
"name" varchar(256) NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"provider_type" "resources_provider" NOT NULL,
"provider_id" varchar(256) NOT NULL,
"name" varchar(256) NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "provider_type_id" ON "resources" ("provider_type","provider_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "sort_by_created_at" ON "resources" ("created_at");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "fansub_index" ON "resources" ("fansub_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "publisher_index" ON "resources" ("publisher_id");--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resources" ADD CONSTRAINT "resources_fansub_id_teams_id_fk" FOREIGN KEY ("fansub_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resources" ADD CONSTRAINT "resources_publisher_id_users_id_fk" FOREIGN KEY ("publisher_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
260 changes: 260 additions & 0 deletions packages/database/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"id": "be0483f5-b846-4d0b-86dc-95a86593343c",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"resources": {
"name": "resources",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"provider_type": {
"name": "provider_type",
"type": "resources_provider",
"primaryKey": false,
"notNull": true
},
"provider_id": {
"name": "provider_id",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"href": {
"name": "href",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"title": {
"name": "title",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"title_alt": {
"name": "title_alt",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"type": {
"name": "type",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"size": {
"name": "size",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"magnet": {
"name": "magnet",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"fetched_at": {
"name": "fetched_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"anitomy": {
"name": "anitomy",
"type": "json",
"primaryKey": false,
"notNull": false
},
"fansub_id": {
"name": "fansub_id",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"publisher_id": {
"name": "publisher_id",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"is_deleted": {
"name": "is_deleted",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
},
"is_duplicated": {
"name": "is_duplicated",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
}
},
"indexes": {
"provider_type_id": {
"name": "provider_type_id",
"columns": [
"provider_type",
"provider_id"
],
"isUnique": true
},
"sort_by_created_at": {
"name": "sort_by_created_at",
"columns": [
"created_at"
],
"isUnique": false
},
"fansub_index": {
"name": "fansub_index",
"columns": [
"fansub_id"
],
"isUnique": false
},
"publisher_index": {
"name": "publisher_index",
"columns": [
"publisher_id"
],
"isUnique": false
}
},
"foreignKeys": {
"resources_fansub_id_teams_id_fk": {
"name": "resources_fansub_id_teams_id_fk",
"tableFrom": "resources",
"tableTo": "teams",
"schemaTo": "public",
"columnsFrom": [
"fansub_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"resources_publisher_id_users_id_fk": {
"name": "resources_publisher_id_users_id_fk",
"tableFrom": "resources",
"tableTo": "users",
"schemaTo": "public",
"columnsFrom": [
"publisher_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"teams": {
"name": "teams",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"provider_type": {
"name": "provider_type",
"type": "resources_provider",
"primaryKey": false,
"notNull": true
},
"provider_id": {
"name": "provider_id",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"provider_type": {
"name": "provider_type",
"type": "resources_provider",
"primaryKey": false,
"notNull": true
},
"provider_id": {
"name": "provider_id",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {
"resources_provider": {
"name": "resources_provider",
"values": {
"dmhy": "dmhy",
"moe": "moe"
}
}
},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions packages/database/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1704965651813,
"tag": "0000_groovy_the_watchers",
"breakpoints": true
}
]
}
1 change: 1 addition & 0 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"animegarden": "workspace:*",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.29.3",
"postgres": "^3.4.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const hello = 1;
export * from './schema';
15 changes: 15 additions & 0 deletions packages/database/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { teams } from './team';
import { users } from './user';
import { resources } from './resource';

export type User = typeof users.$inferSelect;

export type NewUser = typeof users.$inferInsert;

export type Team = typeof teams.$inferSelect;

export type NewTeam = typeof teams.$inferInsert;

export type Resource = typeof resources.$inferSelect;

export type NewResource = typeof resources.$inferInsert;
3 changes: 3 additions & 0 deletions packages/database/src/schema/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { pgEnum } from 'drizzle-orm/pg-core';

export const providerEnum = pgEnum('resources_provider', ['dmhy', 'moe']);
Loading

0 comments on commit 7f5ebf1

Please sign in to comment.