Skip to content

Commit 5a528f6

Browse files
authored
docs(NODE-4561): update readme (#6)
1 parent 41437ea commit 5a528f6

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2022 MongoDB
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

readme.md

+102-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,107 @@
1-
# MongoDB Node.js Callback Legacy Package
1+
# MongoDB Node.js Driver with Optional Callback Support Legacy Package
22

3-
# Work In Progress - Not intended for use at this time
3+
**Attention :memo:**
44

5-
Here is the legacy package for callback support in the MongoDB Driver. Similar to how Node.js has begun to ship promise alternatives to our well known stdlib APIs:
5+
This is a wrapper of the `mongodb` driver, if you are starting a new project you likely just want to use the driver directly:
66

7-
```js
8-
const fs = require('fs/promises')
9-
await fs.readFile('...')
7+
- [Driver Source](https://github.com/mongodb/node-mongodb-native/)
8+
- [Driver NPM Package](https://www.npmjs.com/package/mongodb)
9+
10+
## Purpose
11+
12+
This package is intended to assist in migrating to promise based APIs.
13+
We have wrapped every driver method to continue offering the optional callback support some projects may be relying on to incrementally migrate to promises.
14+
Any new APIs added to the driver will not offer optional callback support.
15+
If callback usage is needed for any new APIs, we would suggest using `.then`/`.catch` or node's [callbackify](https://nodejs.org/dist/latest-v16.x/docs/api/util.html#utilcallbackifyoriginal) to get that desired API.
16+
17+
The main driver package `mongodb` will be dropping optional callback support in the next major version (v5) in favor of `async`/`await` syntax.
18+
19+
### Example usage of equivalent callback and promise usage
20+
21+
```ts
22+
// Just add '-legacy' to my mongodb import
23+
import { MongoClient } from 'mongodb-legacy';
24+
const client = new MongoClient();
25+
const db = client.db();
26+
const collection = db.collection('pets');
27+
28+
// Legacy projects may have intermixed API usage:
29+
app.get('/endpoint_promises', (req, res) => {
30+
collection
31+
.findOne({})
32+
.then(result => {
33+
res.end(JSON.stringify(result));
34+
})
35+
.catch(error => {
36+
res.errorHandling(error);
37+
});
38+
});
39+
40+
app.get('/endpoint_callbacks', (req, res) => {
41+
collection.findOne({}, (error, result) => {
42+
if (error) return res.errorHandling(error);
43+
res.end(JSON.stringify(result));
44+
});
45+
});
1046
```
1147

12-
We are soon shipping our `'mongodb'` with promise only APIs, but this package can help those who have difficulty adopting that change by continuing to offer our API in it's existing form a combination of callback and promise support. We hope that this module will require only the change of the import string from `'mongodb'` to `'mongodb-legacy'` along with adding `'mongodb-legacy'` to your `package.json`. Our intent is to ensure that the existing APIs offer precisely the same behavior as before, the logic for handling callback or promise been moved to these light wrappers. Please let us know if you encounter any differences if you have need of this package.
48+
## How to install
49+
50+
In your existing project add `mongodb-legacy` to your `package.json` with the following command.
51+
52+
```sh
53+
npm install mongodb-legacy
54+
```
55+
56+
### Versioning
57+
58+
We recommend replacing your `mongodb` dependency with this one.
59+
This package uses caret semver range for the main `mongodb` package, (ex. `^4.10.0`) which will adopt minor version bumps as they are released.
60+
61+
The next major release of the driver (v5) will drop support for callbacks.
62+
This package will also have a major release at that time to update the dependency requirement to `^5.0.0`.
63+
Users can expect to be able to upgrade to `v5` adopting the changes and features shipped in that version while using this module to maintain any callback code they still need to work on migrating.
64+
65+
## [API](https://mongodb.github.io/node-mongodb-native/)
66+
67+
The API is inherited from the driver, which is [documented here](https://mongodb.github.io/node-mongodb-native/).
68+
If it is relevant to inspect the precise differences, you can peruse the [type definitions of wrapped APIs](https://github.com/mongodb-js/nodejs-mongodb-legacy/blob/main/mongodb-legacy.d.ts).
69+
70+
The wrappers are implemented as subclasses of each of the existing driver's classes with extra logic to handle the optional callback behavior. And all other driver exports are re-exported directly from the wrapper. This means any new APIs added to the driver will be automatically pulled in as long as the updated driver is installed.
71+
72+
Take this hypothetical example:
73+
74+
```ts
75+
// Just add '-legacy' to my mongodb import
76+
import { MongoClient } from 'mongodb-legacy';
77+
const client = new MongoClient();
78+
const db = client.db();
79+
80+
const collection = db.collection('pets');
81+
// Returns an instance of LegacyFindCursor which is a subclass of FindCursor
82+
const dogCursor = collection.find({ kind: 'dog' });
83+
// Using .next with callbacks still works!
84+
dogCursor.next((error, dog) => {
85+
if (error) return handling(error);
86+
console.log(dog);
87+
});
88+
// Brand new hypothetical api that pets all dogs! (does not support callbacks)
89+
dogCursor.petAll().then(result => {
90+
console.log('all dogs got pats!');
91+
});
92+
```
93+
94+
> NOTE: The `petAll()` api is brand new in this hypothetical example and so would not support an optional callback. If adopting this API is deep inside code that already relies on eventually calling a callback to indicate the end of an operation it is possible to use `.then`/`.catch` chains to handle the cases that are needed. We recommend offloading this complexity to node's [callbackify utility](https://nodejs.org/dist/latest-v16.x/docs/api/util.html#utilcallbackifyoriginal): `callbackify(() => dogCursor.petAll())(callback)`
95+
96+
The new example `petAll()` API will be pulled in since we're building off the existing driver API.
97+
The typescript definitions work the same way so `next()` still reports its promise and callback variants and the `petAll()` API is pulled in from the driver's definitions.
98+
99+
## Bugs or Features
100+
101+
You can reach out on our JIRA: https://jira.mongodb.org/projects/NODE and let us know any issues your run into while using this package.
102+
103+
## License
104+
105+
[Apache 2.0](https://github.com/mongodb-js/nodejs-mongodb-legacy/blob/main/LICENSE)
106+
107+
:copyright: 2022-present MongoDB

0 commit comments

Comments
 (0)