-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.ts
104 lines (84 loc) · 2.05 KB
/
example.ts
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
import { type CursorConfig, generateCursor } from "../src";
import { db, schema } from "./db";
const page_size = 10;
const cursorConfig: CursorConfig = {
cursors: [
{ order: "ASC", key: "lastName", schema: schema.users.lastName },
{ order: "ASC", key: "firstName", schema: schema.users.firstName },
{ order: "ASC", key: "middleName", schema: schema.users.middleName },
],
primaryCursor: { order: "ASC", key: "id", schema: schema.users.id },
};
async function main() {
const cursor = generateCursor(cursorConfig);
const data1 = await db
.select({
lastName: schema.users.lastName,
firstName: schema.users.firstName,
middleName: schema.users.middleName,
id: schema.users.id,
})
.from(schema.users)
.orderBy(...cursor.orderBy)
.where(cursor.where())
.limit(page_size);
const last1 = data1.at(-1);
if (!last1) {
return;
}
console.log(data1);
const data2 = await db
.select({
lastName: schema.users.lastName,
firstName: schema.users.firstName,
middleName: schema.users.middleName,
id: schema.users.id,
})
.from(schema.users)
.orderBy(...cursor.orderBy)
.where(cursor.where(last1))
.limit(page_size);
const last2 = data2.at(-1);
if (!last2) {
return;
}
console.log("-- 2 --");
console.log(data2);
const data3 = await db.query.users.findMany({
columns: {
lastName: true,
firstName: true,
middleName: true,
id: true,
},
orderBy: cursor.orderBy,
where: cursor.where(last2),
limit: page_size,
});
const last3 = data3.at(-1);
if (!last3) {
return;
}
console.log("-- 3 --");
console.log(data3);
const last3CursorString = cursor.serialize(last3);
const data4 = await db
.select({
lastName: schema.users.lastName,
firstName: schema.users.firstName,
middleName: schema.users.middleName,
id: schema.users.id,
})
.from(schema.users)
.orderBy(...cursor.orderBy)
.where(cursor.where(last3CursorString))
.limit(page_size);
const last4 = data4.at(-1);
if (!last4) {
return;
}
console.log("-- 4 --");
console.log({ last3CursorString });
console.log(data4);
}
main();