|
1 | 1 | # Writing database adapters |
2 | 2 |
|
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`. |
4 | 4 |
|
5 | 5 | ## Overview |
6 | 6 |
|
7 | | -The five methods every adapter has to have are: |
| 7 | +The methods every adapter has to have are: |
8 | 8 |
|
9 | 9 | - `get(key: string)`: Get a value from the database |
10 | 10 | - `put(key: string, value: object)`: Put a value into the database |
@@ -32,16 +32,36 @@ module.exports = { |
32 | 32 |
|
33 | 33 | Let's dive into the individual methods: |
34 | 34 |
|
35 | | -### `get(key: string): Promise` |
| 35 | +### `get(key: string, options?: { filter?: object }): Promise` |
36 | 36 |
|
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. |
38 | 38 |
|
39 | 39 | #### Usage |
40 | 40 |
|
41 | 41 | ```JS |
42 | 42 | try { |
43 | 43 | 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 } |
45 | 65 | ``` |
46 | 66 |
|
47 | 67 | ### `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 |
123 | 143 |
|
124 | 144 | ##### `filter: { before: UTCTime, after: UTCTime }` |
125 | 145 |
|
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. |
127 | 147 |
|
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: |
129 | 149 |
|
130 | 150 | ```JS |
131 | 151 | // Return all records |
132 | | -await.getAll() |
| 152 | +await adapter.getAll() // -> { '/car': { views: [{ time: 1900 }, { time: 1210 }, { time: 1235 }]}} |
133 | 153 |
|
134 | 154 | // 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 }]}} |
136 | 156 |
|
137 | 157 | // 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 }]}} |
139 | 159 |
|
140 | 160 | // 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 }}) |
142 | 162 | ``` |
143 | 163 |
|
144 | 164 | ### `subscribe(pathname?: string): Observable` |
|
0 commit comments