Skip to content

Commit 3c922ad

Browse files
committed
refactor(types): modernize all types, remove cyclic references
1 parent f8920c6 commit 3c922ad

21 files changed

+2075
-1000
lines changed

dist/bson.js

Lines changed: 1173 additions & 98 deletions
Large diffs are not rendered by default.

lib/binary.js

Lines changed: 257 additions & 260 deletions
Large diffs are not rendered by default.

lib/code.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
/**
44
* A class representation of the BSON Code type.
5-
*
6-
* @class
7-
* @param {(string|function)} code a string or function.
8-
* @param {Object} [scope] an optional scope for the function.
9-
* @return {Code}
105
*/
11-
function Code(code, scope) {
12-
if (!(this instanceof Code)) return new Code(code, scope);
13-
this._bsontype = 'Code';
14-
this.code = code;
15-
this.scope = scope;
16-
}
6+
class Code {
7+
/**
8+
* Create a Code type
9+
*
10+
* @param {(string|function)} code a string or function.
11+
* @param {Object} [scope] an optional scope for the function.
12+
* @return {Code}
13+
*/
14+
constructor(code, scope) {
15+
this._bsontype = 'Code';
16+
this.code = code;
17+
this.scope = scope;
18+
}
1719

18-
/**
19-
* @ignore
20-
*/
21-
Code.prototype.toJSON = function() {
22-
return { scope: this.scope, code: this.code };
23-
};
20+
/**
21+
* @ignore
22+
*/
23+
toJSON() {
24+
return { scope: this.scope, code: this.code };
25+
}
26+
}
2427

2528
module.exports = Code;
26-
module.exports.Code = Code;

lib/db_ref.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
'use strict';
22
/**
33
* A class representation of the BSON DBRef type.
4-
*
5-
* @class
6-
* @param {string} collection the collection name.
7-
* @param {ObjectID} oid the reference ObjectID.
8-
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
9-
* @return {DBRef}
104
*/
11-
function DBRef(collection, oid, db, fields) {
12-
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
5+
class DBRef {
6+
/**
7+
* Create a DBRef type
8+
*
9+
* @param {string} collection the collection name.
10+
* @param {ObjectID} oid the reference ObjectID.
11+
* @param {string} [db] optional db name, if omitted the reference is local to the current db.
12+
* @return {DBRef}
13+
*/
14+
constructor(collection, oid, db, fields) {
15+
// check if namespace has been provided
16+
const parts = collection.split('.');
17+
if (parts.length === 2) {
18+
db = parts.shift();
19+
collection = parts.shift();
20+
}
1321

14-
// check if namespace has been provided
15-
var parts = collection.split('.');
16-
if (parts.length === 2) {
17-
db = parts.shift();
18-
collection = parts.shift();
22+
this._bsontype = 'DBRef';
23+
this.collection = collection;
24+
this.oid = oid;
25+
this.db = db;
26+
this.fields = fields || {};
1927
}
2028

21-
this._bsontype = 'DBRef';
22-
this.collection = collection;
23-
this.oid = oid;
24-
this.db = db;
25-
this.fields = fields || {};
26-
}
27-
28-
/**
29-
* @ignore
30-
* @api private
31-
*/
32-
DBRef.prototype.toJSON = function() {
33-
var o = {
34-
$ref: this.collection,
35-
$id: this.oid
36-
};
29+
/**
30+
* @ignore
31+
* @api private
32+
*/
33+
toJSON() {
34+
const o = Object.assign(
35+
{
36+
$ref: this.collection,
37+
$id: this.oid
38+
},
39+
this.fields
40+
);
3741

38-
if (this.db != null) o.$db = this.db;
39-
o = Object.assign(o, this.fields);
40-
return o;
41-
};
42+
if (this.db != null) o.$db = this.db;
43+
return o;
44+
}
45+
}
4246

4347
module.exports = DBRef;
44-
module.exports.DBRef = DBRef;

0 commit comments

Comments
 (0)