-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique-writes.ts
More file actions
69 lines (62 loc) · 1.15 KB
/
unique-writes.ts
File metadata and controls
69 lines (62 loc) · 1.15 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
import { z } from "zod";
import { kind, kindstore } from "kindstore";
const User = z.object({
email: z.string(),
name: z.string(),
accountId: z.string(),
role: z.enum(["owner", "member"]),
});
const db = kindstore({
filename: ":memory:",
schema: {
users: kind("usr", User).createdAt().updatedAt().index("email", { unique: true }).multi(
"account_role",
{
accountId: "asc",
role: "asc",
},
{ unique: true },
),
},
});
const created = db.users.putByUnique(
{
email: "jane@example.com",
},
{
email: "jane@example.com",
name: "Jane",
accountId: "acct_1",
role: "owner",
},
);
const replaced = db.users.putByUnique(
{
email: "jane@example.com",
},
{
email: "jane@example.com",
name: "Jane Doe",
accountId: "acct_1",
role: "owner",
},
);
const createdByComposite = db.users.putByUnique(
{
accountId: "acct_2",
role: "member",
},
{
email: "john@example.com",
name: "John",
accountId: "acct_2",
role: "member",
},
);
console.log({
created,
replaced,
createdByComposite,
sameId: created.id === replaced.id,
});
db.close();