Skip to content

Commit

Permalink
add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
lambtron committed May 23, 2023
1 parent b8db99a commit 6654b91
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions with-oak-deno-kv/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export async function updateUserAndAddress(user: User, address: Address) {
const userByEmailKey = ["user_by_email", user.email];
const addressKey = ["user_address", user.id];

const [userRes, userFromEmailRes, addressRes] = await kv.getMany([userKey, userByEmailKey, addressKey]);
const [userRes, userFromEmailRes, addressRes] = await kv.getMany([
userKey,
userByEmailKey,
addressKey,
]);

await kv.atomic()
.check(userRes)
Expand All @@ -59,15 +63,17 @@ export async function updateUserAndAddress(user: User, address: Address) {
}

/**
* Get all users.
* Get all users with pagination.
* @returns <User>
*/

export async function getAllUsers() {
const iter = await kv.list<User>({ prefix: ["user"] });
let iter = await kv.list<User>({ prefix: ["user"] });
const users = [];
for await (const res of iter) {
if (res.value.id) users.push(res.value);
for await (const res of iter) users.push(res.value);
while (iter.cursor) {
iter = await kv.list<User>({ prefix: ["user"] }, { cursor: iter.cursor });
for await (const res of iter) users.push(res.value);
}
return users;
}
Expand Down Expand Up @@ -116,7 +122,10 @@ export async function deleteUserById(id: string) {
const userByEmailKey = ["user_by_email", userRes.value.email];
const addressKey = ["user_address", id];

const [userFromEmailRes, addressRes] = await kv.getMany([userByEmailKey, addressKey]);
const [userFromEmailRes, addressRes] = await kv.getMany([
userByEmailKey,
addressKey,
]);

await kv.atomic()
.check(userRes)
Expand Down

0 comments on commit 6654b91

Please sign in to comment.