Skip to content

refactor: 設定系ストアを汎用ファクトリへ集約し class を廃止 - #51

Merged
ansanloms merged 1 commit into
mainfrom
refactor/store-closure-factory
Jun 14, 2026
Merged

refactor: 設定系ストアを汎用ファクトリへ集約し class を廃止#51
ansanloms merged 1 commit into
mainfrom
refactor/store-closure-factory

Conversation

@ansanloms

Copy link
Copy Markdown
Owner

概要

設定系データの永続化ストア (server/store/) を、class ベースからクロージャベースの汎用ファクトリへ集約するリファクタ。挙動は変更しない。

動機

KeywordRuleStoreNotificationSettingsStore は「KV 配線 + CRUD」の骨格が重複しており、各 store が #kv の保持と注入を個別に書いていた。骨格を汎用化し、各ドメインは「キー設計・型ガード・正規化」を渡すだけの薄い構成にする。あわせて class を廃止し、依存はクロージャに束ねる。

変更点

  • store/kv.ts
    • KvclasscreateKv() クロージャへ。lazy-open 状態 (旧 #kv private) はクロージャ変数に閉じる。
    • 汎用 collectionStore(prefix 配下に複数レコード、各 id + createdAt 持ち)と singletonStore(単一キー)を追加。型 CollectionStore<T, Input> / SingletonStore<T> も export。
    • collection の既定ソートは createdAt 降順 → id(登録の新しい順)。
  • store/keyword-rules.ts / store/notification-settings.ts
    • class を廃止し、createKeywordRuleStore(kv) / createNotificationSettingsStore(kv) に。prefix・型ガード(isKeywordRule)・正規化(normalizeNotificationSettings)を渡すだけの薄い factory に縮小。
  • main.ts: new X()createX() の置換のみ。routes はストアのサブセット型(*Like)に構造的に依存するため無変更。
  • store/*.test.ts: new X(kv)createX(kv) に追従。挙動契約は不変。

設計判断

  • kv.ts 一本化はしない: collection 型(複数レコード + id)と singleton 型(単一値)は形が異なり、単一 CRUD interface に押し込めると singleton 側に無意味な list/add/remove(id) が生える。2 種のファクトリへ分けた。
  • 読み戻し検証は据え置き: keyword-rules はスキーマ駆動の isKeywordRule(= matchesStoredSchema)、notification-settings は旧形状補完を含む normalizeNotificationSettings。汎用 list<T>() の無検証キャストでは KV に残る旧形状・壊れた値を drop できないため、検証はドメイン側に残す。
  • id は現状維持: Deno KV はレコードに id を自動採番しない。versionstamp は更新のたびに変わり安定 id にならないため、crypto.randomUUID() による採番をそのまま使う。

確認

  • deno task test:server → 92 passed / 0 failed(store/keyword-rules.tsstore/notification-settings.ts は行・分岐 100%)。
  • deno check server/main.ts(ファクトリ返り値 → routes の *Like 構造適合)通過。
  • deno lint / deno fmt --check(変更ファイル)通過。

🤖 Generated with Claude Code

KeywordRuleStore / NotificationSettingsStore / Kv の 3 つの class を廃止し、
クロージャベースのファクトリへ統一した。

- Kv を class から createKv() クロージャへ。lazy-open 状態 (旧 #kv private
  フィールド) はクロージャ変数に閉じる
- CRUD の骨格を汎用化した collectionStore / singletonStore を server/store/kv.ts
  に追加。型 CollectionStore<T, Input> / SingletonStore<T> も export する
- keyword-rules / notification-settings は prefix・型ガード・正規化を渡すだけの
  薄いファクトリ (createKeywordRuleStore / createNotificationSettingsStore) に縮めた
- collection の既定ソートを createdAt 降順 → id (登録の新しい順) にし、
  keyword-rules 側の重複ソート指定を削除
- main.ts は new X() を createX() に置換。routes はストアのサブセット型
  (KeywordRuleStoreLike / NotificationSettingsStoreLike) に構造的に依存するため無変更
- store 系テストは new X(kv) を createX(kv) に追従。挙動契約は変更なし

なお Deno KV はレコードに id を自動採番しない (versionstamp は更新のたびに
変わり安定 id にならない) ため、keyword-rules の crypto.randomUUID() による
id 採番は現状維持とした。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ansanloms
ansanloms merged commit 398534c into main Jun 14, 2026
1 check passed
@ansanloms
ansanloms deleted the refactor/store-closure-factory branch June 14, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant