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/dist/index.mjs
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
import { Result } from "@praha/byethrow";
|
|
2
|
+
import { fromEntries, getOrEmpty, mapValues } from "as-free";
|
|
3
|
+
// src/shared/panic.ts
|
|
4
|
+
var Panic = class extends Error {
|
|
5
|
+
name = "Panic";
|
|
6
|
+
};
|
|
7
|
+
// src/port/provider.ts
|
|
8
|
+
const register = (impls, scope, impl, name) => {
|
|
9
|
+
if (impls.has(scope)) throw new Panic(`bind: port "${name}" の provider が重複しています`);
|
|
10
|
+
impls.set(scope, impl);
|
|
11
|
+
};
|
|
12
|
+
var Provider = class {
|
|
13
|
+
name;
|
|
14
|
+
#register;
|
|
15
|
+
constructor(name, register) {
|
|
16
|
+
this.name = name;
|
|
17
|
+
this.#register = register;
|
|
18
|
+
Object.freeze(this);
|
|
19
|
+
}
|
|
20
|
+
registerIn = (scope) => this.#register(scope);
|
|
21
|
+
};
|
|
22
|
+
const provide = Object.assign((target, impl) => target.provider(impl), { value: (target, v) => target.valued(v) });
|
|
23
|
+
// src/port/clock.ts
|
|
24
|
+
var Clock = class {
|
|
25
|
+
kind = "clock";
|
|
26
|
+
name = "clock";
|
|
27
|
+
#impls = /* @__PURE__ */ new WeakMap();
|
|
28
|
+
constructor() {
|
|
29
|
+
Object.freeze(this);
|
|
30
|
+
}
|
|
31
|
+
provider = (now) => new Provider(this.name, (scope) => register(this.#impls, scope, now, this.name));
|
|
32
|
+
valued = (v) => this.provider(() => v);
|
|
33
|
+
providedIn = (scope) => this.#impls.has(scope);
|
|
34
|
+
nowIn = (scope) => this.#impls.get(scope)?.();
|
|
35
|
+
};
|
|
36
|
+
const clock = new Clock();
|
|
37
|
+
// src/port/port.ts
|
|
38
|
+
var Slots = class {
|
|
39
|
+
impls = /* @__PURE__ */ new WeakMap();
|
|
40
|
+
memos = /* @__PURE__ */ new WeakMap();
|
|
41
|
+
};
|
|
42
|
+
var Port = class Port {
|
|
43
|
+
kind = "port";
|
|
44
|
+
name;
|
|
45
|
+
cached;
|
|
46
|
+
#slots;
|
|
47
|
+
constructor(name, cached, slots) {
|
|
48
|
+
this.name = name;
|
|
49
|
+
this.cached = cached;
|
|
50
|
+
this.#slots = slots;
|
|
51
|
+
Object.freeze(this);
|
|
52
|
+
}
|
|
53
|
+
provider = (impl) => new Provider(this.name, (scope) => register(this.#slots.impls, scope, impl, this.name));
|
|
54
|
+
valued = (v) => this.provider(() => Result.succeed(v));
|
|
55
|
+
providedIn = (scope) => this.#slots.impls.has(scope);
|
|
56
|
+
withCache = () => new Port(this.name, true, this.#slots);
|
|
57
|
+
callIn = async (invocation, args) => {
|
|
58
|
+
const impl = this.#slots.impls.get(invocation.scope);
|
|
59
|
+
if (impl === void 0) throw new Panic(`port "${this.name}" の provider がありません`);
|
|
60
|
+
if (!this.cached) return impl(...args);
|
|
61
|
+
const memo = this.#slots.memos.get(invocation) ?? /* @__PURE__ */ new Map();
|
|
62
|
+
this.#slots.memos.set(invocation, memo);
|
|
63
|
+
const key = JSON.stringify(args);
|
|
64
|
+
const hit = memo.get(key);
|
|
65
|
+
if (hit !== void 0) return hit;
|
|
66
|
+
const pending = Promise.resolve(impl(...args));
|
|
67
|
+
memo.set(key, pending);
|
|
68
|
+
return pending;
|
|
69
|
+
};
|
|
70
|
+
resolveIn = (invocation) => (...args) => this.callIn(invocation, args);
|
|
71
|
+
unwrapIn = (invocation) => {
|
|
72
|
+
const call = (args) => this.callIn(invocation, args);
|
|
73
|
+
const name = this.name;
|
|
74
|
+
return async function* (...args) {
|
|
75
|
+
const r = await call(args);
|
|
76
|
+
if (Result.isFailure(r)) {
|
|
77
|
+
yield r;
|
|
78
|
+
throw new Panic(`port "${name}" の失敗を yield した後に再開されました`);
|
|
79
|
+
}
|
|
80
|
+
return r.value;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
const port = (name) => ({ of: () => new Port(name, false, new Slots()) });
|
|
85
|
+
const cached = (p) => p.withCache();
|
|
86
|
+
// src/pipeline/requires.ts
|
|
87
|
+
const resolvePorts = (requires, invocation) => mapValues(requires, (p) => p.resolveIn(invocation));
|
|
88
|
+
const unwrapPorts = (requires, invocation) => mapValues(requires, (p) => p.unwrapIn(invocation));
|
|
89
|
+
// src/pipeline/safe-try.ts
|
|
90
|
+
const safeTryWith = (unwrapped) => async (body) => {
|
|
91
|
+
const it = body(unwrapped);
|
|
92
|
+
const step = await it.next();
|
|
93
|
+
if (step.done === true) return step.value;
|
|
94
|
+
await it.return?.();
|
|
95
|
+
return step.value;
|
|
96
|
+
};
|
|
97
|
+
// src/pipeline/when/step.ts
|
|
98
|
+
const objectOf = (value) => typeof value === "object" && value !== null ? value : void 0;
|
|
99
|
+
const nextOf = (value) => {
|
|
100
|
+
const o = objectOf(value);
|
|
101
|
+
return o === void 0 ? void 0 : objectOf(new Map(Object.entries(o)).get("next"));
|
|
102
|
+
};
|
|
103
|
+
const nestedNext = (s, v) => {
|
|
104
|
+
const before = nextOf(s);
|
|
105
|
+
const added = nextOf(v);
|
|
106
|
+
return before !== void 0 && added !== void 0 ? { next: {
|
|
107
|
+
...before,
|
|
108
|
+
...added
|
|
109
|
+
} } : {};
|
|
110
|
+
};
|
|
111
|
+
const merged = (s, v) => Object.assign({}, s, v, nestedNext(s, v));
|
|
112
|
+
const applied = async (s, check) => {
|
|
113
|
+
const r = await check(s);
|
|
114
|
+
return Result.isFailure(r) ? r : Result.succeed(merged(s, r.value));
|
|
115
|
+
};
|
|
116
|
+
const conditionalStep = (test) => async (s, check) => test(s) ? applied(s, check) : Result.succeed(s);
|
|
117
|
+
const always = Object.freeze(Object.assign(() => true, {
|
|
118
|
+
description: { kind: "always" },
|
|
119
|
+
step: applied
|
|
120
|
+
}));
|
|
121
|
+
// src/pipeline/rule.ts
|
|
122
|
+
function rule(track, def) {
|
|
123
|
+
const requires = getOrEmpty(def, "requires");
|
|
124
|
+
const check = def.check;
|
|
125
|
+
const run = async (s, invocation) => check(s, resolvePorts(requires, invocation), { safeTry: safeTryWith(unwrapPorts(requires, invocation)) });
|
|
126
|
+
return Object.freeze({
|
|
127
|
+
kind: "rule",
|
|
128
|
+
track: {
|
|
129
|
+
kind: track.kind,
|
|
130
|
+
target: track.target
|
|
131
|
+
},
|
|
132
|
+
name: def.name,
|
|
133
|
+
ports: Object.values(requires),
|
|
134
|
+
when: Object.freeze(Object.assign((s) => def.when(s), { step: conditionalStep(def.when) }, def.when)),
|
|
135
|
+
run
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
// src/pipeline/when/when.ts
|
|
139
|
+
const whenOf = (description, f) => Object.freeze(Object.assign((s) => f(s), { description }));
|
|
140
|
+
const describeWhen = (w) => w.description ?? { kind: "custom" };
|
|
141
|
+
// src/pipeline/when/builders.ts
|
|
142
|
+
const onCreate = whenOf({ kind: "onCreate" }, (s) => s.pre === null);
|
|
143
|
+
const fieldOf = (o, field) => o[field];
|
|
144
|
+
const matches = (m, v) => m === void 0 || Object.is(m, v);
|
|
145
|
+
const changed = (field, values = {}) => whenOf({
|
|
146
|
+
kind: "changed",
|
|
147
|
+
field,
|
|
148
|
+
...values
|
|
149
|
+
}, (s) => {
|
|
150
|
+
const after = fieldOf(s.next, field);
|
|
151
|
+
if (s.pre === null) return values.pre === void 0 && matches(values.next, after);
|
|
152
|
+
const before = fieldOf(s.pre, field);
|
|
153
|
+
return !Object.is(before, after) && matches(values.pre, before) && matches(values.next, after);
|
|
154
|
+
});
|
|
155
|
+
const next = (predicate) => whenOf({ kind: "next" }, (s) => predicate(s.next));
|
|
156
|
+
const pre = (predicate) => whenOf({ kind: "pre" }, (s) => s.pre !== null && predicate(s.pre));
|
|
157
|
+
const actor = (predicate) => whenOf({ kind: "actor" }, (s) => predicate(s.actor, s));
|
|
158
|
+
const and = (...ws) => {
|
|
159
|
+
const tests = ws;
|
|
160
|
+
return whenOf({
|
|
161
|
+
kind: "and",
|
|
162
|
+
conditions: ws.map(describeWhen)
|
|
163
|
+
}, (s) => tests.every((w) => w(s)));
|
|
164
|
+
};
|
|
165
|
+
const or = (...ws) => {
|
|
166
|
+
const tests = ws;
|
|
167
|
+
return whenOf({
|
|
168
|
+
kind: "or",
|
|
169
|
+
conditions: ws.map(describeWhen)
|
|
170
|
+
}, (s) => tests.some((w) => w(s)));
|
|
171
|
+
};
|
|
172
|
+
const not = (w) => whenOf({
|
|
173
|
+
kind: "not",
|
|
174
|
+
condition: describeWhen(w)
|
|
175
|
+
}, (s) => !w(s));
|
|
176
|
+
const describe = (description, f) => whenOf({
|
|
177
|
+
kind: "custom",
|
|
178
|
+
description
|
|
179
|
+
}, f);
|
|
180
|
+
// src/pipeline/builtin-rules.ts
|
|
181
|
+
const valueOf = (o, f) => o === null ? void 0 : new Map(Object.entries(o)).get(f);
|
|
182
|
+
const immutable = (track, fields) => {
|
|
183
|
+
const changedField = (s) => fields.find((f) => !Object.is(valueOf(s.pre, f), valueOf(s.next, f)));
|
|
184
|
+
return rule(track, {
|
|
185
|
+
name: `作成後に変えられない項目: ${fields.join(", ")}`,
|
|
186
|
+
when: whenOf({
|
|
187
|
+
kind: "and",
|
|
188
|
+
conditions: [{
|
|
189
|
+
kind: "not",
|
|
190
|
+
condition: describeWhen(onCreate)
|
|
191
|
+
}, {
|
|
192
|
+
kind: "or",
|
|
193
|
+
conditions: fields.map((f) => describeWhen(changed(f)))
|
|
194
|
+
}]
|
|
195
|
+
}, (s) => s.pre !== null && changedField(s) !== void 0),
|
|
196
|
+
check: (s) => {
|
|
197
|
+
const field = changedField(s);
|
|
198
|
+
return field === void 0 ? Result.succeed() : Result.fail({
|
|
199
|
+
code: "IMMUTABLE_FIELD_CHANGED",
|
|
200
|
+
field
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
const alwaysAllow = Object.freeze({
|
|
206
|
+
kind: "rule",
|
|
207
|
+
name: "常に許可",
|
|
208
|
+
ports: [],
|
|
209
|
+
when: always,
|
|
210
|
+
run: async () => Result.succeed()
|
|
211
|
+
});
|
|
212
|
+
// src/pipeline/ruleset.ts
|
|
213
|
+
var Ruleset = class Ruleset {
|
|
214
|
+
kind = "ruleset";
|
|
215
|
+
plan;
|
|
216
|
+
ports;
|
|
217
|
+
run;
|
|
218
|
+
constructor(plan, ports, run) {
|
|
219
|
+
this.plan = plan;
|
|
220
|
+
this.ports = ports;
|
|
221
|
+
this.run = run;
|
|
222
|
+
Object.freeze(this);
|
|
223
|
+
}
|
|
224
|
+
add(r) {
|
|
225
|
+
const previous = this.run;
|
|
226
|
+
const ports = r.ports;
|
|
227
|
+
const step = r.when.step;
|
|
228
|
+
return new Ruleset([...this.plan, {
|
|
229
|
+
name: r.name,
|
|
230
|
+
when: describeWhen(r.when),
|
|
231
|
+
ports: ports.map((p) => p.name)
|
|
232
|
+
}], [...this.ports, ...ports], async (s0, invocation) => {
|
|
233
|
+
const prev = await previous(s0, invocation);
|
|
234
|
+
if (Result.isFailure(prev)) return prev;
|
|
235
|
+
return step(prev.value, async (s) => {
|
|
236
|
+
const out = await r.run(s, invocation);
|
|
237
|
+
return Result.isFailure(out) ? Result.fail({
|
|
238
|
+
step: r.name,
|
|
239
|
+
error: out.error
|
|
240
|
+
}) : out;
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const start = (first) => new Ruleset([], [], async (s) => Result.succeed(s)).add(first);
|
|
246
|
+
const ruleset = start;
|
|
247
|
+
// src/method/seal.ts
|
|
248
|
+
var Sealed = class {
|
|
249
|
+
#contents;
|
|
250
|
+
constructor(contents) {
|
|
251
|
+
this.#contents = Object.freeze(contents);
|
|
252
|
+
Object.setPrototypeOf(this, null);
|
|
253
|
+
Object.freeze(this);
|
|
254
|
+
}
|
|
255
|
+
static contents(s) {
|
|
256
|
+
if (!(#contents in s)) throw new Panic("open: メソッドが作った封をした値ではありません");
|
|
257
|
+
return s.#contents;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const seal = (contents) => new Sealed(contents);
|
|
261
|
+
const open = (s) => Sealed.contents(s);
|
|
262
|
+
// src/method/validate.ts
|
|
263
|
+
const inputFailure = (issues) => ({
|
|
264
|
+
step: "input",
|
|
265
|
+
error: { issues }
|
|
266
|
+
});
|
|
267
|
+
const validate = async (schema, value, path = []) => {
|
|
268
|
+
const r = await schema["~standard"].validate(value);
|
|
269
|
+
return r.issues !== void 0 ? Result.fail(r.issues.map((i) => ({
|
|
270
|
+
...i,
|
|
271
|
+
path: [...path, ...i.path ?? []]
|
|
272
|
+
}))) : Result.succeed(r.value);
|
|
273
|
+
};
|
|
274
|
+
const unknownKeys = (entries, input, allowed) => Object.keys(input).filter((key) => !Object.hasOwn(entries, key) || !allowed(key)).map((key) => ({
|
|
275
|
+
message: `schema にない項目です: ${key}`,
|
|
276
|
+
path: [key]
|
|
277
|
+
}));
|
|
278
|
+
const definedEntries = (input) => Object.fromEntries(Object.entries(input).filter(([, v]) => v !== void 0));
|
|
279
|
+
const validateWhole = async (schema, input) => {
|
|
280
|
+
const defined = definedEntries(input);
|
|
281
|
+
const unknown = unknownKeys(schema.entries, defined, (key) => key !== "id");
|
|
282
|
+
const r = await validate(schema, defined);
|
|
283
|
+
const issues = [...unknown, ...Result.isFailure(r) ? r.error : []];
|
|
284
|
+
if (issues.length > 0 || Result.isFailure(r)) return Result.fail(inputFailure(issues));
|
|
285
|
+
return Result.succeed(r.value);
|
|
286
|
+
};
|
|
287
|
+
const validatePartial = async (entries, input) => {
|
|
288
|
+
const issues = unknownKeys(entries, input, () => true);
|
|
289
|
+
const defined = definedEntries(input);
|
|
290
|
+
const out = {};
|
|
291
|
+
for (const key in entries) {
|
|
292
|
+
if (!Object.hasOwn(defined, key)) continue;
|
|
293
|
+
const r = await validate(entries[key], defined[key], [key]);
|
|
294
|
+
if (Result.isFailure(r)) issues.push(...r.error);
|
|
295
|
+
else out[key] = r.value;
|
|
296
|
+
}
|
|
297
|
+
return issues.length > 0 ? Result.fail(inputFailure(issues)) : Result.succeed(out);
|
|
298
|
+
};
|
|
299
|
+
// src/method/action-method.ts
|
|
300
|
+
const actionMethods = (ctx, input, rules) => {
|
|
301
|
+
const decide = async ({ input: value, actor }) => {
|
|
302
|
+
const validated = await validate(input, value);
|
|
303
|
+
if (Result.isFailure(validated)) return Result.fail(inputFailure(validated.error));
|
|
304
|
+
const inv = ctx.invocation();
|
|
305
|
+
const r = await rules.run({
|
|
306
|
+
input: validated.value,
|
|
307
|
+
actor,
|
|
308
|
+
now: inv.now
|
|
309
|
+
}, inv);
|
|
310
|
+
if (Result.isFailure(r)) return r;
|
|
311
|
+
return Result.succeed(seal(r.value));
|
|
312
|
+
};
|
|
313
|
+
return Object.freeze({ decide });
|
|
314
|
+
};
|
|
315
|
+
// src/rule-set/action-rules.ts
|
|
316
|
+
const actorOf = (x) => x.actor;
|
|
317
|
+
function actionRules(a, def) {
|
|
318
|
+
const { rules, output } = def;
|
|
319
|
+
const decided = {
|
|
320
|
+
...rules,
|
|
321
|
+
run: async (s, call) => {
|
|
322
|
+
const r = await rules.run(s, call);
|
|
323
|
+
return Result.isFailure(r) ? r : Result.succeed({
|
|
324
|
+
value: output(r.value),
|
|
325
|
+
actor: actorOf(r.value)
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
return Object.freeze({
|
|
330
|
+
kind: "actionRules",
|
|
331
|
+
name: a.name,
|
|
332
|
+
action: a,
|
|
333
|
+
rules: Object.freeze(decided),
|
|
334
|
+
resolveIn: (ctx) => actionMethods(ctx, a.input, decided)
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
// src/method/model-methods.ts
|
|
338
|
+
const load = async (inv, port, id) => {
|
|
339
|
+
const r = await port.callIn(inv, [id]);
|
|
340
|
+
if (Result.isFailure(r)) return Result.fail({
|
|
341
|
+
step: "load",
|
|
342
|
+
error: r.error
|
|
343
|
+
});
|
|
344
|
+
const existing = r.value;
|
|
345
|
+
return existing === null || existing === void 0 ? Result.fail({
|
|
346
|
+
step: "load",
|
|
347
|
+
error: "NOT_FOUND"
|
|
348
|
+
}) : Result.succeed(existing);
|
|
349
|
+
};
|
|
350
|
+
const saveWith = async (rules, inv, pre, next, actor) => {
|
|
351
|
+
const r = await rules.run({
|
|
352
|
+
pre,
|
|
353
|
+
next,
|
|
354
|
+
actor,
|
|
355
|
+
now: inv.now
|
|
356
|
+
}, inv);
|
|
357
|
+
if (Result.isFailure(r)) return r;
|
|
358
|
+
return Result.succeed({
|
|
359
|
+
next: Object.freeze(r.value.next),
|
|
360
|
+
actor: r.value.actor
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
const createIn = async (ctx, model, rules, input, actor) => {
|
|
364
|
+
const next = await validateWhole(model.schema.input, input);
|
|
365
|
+
if (Result.isFailure(next)) return next;
|
|
366
|
+
const r = await saveWith(rules, ctx.invocation(), null, next.value, actor);
|
|
367
|
+
if (Result.isFailure(r)) return r;
|
|
368
|
+
return Result.succeed(seal({
|
|
369
|
+
pre: null,
|
|
370
|
+
...r.value
|
|
371
|
+
}));
|
|
372
|
+
};
|
|
373
|
+
const loadExisting = async (ctx, model, port, input) => {
|
|
374
|
+
const validated = await validatePartial(model.schema.input.entries, input);
|
|
375
|
+
if (Result.isFailure(validated)) return validated;
|
|
376
|
+
const id = validated.value.id;
|
|
377
|
+
if (id === void 0) return Result.fail(inputFailure([{
|
|
378
|
+
message: "id は必須です",
|
|
379
|
+
path: ["id"]
|
|
380
|
+
}]));
|
|
381
|
+
const inv = ctx.invocation();
|
|
382
|
+
const pre = await load(inv, port, id);
|
|
383
|
+
if (Result.isFailure(pre)) return pre;
|
|
384
|
+
return Result.succeed({
|
|
385
|
+
inv,
|
|
386
|
+
pre: pre.value,
|
|
387
|
+
validated: validated.value
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
const updateIn = async (ctx, model, port, rules, input, actor) => {
|
|
391
|
+
const existing = await loadExisting(ctx, model, port, input);
|
|
392
|
+
if (Result.isFailure(existing)) return existing;
|
|
393
|
+
const { inv, pre, validated } = existing.value;
|
|
394
|
+
const next = {
|
|
395
|
+
...pre,
|
|
396
|
+
...validated
|
|
397
|
+
};
|
|
398
|
+
const r = await saveWith(rules, inv, pre, next, actor);
|
|
399
|
+
if (Result.isFailure(r)) return r;
|
|
400
|
+
return Result.succeed(seal({
|
|
401
|
+
pre: Object.freeze({ ...pre }),
|
|
402
|
+
...r.value
|
|
403
|
+
}));
|
|
404
|
+
};
|
|
405
|
+
const createMethod = (model, rules) => (ctx) => ({ input, actor }) => createIn(ctx, model, rules, input, actor);
|
|
406
|
+
const updateMethod = (model, port, rules) => (ctx) => ({ input, actor }) => updateIn(ctx, model, port, rules, input, actor);
|
|
407
|
+
const upsertMethod = (model, port, rules) => (ctx) => ({ input, actor }) => {
|
|
408
|
+
return input["id"] === void 0 ? createIn(ctx, model, rules, input, actor) : updateIn(ctx, model, port, rules, input, actor);
|
|
409
|
+
};
|
|
410
|
+
const deleteMethod = (model, port, rules) => (ctx) => async ({ input, actor }) => {
|
|
411
|
+
const existing = await loadExisting(ctx, model, port, input);
|
|
412
|
+
if (Result.isFailure(existing)) return existing;
|
|
413
|
+
const { inv, pre } = existing.value;
|
|
414
|
+
const r = await rules.run({
|
|
415
|
+
pre,
|
|
416
|
+
next: null,
|
|
417
|
+
actor,
|
|
418
|
+
now: inv.now
|
|
419
|
+
}, inv);
|
|
420
|
+
if (Result.isFailure(r)) return r;
|
|
421
|
+
return Result.succeed(seal({
|
|
422
|
+
pre: Object.freeze({ ...pre }),
|
|
423
|
+
actor: r.value.actor
|
|
424
|
+
}));
|
|
425
|
+
};
|
|
426
|
+
// src/rule-set/model-rules.ts
|
|
427
|
+
var ModelRulesDone = class {
|
|
428
|
+
kind = "modelRules";
|
|
429
|
+
name;
|
|
430
|
+
plans;
|
|
431
|
+
ports;
|
|
432
|
+
resolveIn;
|
|
433
|
+
constructor(name, plans, ports, resolveIn) {
|
|
434
|
+
this.name = name;
|
|
435
|
+
this.plans = plans;
|
|
436
|
+
this.ports = ports;
|
|
437
|
+
this.resolveIn = resolveIn;
|
|
438
|
+
Object.freeze(this);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
var ModelRulesStart = class {
|
|
442
|
+
kind = "modelRules";
|
|
443
|
+
name;
|
|
444
|
+
model;
|
|
445
|
+
plans = {};
|
|
446
|
+
ports = [];
|
|
447
|
+
resolveIn = () => Object.freeze({});
|
|
448
|
+
constructor(model) {
|
|
449
|
+
this.name = model.name;
|
|
450
|
+
this.model = model;
|
|
451
|
+
Object.freeze(this);
|
|
452
|
+
}
|
|
453
|
+
load(port) {
|
|
454
|
+
return new ModelRulesLoaded(this.model, port, {}, [port], this.resolveIn);
|
|
455
|
+
}
|
|
456
|
+
save(rules) {
|
|
457
|
+
const create = createMethod(this.model, rules);
|
|
458
|
+
return new ModelRulesDone(this.name, { save: rules.plan }, rules.ports, (ctx) => Object.freeze({ create: create(ctx) }));
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
var ModelRulesLoaded = class ModelRulesLoaded {
|
|
462
|
+
kind = "modelRules";
|
|
463
|
+
name;
|
|
464
|
+
model;
|
|
465
|
+
port;
|
|
466
|
+
plans;
|
|
467
|
+
ports;
|
|
468
|
+
resolveIn;
|
|
469
|
+
constructor(model, port, plans, ports, resolveIn) {
|
|
470
|
+
this.name = model.name;
|
|
471
|
+
this.model = model;
|
|
472
|
+
this.port = port;
|
|
473
|
+
this.plans = plans;
|
|
474
|
+
this.ports = ports;
|
|
475
|
+
this.resolveIn = resolveIn;
|
|
476
|
+
Object.freeze(this);
|
|
477
|
+
}
|
|
478
|
+
save(rules) {
|
|
479
|
+
const { model, port, resolveIn } = this;
|
|
480
|
+
const create = createMethod(model, rules);
|
|
481
|
+
const update = updateMethod(model, port, rules);
|
|
482
|
+
const upsert = upsertMethod(model, port, rules);
|
|
483
|
+
return new ModelRulesLoaded(model, port, {
|
|
484
|
+
...this.plans,
|
|
485
|
+
save: rules.plan
|
|
486
|
+
}, [...this.ports, ...rules.ports], methods);
|
|
487
|
+
function methods(ctx) {
|
|
488
|
+
return Object.freeze({
|
|
489
|
+
...resolveIn(ctx),
|
|
490
|
+
create: create(ctx),
|
|
491
|
+
update: update(ctx),
|
|
492
|
+
upsert: upsert(ctx)
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
delete(rules) {
|
|
497
|
+
const { model, port, resolveIn } = this;
|
|
498
|
+
const remove = deleteMethod(model, port, rules);
|
|
499
|
+
return new ModelRulesLoaded(model, port, {
|
|
500
|
+
...this.plans,
|
|
501
|
+
delete: rules.plan
|
|
502
|
+
}, [...this.ports, ...rules.ports], methods);
|
|
503
|
+
function methods(ctx) {
|
|
504
|
+
return Object.freeze({
|
|
505
|
+
...resolveIn(ctx),
|
|
506
|
+
delete: remove(ctx)
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
const modelRules = (m) => new ModelRulesStart(m);
|
|
512
|
+
// src/rule-set/rule-set.ts
|
|
513
|
+
function plan(set) {
|
|
514
|
+
return set.kind === "actionRules" ? { rules: set.rules.plan } : set.plans;
|
|
515
|
+
}
|
|
516
|
+
// src/bind.ts
|
|
517
|
+
function bind(sets, options) {
|
|
518
|
+
const scope = {};
|
|
519
|
+
for (const p of options.providers) p.registerIn(scope);
|
|
520
|
+
const names = /* @__PURE__ */ new Set();
|
|
521
|
+
for (const set of sets) {
|
|
522
|
+
if (names.has(set.name)) throw new Panic(`bind: "${set.name}" の rule の組が重複しています`);
|
|
523
|
+
names.add(set.name);
|
|
524
|
+
const missing = requiredPorts(set).filter((port) => !port.providedIn(scope)).map((port) => port.name);
|
|
525
|
+
if (missing.length > 0) throw new Panic(`bind: provider が足りない port: ${missing.join(", ")}`);
|
|
526
|
+
}
|
|
527
|
+
const ctx = { invocation: () => invocation(scope) };
|
|
528
|
+
return fromEntries(mapValues(sets, (set) => [set.name, set.resolveIn(ctx)]));
|
|
529
|
+
}
|
|
530
|
+
const requiredPorts = (set) => set.kind === "modelRules" ? [clock, ...set.ports] : [clock, ...set.rules.ports];
|
|
531
|
+
const invocation = (scope) => {
|
|
532
|
+
const now = clock.nowIn(scope);
|
|
533
|
+
if (now === void 0) throw new Panic("bind: clock の provider がありません");
|
|
534
|
+
return {
|
|
535
|
+
scope,
|
|
536
|
+
now
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
// src/definition/track.ts
|
|
540
|
+
var Track = class Track {
|
|
541
|
+
kind;
|
|
542
|
+
target;
|
|
543
|
+
constructor(kind, target) {
|
|
544
|
+
this.kind = kind;
|
|
545
|
+
this.target = target;
|
|
546
|
+
Object.freeze(this);
|
|
547
|
+
}
|
|
548
|
+
assuming() {
|
|
549
|
+
return new Track(this.kind, this.target);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
// src/definition/action.ts
|
|
553
|
+
const defineAction = () => (def) => Object.freeze({
|
|
554
|
+
kind: "action",
|
|
555
|
+
name: def.name,
|
|
556
|
+
input: def.input,
|
|
557
|
+
onDecide: new Track("decide", def.name)
|
|
558
|
+
});
|
|
559
|
+
// src/definition/model.ts
|
|
560
|
+
const defineModel = () => (def) => Object.freeze({
|
|
561
|
+
kind: "model",
|
|
562
|
+
name: def.name,
|
|
563
|
+
schema: def.schema,
|
|
564
|
+
onSave: new Track("save", def.name),
|
|
565
|
+
onDelete: new Track("delete", def.name)
|
|
566
|
+
});
|
|
567
|
+
// src/domain-definition-kit.ts
|
|
568
|
+
const domainDefinitionKit = () => Object.freeze({
|
|
569
|
+
model: defineModel(),
|
|
570
|
+
action: defineAction(),
|
|
571
|
+
actor: (predicate) => actor(predicate)
|
|
572
|
+
});
|
|
573
|
+
export { actionRules, always, alwaysAllow, and, bind, cached, changed, clock, describe, domainDefinitionKit, immutable, modelRules, next, not, onCreate, open, or, plan, port, pre, provide, rule, ruleset };
|
package/package.json
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "domain-rules",
|
|
3
|
-
"version": "0.0.0",
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"description": "Define business rules in small units and guarantee validated data with TypeScript types.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/9wick/domain-rules.git"
|
|
9
|
+
},
|
|
6
10
|
"files": [
|
|
7
|
-
"
|
|
11
|
+
"dist"
|
|
8
12
|
],
|
|
9
|
-
"
|
|
10
|
-
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"default": "./dist/index.mjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@standard-schema/spec": "1.1.0",
|
|
25
|
+
"as-free": "0.0.2-beta.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@praha/byethrow": "0.13.0",
|
|
29
|
+
"typescript": "7.0.2",
|
|
30
|
+
"valibot": "1.5.0",
|
|
31
|
+
"vite-plus": "1.5.1",
|
|
32
|
+
"vitest": "5.1.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@praha/byethrow": "0.13.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "vp pack",
|
|
39
|
+
"check": "vp check",
|
|
40
|
+
"test": "vp test run"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
throw new Error('This version is not intended for use.');
|