Skip to content

Commit

Permalink
Merge pull request #18 from harley-codes/task/setup-db-interface
Browse files Browse the repository at this point in the history
Implement database client
  • Loading branch information
harley-codes authored Dec 23, 2023
2 parents 638d0e6 + c3cae2f commit 067d8d6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/modules/database/databaseClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export interface DatabaseClient {
getProjectListAsync(): Promise<ProjectListItem[]>;
}
25 changes: 25 additions & 0 deletions src/modules/database/databaseFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DatabaseClient } from '@/modules/database/databaseClient'
import { PrismaCockroachDatabaseClient } from '@/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient'

export function getDatabaseClientAsync(): Promise<DatabaseClient>
{
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)
}
32 changes: 32 additions & 0 deletions src/modules/database/models.ts
Original file line number Diff line number Diff line change
@@ -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<string, Record<string, string>>;
meta: Record<string, string>;
tags: string[];
status: PostStatus;
project: Project | null;
}

type PostStatus = 'ACTIVE' | 'DISABLED' | 'HIDDEN'

type ProjectListItem = Pick<Project, 'id' | 'name' | 'active'>;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<ProjectListItem[]>
{
const projects = await this.prisma.project.findMany({
select: {
id: true,
name: true,
active: true,
}
})

return projects
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 067d8d6

Please sign in to comment.