Skip to content

Commit

Permalink
feat: remove postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed May 11, 2024
1 parent 502e9a3 commit 8f38b9b
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 398 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ jobs:
- run: npm run generate
- run: npm run lint
- run: npm run typecheck
- run: |
cp server/.env.example server/.env
docker compose up -d
- run: cp server/.env.example server/.env
- run: npm test
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"bierner.markdown-mermaid",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"prisma.prisma",
"stylelint.vscode-stylelint",
"styled-components.vscode-styled-components",
"yoavbls.pretty-ts-errors"
Expand Down
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"source.fixAll.eslint": "explicit",
"source.fixAll.stylelint": "explicit"
},
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
},
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.referencesCodeLens.enabled": true,
"files.eol": "\n",
Expand Down
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ $ npm i --prefix server
$ cp server/.env.example server/.env
```

### ミドルウェアのセットアップ

```sh
$ docker compose up -d
```

### 開発サーバー起動

次回以降は以下のコマンドだけで開発できる
Expand All @@ -49,10 +43,3 @@ Web ブラウザで http://localhost:3000 を開く
[Node.js モノレポ開発のターミナルログ混雑解消のための新作 CLI ツール notios](https://zenn.dev/luma/articles/nodejs-new-cli-tool-notios)

閉じるときは `Ctrl + C` を 2 回連続で入力

#### PostgreSQL UI

```sh
$ cd server
$ npx prisma studio
```
17 changes: 0 additions & 17 deletions compose.yml

This file was deleted.

2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ PORT=31577
API_BASE_PATH=/api
API_ORIGIN=http://localhost:31577
CORS_ORIGIN=http://localhost:3000
DATABASE_URL=postgresql://root:root@localhost:5432/app
TEST_DATABASE_URL=postgresql://root:root@localhost:5432/test
MINIO_ROOT_USER=minio
MINIO_ROOT_PASSWORD=password
9 changes: 1 addition & 8 deletions server/api/health/controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { prismaClient } from 'service/prismaClient';
import { defineController } from './$relay';

export default defineController(() => ({
get: async () => ({
status: 200,
body: {
server: 'ok',
db: await prismaClient.$queryRaw`SELECT CURRENT_TIMESTAMP;`.then(() => 'ok' as const),
},
}),
get: () => ({ status: 200, body: { server: 'ok' } }),
}));
2 changes: 1 addition & 1 deletion server/api/health/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { DefineMethods } from 'aspida';

export type Methods = DefineMethods<{
get: {
resBody: Record<'server' | 'db', 'ok'>;
resBody: { server: 'ok' };
};
}>;
8 changes: 4 additions & 4 deletions server/api/tasks/_taskId@string/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { defineController } from './$relay';
export default defineController(() => ({
patch: {
validators: { body: z.object({ label: z.string().optional(), done: z.boolean().optional() }) },
handler: async ({ body, params }) => {
const task = await taskUseCase.update({
handler: ({ body, params }) => {
const task = taskUseCase.update({
...body,
taskId: taskIdParser.parse(params.taskId),
});

return { status: 204, body: task };
},
},
delete: async ({ params }) => {
const task = await taskUseCase.delete(taskIdParser.parse(params.taskId));
delete: ({ params }) => {
const task = taskUseCase.delete(taskIdParser.parse(params.taskId));

return { status: 204, body: task };
},
Expand Down
23 changes: 4 additions & 19 deletions server/api/tasks/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,21 @@ import { taskQuery } from 'domain/task/repository/taskQuery';
import { taskValidator } from 'domain/task/service/taskValidator';
import { taskUseCase } from 'domain/task/useCase/taskUseCase';
import { taskIdParser } from 'service/idParsers';
import { prismaClient } from 'service/prismaClient';
import { z } from 'zod';
import { defineController } from './$relay';

export default defineController(() => ({
get: async ({ query }) => ({
status: 200,
body: await taskQuery.findMany(prismaClient, query?.limit),
}),
get: () => ({ status: 200, body: taskQuery.findMany() }),
post: {
validators: { body: taskValidator.taskCreate },
handler: async ({ body }) => ({
status: 201,
body: await taskUseCase.create(body),
}),
handler: ({ body }) => ({ status: 201, body: taskUseCase.create(body) }),
},
patch: {
validators: { body: taskValidator.taskUpdate },
handler: async ({ body }) => {
const task = await taskUseCase.update(body);

return { status: 204, body: task };
},
handler: ({ body }) => ({ status: 204, body: taskUseCase.update(body) }),
},
delete: {
validators: { body: z.object({ taskId: taskIdParser }) },
handler: async ({ body }) => {
const task = await taskUseCase.delete(body.taskId);

return { status: 204, body: task };
},
handler: ({ body }) => ({ status: 204, body: taskUseCase.delete(body.taskId) }),
},
}));
3 changes: 0 additions & 3 deletions server/api/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import type { DefineMethods } from 'aspida';

export type Methods = DefineMethods<{
get: {
query?: {
limit?: number;
};
resBody: TaskEntity[];
};

Expand Down
2 changes: 1 addition & 1 deletion server/domain/task/model/taskMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { deletableTaskIdParser, taskIdParser } from 'service/idParsers';
import type { TaskDeleteVal } from './taskEntity';

export const taskMethod = {
create: async (val: TaskCreateVal): Promise<TaskEntity> => {
create: (val: TaskCreateVal): TaskEntity => {
return {
id: taskIdParser.parse(randomUUID()),
done: false,
Expand Down
22 changes: 8 additions & 14 deletions server/domain/task/repository/taskCommand.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import type { Prisma } from '@prisma/client';
import type { TaskEntity } from 'api/@types/task';
import type { TaskDeleteVal } from '../model/taskEntity';

export let taskStorage: TaskEntity[] = [];

export const taskCommand = {
save: async (tx: Prisma.TransactionClient, task: TaskEntity): Promise<void> => {
await tx.task.upsert({
where: { id: task.id },
update: { label: task.label, done: task.done },
create: {
id: task.id,
label: task.label,
done: task.done,
createdAt: new Date(task.createdTime),
},
});
save: (task: TaskEntity): void => {
taskStorage = taskStorage.some((t) => t.id === task.id)
? taskStorage.map((t) => (t.id === task.id ? task : t))
: (taskStorage = [...taskStorage, task]);
},
delete: async (tx: Prisma.TransactionClient, val: TaskDeleteVal): Promise<void> => {
await tx.task.delete({ where: { id: val.deletableId } });
delete: (val: TaskDeleteVal): void => {
taskStorage = taskStorage.filter((t) => t.id !== val.task.id);
},
};
30 changes: 9 additions & 21 deletions server/domain/task/repository/taskQuery.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import type { Prisma, Task } from '@prisma/client';
import type { Maybe, TaskId } from 'api/@types/brandedId';
import type { TaskEntity } from 'api/@types/task';
import { taskIdParser } from 'service/idParsers';

const toModel = async (prismaTask: Task): Promise<TaskEntity> => ({
id: taskIdParser.parse(prismaTask.id),
label: prismaTask.label,
done: prismaTask.done,
createdTime: prismaTask.createdAt.getTime(),
});

const findMany = async (tx: Prisma.TransactionClient, limit?: number): Promise<TaskEntity[]> => {
const prismaTasks = await tx.task.findMany({
take: limit,
orderBy: { createdAt: 'desc' },
});

return Promise.all(prismaTasks.map(toModel));
};
import assert from 'assert';
import { taskStorage } from './taskCommand';

export const taskQuery = {
findMany,
findById: async (tx: Prisma.TransactionClient, taskId: Maybe<TaskId>): Promise<TaskEntity> =>
tx.task.findUniqueOrThrow({ where: { id: taskId } }).then(toModel),
findMany: (): TaskEntity[] => taskStorage,
findById: (taskId: Maybe<TaskId>): TaskEntity => {
const task = taskStorage.find((t) => t.id === taskId);
assert(task);

return task;
},
};
38 changes: 17 additions & 21 deletions server/domain/task/useCase/taskUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import type { Maybe, TaskId } from 'api/@types/brandedId';
import type { TaskCreateVal, TaskEntity, TaskUpdateVal } from 'api/@types/task';
import { transaction } from 'service/prismaClient';
import { taskMethod } from '../model/taskMethod';
import { taskCommand } from '../repository/taskCommand';
import { taskQuery } from '../repository/taskQuery';

export const taskUseCase = {
create: (val: TaskCreateVal): Promise<TaskEntity> =>
transaction('RepeatableRead', async (tx) => {
const created = await taskMethod.create(val);
create: (val: TaskCreateVal): TaskEntity => {
const created = taskMethod.create(val);

await taskCommand.save(tx, created);
taskCommand.save(created);

return created;
}),
update: (val: TaskUpdateVal): Promise<TaskEntity> =>
transaction('RepeatableRead', async (tx) => {
const task = await taskQuery.findById(tx, val.taskId);
const updated = await taskMethod.update(task, val);
return created;
},
update: (val: TaskUpdateVal): TaskEntity => {
const task = taskQuery.findById(val.taskId);
const updated = taskMethod.update(task, val);

await taskCommand.save(tx, updated);
taskCommand.save(updated);

return updated;
}),
delete: (taskId: Maybe<TaskId>): Promise<TaskEntity> =>
transaction('RepeatableRead', async (tx) => {
const task = await taskQuery.findById(tx, taskId);
const deleted = taskMethod.delete(task);
return updated;
},
delete: (taskId: Maybe<TaskId>): TaskEntity => {
const task = taskQuery.findById(taskId);
const deleted = taskMethod.delete(task);

await taskCommand.delete(tx, deleted);
taskCommand.delete(deleted);

return task;
}),
return task;
},
};
Loading

0 comments on commit 8f38b9b

Please sign in to comment.