Skip to content

Commit 74a5586

Browse files
feat: Use mongodb v4.* (#36)
BREAKING CHANGE: Requires peer dependency mongodb v4.* BREAKING CHANGE: TypeScript types have changed - now using the embedded types from mongodb package.
1 parent 80ef74a commit 74a5586

File tree

19 files changed

+317
-270
lines changed

19 files changed

+317
-270
lines changed

.eslintrc.ts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"allowTemplateLiterals": true
112112
}
113113
],
114+
"@typescript-eslint/require-await": "off",
114115
"@typescript-eslint/semi": "error",
115116
"@typescript-eslint/type-annotation-spacing": "error",
116117
"@typescript-eslint/unified-signatures": "error",

benchmark/run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mongodbLib from 'mongodb';
1+
import { ObjectId } from 'mongodb';
22
import SampleMongoose from './mongoose.js';
33
import SamplePapr from './papr.js';
44
import setup, { db } from './setup.js';
@@ -40,7 +40,7 @@ function randomDocument(source) {
4040
localization: {
4141
foo: 'bar',
4242
},
43-
reference: new mongodbLib.ObjectId(),
43+
reference: new ObjectId(),
4444
reviews: [
4545
{ score: random(10) },
4646
{ score: random(10) },

benchmark/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ export let db;
4141

4242
export default async function setup() {
4343
const connection = await mongodb.MongoClient.connect(URL, {
44-
useNewUrlParser: true,
45-
useUnifiedTopology: true,
44+
directConnection: true
4645
});
4746
db = connection.db(DATABASE);
4847

@@ -59,6 +58,7 @@ export default async function setup() {
5958
papr.updateSchemas();
6059

6160
mongoose.connect(`${URL}/${DATABASE}`, {
61+
directConnection: true,
6262
useNewUrlParser: true,
6363
useUnifiedTopology: true,
6464
});

docs/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { schema, types } from 'papr';
1313

1414
const papr = new Papr();
1515

16-
connection = await mongodb.MongoClient.connect('mongodb://localhost:27017', { useUnifiedTopology: true });
16+
connection = await mongodb.MongoClient.connect('mongodb://localhost:27017');
1717
papr.initialize(connection.db('test'));
1818
await papr.updateSchemas();
1919

@@ -75,9 +75,12 @@ The benchmark above are from a benchmark with:
7575
- Node.js 14.16.0.
7676
- MongoDB server v4.4.6
7777
- `mongodb` v3.6.10
78+
- `papr` v1.0.1
7879

7980
This benchmark can be run locally with `yarn benchmark`. Run `yarn benchmark --help` for more information on available arguments.
8081

82+
Note: Until `mongoose` will add support for using `mongodb` v4+, the benchmark will not produce accurate numbers.
83+
8184
---
8285

8386
Read how to [get started](getting-started.md) with `Papr`.

docs/api/model.md

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Calls the MongoDB [`aggregate()`](https://mongodb.github.io/node-mongodb-native/
2525
| Name | Type | Attribute |
2626
| --- | --- | --- |
2727
| `pipeline` | `Array<Record<string, unknown>>` | required |
28-
| `options` | `CollectionAggregationOptions` | optional |
28+
| `options` | `AggregateOptions` | optional |
2929

3030
**Returns:**
3131

@@ -49,12 +49,12 @@ Calls the MongoDB [`bulkWrite()`](https://mongodb.github.io/node-mongodb-native/
4949

5050
| Name | Type | Attribute |
5151
| --- | --- | --- |
52-
| `operations` | `Array<BulkWriteOperation<TSchema>>` | required |
53-
| `options` | `CollectionBulkWriteOptions` | optional |
52+
| `operations` | `Array<AnyBulkWriteOperation<TSchema>>` | required |
53+
| `options` | `BulkWriteOptions` | optional |
5454

5555
**Returns:**
5656

57-
[`Promise<BulkWriteOpResultObject>`](https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~BulkWriteOpResult)
57+
[`Promise<BulkWriteResult>`](https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~BulkWriteOpResult)
5858

5959
**Example:**
6060

@@ -88,8 +88,8 @@ Calls the MongoDB [`countDocuments()`](https://mongodb.github.io/node-mongodb-na
8888

8989
| Name | Type | Attribute |
9090
| --- | --- | --- |
91-
| `filter` | `FilterQuery<TSchema>` | required |
92-
| `options` | `MongoCountPreferences` | optional |
91+
| `filter` | `Filter<TSchema>` | required |
92+
| `options` | `CountDocumentsOptions` | optional |
9393

9494
**Returns:**
9595

@@ -111,12 +111,12 @@ Calls the MongoDB [`deleteMany()`](https://mongodb.github.io/node-mongodb-native
111111

112112
| Name | Type | Attribute |
113113
| --- | --- | --- |
114-
| `filter` | `FilterQuery<TSchema>` | required |
115-
| `options` | `CommonOptions` | optional |
114+
| `filter` | `Filter<TSchema>` | required |
115+
| `options` | `DeleteOptions` | optional |
116116

117117
**Returns:**
118118

119-
[`Promise<DeleteWriteOpResultObject>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~deleteWriteOpResult)
119+
[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~deleteWriteOpResult)
120120

121121
**Example:**
122122

@@ -133,12 +133,12 @@ Calls the MongoDB [`deleteOne()`](https://mongodb.github.io/node-mongodb-native/
133133

134134
| Name | Type | Attribute |
135135
| --- | --- | --- |
136-
| `filter` | `FilterQuery<TSchema>` | required |
137-
| `options` | `CommonOptions` | optional |
136+
| `filter` | `Filter<TSchema>` | required |
137+
| `options` | `DeleteOptions` | optional |
138138

139139
**Returns:**
140140

141-
[`Promise<DeleteWriteOpResultObject>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~deleteWriteOpResult)
141+
[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~deleteWriteOpResult)
142142

143143
**Example:**
144144

@@ -156,8 +156,8 @@ Calls the MongoDB [`distinct()`](https://mongodb.github.io/node-mongodb-native/3
156156
| Name | Type | Attribute |
157157
| --- | --- | --- |
158158
| `key` | `keyof TSchema` | required |
159-
| `filter` | `FilterQuery<TSchema>` | optional |
160-
| `options` | `MongoDistinctPreferences` | optional |
159+
| `filter` | `Filter<TSchema>` | optional |
160+
| `options` | `DistinctOptions` | optional |
161161

162162
**Returns:**
163163

@@ -180,8 +180,8 @@ The result type (`TProjected`) takes into account the projection for this query
180180

181181
| Name | Type | Attribute |
182182
| --- | --- | --- |
183-
| `filter` | `FilterQuery<TSchema>` | required |
184-
| `options` | `FindOneOptions<TSchema>` | optional |
183+
| `filter` | `Filter<TSchema>` | required |
184+
| `options` | `FindOptions<TSchema>` | optional |
185185

186186
**Returns:**
187187

@@ -214,7 +214,7 @@ The result type (`TProjected`) takes into account the projection for this query
214214
| Name | Type | Attribute |
215215
| --- | --- | --- |
216216
| `id` | `string \| ObjectId` | required |
217-
| `options` | `FindOneOptions<TSchema>` | optional |
217+
| `options` | `FindOptions<TSchema>` | optional |
218218

219219
**Returns:**
220220

@@ -246,8 +246,8 @@ The result type (`TProjected`) takes into account the projection for this query
246246

247247
| Name | Type | Attribute |
248248
| --- | --- | --- |
249-
| `filter` | `FilterQuery<TSchema>` | required |
250-
| `options` | `FindOneOptions<TSchema>` | optional |
249+
| `filter` | `Filter<TSchema>` | required |
250+
| `options` | `FindOptions<TSchema>` | optional |
251251

252252
**Returns:**
253253

@@ -279,9 +279,9 @@ The result type (`TProjected`) takes into account the projection for this query
279279

280280
| Name | Type | Attribute |
281281
| --- | --- | --- |
282-
| `filter` | `FilterQuery<TSchema>` | required |
283-
| `update` | `UpdateQuery<TSchema>` | required |
284-
| `options` | `FindOneAndUpdateOption<TSchema>` | optional |
282+
| `filter` | `Filter<TSchema>` | required |
283+
| `update` | `UpdateFilter<TSchema>` | required |
284+
| `options` | `FindOneAndUpdateOptions` | optional |
285285

286286
**Returns:**
287287

@@ -316,7 +316,7 @@ Calls the MongoDB [`insertMany()`](https://mongodb.github.io/node-mongodb-native
316316
| Name | Type | Attribute |
317317
| --- | --- | --- |
318318
| `documents` | `Array<TSchema>` | required |
319-
| `options` | `CollectionInsertManyOptions` | optional |
319+
| `options` | `BulkWriteOptions` | optional |
320320

321321
**Returns:**
322322

@@ -341,7 +341,7 @@ Calls the MongoDB [`insertOne()`](https://mongodb.github.io/node-mongodb-native/
341341
| Name | Type | Attribute |
342342
| --- | --- | --- |
343343
| `document` | `TSchema` | required |
344-
| `options` | `CollectionInsertOneOptions` | optional |
344+
| `options` | `InsertOneOptions` | optional |
345345

346346
**Returns:**
347347

@@ -365,13 +365,13 @@ Calls the MongoDB [`updateMany()`](https://mongodb.github.io/node-mongodb-native
365365

366366
| Name | Type | Attribute |
367367
| --- | --- | --- |
368-
| `filter` | `FilterQuery<TSchema>` | required |
369-
| `update` | `UpdateQuery<TSchema>` | required |
370-
| `options` | `UpdateManyOptions` | optional |
368+
| `filter` | `Filter<TSchema>` | required |
369+
| `update` | `UpdateFilter<TSchema>` | required |
370+
| `options` | `UpdateOptions` | optional |
371371

372372
**Returns:**
373373

374-
[`Promise<UpdateWriteOpResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult)
374+
[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult)
375375

376376
**Example:**
377377

@@ -391,13 +391,13 @@ Calls the MongoDB [`updateOne()`](https://mongodb.github.io/node-mongodb-native/
391391

392392
| Name | Type | Attribute |
393393
| --- | --- | --- |
394-
| `filter` | `FilterQuery<TSchema>` | required |
395-
| `update` | `UpdateQuery<TSchema>` | required |
396-
| `options` | `UpdateOneOptions` | optional |
394+
| `filter` | `Filter<TSchema>` | required |
395+
| `update` | `UpdateFilter<TSchema>` | required |
396+
| `options` | `UpdateOptions` | optional |
397397

398398
**Returns:**
399399

400-
[`Promise<UpdateWriteOpResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult)
400+
[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult)
401401

402402
**Example:**
403403

@@ -417,9 +417,8 @@ Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-
417417

418418
| Name | Type | Attribute |
419419
| --- | --- | --- |
420-
| `filter` | `FilterQuery<TSchema>` | required |
421-
| `update` | `UpdateQuery<TSchema>` | required |
422-
| `options` | `UpdateOneOptions` | optional |
420+
| `filter` | `Filter<TSchema>` | required |
421+
| `update` | `UpdateFilter<TSchema>` | required |
423422

424423
**Returns:**
425424

docs/getting-started.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export let client: MongoClient;
3131
const papr = new Papr();
3232

3333
export async function connect() {
34-
client = await mongodb.MongoClient.connect('mongodb://localhost:27017', {
35-
useUnifiedTopology: true,
36-
});
34+
client = await mongodb.MongoClient.connect('mongodb://localhost:27017');
3735

3836
papr.initialize(client.db('test'));
3937

example/papr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const papr = new Papr();
66

77
export async function connect(): Promise<void> {
88
client = await mongodb.MongoClient.connect('mongodb://localhost:27017', {
9-
useUnifiedTopology: true,
9+
directConnection: true,
1010
});
1111

1212
papr.initialize(client.db('test'));

example/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ try {
2424
console.log(user1, user2);
2525
});
2626
} finally {
27-
session.endSession();
27+
await session.endSession();
2828
}
2929

3030
await disconnect();

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"@commitlint/cli": "12.1.4",
5959
"@commitlint/config-conventional": "12.1.4",
6060
"@types/jest": "26.0.23",
61-
"@types/mongodb": "3.6.17",
6261
"@typescript-eslint/eslint-plugin": "4.28.0",
6362
"@typescript-eslint/parser": "4.28.0",
6463
"arg": "5.0.0",
@@ -75,7 +74,7 @@
7574
"jsdoc-api": "7.0.1",
7675
"jsdoc-parse": "6.0.0",
7776
"lint-staged": "11.0.0",
78-
"mongodb": "3.6.10",
77+
"mongodb": "4.0.0",
7978
"mongodb-memory-server-global-4.4": "7.2.0",
8079
"mongoose": "5.12.12",
8180
"prettier": "2.3.0",
@@ -85,10 +84,7 @@
8584
"typescript": "4.3.2"
8685
},
8786
"peerDependencies": {
88-
"mongodb": "3.x"
89-
},
90-
"resolutions": {
91-
"mongodb": "3.6.10"
87+
"mongodb": "4.x"
9288
},
9389
"commitlint": {
9490
"extends": [

src/__tests__/model.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BulkWriteOperation, Collection, MongoError, ObjectId } from 'mongodb';
1+
import { AnyBulkWriteOperation, Collection, MongoError, ObjectId } from 'mongodb';
22
import { expectType } from 'ts-expect';
33
import { Hooks } from '../hooks';
44
import { abstract, build, Model } from '../model';
@@ -74,20 +74,13 @@ describe('model', () => {
7474
value: doc,
7575
}),
7676
insertMany: jest.fn().mockResolvedValue({
77+
acknowledged: true,
7778
insertedCount: 2,
78-
ops: [true, true],
79-
result: {
80-
n: 2,
81-
ok: 1,
82-
},
79+
insertedIds: [new ObjectId(), new ObjectId()],
8380
}),
8481
insertOne: jest.fn().mockResolvedValue({
85-
insertedCount: 1,
86-
ops: [true],
87-
result: {
88-
n: 1,
89-
ok: 1,
90-
},
82+
acknowledged: true,
83+
insertedId: new ObjectId(),
9184
}),
9285
updateMany: jest.fn().mockResolvedValue({
9386
modifiedCount: 1,
@@ -107,10 +100,12 @@ describe('model', () => {
107100

108101
// @ts-expect-error Ignore abstract assignment
109102
simpleModel = abstract(simpleSchema);
103+
// @ts-expect-error Ignore schema types
110104
build(simpleSchema, simpleModel, collection);
111105

112106
// @ts-expect-error Ignore abstract assignment
113107
timestampsModel = abstract(timestampsSchema);
108+
// @ts-expect-error Ignore schema types
114109
build(timestampsSchema, timestampsModel, collection);
115110
});
116111

@@ -144,7 +139,7 @@ describe('model', () => {
144139

145140
describe('bulkWrite', () => {
146141
test('simple schema', async () => {
147-
const operations: BulkWriteOperation<SimpleDocument>[] = [
142+
const operations: AnyBulkWriteOperation<SimpleDocument>[] = [
148143
{
149144
insertOne: {
150145
document: {
@@ -199,7 +194,7 @@ describe('model', () => {
199194
});
200195

201196
test('schema with defaults', async () => {
202-
const operations: BulkWriteOperation<SimpleDocument>[] = [
197+
const operations: AnyBulkWriteOperation<SimpleDocument>[] = [
203198
{
204199
insertOne: {
205200
document: {
@@ -873,6 +868,7 @@ describe('model', () => {
873868

874869
// @ts-expect-error Ignore abstract assignment
875870
hooksModel = abstract(simpleSchema);
871+
// @ts-expect-error Ignore schema types
876872
build(simpleSchema, hooksModel, collection, { hooks });
877873
});
878874

@@ -1163,6 +1159,7 @@ describe('model', () => {
11631159
beforeEach(() => {
11641160
// @ts-expect-error Ignore abstract assignment
11651161
maxTimeModel = abstract(simpleSchema);
1162+
// @ts-expect-error Ignore schema types
11661163
build(simpleSchema, maxTimeModel, collection, { maxTime: 1000 });
11671164
});
11681165

0 commit comments

Comments
 (0)