domain-rules 0.0.0 → 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +126 -1
- package/dist/index.d.mts +597 -0
- package/dist/index.mjs +573 -0
- package/package.json +38 -6
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 9wick / Kohei Kido
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1 +1,126 @@
|
|
|
1
|
-
|
|
1
|
+
# domain-rules
|
|
2
|
+
|
|
3
|
+
データの作成・更新・削除やアクション実行前のビジネスルールを小さな単位で定義し、必要なデータだけを取得・検証して、「検証済みであること」を TypeScript の型で保証するライブラリです。
|
|
4
|
+
|
|
5
|
+
> **Status**: 試作段階です。main の最新は npm の `beta` タグで公開しています。より実践的な定義例は [test/blog/domain.ts](test/blog/domain.ts) にあり、型検査とテストで動作を確認しています。
|
|
6
|
+
|
|
7
|
+
## 何ができるか
|
|
8
|
+
|
|
9
|
+
- **ルールを小さな単位で宣言できる**: 「いつ適用するか (`when`)」と「何を検証するか (`check`)」を分けて定義し、モデルごとに 1 本の検証パイプライン (`ruleset`) として組み立てます。
|
|
10
|
+
- **検証結果を型に反映できる(型の昇格)**: 前段のルールで確認した値(採番済みの ID や存在確認済みの値など)を状態へマージして型を絞り込み、保存可能な型を満たしているかをコンパイル時に検証します。
|
|
11
|
+
- **外部依存を分離してテストしやすい**: DB 参照などの副作用は `port` として宣言し、起動時に `bind` で実装 (`provider`) を注入します。テストでは `provide.value` で固定値を渡すだけで、リポジトリのモックは不要です。
|
|
12
|
+
- **未検証データの保存を型で防ぐ**: すべてのルールを通過した値だけが `Persistable` などの検証済みラッパーとして返り、`open()` でのみ中身を取り出せます。
|
|
13
|
+
|
|
14
|
+
## 前提
|
|
15
|
+
|
|
16
|
+
- TypeScript
|
|
17
|
+
- [Standard Schema](https://github.com/standard-schema/spec) 準拠のスキーマライブラリ(例: Valibot)
|
|
18
|
+
- [`@praha/byethrow`](https://github.com/praha-inc/byethrow)(成功・失敗を `Result` / `ResultAsync` で表現)
|
|
19
|
+
|
|
20
|
+
## インストール
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add domain-rules@beta @praha/byethrow
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
ESM 専用パッケージです(`.d.mts` 型定義同梱)。
|
|
27
|
+
|
|
28
|
+
## クイックスタート
|
|
29
|
+
|
|
30
|
+
全体の流れは **1. 定義(モデル・ポート・ルール) → 2. 注入(`bind`) → 3. 実行と取り出し(`open`)** です。
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { Result } from "@praha/byethrow";
|
|
34
|
+
import * as v from "valibot";
|
|
35
|
+
import {
|
|
36
|
+
always,
|
|
37
|
+
bind,
|
|
38
|
+
changed,
|
|
39
|
+
clock,
|
|
40
|
+
domainDefinitionKit,
|
|
41
|
+
modelRules,
|
|
42
|
+
open,
|
|
43
|
+
port,
|
|
44
|
+
provide,
|
|
45
|
+
rule,
|
|
46
|
+
ruleset,
|
|
47
|
+
type Persisted,
|
|
48
|
+
} from "domain-rules";
|
|
49
|
+
|
|
50
|
+
// 1. モデル・ポート・ルールを定義する(ドメイン層)
|
|
51
|
+
// actor の型はここで1回だけ決める。rule の check はこの型の actor を受け取る
|
|
52
|
+
const { model } = domainDefinitionKit<{ actor: { readonly id: string } }>();
|
|
53
|
+
|
|
54
|
+
const articleInput = v.object({
|
|
55
|
+
id: v.optional(v.string()),
|
|
56
|
+
title: v.pipe(v.string(), v.minLength(1)),
|
|
57
|
+
status: v.optional(v.picklist(["DRAFT", "PUBLISHED"]), "DRAFT"),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const article = model({
|
|
61
|
+
name: "article",
|
|
62
|
+
schema: {
|
|
63
|
+
input: articleInput,
|
|
64
|
+
persisted: v.object({ ...articleInput.entries, id: v.string() }),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const findArticleById = port("findArticleById").of<(id: string) => Persisted<typeof article> | null, never>();
|
|
69
|
+
const newArticleId = port("newArticleId").of<() => string, never>();
|
|
70
|
+
|
|
71
|
+
const publishRequiresLongTitle = rule(article.onSave, {
|
|
72
|
+
name: "公開時はタイトルが5文字以上",
|
|
73
|
+
when: changed("status", { next: "PUBLISHED" }),
|
|
74
|
+
check: ({ next }) => (next.title.length >= 5 ? Result.succeed() : Result.fail("TITLE_TOO_SHORT")),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const assignArticleId = rule(article.onSave, {
|
|
78
|
+
name: "ID を確定する",
|
|
79
|
+
requires: { newArticleId },
|
|
80
|
+
when: always,
|
|
81
|
+
check: async ({ pre }, { newArticleId }) => {
|
|
82
|
+
if (pre !== null) return Result.succeed({ next: { id: pre.id } });
|
|
83
|
+
const id = await newArticleId();
|
|
84
|
+
return Result.isFailure(id) ? id : Result.succeed({ next: { id: id.value } });
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const articleRules = modelRules(article)
|
|
89
|
+
.load(findArticleById)
|
|
90
|
+
.save(ruleset(publishRequiresLongTitle).add(assignArticleId));
|
|
91
|
+
|
|
92
|
+
// 2. provider を注入して呼び出し可能な API を組み立てる(起動時 / DI)
|
|
93
|
+
const domain = bind([articleRules], {
|
|
94
|
+
providers: [
|
|
95
|
+
provide(findArticleById, (id) => articleRepo.findById(id)),
|
|
96
|
+
provide(newArticleId, () => Result.succeed(crypto.randomUUID())),
|
|
97
|
+
provide(clock, () => Date.now()),
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 3. 実行し、検証済みの値を取り出して保存する(Usecase)
|
|
102
|
+
const result = await domain.article.create({
|
|
103
|
+
input: { title: "Hello domain-rules" },
|
|
104
|
+
actor: { id: "usr_1" },
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
if (Result.isSuccess(result)) {
|
|
108
|
+
const { next } = open(result.value); // next は Persisted<typeof article> 型
|
|
109
|
+
await articleRepo.save(next);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`modelRules` の定義に応じて、`domain.article` には `create` / `update` / `upsert` / `delete` が生成されます。永続化を伴わない操作は `action` / `actionRules`(`decide`)で定義できます。
|
|
114
|
+
|
|
115
|
+
## ドキュメント
|
|
116
|
+
|
|
117
|
+
目的に応じて `docs/` 配下のドキュメントを参照してください。
|
|
118
|
+
|
|
119
|
+
| 知りたいこと | ドキュメント | 主な内容 |
|
|
120
|
+
| -------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
121
|
+
| 使い方と実行時の動作仕様 | [利用ガイド (`docs/guide.md`)](docs/guide.md) | モデル・ルール・`when`・型の昇格・`action`・DI・テストの書き方に加え、実行順序・マージ仕様・失敗の型・`cached` の挙動・`open()`・`plan()` まで実装時に必要なリファレンス |
|
|
122
|
+
| 設計の意図・構成・スコープ | [設計思想とスコープ (`docs/design.md`)](docs/design.md) | 解決したい課題、ドメイン層・DI層・ユースケース層の責務分割、設計原則、スコープ外(TOCTOU や複数レコード更新など)、先行事例、未決定の論点 |
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|