Repository navigation
Expand file tree
/
Copy pathsort.ts
More file actions
71 lines (65 loc) · 1.5 KB
/
Copy pathsort.ts
File metadata and controls
71 lines (65 loc) · 1.5 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
/**
* Show how to create a sorting secondary index
*/
import { deleteAllRecords, showRecords } from "./util.ts";
// User type
interface User {
id: string;
firstName: string;
lastName: string;
email: string;
role: string;
}
// An array of example users
const users: User[] = [
{
id: crypto.randomUUID(),
firstName: "John",
lastName: "Dow",
email: "john@example.com",
role: "user",
},
{
id: crypto.randomUUID(),
firstName: "Barbara",
lastName: "Bowie",
email: "barb@example.com",
role: "user",
},
{
id: crypto.randomUUID(),
firstName: "Jane",
lastName: "Dow",
email: "jane@example.com",
role: "user",
},
{
id: crypto.randomUUID(),
firstName: "Annie",
lastName: "Anderson",
email: "annie@example.com",
role: "user",
},
{
id: crypto.randomUUID(),
firstName: "Fred",
lastName: "Foobar",
email: "fredk@example.com",
role: "admin",
},
];
console.log("DELETE ALL PREVIOUS RECORDS");
await deleteAllRecords();
// open a connection to Deno KV
const kv = await Deno.openKv();
for await (const user of users) {
await kv.atomic()
// no checks sorry!
.set(["users", user.id], user) // primary index
.set(["user_by_name", user.lastName, user.firstName], user) // secondary sorting index
.commit();
}
console.log("UNSORTED USERS");
await showRecords<User>(["users"], ["firstName", "lastName"]);
console.log("SORTED BY NAME");
await showRecords<User>(["user_by_name"], ["firstName", "lastName"]);