Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(NODE-4862): add BSONType enum and remove internal constants from export #532

Merged
merged 4 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 1 addition & 36 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,6 @@ import { Timestamp } from './timestamp';
import { ByteUtils } from './utils/byte_utils';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export {
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
BSON_BINARY_SUBTYPE_DEFAULT,
BSON_BINARY_SUBTYPE_FUNCTION,
BSON_BINARY_SUBTYPE_MD5,
BSON_BINARY_SUBTYPE_USER_DEFINED,
BSON_BINARY_SUBTYPE_UUID,
BSON_BINARY_SUBTYPE_UUID_NEW,
BSON_BINARY_SUBTYPE_ENCRYPTED,
BSON_BINARY_SUBTYPE_COLUMN,
BSON_DATA_ARRAY,
BSON_DATA_BINARY,
BSON_DATA_BOOLEAN,
BSON_DATA_CODE,
BSON_DATA_CODE_W_SCOPE,
BSON_DATA_DATE,
BSON_DATA_DBPOINTER,
BSON_DATA_DECIMAL128,
BSON_DATA_INT,
BSON_DATA_LONG,
BSON_DATA_MAX_KEY,
BSON_DATA_MIN_KEY,
BSON_DATA_NULL,
BSON_DATA_NUMBER,
BSON_DATA_OBJECT,
BSON_DATA_OID,
BSON_DATA_REGEXP,
BSON_DATA_STRING,
BSON_DATA_SYMBOL,
BSON_DATA_TIMESTAMP,
BSON_DATA_UNDEFINED,
BSON_INT32_MAX,
BSON_INT32_MIN,
BSON_INT64_MAX,
BSON_INT64_MIN
} from './constants';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
Expand Down Expand Up @@ -92,6 +56,7 @@ export {
ObjectId as ObjectID
};
export { BSONError, BSONTypeError } from './error';
export { BSONType } from './constants';

/** @public */
export interface Document {
Expand Down
28 changes: 28 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,31 @@ export const BSON_BINARY_SUBTYPE_COLUMN = 7;

/** Binary User Defined Type @internal */
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;

/** @public */
export const BSONType = Object.freeze({
double: 1,
string: 2,
object: 3,
array: 4,
binData: 5,
undefined: 6,
objectId: 7,
bool: 8,
date: 9,
null: 10,
regex: 11,
dbPointer: 12,
javascript: 13,
symbol: 14,
javascriptWithScope: 15,
int: 16,
timestamp: 17,
long: 18,
decimal: 19,
minKey: -1,
maxKey: 127
} as const);

/** @public */
export type BSONType = typeof BSONType[keyof typeof BSONType];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the enum pattern here, it makes BSONType the annotation equal to a union of the values.
BSONType the runtime value is an object with properties.

9 changes: 5 additions & 4 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { Buffer } = require('buffer');
const BSON = require('../register-bson');
const { BSON_INT32_MAX, BSON_BINARY_SUBTYPE_USER_DEFINED } = require('../../src/constants');
const Code = BSON.Code;
const BSONRegExp = BSON.BSONRegExp;
const Binary = BSON.Binary;
Expand Down Expand Up @@ -434,7 +435,7 @@ describe('BSON', function () {
* @ignore
*/
it('Should Correctly Serialize and Deserialize Number 4', function (done) {
var doc = { doc: BSON.BSON_INT32_MAX + 10 };
var doc = { doc: BSON_INT32_MAX + 10 };
var serialized_data = BSON.serialize(doc);

var serialized_data2 = Buffer.alloc(BSON.calculateObjectSize(doc));
Expand All @@ -443,7 +444,7 @@ describe('BSON', function () {

var deserialized = BSON.deserialize(serialized_data);
// expect(deserialized.doc instanceof Binary).to.be.ok;
expect(BSON.BSON_INT32_MAX + 10).to.equal(deserialized.doc);
expect(BSON_INT32_MAX + 10).to.equal(deserialized.doc);
done();
});

Expand Down Expand Up @@ -853,7 +854,7 @@ describe('BSON', function () {
*/
it('Should Correctly Serialize and Deserialize a User defined Binary object', function (done) {
var bin = new Binary();
bin.sub_type = BSON.BSON_BINARY_SUBTYPE_USER_DEFINED;
bin.sub_type = BSON_BINARY_SUBTYPE_USER_DEFINED;
var string = 'binstring';
for (var index = 0; index < string.length; index++) {
bin.put(string.charAt(index));
Expand All @@ -867,7 +868,7 @@ describe('BSON', function () {
assertBuffersEqual(done, serialized_data, serialized_data2, 0);
var deserialized_data = BSON.deserialize(serialized_data);

expect(deserialized_data.doc.sub_type).to.deep.equal(BSON.BSON_BINARY_SUBTYPE_USER_DEFINED);
expect(deserialized_data.doc.sub_type).to.deep.equal(BSON_BINARY_SUBTYPE_USER_DEFINED);
expect(doc.doc.value()).to.deep.equal(deserialized_data.doc.value());
done();
});
Expand Down
142 changes: 0 additions & 142 deletions test/node/check_constants.js

This file was deleted.

Loading