Repository navigation
Expand file tree
/
Copy pathdomain.ts
More file actions
134 lines (122 loc) · 5.07 KB
/
Copy pathdomain.ts
File metadata and controls
134 lines (122 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* What client and server share: one Entity, who may change it, what every row
* of it holds, what every change of it holds, its states, and five declared
* writes. Nothing here runs a query or touches a table.
*/
import { Schema } from 'effect'
import { Entity, Expr, Write } from 'foldkit-entity'
import { Guards } from 'foldkit-guard'
import { Mutation } from 'foldkit-remote'
export const User = Schema.Struct({ id: Schema.String })
export type User = typeof User.Type
// The descriptor the guard rules are declared over. It judges the invariants as `Product` does,
// but carries no guards: a server binds `Product`, and a guard seam refuses this one by name.
const ProductFields = Entity.define(
'Product',
Schema.Struct({
id: Schema.String,
ownerId: Schema.String,
name: Schema.String,
status: Schema.Literals(['draft', 'published', 'archived']),
stock: Schema.Number,
reserved: Schema.Number,
revision: Schema.Number,
}),
)
export const Shop = Guards.declare(
'Shop',
User,
guard => ({
isOwner: guard.row(ProductFields, user => Expr.eq(ProductFields.fields.ownerId, user.id)),
}),
{ key: user => user.id, unguarded: 'allow' },
)
/** What every product holds, whoever writes it: no more reserved than is in stock. */
export const reservedWithinStock = Entity.invariant<typeof ProductFields>(
'reservedWithinStock',
({ after }) => Expr.lte(after.reserved, after.stock),
{ message: 'Reserved stock cannot exceed the stock', path: ['reserved'] },
)
/** A draft and a published product are both valid; going from one back to the other is not. */
export const publishedStaysPublished = Entity.invariant<typeof ProductFields>(
'publishedStaysPublished',
({ before, after }) =>
Expr.or(
Expr.not(Expr.eq(before.status, 'published')),
Expr.not(Expr.eq(after.status, 'draft')),
),
{ message: 'A published product does not go back to draft', path: ['status'] },
)
/**
* A product is its owner's to read and to change, and whoever changes it holds
* its invariants: who may is a rule, what every row and change holds an invariant.
*/
export const Product = ProductFields.pipe(
Guards.attach(Shop, { row: { Read: [Shop.isOwner], Update: [Shop.isOwner] } }),
reservedWithinStock,
publishedStaysPublished,
)
/** A product's states, declared once: what a write starts from and leaves, and what a query asks for. */
export const Draft = Entity.refine(Product, 'Draft', { status: 'draft' })
export const Published = Entity.refine(Product, 'Published', { status: 'published' })
export const Archived = Entity.refine(Product, 'Archived', { status: 'archived' })
/**
* Stock left over what is reserved: over numbers, so a write declared to leave
* a product in it is held to that where it commits, not proved where declared.
*/
export const InStock = Entity.refine(Product, 'InStock', ({ row }) =>
Expr.gt(row.stock, row.reserved),
)
/** A guarded rename: the rules decide who may, the writer's revision pin when it lands. */
export const Rename = Mutation.update('Rename', Product, { set: { name: Schema.String } })
/**
* A transition that makes sense from one state only: a draft is published.
* An archived product is not; a published one already is, so publishing it
* again changes nothing and is answered as written, as a retry would be.
* That it leaves the product published is proved where it is declared.
*/
export const Publish = Mutation.update('Publish', Product, {
set: { status: Schema.Literal('published') },
from: Draft,
becomes: Published,
})
/** Sets a product back to draft, which its transition invariant allows only of one never published. */
export const Revert = Mutation.update('Revert', Product, {
set: { status: Schema.Literal('draft') },
})
/**
* A computed write: the stock moves by the quantity where it commits. It
* expects the revision it was read at, so a retry of one that landed is a
* conflict, never a second restock. Its `quantity` is no field but an amount
* (`Entity.unmapped`), so it is written out: the input, then the write, then
* the mutation.
*/
export const Restock = Mutation.write(
'Restock',
Write.update(
Entity.input(
Product,
Schema.Struct({ id: Schema.String, revision: Schema.Number, quantity: Schema.Int }),
{ quantity: Entity.unmapped },
),
{ id: 'id', expect: 'revision', increment: { stock: 'quantity' } },
),
)
/**
* A sale takes from the stock where it commits, expecting no revision: two
* sales made from one read both land. The server's writer for it keeps a
* ledger, so a retry of a sale is never sold twice; it is answered with the row
* as it now is. It leaves some stock unreserved (`becomes: InStock`): a stock
* moved by an amount cannot be enumerated, so the writer holds the sale to it
* in the statement, and refuses one that would leave none as `Invalid`, rule
* `becomes:InStock`.
*/
export const Sell = Mutation.write(
'Sell',
Write.update(
Entity.input(Product, Schema.Struct({ id: Schema.String, quantity: Schema.Int }), {
quantity: Entity.unmapped,
}),
{ id: 'id', increment: { stock: 'quantity' }, becomes: InStock },
),
)