Repository navigation
Expand file tree
/
Copy pathsort-dup.ts
More file actions
79 lines (73 loc) · 2.21 KB
/
Copy pathsort-dup.ts
File metadata and controls
79 lines (73 loc) · 2.21 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
/**
* Sorting records with duplicates. In this case, we want to sort by name but there are duplicate names
* so we add the id to the sort key so that all of the duplicates will be presented in the results.
* If the id were not part of the sort key, only the first record in the index will be processed.
* See the last two console prints for a demonstration.
*/
import { deleteAllRecords, showRecords } from "./util.ts";
// User type
interface User {
id: string;
lastName: string;
firstName: string;
}
// An array of example users
const users: User[] = [
{
id: crypto.randomUUID(),
lastName: "Smith",
firstName: "John",
},
{
id: crypto.randomUUID(),
lastName: "Dow",
firstName: "Josephine",
},
{
id: crypto.randomUUID(),
lastName: "Smith",
firstName: "Jackie",
},
{
id: crypto.randomUUID(),
lastName: "Smith",
firstName: "Jean",
},
{
id: crypto.randomUUID(),
lastName: "Dow",
firstName: "Joe",
},
{
id: crypto.randomUUID(),
lastName: "Smith",
firstName: "Jean",
},
];
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(["sort_by_full_name", user.lastName, user.firstName], user) // secondary sorting index
.set(["sort_by_last_name_id", user.lastName, user.id], user) // secondary sorting index
.set(["sort_by_last_name", user.lastName], user) // broken secondary sorting index
.commit();
}
console.log("SHOW UNSORTED USERS");
await showRecords<User>(["users"], ["firstName", "lastName", "id"]);
console.log("USERS SORT BY LAST AND FIRST NAME WITH ID");
await showRecords<User>(["sort_by_full_name"], ["firstName", "lastName", "id"]);
console.log("USERS SORT BY LAST NAME WITH ID");
await showRecords<User>(["sort_by_last_name_id"], [
"firstName",
"lastName",
"id",
]);
// create a `sort_by_last_name` index for sort by last name
// NOTE: The duplicates are missed and not printed out
console.log("USER SORT BY LAST NAME WITHOUT ID");
await showRecords<User>(["sort_by_last_name"], ["firstName", "lastName", "id"]);