Skip to content

Commit eb4c022

Browse files
committed
Update based on comments
1 parent e44bee1 commit eb4c022

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

writing-adapters.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Writing database adapters
22

3-
`micro-analytics` database adapters are simple JavaScript modules which export an object with five methods. They _have_ to be called `micro-analytics-adapter-xyz`, where `xyz` is the name users will pass to the `DB_ADAPTER` environment variable when starting `micro-analytics`.
3+
`micro-analytics` database adapters are simple JavaScript modules which export an object with some methods. They _have_ to be called `micro-analytics-adapter-xyz`, where `xyz` is the name users will pass to the `DB_ADAPTER` environment variable when starting `micro-analytics`.
44

55
## Overview
66

7-
The five methods every adapter has to have are:
7+
The methods every adapter has to have are:
88

99
- `get(key: string)`: Get a value from the database
1010
- `put(key: string, value: object)`: Put a value into the database
@@ -32,16 +32,36 @@ module.exports = {
3232

3333
Let's dive into the individual methods:
3434

35-
### `get(key: string): Promise`
35+
### `get(key: string, options?: { filter?: object }): Promise`
3636

37-
Should resolve the Promise with the value stored in the database, if there is one, or reject it, if not.
37+
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.
3838

3939
#### Usage
4040

4141
```JS
4242
try {
4343
const value = await adapter.get('/hello')
44-
} catch (err) {/* New record added here */}
44+
console.log(value) // { views: 123 }
45+
} catch (err) {/* ... */}
46+
```
47+
48+
#### Options
49+
50+
##### `filter: { before?: UTCTime, after?: UTCTime }`
51+
52+
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.
53+
54+
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:
55+
56+
```JS
57+
// Return record with key /hello
58+
await adapter.get('/hello') // -> { views: 125 }
59+
60+
// Return record with key /hello and its number of views that happened before 1234 UTC
61+
await adapter.get('/hello', { filter: { before: 1234 }}) // -> { views: 100 }
62+
63+
// Return record with key /hello and its number of views that happened 1234 UTC but after 1200 UTC
64+
await adapter.get('/hello', { filter: { after: 1200, before: 1234 }}) // -> { views: 20 }
4565
```
4666

4767
### `put(key: string, value: object): Promise`
@@ -123,22 +143,22 @@ This would not only return the values for the record with the key `/car`, but al
123143

124144
##### `filter: { before: UTCTime, after: UTCTime }`
125145

126-
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.
146+
`getAll` should have the same behaviour as `get` in terms of the filtering.
127147

128-
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:
148+
Here's some examples of valid queries:
129149

130150
```JS
131151
// Return all records
132-
await.getAll()
152+
await adapter.getAll() // -> { '/car': { views: [{ time: 1900 }, { time: 1210 }, { time: 1235 }]}}
133153

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

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

140160
// Return all keys that start with /car and their views that happened before 1234 UTC but after 1200 UTC
141-
await.getAll({ pathname: '/car', filter: { after: 1200, before: 1234 }})
161+
await adapter.getAll({ pathname: '/car', filter: { after: 1200, before: 1234 }})
142162
```
143163

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

0 commit comments

Comments
 (0)