-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from harley-codes/task/setup-db-interface
Implement database client
- Loading branch information
Showing
7 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
export interface DatabaseClient { | ||
getProjectListAsync(): Promise<ProjectListItem[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'>; |
13 changes: 13 additions & 0 deletions
13
...modules/database/vendors/prisma-cockroach/migrations/20231223131758_migrate/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
25 changes: 25 additions & 0 deletions
25
src/modules/database/vendors/prisma-cockroach/prismaCockroachDatabaseClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters