Skip to content

Commit 7421f29

Browse files
committed
docs: Fix inconsistency of adapter spec and implementation
1 parent 3973c74 commit 7421f29

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

writing-adapters.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
If you want to see an example adapter, check out the default [`flat-file-db` adapter](https://github.com/mxstbr/micro-analytics-adapter-flat-file-db)!
66

7+
This document is written and verified by humans so mistakes might happen. If you find something
8+
to not work, please refer to tests or the flatfile db for how might work at the moment. The tests
9+
described further below are ran against the flat-file-db adapter on every commit so they should be
10+
up to date. Also, if you find any errors in this document please open an issue or pull-request.
11+
712
## Overview
813

914
The methods every adapter has to have are:
@@ -62,12 +67,13 @@ The option object takes the following properties.
6267
* `afterEach` - Will be called in jest afterEach hook, return a promise if it needs to do something async.
6368
* `beforeAll` - Will be called in jest beforeAll hook, return a promise if it needs to do something async.
6469
* `afterAll` - Will be called in jest afterAll hook, return a promise if it needs to do something async.
70+
* `initOptions` - Object passed to `init()`.
6571

6672
Let's dive into the individual methods and what they should do.
6773

6874
## Methods
6975

70-
### `get(key: string, options?: { filter?: object }): Promise`
76+
### `get(key: string, options?: object): Promise`
7177

7278
Should resolve the Promise with an object containing the number of views at that path or, if there is no record with that path yet, reject it.
7379

@@ -82,9 +88,9 @@ try {
8288

8389
#### Options
8490

85-
##### `filter: { before?: UTCTime, after?: UTCTime }`
91+
##### `before?: UTCTime` and `after?: UTCTime`
8692

87-
The adapter should return filter all records returned to only contain the views before `before` and after `after`. The times are passed in UTC, so a simple `record.views[x].time < filter.before` is good enough.
93+
The adapter should return filter all records returned to only contain the views before `before` and after `after`. The times are passed in UTC, so a simple `record.views[x].time < before` is good enough.
8894

8995
Both, either one or none of them might be specified. It also has to work in conjunction with `pathname`. These are all valid queries, including any further combination of those:
9096

@@ -93,10 +99,10 @@ Both, either one or none of them might be specified. It also has to work in conj
9399
await adapter.get('/hello') // -> { views: 125 }
94100

95101
// Return record with key /hello and its number of views that happened before 1234 UTC
96-
await adapter.get('/hello', { filter: { before: 1234 }}) // -> { views: 100 }
102+
await adapter.get('/hello', { before: 1234 }) // -> { views: 100 }
97103

98104
// Return record with key /hello and its number of views that happened 1234 UTC but after 1200 UTC
99-
await adapter.get('/hello', { filter: { after: 1200, before: 1234 }}) // -> { views: 20 }
105+
await adapter.get('/hello', { after: 1200, before: 1234 }) // -> { views: 20 }
100106
```
101107

102108
### `put(key: string, value: object): Promise`
@@ -157,10 +163,8 @@ The passed `options` can contain the following keys:
157163
```JS
158164
{
159165
pathname?: string,
160-
filter?: {
161-
before?: UTCTime,
162-
after?: UTCTime,
163-
}
166+
before?: UTCTime,
167+
after?: UTCTime,
164168
}
165169
```
166170

@@ -176,7 +180,7 @@ await adapter.getAll({ pathname: '/car' })
176180

177181
This would not only return the values for the record with the key `/car`, but also for `/car2`, `/car/make/toyota`, `/caramba`, etc, essentially for _any key that starts with the string `/car`_.
178182

179-
##### `filter: { before: UTCTime, after: UTCTime }`
183+
##### `before: UTCTime` and `after: UTCTime`
180184

181185
`getAll` should have the same behaviour as `get` in terms of the filtering.
182186

@@ -187,13 +191,13 @@ Here's some examples of valid queries:
187191
await adapter.getAll() // -> { '/car': { views: [{ time: 1900 }, { time: 1210 }, { time: 1235 }]}}
188192

189193
// Return all keys and their views that happened before 1234 UTC
190-
await adapter.getAll({ filter: { before: 1234 }}) // -> { '/car': { views: [{ time: 1900 }, { time: 1210 }]}}
194+
await adapter.getAll({ before: 1234}) // -> { '/car': { views: [{ time: 1900 }, { time: 1210 }]}}
191195

192196
// Return all keys and their views that happened before 1234 UTC but after 1200 UTC
193-
await adapter.getAll({ filter: { after: 1200, before: 1234 }}) // -> { '/car': { views: [{ time: 1210 }]}}
197+
await adapter.getAll({ after: 1200, before: 1234 }) // -> { '/car': { views: [{ time: 1210 }]}}
194198

195199
// Return all keys that start with /car and their views that happened before 1234 UTC but after 1200 UTC
196-
await adapter.getAll({ pathname: '/car', filter: { after: 1200, before: 1234 }})
200+
await adapter.getAll({ pathname: '/car', after: 1200, before: 1234 })
197201
```
198202

199203
### `subscribe(pathname?: string): Observable`

0 commit comments

Comments
 (0)