Skip to content

Commit 621bdce

Browse files
committed
fixes
1 parent 03cf7d2 commit 621bdce

File tree

6 files changed

+74
-34
lines changed

6 files changed

+74
-34
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
transform: { '^.+\\.ts?$': 'ts-jest' },
3+
testMatch: ['**/__tests__/**/*test.ts?(x)'],
4+
testEnvironment: 'node',
5+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
6+
collectCoverageFrom: ['src/**/*.ts'],
7+
};

packages/store/node-server-sdk-mongodb/jest.config.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/store/node-server-sdk-mongodb/src/MongoDBBigSegmentStore.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ import { interfaces, LDLogger } from '@launchdarkly/node-server-sdk';
33
import LDMongoDBOptions from './LDMongoDBOptions';
44
import MongoDBClientState from './MongoDBClientState';
55

6+
/**
7+
* @internal
8+
*/
9+
interface MetadataDocument {
10+
_id: string;
11+
[FIELD_LAST_UP_TO_DATE]?: number;
12+
}
13+
14+
/**
15+
* @internal
16+
*/
17+
interface UserDocument {
18+
_id?: string;
19+
[FIELD_USER_HASH]: string;
20+
[FIELD_INCLUDED]?: string[];
21+
[FIELD_EXCLUDED]?: string[];
22+
}
23+
624
/**
725
* @internal
826
*/
@@ -65,14 +83,14 @@ export default class MongoDBBigSegmentStore implements interfaces.BigSegmentStor
6583
*/
6684
async getMetadata(): Promise<interfaces.BigSegmentStoreMetadata | undefined> {
6785
try {
68-
const metadataCollection = await this._state.getCollection(COLLECTION_BIG_SEGMENTS_METADATA);
69-
86+
const metadataCollection = await this._state.getCollection<MetadataDocument>(COLLECTION_BIG_SEGMENTS_METADATA);
87+
7088
const metadata = await metadataCollection.findOne({ _id: METADATA_KEY });
71-
89+
7290
if (metadata && metadata[FIELD_LAST_UP_TO_DATE]) {
7391
return { lastUpToDate: metadata[FIELD_LAST_UP_TO_DATE] };
7492
}
75-
93+
7694
return {};
7795
} catch (error) {
7896
this._logger?.error(`MongoDB big segment store getMetadata error: ${error}`);
@@ -90,23 +108,23 @@ export default class MongoDBBigSegmentStore implements interfaces.BigSegmentStor
90108
userHash: string,
91109
): Promise<interfaces.BigSegmentStoreMembership | undefined> {
92110
try {
93-
const userCollection = await this._state.getCollection(COLLECTION_BIG_SEGMENTS_USER);
94-
111+
const userCollection = await this._state.getCollection<UserDocument>(COLLECTION_BIG_SEGMENTS_USER);
112+
95113
const userData = await userCollection.findOne({ [FIELD_USER_HASH]: userHash });
96-
114+
97115
if (!userData) {
98116
return undefined;
99117
}
100118

101119
const membership: interfaces.BigSegmentStoreMembership = {};
102-
120+
103121
// Process excluded segment references
104122
if (userData[FIELD_EXCLUDED] && Array.isArray(userData[FIELD_EXCLUDED])) {
105123
userData[FIELD_EXCLUDED].forEach((segmentRef: string) => {
106124
membership[segmentRef] = false;
107125
});
108126
}
109-
127+
110128
// Process included segment references
111129
if (userData[FIELD_INCLUDED] && Array.isArray(userData[FIELD_INCLUDED])) {
112130
userData[FIELD_INCLUDED].forEach((segmentRef: string) => {

packages/store/node-server-sdk-mongodb/src/MongoDBClientState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Collection, Db, MongoClient, MongoServerError } from 'mongodb';
1+
import { Collection, Db, Document, MongoClient, MongoServerError } from 'mongodb';
22

33
import LDMongoDBOptions from './LDMongoDBOptions';
44

@@ -45,7 +45,7 @@ export default class MongoDBClientState {
4545
/**
4646
* Gets a MongoDB collection by name.
4747
*/
48-
public async getCollection<T = any>(name: string): Promise<Collection<T>> {
48+
public async getCollection<T extends Document = Document>(name: string): Promise<Collection<T>> {
4949
const db = await this.getDatabase();
5050
return db.collection<T>(this.prefixedCollection(name));
5151
}

packages/store/node-server-sdk-mongodb/src/MongoDBCore.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import { interfaces, LDLogger } from '@launchdarkly/node-server-sdk';
22

33
import MongoDBClientState from './MongoDBClientState';
44

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+
525
/**
626
* @internal
727
*/
@@ -66,7 +86,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
6686

6787
for (const collection of allData) {
6888
const { namespace } = collection.key;
69-
const mongoCollection = await this._state.getCollection(namespace);
89+
const mongoCollection = await this._state.getCollection<FeatureDocument>(namespace);
7090
const existingDocs = await mongoCollection.find({}, { projection: { _id: 1 } }).toArray();
7191

7292
for (const doc of existingDocs) {
@@ -80,7 +100,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
80100
for (const collection of allData) {
81101
const { namespace } = collection.key;
82102
const items = collection.item;
83-
const mongoCollection = await this._state.getCollection(namespace);
103+
const mongoCollection = await this._state.getCollection<FeatureDocument>(namespace);
84104

85105
// Prepare bulk operations for this namespace
86106
const bulkOps: any[] = [];
@@ -89,11 +109,11 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
89109
const itemKey = `${namespace}:${keyedItem.key}`;
90110
itemsToKeep.add(itemKey);
91111

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+
};
97117

98118
if (keyedItem.item.deleted) {
99119
doc.deleted = true;
@@ -119,7 +139,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
119139
// Delete items that are no longer present in the new data
120140
for (const collection of allData) {
121141
const { namespace } = collection.key;
122-
const mongoCollection = await this._state.getCollection(namespace);
142+
const mongoCollection = await this._state.getCollection<FeatureDocument>(namespace);
123143

124144
const itemsToDelete: string[] = [];
125145
for (const existingItem of existingItems) {
@@ -134,10 +154,10 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
134154
}
135155

136156
// Set the initialized flag
137-
const initCollection = await this._state.getCollection(COLLECTION_INITIALIZED);
157+
const initCollection = await this._state.getCollection<InitializedDocument>(COLLECTION_INITIALIZED);
138158
await initCollection.replaceOne(
139159
{ _id: this._initedKey },
140-
{ _id: this._initedKey, initialized: true, timestamp: new Date() },
160+
{ initialized: true, timestamp: new Date() } as any,
141161
{ upsert: true }
142162
);
143163

@@ -154,7 +174,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
154174
callback: (descriptor: interfaces.SerializedItemDescriptor | undefined) => void,
155175
): Promise<void> {
156176
try {
157-
const collection = await this._state.getCollection(kind.namespace);
177+
const collection = await this._state.getCollection<FeatureDocument>(kind.namespace);
158178
const doc = await collection.findOne({ _id: key });
159179

160180
if (doc) {
@@ -180,7 +200,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
180200
) => void,
181201
): Promise<void> {
182202
try {
183-
const collection = await this._state.getCollection(kind.namespace);
203+
const collection = await this._state.getCollection<FeatureDocument>(kind.namespace);
184204
const docs = await collection.find({ deleted: { $ne: true } }).toArray();
185205

186206
const results: interfaces.KeyedItem<string, interfaces.SerializedItemDescriptor>[] = [];
@@ -213,9 +233,9 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
213233
) => void,
214234
): Promise<void> {
215235
try {
216-
const collection = await this._state.getCollection(kind.namespace);
236+
const collection = await this._state.getCollection<FeatureDocument>(kind.namespace);
217237

218-
const doc: any = {
238+
const doc: FeatureDocument = {
219239
_id: key,
220240
namespace: kind.namespace,
221241
version: descriptor.version,
@@ -256,7 +276,7 @@ export default class MongoDBCore implements interfaces.PersistentDataStore {
256276

257277
async initialized(callback: (isInitialized: boolean) => void): Promise<void> {
258278
try {
259-
const collection = await this._state.getCollection(COLLECTION_INITIALIZED);
279+
const collection = await this._state.getCollection<InitializedDocument>(COLLECTION_INITIALIZED);
260280
const doc = await collection.findOne({ _id: this._initedKey });
261281
callback(!!doc?.initialized);
262282
} catch (error) {

packages/store/node-server-sdk-mongodb/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rootDir": ".",
55
"outDir": "dist",
66
"target": "es2017",
7-
"lib": ["es6"],
7+
"lib": ["es2017"],
88
"module": "commonjs",
99
"strict": true,
1010
"noImplicitOverride": true,
@@ -15,7 +15,8 @@
1515
"declarationMap": true, // enables importers to jump to source
1616
"resolveJsonModule": true,
1717
"stripInternal": true,
18-
"moduleResolution": "node"
18+
"moduleResolution": "node",
19+
"skipLibCheck": true
1920
},
2021
"exclude": ["**/*.test.ts", "dist", "node_modules", "__tests__"]
2122
}

0 commit comments

Comments
 (0)