Skip to content

Commit

Permalink
jaredwray#1084 Implement disconnect for mongodb adapter for a gracefu…
Browse files Browse the repository at this point in the history
…l shutdown.
  • Loading branch information
Tate Jones committed Jul 15, 2024
1 parent 0cec7ce commit afc3396
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class KeyvMongo extends EventEmitter implements KeyvStoreAdapter {
await store.createIndex({'metadata.expiresAt': 1});
await store.createIndex({'metadata.lastAccessed': 1});

resolve({bucket, store, db: database});
resolve({
bucket, store, db: database, mongoClient: client,
});
} else {
let collection = 'keyv';
if (this.opts.collection) {
Expand All @@ -87,7 +89,7 @@ class KeyvMongo extends EventEmitter implements KeyvStoreAdapter {
await store.createIndex({key: 1}, {unique: true, background: true});
await store.createIndex({expiresAt: 1}, {expireAfterSeconds: 0, background: true});

resolve({store});
resolve({store, mongoClient: client});
}
} catch (error) {
this.emit('error', error);
Expand Down Expand Up @@ -317,6 +319,11 @@ class KeyvMongo extends EventEmitter implements KeyvStoreAdapter {
const document = await client.store.count(filter);
return document !== 0;
}

async disconnect(): Promise<void> {
const client = await this.connect;
await client.mongoClient.close();
}
}

export default KeyvMongo;
2 changes: 2 additions & 0 deletions packages/mongo/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type MongoClient,
type Collection, type Db, type GridFSBucket, type ReadPreference,
} from 'mongodb';

Expand All @@ -22,6 +23,7 @@ export type KeyvMongoConnect = {
bucket?: GridFSBucket;
store: Collection;
db?: Db;
mongoClient: MongoClient;
};

export type PifyFunction = (...arguments_: any[]) => any;
34 changes: 34 additions & 0 deletions packages/mongo/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test.afterAll(async () => {
await keyv.clear();
keyv = new KeyvMongo({collection: 'foo', useGridFS: true, ...options});
await keyv.clear();
await keyv.disconnect();
});

test.beforeEach(async () => {
Expand Down Expand Up @@ -205,3 +206,36 @@ test.it('iterator with namespace', async t => {
entry = await iterator.next();
t.expect(entry.value).toBeUndefined();
});

test.it('Close connection successfully on GridFS', async t => {
const keyv = new KeyvMongo({useGridFS: true, ...options});
t.expect(await keyv.get('foo')).toBeUndefined();
await keyv.disconnect();
try {
await keyv.get('foo');
t.expect.fail();
} catch {
t.expect(true).toBeTruthy();
}
});

test.it('Close connection successfully', async t => {
const keyv = new KeyvMongo({namespace: 'key1', ...options});
t.expect(await keyv.get('foo')).toBeUndefined();
await keyv.disconnect();
try {
await keyv.get('foo');
t.expect.fail();
} catch {
t.expect(true).toBeTruthy();
}
});

test.it('Close connection should fail', async t => {
const keyv = new KeyvMongo({namespace: 'key1', ...options});
try {
await keyv.disconnect();
} catch {
t.expect(true).toBeTruthy();
}
});

0 comments on commit afc3396

Please sign in to comment.