Skip to content

Commit 3afeaad

Browse files
committed
change removeMany to return the number of items deleted
1 parent 6e5f6ec commit 3afeaad

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

packages/db/src/core/remove.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function internalRemove<T extends Entity<T>, P extends PrimaryKeyOf<T>>(
3232
table: Table<T, P>,
3333
entity: Ids<T, P>
3434
): Promise<boolean> {
35-
return internalRemoveMany(table, [entity]);
35+
return internalRemoveMany(table, [entity]).then((n) => n === 1);
3636
}
3737

3838
/**

packages/db/src/core/removeMany.spec.ts

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,15 @@ beforeEach(async () => {
2424
await insertMany(userTable, users);
2525
});
2626

27-
it("should return false if the primary key was not found", async () => {
28-
const n = (await many(userTable)).length
29-
const res = await removeMany(userTable, [{ id: "1337" }, { id: users[0].id }]);
30-
31-
expect(res).toBe(false);
32-
expect((await many(userTable)).length).toBe(n-1);
33-
});
34-
35-
it("should return false if one of the primary keys was not found", async () => {
36-
const res = await removeMany(userTable, [{ id: "1" }, { id: "2" }, { id: "1337" }]);
37-
38-
expect(res).toBe(false);
39-
});
40-
41-
it("should return true if the primary key was found", async () => {
42-
const res = await removeMany(userTable, [{ id: "1" }]);
43-
44-
expect(res).toBe(true);
45-
});
46-
47-
it("should return true if all of the primary keys were found", async () => {
48-
const res = await removeMany(userTable, [{ id: "1" }, { id: "2" }, { id: "5" }]);
49-
50-
expect(res).toBe(true);
27+
it("should return the number of deleted entities", async () => {
28+
const res = await removeMany(userTable, [
29+
{ id: "0" },
30+
{ id: "0" },
31+
{ id: "1" },
32+
{ id: "1337" },
33+
]);
34+
35+
expect(res).toBe(2);
5136
});
5237

5338
it("should remove an entity", async () => {
@@ -67,7 +52,7 @@ it("should remove entities", async () => {
6752
it("should correctly remove an entity from a table with index", async () => {
6853
const firstUser = await one(userTable, { where: { id: "30" } });
6954

70-
await removeMany(userTable, [firstUser]);
55+
await removeMany(userTable, [firstUser, { id: "1337" }]);
7156

7257
const userWithIndex = await first(userTable, { where: { id: firstUser.id } });
7358
expect(userWithIndex).not.toStrictEqual(firstUser);

packages/db/src/core/removeMany.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Ids } from "./remove";
77
/**
88
* Removes the given `entities` from the `table`.
99
*
10-
* @returns true if all entities were found and removed, false otherwise.
10+
* @returns the number of removed entities.
1111
*
1212
* @example
1313
* const db = createDB();
@@ -19,7 +19,7 @@ import { Ids } from "./remove";
1919
export function removeMany<T extends Entity<T>, P extends PrimaryKeyOf<T>>(
2020
table: Table<T, P>,
2121
entities: Ids<T, P>[]
22-
): Promise<boolean> {
22+
): Promise<number> {
2323
return Promise.resolve(
2424
middleware<T, P, "removeMany">(
2525
table,
@@ -32,20 +32,17 @@ export function removeMany<T extends Entity<T>, P extends PrimaryKeyOf<T>>(
3232
export function internalRemoveMany<T extends Entity<T>, P extends PrimaryKeyOf<T>>(
3333
table: Table<T, P>,
3434
entities: Ids<T, P>[]
35-
): Promise<boolean> {
35+
): Promise<number> {
3636
const events: { entity: T }[] = [];
37-
let allEntitiesRemoved = true;
37+
let numEntitiesRemoved = 0;
3838
for (const entity of entities) {
3939
const primaryKeyProperty = table[BlinkKey].options.primary;
4040
const primaryKey = entity[primaryKeyProperty];
4141

4242
const indexes = table[BlinkKey].storage.indexes;
4343
if (Object.keys(indexes).length > 0) {
4444
const item = table[BlinkKey].storage.primary.get(primaryKey);
45-
if (!item) {
46-
allEntitiesRemoved = false;
47-
continue;
48-
}
45+
if (!item) continue;
4946
for (const property in indexes) {
5047
const btree = indexes[property]!;
5148
const key = item[property];
@@ -67,9 +64,9 @@ export function internalRemoveMany<T extends Entity<T>, P extends PrimaryKeyOf<T
6764
const hasDeleted = table[BlinkKey].storage.primary.delete(primaryKey);
6865
if (hasDeleted) {
6966
table[BlinkKey].storage.primary.totalItemSize--;
67+
numEntitiesRemoved++;
7068
}
71-
allEntitiesRemoved = allEntitiesRemoved && hasDeleted;
7269
}
7370
void table[BlinkKey].events.onRemove.dispatch(events);
74-
return Promise.resolve(allEntitiesRemoved);
71+
return Promise.resolve(numEntitiesRemoved);
7572
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy