Skip to content

Commit f1cccf2

Browse files
nbbeekendurran
andauthored
feat!(NODE-4704): remove deprecated ObjectId methods (#525)
Co-authored-by: Durran Jordan <[email protected]>
1 parent 14a7473 commit f1cccf2

File tree

4 files changed

+39
-61
lines changed

4 files changed

+39
-61
lines changed

docs/upgrade-to-v5.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@ This library no longer polyfills [ES Map](https://developer.mozilla.org/en-US/do
8787
### `Decimal128` `toObject()` mapper support removed
8888

8989
`Decimal128` can no longer have a `toObject()` method added on to its prototype for mapping to a custom value. This feature was undocumented and inconsistent with the rest of our BSON types. At this time there is no direct migration: cursors in the driver support transformations via `.map`, otherwise the `Decimal128` instances will require manual transformation. There is a plan to provide a better mechanism for consistently transforming BSON values tracked in [NODE-4680](https://jira.mongodb.org/browse/NODE-4680), please feel free to add a vote or comment with a use case to help us land the feature in the most useful form.
90+
91+
### Remove deprecated ObjectId methods
92+
93+
The following deprecated methods have been removed:
94+
95+
- `ObjectId.prototype.generate`
96+
- Instead, generate a new ObjectId with the constructor: `new ObjectId()` or using the `static generate(time?: number)` method.
97+
- `ObjectId.prototype.generationTime`
98+
- Instead, use `static createFromTime()` and `getTimestamp()` to set and inspect these values on an `ObjectId()`
99+
- `ObjectId.prototype.getInc`
100+
- `ObjectId.prototype.get_inc`
101+
- `ObjectId.get_inc`
102+
- The `static getInc()` is private since invoking it increments the next `ObjectId` index, so invoking would impact the creation of subsequent ObjectIds.

src/objectid.ts

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BSONTypeError } from './error';
2-
import { deprecate, isUint8Array, randomBytes } from './parser/utils';
2+
import { isUint8Array, randomBytes } from './parser/utils';
33
import { BSONDataView, ByteUtils } from './utils/byte_utils';
44

55
// Regular expression that checks for hex value
@@ -33,7 +33,7 @@ export class ObjectId {
3333
}
3434

3535
/** @internal */
36-
static index = Math.floor(Math.random() * 0xffffff);
36+
private static index = Math.floor(Math.random() * 0xffffff);
3737

3838
static cacheHexString: boolean;
3939

@@ -113,19 +113,6 @@ export class ObjectId {
113113
}
114114
}
115115

116-
/**
117-
* The generation time of this ObjectId instance
118-
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
119-
*/
120-
get generationTime(): number {
121-
return BSONDataView.fromUint8Array(this.id).getUint32(0, false);
122-
}
123-
124-
set generationTime(value: number) {
125-
// Encode time into first 4 bytes
126-
BSONDataView.fromUint8Array(this.id).setUint32(0, value, false);
127-
}
128-
129116
/** Returns the ObjectId id as a 24 character hex string representation */
130117
toHexString(): string {
131118
if (ObjectId.cacheHexString && this.__id) {
@@ -143,11 +130,9 @@ export class ObjectId {
143130

144131
/**
145132
* Update the ObjectId index
146-
* @privateRemarks
147-
* Used in generating new ObjectId's on the driver
148133
* @internal
149134
*/
150-
static getInc(): number {
135+
private static getInc(): number {
151136
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
152137
}
153138

@@ -330,23 +315,3 @@ export class ObjectId {
330315
return `new ObjectId("${this.toHexString()}")`;
331316
}
332317
}
333-
334-
// Deprecated methods
335-
Object.defineProperty(ObjectId.prototype, 'generate', {
336-
value: deprecate(
337-
(time: number) => ObjectId.generate(time),
338-
'Please use the static `ObjectId.generate(time)` instead'
339-
)
340-
});
341-
342-
Object.defineProperty(ObjectId.prototype, 'getInc', {
343-
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
344-
});
345-
346-
Object.defineProperty(ObjectId.prototype, 'get_inc', {
347-
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
348-
});
349-
350-
Object.defineProperty(ObjectId, 'get_inc', {
351-
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
352-
});

src/parser/utils.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ByteUtils } from '../utils/byte_utils';
22
import { getGlobal } from '../utils/global';
33

44
type RandomBytesFunction = (size: number) => Uint8Array;
5+
declare let console: { warn(...message: unknown[]): void };
56

67
/**
78
* Normalizes our expected stringified form of a function across versions of node
@@ -107,16 +108,3 @@ export function isDate(d: unknown): d is Date {
107108
export function isObjectLike(candidate: unknown): candidate is Record<string, unknown> {
108109
return typeof candidate === 'object' && candidate !== null;
109110
}
110-
111-
declare let console: { warn(...message: unknown[]): void };
112-
export function deprecate<T extends Function>(fn: T, message: string): T {
113-
let warned = false;
114-
function deprecated(this: unknown, ...args: unknown[]) {
115-
if (!warned) {
116-
console.warn(message);
117-
warned = true;
118-
}
119-
return fn.apply(this, args);
120-
}
121-
return deprecated as unknown as T;
122-
}

test/node/object_id_tests.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,30 @@ const getSymbolFrom = require('./tools/utils').getSymbolFrom;
99
const isBufferOrUint8Array = require('./tools/utils').isBufferOrUint8Array;
1010

1111
describe('ObjectId', function () {
12-
it('should correctly handle objectId timestamps', function (done) {
13-
const a = ObjectId.createFromTime(1);
14-
expect(Buffer.from([0, 0, 0, 1])).to.deep.equal(a.id.slice(0, 4));
15-
expect(1000).to.equal(a.getTimestamp().getTime());
12+
describe('static createFromTime()', () => {
13+
it('creates an objectId with user defined value in the timestamp field', function () {
14+
const a = ObjectId.createFromTime(1);
15+
expect(a.id.slice(0, 4)).to.deep.equal(Buffer.from([0, 0, 0, 1]));
16+
expect(a.getTimestamp()).to.deep.equal(new Date(1 * 1000));
17+
expect(a.getTimestamp().getTime()).to.equal(1000);
18+
});
19+
});
1620

17-
const b = new ObjectId();
18-
b.generationTime = 1;
19-
expect(Buffer.from([0, 0, 0, 1])).to.deep.equal(b.id.slice(0, 4));
20-
expect(1).to.equal(b.generationTime);
21-
expect(1000).to.equal(b.getTimestamp().getTime());
21+
describe('getTimestamp()', () => {
22+
it('fetches the big endian int32 leading the Oid and create a Date instance', function () {
23+
const a = new ObjectId('00000002' + '00'.repeat(8));
24+
expect(a.id.slice(0, 4)).to.deep.equal(Buffer.from([0, 0, 0, 2]));
25+
expect(Object.prototype.toString.call(a.getTimestamp())).to.equal('[object Date]');
26+
expect(a.getTimestamp()).to.deep.equal(new Date(2 * 1000));
27+
expect(a.getTimestamp().getTime()).to.equal(2000);
28+
});
29+
});
2230

23-
done();
31+
it('creates an objectId with user defined value in the timestamp field', function () {
32+
const a = ObjectId.createFromTime(1);
33+
expect(a.id.slice(0, 4)).to.deep.equal(Buffer.from([0, 0, 0, 1]));
34+
expect(a.getTimestamp()).to.deep.equal(new Date(1 * 1000));
35+
expect(a.getTimestamp().getTime()).to.equal(1000);
2436
});
2537

2638
it('should correctly create ObjectId from ObjectId', function () {

0 commit comments

Comments
 (0)