SierraSoftworks / Iridium

A high performance MongoDB ORM for Node.js
http://sierrasoftworks.github.io/Iridium/
Other
569 stars 25 forks source link

Support MongoDB's DBRef #65

Open bdfoster opened 7 years ago

bdfoster commented 7 years ago

While in most cases, manual referencing is a better alternative to using DBRef (https://docs.mongodb.com/manual/reference/database-references/#dbref-explanation), there are many use cases where it is preferred.

The Node.js driver does support DBRef (http://mongodb.github.io/node-mongodb-native/api-bson-generated/db_ref.html) and this could be implemented as a decorator.

DBRefs do take a performance hit, so this isn't something that should be integrated tightly as a default for resolving other documents. It can add some bloat to the referencing document and causes additional queries automatically (but local to the mongod instance itself).

As this may not be a good fit for the project, is there a good way to implement this as a module/separate project? If it does fit for this project, I'd be willing to work up a PR for this feature (with a little guidance).

notheotherben commented 7 years ago

Looks like a very nice extension, Iridium is meant to offer a 1:1 map of MongoDB's core feature set, so I think it's definitely worth adding it. I also agree that it shouldn't be a default and I'd even take it one step further and still make it entirely explicit (i.e. don't obscure it behind any mapping stuff) - but a decorator should be perfect.

As far as the decorator's API goes, I think the nicest approach would be something like this:

class MyModel extends Instance<MyDoc, MyModel> {
  @ObjectID
  _id: string;

  @DBRef("users")
  user: string;
}

As far as the implementation goes, it could work much the same as the ObjectID decorator currently does (emulating a Property and Transform decorator pairing), in this case it would transform from a (value: string) => { $ref: collection, $id: value } (and perhaps, optionally have a db argument) as well as from a (value: { $ref: string; $id: string; $db?: string }) => value.$ref. Validation would be identical to the current ObjectID validator (and you could probably use the same property type there without issues).

If you'd like the practice and want to work on the PR yourself, I'd be completely happy to help mentor you along that road, otherwise let me know and I can throw it together for you myself.

Kind regards, Benjamin

bdfoster commented 7 years ago

With the holidays coming up, I wouldn't be able to get to it until after New Year's, but I'd be interested in taking that challenge on.

On Dec 20, 2016 5:11 PM, "Benjamin Pannell" notifications@github.com wrote:

Looks like a very nice extension, Iridium is meant to offer a 1:1 map of MongoDB's core feature set, so I think it's definitely worth adding it. I also agree that it shouldn't be a default and I'd even take it one step further and still make it entirely explicit (i.e. don't obscure it behind any mapping stuff) - but a decorator should be perfect.

As far as the decorator's API goes, I think the nicest approach would be something like this:

class MyModel extends Instance<MyDoc, MyModel> { @ObjectID _id: string;

@DBRef("users") user: string; }

As far as the implementation goes, it could work much the same as the ObjectID decorator currently does (emulating a Property and Transform decorator pairing), in this case it would transform from a (value: string) => { $ref: collection, $id: value } (and perhaps, optionally have a db argument) as well as from a (value: { $ref: string; $id: string; $db?: string }) => value.$ref. Validation would be identical to the current ObjectID validator (and you could probably use the same property type there without issues).

If you'd like the practice and want to work on the PR yourself, I'd be completely happy to help mentor you along that road, otherwise let me know and I can throw it together for you myself.

Kind regards, Benjamin

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SierraSoftworks/Iridium/issues/65#issuecomment-268374186, or mute the thread https://github.com/notifications/unsubscribe-auth/ACA19rAXsVu4bVoBl31IoZz9sQdeQPhrks5rKFKfgaJpZM4LSRMr .

notheotherben commented 7 years ago

Of course, I don't think there's anybody else waiting on the functionality - so for now I'll leave it in your court, let me know if you have any questions or would like some advice/input.

If I get anybody else asking for this in the mean time, I'll let you know here and we can figure out what the best plan of action is.

Enjoy the holidays and all the best, Benjamin

contrasam commented 7 years ago

@SPARTAN563 Hi I was also looking for this feature, @bdfoster have you started working on a PR for this? I am also interested in contributing :-)

notheotherben commented 7 years ago

Okay, I'm busy this evening I'm afraid but I can try and take a look at implementing it for you some time this week - let me know if that works for you. Of course, if either you or @bdfoster would prefer to do so then I'm more than happy to give you the opportunity.

Regards, Benjamin