Skip to content

Commit e4e81dd

Browse files
committed
allow modifying parameters in hooks
1 parent 5613d20 commit e4e81dd

30 files changed

+137
-88
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createDB } from "./createDB";
44
import { createTable, Table } from "./createTable";
55
import { insertMany } from "./insertMany";
66
import { many } from "./many";
7-
import { isAction, use } from "./use";
7+
import { use } from "./use";
88

99
let users: User[];
1010
let userTable: Table<User, "id">;
@@ -28,7 +28,7 @@ it("should execute clear hooks", async () => {
2828

2929
use(userTable, (ctx) => {
3030
fn(ctx.action);
31-
return ctx.next();
31+
return ctx.next(...ctx.params);
3232
});
3333
await clear(userTable);
3434

packages/db/src/core/clear.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Table } from "./createTable";
1313
export async function clear<T extends object, P extends keyof T>(
1414
table: Table<T, P>
1515
): Promise<void> {
16-
return middleware<T, P, "clear">(table, { action: "clear", params: [table] }, () =>
16+
return middleware<T, P, "clear">(table, { action: "clear", params: [table] }, (table) =>
1717
internalClear(table)
1818
);
1919
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { analyze } from "../query/analyze";
21
import { generateRandomUsers, User } from "../tests/utils";
32
import { count } from "./count";
43
import { createDB } from "./createDB";
54
import { createTable, Table } from "./createTable";
65
import { insertMany } from "./insertMany";
7-
import { isAction, use } from "./use";
6+
import { use } from "./use";
87

98
let users: User[];
109
let userTable: Table<User, "id">;
@@ -58,7 +57,7 @@ it("should execute count hooks", async () => {
5857

5958
use(userTable, (ctx) => {
6059
fn(ctx.action);
61-
return ctx.next();
60+
return ctx.next(...ctx.params);
6261
});
6362
await count(userTable);
6463

packages/db/src/core/count.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export async function count<T extends object, P extends keyof T>(
4646
filter?: Filter<T>,
4747
options: { exact: boolean } = { exact: true }
4848
): Promise<number> {
49-
return middleware(
49+
return middleware<T, P, "count">(
5050
table,
5151
{
5252
action: "count",
5353
params: [table, filter, options],
5454
},
55-
() => internalCount(table, filter, options)
55+
(table, filter, options) => internalCount(table, filter, options)
5656
);
5757
}
5858

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { User, generateRandomUsers } from "../tests/utils";
1+
import { generateRandomUsers, User } from "../tests/utils";
22
import { clear } from "./clear";
3-
import { createDB, Database } from "./createDB";
3+
import { createDB } from "./createDB";
44
import { createTable, Table } from "./createTable";
55
import { first } from "./first";
6-
import { insert } from "./insert";
76
import { insertMany } from "./insertMany";
87
import { use } from "./use";
98

@@ -69,7 +68,7 @@ it("should execute first hooks", async () => {
6968

7069
use(userTable, (ctx) => {
7170
fn(ctx.action);
72-
return ctx.next();
71+
return ctx.next(...ctx.params);
7372
});
7473
await first(userTable);
7574

packages/db/src/core/first.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ export async function first<T extends object, P extends keyof T>(
3636
table: Table<T, P>,
3737
query?: Query<T, P>
3838
): Promise<T | null> {
39-
return middleware(table, { action: "first", params: [table, query] }, () =>
40-
internalFirst(table, query)
39+
return middleware<T, P, "first">(
40+
table,
41+
{ action: "first", params: [table, query] },
42+
(table, query) => internalFirst(table, query)
4143
);
4244
}
4345

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ it("should execute insert hooks", async () => {
5959

6060
use(userTable, (ctx) => {
6161
fn(ctx.action);
62-
return ctx.next();
62+
return ctx.next(...ctx.params);
6363
});
6464
await insert(userTable, users[0]);
6565

packages/db/src/core/insert.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export async function insert<T extends object, P extends keyof T>(
1818
table: Table<T, P>,
1919
entity: Create<T, P>
2020
): Promise<T[P]> {
21-
return middleware(table, { action: "insert", params: [table, entity] }, () =>
22-
internalInsert(table, entity)
21+
return middleware<T, P, "insert">(
22+
table,
23+
{ action: "insert", params: [table, entity] },
24+
(table, entity) => internalInsert(table, entity)
2325
);
2426
}
2527

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { generateRandomUsers, User } from "../tests/utils";
2-
import { createDB, Database } from "./createDB";
2+
import { createDB } from "./createDB";
33
import { createTable, Table } from "./createTable";
44
import { insertMany } from "./insertMany";
55
import { one } from "./one";
@@ -56,7 +56,7 @@ it("should execute insertMany hooks", async () => {
5656

5757
use(userTable, (ctx) => {
5858
fn(ctx.action);
59-
return ctx.next();
59+
return ctx.next(...ctx.params);
6060
});
6161
await insertMany(userTable, [users[0], users[1]]);
6262

packages/db/src/core/insertMany.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export async function insertMany<T extends object, P extends keyof T>(
2424
table: Table<T, P>,
2525
entities: Create<T, P>[]
2626
): Promise<T[P][]> {
27-
return middleware(table, { action: "insertMany", params: [table, entities] }, () =>
28-
internalInsertMany(table, entities)
27+
return middleware<T, P, "insertMany">(
28+
table,
29+
{ action: "insertMany", params: [table, entities] },
30+
(table, entities) => internalInsertMany(table, entities)
2931
);
3032
}
3133

0 commit comments

Comments
 (0)