db_ref.js 3.2 KB

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