db_ref.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DBRef = exports.isDBRefLike = void 0;
  4. var utils_1 = require("./parser/utils");
  5. /** @internal */
  6. function isDBRefLike(value) {
  7. return (utils_1.isObjectLike(value) &&
  8. value.$id != null &&
  9. typeof value.$ref === 'string' &&
  10. (value.$db == null || typeof value.$db === 'string'));
  11. }
  12. exports.isDBRefLike = isDBRefLike;
  13. /**
  14. * A class representation of the BSON DBRef type.
  15. * @public
  16. * @category BSONType
  17. */
  18. var DBRef = /** @class */ (function () {
  19. /**
  20. * @param collection - the collection name.
  21. * @param oid - the reference ObjectId.
  22. * @param db - optional db name, if omitted the reference is local to the current db.
  23. */
  24. function DBRef(collection, oid, db, fields) {
  25. if (!(this instanceof DBRef))
  26. return new DBRef(collection, oid, db, fields);
  27. // check if namespace has been provided
  28. var parts = collection.split('.');
  29. if (parts.length === 2) {
  30. db = parts.shift();
  31. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  32. collection = parts.shift();
  33. }
  34. this.collection = collection;
  35. this.oid = oid;
  36. this.db = db;
  37. this.fields = fields || {};
  38. }
  39. Object.defineProperty(DBRef.prototype, "namespace", {
  40. // Property provided for compatibility with the 1.x parser
  41. // the 1.x parser used a "namespace" property, while 4.x uses "collection"
  42. /** @internal */
  43. get: function () {
  44. return this.collection;
  45. },
  46. set: function (value) {
  47. this.collection = value;
  48. },
  49. enumerable: false,
  50. configurable: true
  51. });
  52. DBRef.prototype.toJSON = function () {
  53. var o = Object.assign({
  54. $ref: this.collection,
  55. $id: this.oid
  56. }, this.fields);
  57. if (this.db != null)
  58. o.$db = this.db;
  59. return o;
  60. };
  61. /** @internal */
  62. DBRef.prototype.toExtendedJSON = function (options) {
  63. options = options || {};
  64. var o = {
  65. $ref: this.collection,
  66. $id: this.oid
  67. };
  68. if (options.legacy) {
  69. return o;
  70. }
  71. if (this.db)
  72. o.$db = this.db;
  73. o = Object.assign(o, this.fields);
  74. return o;
  75. };
  76. /** @internal */
  77. DBRef.fromExtendedJSON = function (doc) {
  78. var copy = Object.assign({}, doc);
  79. delete copy.$ref;
  80. delete copy.$id;
  81. delete copy.$db;
  82. return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
  83. };
  84. /** @internal */
  85. DBRef.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  86. return this.inspect();
  87. };
  88. DBRef.prototype.inspect = function () {
  89. // NOTE: if OID is an ObjectId class it will just print the oid string.
  90. var oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
  91. return "new DBRef(\"" + this.namespace + "\", new ObjectId(\"" + oid + "\")" + (this.db ? ", \"" + this.db + "\"" : '') + ")";
  92. };
  93. return DBRef;
  94. }());
  95. exports.DBRef = DBRef;
  96. Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
  97. //# sourceMappingURL=db_ref.js.map