Skip to content

Commit

Permalink
feat: add mongodb@4.x support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: it is hard to support both versions (3.x and 4.x) of
native driver on the same branch because 4.x has native types
definitions and they diverge from the ones provided by @types/mongodb.

This means that this version might still be compatible with previous
versions of mongodb native driver (3.6.x and 3.7.x), but we aren't
testing them due to typescript mismatch.

Please open an issue and we will try to support your use case.
  • Loading branch information
alias-mac committed Jan 19, 2022
1 parent 707d3b6 commit f50e7e1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 70 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ jobs:
matrix:
node-version: [12.x, 14.x, 16.x]
mongodb-version: ['4.0', '4.2', '4.4']
# these should match the minimum peerDependencies definitions
mongodb-types: ['3.6.20', '^3.6.20']
mongodb-lib: ['3.6.7', '^3.6.7']
mongodb-lib: ['4.3.1', '^4.3.1']

env:
NODE: ${{ matrix.node-version }}
Expand All @@ -56,7 +54,6 @@ jobs:
- run: yarn install --frozen-lockfile

# peerDeps installs
- run: yarn add -D @types/mongodb@${{matrix.mongodb-types}}
- run: yarn add -D mongodb@${{matrix.mongodb-lib}}

- run: yarn test --coverage
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"test": "jest"
},
"peerDependencies": {
"@types/mongodb": "^3.6.20",
"mongodb": "^3.6.7"
"mongodb": "^4.3.1"
},
"peerDependenciesMeta": {
"@types/mongodb": {
Expand All @@ -40,7 +39,7 @@
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/jest": "^27.4.0",
"@types/mongodb": "^3.6.20",
"@types/mongodb": "^4.0.7",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"builtin-modules": "^3.2.0",
Expand All @@ -51,7 +50,7 @@
"husky": "^7.0.4",
"jest": "^27.4.7",
"lint-staged": "^12.1.7",
"mongodb": "^3.6.7",
"mongodb": "^4.3.1",
"prettier": "^2.5.1",
"rollup": "^2.64.0",
"standard-version": "^9.3.2",
Expand Down
7 changes: 3 additions & 4 deletions src/mongodb-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
*/

import crypto from 'crypto';
import type { Db, FilterQuery, ObjectId, UpdateQuery } from 'mongodb';
import type { Db, Filter, ObjectId, UpdateFilter } from 'mongodb';

// some helper functions
function id() {
return crypto.randomBytes(16).toString('hex');
}

type MessageSchema = {
_id: ObjectId;
createdAt: Date;
updatedAt?: Date;
visible: Date;
Expand Down Expand Up @@ -104,7 +103,7 @@ class MongoDbQueueImpl<T = unknown> implements MongoDbQueue {
return result.insertedId.toHexString();
}

let filter: FilterQuery<MessageSchema> = {
let filter: Filter<MessageSchema> = {
payload: { $eq: hashKey },
};

Expand Down Expand Up @@ -144,7 +143,7 @@ class MongoDbQueueImpl<T = unknown> implements MongoDbQueue {
deleted: null,
visible: { $lte: new Date(now) },
};
const update: UpdateQuery<MessageSchema> = {
const update: UpdateFilter<MessageSchema> = {
$inc: { tries: 1 },
$set: {
updatedAt: new Date(now),
Expand Down
6 changes: 1 addition & 5 deletions src/specs/__helpers__/setup-mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class SetupMongo {
private _dbName: string;

get client() {
if (!this._client.isConnected()) {
throw new Error('Please wait for `connection()` on `beforeAll` hook');
}

return this._client;
}

Expand All @@ -17,7 +13,7 @@ class SetupMongo {
}

constructor(url: string, dbName: string) {
this._client = new MongoClient(url, { useUnifiedTopology: true });
this._client = new MongoClient(url);
this._dbName = dbName;
}

Expand Down
Loading

0 comments on commit f50e7e1

Please sign in to comment.