Skip to content

Commit

Permalink
Document callback handlers for response and responseOnce (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryehb authored Jun 15, 2022
1 parent aa84c20 commit 57c09c1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,35 @@ Each one of `on` methods (`select`, `insert`,`update`, `delete`, `any`) are acce

You can specify the db response by calling:

1. `response<T>(dbData)` - will register a permanent query handler with the same response value
1. `response<T = any>(data: T | ((rawQuery: RawQuery) => (T | Promise<T>)))` - This will register a permanent query handler.

If a value is provided, it will be returned directly.
If a callback is passed, it will be called with the `RawQuery` and should return a value for the tracker to return.

```ts
tracker.on.select('select * from users where `id`=?').response([{ id: 1, name: 'foo' }]);
```
```ts
tracker.on.select('select * from users where `id`=?').response(rawQuery => [{ id: 1, name: 'foo' }]);
```

2. `responseOnce<T = any>(data: T | ((rawQuery: RawQuery) => (T | Promise<T>)))`- This will register a one-time query handler, which will be removed from handlers list after the first usage.

If a value is provided, it will be returned directly. If a callback is passed, it will be called with the `RawQuery` and should return a value for the tracker to return.

2. `responseOnce<T>(dbData)` - will register a one-time query handler, after the first usage it will
be removed from handlers list.
```ts
tracker.on.select('select * from users where `id`=?').responseOnce([{ id: 1, name: 'foo' }]);
```
```ts
tracker.on.select('select * from users where `id`=?').responseOnce(rawQuery => Promise.resolve([{ id: 1, name: 'foo' }]));
```

3. `simulateError(errorMessage: string)` - will register a permanent failure handler for the matched
query
```ts
tracker.on.select('select * from users where `id`=?').simulateError('Connection lost');
```

4. `simulateErrorOnce(errorMessage: string)` - will register a one-time failure handler, after the
first usage it will be removed from handlers list.
```ts
Expand Down

0 comments on commit 57c09c1

Please sign in to comment.