コンテンツにスキップ

データの形を entities/ に分ける

05 章 データの形を entities/ に分ける

メモ 1 件の形(Note)を、notes/entities/ に分けます。エンティティは、そのモジュールが扱うデータの形です。

Note の形が NotesController から entities/ に分かれる

api/src/notes/entities/note.entity.ts を作成する

Section titled “api/src/notes/entities/note.entity.ts を作成する”
api/src/notes/entities/note.entity.ts
1
/** メモ 1 件の形。openapi.yaml の Note。 */
2
export type { Note } from '../../api/index.js';

api/src/notes/notes.controller.ts を変更する

Section titled “api/src/notes/notes.controller.ts を変更する”
api/src/notes/notes.controller.ts
12
12
import { randomUUID } from 'node:crypto';
13
13
import { DatabaseSync } from 'node:sqlite';
14
14
import { z } from 'zod';
15
import type { Note } from '../api/index.js';
16
15
import { createNoteSchema, type CreateNoteDto } from './dto/create-note.dto.js';
17
16
import { updateNoteSchema, type UpdateNoteDto } from './dto/update-note.dto.js';
17
import type { Note } from './entities/note.entity.js';
18
18
19
19
/** URL の :id。メモの ID は UUID。 */
20
20
const noteIdSchema = z.uuid();

npm run start:dev で起動したまま、api/openapi.yaml の 5 つの操作を Try it で送ります。04 章の最後と同じ応答(200・201・204・400・404)が返れば、整理しても動きは変わっていません。

この節は、次の公式ドキュメントをもとに書いています。

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えた状態をダウンロード