diff --git a/src/modules/database/databaseClient.ts b/src/modules/database/databaseClient.ts new file mode 100644 index 0000000..c125983 --- /dev/null +++ b/src/modules/database/databaseClient.ts @@ -0,0 +1,4 @@ + +export interface DatabaseClient { + getProjectListAsync(): Promise; +} diff --git a/src/modules/database/databaseFactory.ts b/src/modules/database/databaseFactory.ts new file mode 100644 index 0000000..bd14d9d --- /dev/null +++ b/src/modules/database/databaseFactory.ts @@ -0,0 +1,25 @@ +import { DatabaseClient } from '@/modules/database/databaseClient' +import { PrismaCockroachDatabaseClient } from '@/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient' + +export function getDatabaseClientAsync(): Promise +{ + const databaseTargetService = process.env.DATABASE_TARGET_SERVICE + + if (!databaseTargetService) + { + throw new Error('DATABASE_TARGET_SERVICE environment variable is not set') + } + + let databaseClient: DatabaseClient + + switch (databaseTargetService) + { + case 'prisma-cockroach': + databaseClient = new PrismaCockroachDatabaseClient() + break + default: + throw new Error(`Unsupported database service: ${databaseTargetService}`) + } + + return Promise.resolve(databaseClient) +} diff --git a/src/modules/database/models.ts b/src/modules/database/models.ts new file mode 100644 index 0000000..1ce75b7 --- /dev/null +++ b/src/modules/database/models.ts @@ -0,0 +1,32 @@ +type Project = { + id: string; + name: string; + active: boolean; + posts: Post[]; + accessTokens: AccessToken[]; +}; + +type AccessToken = { + id: string; + idProject: string; + token: string; + project: Project | null; +} + +type Post = { + id: string; + idProject: string; + title: string; + description: string; + featuredImageURL: string; + date: Date; + blocks: Record>; + meta: Record; + tags: string[]; + status: PostStatus; + project: Project | null; +} + +type PostStatus = 'ACTIVE' | 'DISABLED' | 'HIDDEN' + +type ProjectListItem = Pick; diff --git a/src/modules/database/vendors/prisma-cockroach/migrations/20231223131758_migrate/migration.sql b/src/modules/database/vendors/prisma-cockroach/migrations/20231223131758_migrate/migration.sql new file mode 100644 index 0000000..8baa804 --- /dev/null +++ b/src/modules/database/vendors/prisma-cockroach/migrations/20231223131758_migrate/migration.sql @@ -0,0 +1,13 @@ +/* + Warnings: + + - You are about to drop the column `name` on the `Post` table. All the data in the column will be lost. + - Added the required column `title` to the `Post` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "AccessToken" ALTER COLUMN "token" SET DEFAULT md5(random()::text); + +-- AlterTable +ALTER TABLE "Post" DROP COLUMN "name"; +ALTER TABLE "Post" ADD COLUMN "title" STRING NOT NULL; diff --git a/src/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient.ts b/src/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient.ts new file mode 100644 index 0000000..0352040 --- /dev/null +++ b/src/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient.ts @@ -0,0 +1,25 @@ +import { DatabaseClient } from '@/modules/database/databaseClient' +import { PrismaClient } from '@prisma/client' + +export class PrismaCockroachDatabaseClient implements DatabaseClient +{ + private prisma: PrismaClient + + constructor() + { + this.prisma = new PrismaClient() + } + + async getProjectListAsync(): Promise + { + const projects = await this.prisma.project.findMany({ + select: { + id: true, + name: true, + active: true, + } + }) + + return projects + } +} diff --git a/src/modules/database/vendors/prisma-cockroach/schema.prisma b/src/modules/database/vendors/prisma-cockroach/schema.prisma index ed8aecf..843ee53 100644 --- a/src/modules/database/vendors/prisma-cockroach/schema.prisma +++ b/src/modules/database/vendors/prisma-cockroach/schema.prisma @@ -27,7 +27,7 @@ model AccessToken { model Post { id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid idProject String? @db.Uuid - name String @db.String() + title String @db.String() description String? @db.String() featuredImageURL String? @db.String() date BigInt @db.Int8 diff --git a/src/types/env.d.ts b/src/types/env.d.ts index 1dc5c52..10619bf 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -7,5 +7,6 @@ namespace NodeJS NEXTAUTH_PROVIDER_GITHUB_ADMIN_USER_IDs: string NEXTAUTH_PROVIDER_GITHUB_ID: string NEXTAUTH_PROVIDER_GITHUB_SECRET: string + DATABASE_TARGET_SERVICE: string } }