Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Abstracting mongoose model from domain model

I am building a rest API in node. I am following a general ddd architecture ie repositories, domain models, entities, value objects etc. I chose montodb for my persistence needs and am using mongoose to interface with the dB engine.

In mongoose we create models from a defined schema. I am trying to figure out how I can separate my domain model out from the mongoose model. I want to implement value objects and I don't see how I can do this with mongoose models.

I cant find any information on how I can do this anywhere. I am wondering if it is I'll advised perhaps. I can add methods to a mongoose model so I guess it is acting as a domain model. If this is the case, then how might I implement value objects?

like image 530
apostrophedottilde Avatar asked Dec 25 '17 21:12

apostrophedottilde


1 Answers

Mongoose models are not pure (they have a dependency to infrastructure and some methods - like save - have side effects). I don't think you could have a 100% pure DDD architecture using this library.

On the other side, using plain JavaScript objects as models (Entities, Aggregates and Value Objects) with MongoDB persistence makes perfect sense. There is 100% impedance match between them.

Here is an extras from the MongoDB Javascript driver:

Mongo DB data types

So there is an important thing to keep in mind when working with Mongo DB, and that is the slight mapping difference between types Mongo DB supports and native JavaScript data types. Let’s have a look at the types supported out of the box and then how types are promoted by the driver to fit as close to native JavaScript types as possible.

  • Float is a 8 byte and is directly convertible to the Javascript type Number

  • Double class a special class representing a float value, this is especially useful when using capped collections where you need to ensure your values are always floats.

  • Integers is a bit trickier due to the fact that Javascript represents all Numbers as 64 bit floats meaning that the maximum integer value is at a 53 bit. Mongo has two types for integers, a 32 bit and a 64 bit. The driver will try to fit the value into 32 bits if it can and promote it to 64 bits if it has to. Similarly it will deserialize attempting to fit it into 53 bits if it can. If it cannot it will return an instance of Long to avoid losing precision.

  • Long class a special class that lets you store 64 bit integers and also lets you operate on the 64 bit integers.

  • Date maps directly to a Javascript Date

  • RegExp maps directly to a Javascript RegExp

  • String maps directly to a Javascript String (encoded in utf8)

  • Binary class a special class that lets you store data in Mongo DB Code class a special class that lets you store javascript functions in Mongo DB, can also provide a scope to run the method in

  • ObjectID class a special class that holds a MongoDB document identifier (the equivalent to a Primary key)

  • DbRef class a special class that lets you include a reference in a document pointing to another object

  • Symbol class a special class that lets you specify a symbol, not really relevant for JavaScript but for languages that supports the concept of symbols.

like image 199
Constantin Galbenu Avatar answered Sep 22 '22 17:09

Constantin Galbenu