@@ -2,6 +2,26 @@ import { interfaces, LDLogger } from '@launchdarkly/node-server-sdk';
2
2
3
3
import MongoDBClientState from './MongoDBClientState' ;
4
4
5
+ /**
6
+ * @internal
7
+ */
8
+ interface FeatureDocument {
9
+ _id : string ;
10
+ namespace : string ;
11
+ version : number ;
12
+ item ?: string ;
13
+ deleted ?: boolean ;
14
+ }
15
+
16
+ /**
17
+ * @internal
18
+ */
19
+ interface InitializedDocument {
20
+ _id : string ;
21
+ initialized : boolean ;
22
+ timestamp : Date ;
23
+ }
24
+
5
25
/**
6
26
* @internal
7
27
*/
@@ -66,7 +86,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
66
86
67
87
for ( const collection of allData ) {
68
88
const { namespace } = collection . key ;
69
- const mongoCollection = await this . _state . getCollection ( namespace ) ;
89
+ const mongoCollection = await this . _state . getCollection < FeatureDocument > ( namespace ) ;
70
90
const existingDocs = await mongoCollection . find ( { } , { projection : { _id : 1 } } ) . toArray ( ) ;
71
91
72
92
for ( const doc of existingDocs ) {
@@ -80,7 +100,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
80
100
for ( const collection of allData ) {
81
101
const { namespace } = collection . key ;
82
102
const items = collection . item ;
83
- const mongoCollection = await this . _state . getCollection ( namespace ) ;
103
+ const mongoCollection = await this . _state . getCollection < FeatureDocument > ( namespace ) ;
84
104
85
105
// Prepare bulk operations for this namespace
86
106
const bulkOps : any [ ] = [ ] ;
@@ -89,11 +109,11 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
89
109
const itemKey = `${ namespace } :${ keyedItem . key } ` ;
90
110
itemsToKeep . add ( itemKey ) ;
91
111
92
- const doc : any = {
93
- _id : keyedItem . key ,
94
- namespace,
95
- version : keyedItem . item . version ,
96
- } ;
112
+ const doc : FeatureDocument = {
113
+ _id : keyedItem . key ,
114
+ namespace,
115
+ version : keyedItem . item . version ,
116
+ } ;
97
117
98
118
if ( keyedItem . item . deleted ) {
99
119
doc . deleted = true ;
@@ -119,7 +139,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
119
139
// Delete items that are no longer present in the new data
120
140
for ( const collection of allData ) {
121
141
const { namespace } = collection . key ;
122
- const mongoCollection = await this . _state . getCollection ( namespace ) ;
142
+ const mongoCollection = await this . _state . getCollection < FeatureDocument > ( namespace ) ;
123
143
124
144
const itemsToDelete : string [ ] = [ ] ;
125
145
for ( const existingItem of existingItems ) {
@@ -134,10 +154,10 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
134
154
}
135
155
136
156
// Set the initialized flag
137
- const initCollection = await this . _state . getCollection ( COLLECTION_INITIALIZED ) ;
157
+ const initCollection = await this . _state . getCollection < InitializedDocument > ( COLLECTION_INITIALIZED ) ;
138
158
await initCollection . replaceOne (
139
159
{ _id : this . _initedKey } ,
140
- { _id : this . _initedKey , initialized : true , timestamp : new Date ( ) } ,
160
+ { initialized : true , timestamp : new Date ( ) } as any ,
141
161
{ upsert : true }
142
162
) ;
143
163
@@ -154,7 +174,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
154
174
callback : ( descriptor : interfaces . SerializedItemDescriptor | undefined ) => void ,
155
175
) : Promise < void > {
156
176
try {
157
- const collection = await this . _state . getCollection ( kind . namespace ) ;
177
+ const collection = await this . _state . getCollection < FeatureDocument > ( kind . namespace ) ;
158
178
const doc = await collection . findOne ( { _id : key } ) ;
159
179
160
180
if ( doc ) {
@@ -180,7 +200,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
180
200
) => void ,
181
201
) : Promise < void > {
182
202
try {
183
- const collection = await this . _state . getCollection ( kind . namespace ) ;
203
+ const collection = await this . _state . getCollection < FeatureDocument > ( kind . namespace ) ;
184
204
const docs = await collection . find ( { deleted : { $ne : true } } ) . toArray ( ) ;
185
205
186
206
const results : interfaces . KeyedItem < string , interfaces . SerializedItemDescriptor > [ ] = [ ] ;
@@ -213,9 +233,9 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
213
233
) => void ,
214
234
) : Promise < void > {
215
235
try {
216
- const collection = await this . _state . getCollection ( kind . namespace ) ;
236
+ const collection = await this . _state . getCollection < FeatureDocument > ( kind . namespace ) ;
217
237
218
- const doc : any = {
238
+ const doc : FeatureDocument = {
219
239
_id : key ,
220
240
namespace : kind . namespace ,
221
241
version : descriptor . version ,
@@ -256,7 +276,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
256
276
257
277
async initialized ( callback : ( isInitialized : boolean ) => void ) : Promise < void > {
258
278
try {
259
- const collection = await this . _state . getCollection ( COLLECTION_INITIALIZED ) ;
279
+ const collection = await this . _state . getCollection < InitializedDocument > ( COLLECTION_INITIALIZED ) ;
260
280
const doc = await collection . findOne ( { _id : this . _initedKey } ) ;
261
281
callback ( ! ! doc ?. initialized ) ;
262
282
} catch ( error ) {
0 commit comments