1
1
import { BaseDatastore } from './base.js'
2
2
import map from 'it-map'
3
3
import { pipe } from 'it-pipe'
4
-
5
- /**
6
- * @typedef {import('interface-datastore').Datastore } Datastore
7
- * @typedef {import('interface-datastore').Options } Options
8
- * @typedef {import('interface-datastore').Batch } Batch
9
- * @typedef {import('interface-datastore').Query } Query
10
- * @typedef {import('interface-datastore').KeyQuery } KeyQuery
11
- * @typedef {import('interface-datastore').Key } Key
12
- * @typedef {import('interface-datastore').Pair } Pair
13
- * @typedef {import('./types').KeyTransform } KeyTransform
14
- */
15
-
16
- /**
17
- * @template TEntry
18
- * @typedef {import('interface-store').AwaitIterable<TEntry> } AwaitIterable
19
- */
4
+ import type { KeyTransform } from './index.js'
5
+ import type { Batch , Datastore , Key , KeyQuery , Options , Pair , Query } from 'interface-datastore'
6
+ import type { AwaitIterable } from 'interface-store'
20
7
21
8
/**
22
9
* A datastore shim, that wraps around a given datastore, changing
23
10
* the way keys look to the user, for example namespacing
24
11
* keys, reversing them, etc.
25
- *
26
- * @implements {Datastore}
27
12
*/
28
13
export class KeyTransformDatastore extends BaseDatastore {
29
- /**
30
- * @param {Datastore } child
31
- * @param {KeyTransform } transform
32
- */
33
- constructor ( child , transform ) {
14
+ private readonly child : Datastore
15
+ public transform : KeyTransform
16
+
17
+ constructor ( child : Datastore , transform : KeyTransform ) {
34
18
super ( )
35
19
36
20
this . child = child
37
21
this . transform = transform
38
22
}
39
23
40
- open ( ) {
41
- return this . child . open ( )
42
- }
43
-
44
- /**
45
- * @param {Key } key
46
- * @param {Uint8Array } val
47
- * @param {Options } [options]
48
- */
49
- put ( key , val , options ) {
50
- return this . child . put ( this . transform . convert ( key ) , val , options )
24
+ async put ( key : Key , val : Uint8Array , options ?: Options ) : Promise < void > {
25
+ await this . child . put ( this . transform . convert ( key ) , val , options )
51
26
}
52
27
53
- /**
54
- * @param {Key } key
55
- * @param {Options } [options]
56
- */
57
- get ( key , options ) {
58
- return this . child . get ( this . transform . convert ( key ) , options )
28
+ async get ( key : Key , options ?: Options ) : Promise < Uint8Array > {
29
+ return await this . child . get ( this . transform . convert ( key ) , options )
59
30
}
60
31
61
- /**
62
- * @param {Key } key
63
- * @param {Options } [options]
64
- */
65
- has ( key , options ) {
66
- return this . child . has ( this . transform . convert ( key ) , options )
32
+ async has ( key : Key , options ?: Options ) : Promise < boolean > {
33
+ return await this . child . has ( this . transform . convert ( key ) , options )
67
34
}
68
35
69
- /**
70
- * @param {Key } key
71
- * @param {Options } [options]
72
- */
73
- delete ( key , options ) {
74
- return this . child . delete ( this . transform . convert ( key ) , options )
36
+ async delete ( key : Key , options ?: Options ) : Promise < void > {
37
+ await this . child . delete ( this . transform . convert ( key ) , options )
75
38
}
76
39
77
- /**
78
- * @param {AwaitIterable<Pair> } source
79
- * @param {Options } [options]
80
- * @returns {AsyncIterable<Pair> }
81
- */
82
- async * putMany ( source , options = { } ) {
40
+ async * putMany ( source : AwaitIterable < Pair > , options : Options = { } ) : AsyncIterable < Pair > {
83
41
const transform = this . transform
84
42
const child = this . child
85
43
@@ -103,12 +61,7 @@ export class KeyTransformDatastore extends BaseDatastore {
103
61
)
104
62
}
105
63
106
- /**
107
- * @param {AwaitIterable<Key> } source
108
- * @param {Options } [options]
109
- * @returns {AsyncIterable<Uint8Array> }
110
- */
111
- async * getMany ( source , options = { } ) {
64
+ async * getMany ( source : AwaitIterable < Key > , options : Options = { } ) : AsyncIterable < Uint8Array > {
112
65
const transform = this . transform
113
66
const child = this . child
114
67
@@ -123,12 +76,7 @@ export class KeyTransformDatastore extends BaseDatastore {
123
76
)
124
77
}
125
78
126
- /**
127
- * @param {AwaitIterable<Key> } source
128
- * @param {Options } [options]
129
- * @returns {AsyncIterable<Key> }
130
- */
131
- async * deleteMany ( source , options = { } ) {
79
+ async * deleteMany ( source : AwaitIterable < Key > , options : Options = { } ) : AsyncIterable < Key > {
132
80
const transform = this . transform
133
81
const child = this . child
134
82
@@ -146,10 +94,7 @@ export class KeyTransformDatastore extends BaseDatastore {
146
94
)
147
95
}
148
96
149
- /**
150
- * @returns {Batch }
151
- */
152
- batch ( ) {
97
+ batch ( ) : Batch {
153
98
const b = this . child . batch ( )
154
99
return {
155
100
put : ( key , value ) => {
@@ -158,23 +103,18 @@ export class KeyTransformDatastore extends BaseDatastore {
158
103
delete : ( key ) => {
159
104
b . delete ( this . transform . convert ( key ) )
160
105
} ,
161
- commit : ( options ) => {
162
- return b . commit ( options )
106
+ commit : async ( options ) => {
107
+ await b . commit ( options )
163
108
}
164
109
}
165
110
}
166
111
167
- /**
168
- * @param {Query } q
169
- * @param {Options } [options]
170
- */
171
- query ( q , options ) {
172
- /** @type {Query } */
173
- const query = {
112
+ query ( q : Query , options ?: Options ) : AsyncIterable < Pair > {
113
+ const query : Query = {
174
114
...q
175
115
}
176
116
177
- query . filters = ( query . filters || [ ] ) . map ( filter => {
117
+ query . filters = ( query . filters ?? [ ] ) . map ( filter => {
178
118
return ( { key, value } ) => filter ( { key : this . transform . convert ( key ) , value } )
179
119
} )
180
120
@@ -186,7 +126,7 @@ export class KeyTransformDatastore extends BaseDatastore {
186
126
} )
187
127
}
188
128
189
- if ( query . orders ) {
129
+ if ( query . orders != null ) {
190
130
query . orders = query . orders . map ( order => {
191
131
return ( a , b ) => order (
192
132
{ key : this . transform . invert ( a . key ) , value : a . value } ,
@@ -203,16 +143,12 @@ export class KeyTransformDatastore extends BaseDatastore {
203
143
} )
204
144
}
205
145
206
- /**
207
- * @param {KeyQuery } q
208
- * @param {Options } [options]
209
- */
210
- queryKeys ( q , options ) {
146
+ queryKeys ( q : KeyQuery , options ?: Options ) : AsyncIterable < Key > {
211
147
const query = {
212
148
...q
213
149
}
214
150
215
- query . filters = ( query . filters || [ ] ) . map ( filter => {
151
+ query . filters = ( query . filters ?? [ ] ) . map ( filter => {
216
152
return ( key ) => filter ( this . transform . convert ( key ) )
217
153
} )
218
154
@@ -224,7 +160,7 @@ export class KeyTransformDatastore extends BaseDatastore {
224
160
} )
225
161
}
226
162
227
- if ( query . orders ) {
163
+ if ( query . orders != null ) {
228
164
query . orders = query . orders . map ( order => {
229
165
return ( a , b ) => order (
230
166
this . transform . invert ( a ) ,
@@ -237,8 +173,4 @@ export class KeyTransformDatastore extends BaseDatastore {
237
173
return this . transform . invert ( key )
238
174
} )
239
175
}
240
-
241
- close ( ) {
242
- return this . child . close ( )
243
- }
244
176
}
0 commit comments