Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
101 lines (92 loc) · 3.51 KB
/
Copy pathserver.ts
File metadata and controls
101 lines (92 loc) · 3.51 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
/**
* The server's half: the tables, how a guard's body is decided, and guarded
* writers pinned to the revision their rules were decided over, one of them
* keeping a ledger of the writes it lands.
*/
import { integer, primaryKey, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { Option } from 'effect'
import { GuardDrizzle } from 'foldkit-guard/drizzle'
import { GuardServer } from 'foldkit-guard/server'
import { Remote } from 'foldkit-remote'
import {
bind,
checkConstraints,
source,
writer,
type DrizzleDatabase,
} from 'foldkit-remote-drizzle'
import { RemoteServer } from 'foldkit-remote-server'
import { Product, Publish, Rename, Restock, Revert, Sell, Shop, type User } from './domain.js'
export const products = sqliteTable('products', {
id: text('id').primaryKey(),
ownerId: text('owner_id').notNull(),
name: text('name').notNull(),
status: text('status').notNull(),
stock: integer('stock').notNull(),
reserved: integer('reserved').notNull(),
revision: integer('revision').notNull(),
})
/** Each write the ledgered writer landed, by whose operation it was. */
export const ledger = sqliteTable(
'write_ledger',
{
scope: text('scope').notNull(),
operation: text('operation').notNull(),
target: text('target').notNull(),
answer: text('answer').notNull(),
at: integer('at').notNull(),
request: text('request'),
},
table => [primaryKey({ columns: [table.scope, table.operation, table.target] })],
)
// Every guard is a row guard, decided in SQL: there is no principal guard to implement.
export const Policy = Shop.implement({})
const Db = bind(
{ Product },
{ Product: { table: products, visible: GuardDrizzle.visible(Policy, [Product]).Product } },
)
// The domain's rules as constraints too, so raw SQL is held to them as the writer is.
export const schema = `
create table products (
id text primary key, owner_id text not null, name text not null,
status text not null, stock integer not null, reserved integer not null,
revision integer not null,
${checkConstraints(Db.Product)
.constraints.map(check => check.constraint)
.join(',\n ')}
);
create table write_ledger (
scope text not null, operation text not null, target text not null, answer text not null, at integer not null, request text,
primary key (scope, operation, target)
)`
// Each update and delete lands only on the revision its rules were decided over.
const guarded = GuardServer.writer(Product, Policy, writer(Db.Product), { revision: 'revision' })
// The same, recording each update under its operation, scoped to who asked.
const ledgered = GuardServer.writer(
Product,
Policy,
writer<Option.Option<User>>(Db.Product, {
ledger: {
table: ledger,
scope: principal => Option.match(principal, { onNone: () => '', onSome: user => user.id }),
},
}),
{ revision: 'revision' },
)
export const server = RemoteServer.make<Option.Option<User>, DrizzleDatabase>({
// A product read back, as its owner may see it: a page reads what it writes.
entities: [source(Db.Product, { filter: GuardServer.filter(Product, Policy) })],
mutations: [
RemoteServer.write(Rename, guarded),
RemoteServer.write(Publish, guarded),
RemoteServer.write(Revert, guarded),
RemoteServer.write(Restock, guarded),
RemoteServer.write(Sell, ledgered),
],
})
// At startup: refuses a write past its rules or reading a hidden member; warns if unpinned.
GuardServer.validate(Policy, {
entities: [Product],
domain: Remote.define({ entities: [Product] }),
server,
})