13

I've been working with active record and data mapper implementations of ORM enough to know the problems with using active record implemented ORM in my large projects. Right now I'm thinking to migrate one of my projects to node.js and trying to find the similar tools Im using right now. After research I didn't found any node.js ORM that follows data mapper pattern. They all are active record. Maybe I missing something, and you can tell me is there is a good popular ORM for node.js that doesn't follow active record pattern?

The libraries Ive looked on:

pleerock
  • 16,223
  • 14
  • 93
  • 116
  • Try [StrongLoop](http://strongloop.com/), you can map types to tables and generate a REST client to be used with the REST API generated by the mappings. Dont know if this is what you are looking for but doesnt hurt putting it out there as a comment? :) – furier Apr 12 '15 at 19:11
  • 1
    @furier it doesnt look like orm – pleerock Apr 13 '15 at 07:21

3 Answers3

24

After lot of frustration of currently exist ORMs for JavaScript I have written my own ORM that supports TypeScript / ES6 / ES5 and follows data mapper patterns and all other best practices - TypeORM

pleerock
  • 16,223
  • 14
  • 93
  • 116
4

I wrote an ORM for Node.js called node-data-mapper; it's available here: https://www.npmjs.com/package/node-data-mapper. It's an ORM for Node.js that uses the data-mapper pattern. The developer uses plain old JavaScript objects when reading from and writing to the database. Relationships between tables are not rigidly defined, which makes joining very flexible--in my opinion, anyway--albeit somewhat verbose. The actual data mapping algorithm is fast and short, and the complexity is linear (the transformation from tabular DB data to a normalized JavaScript object is done in one loop).

I also did my best to make it fairly fault tolerant. There's 100% code coverage and, while I know that doesn't prove the absence of defects, I did try to test as thoroughly as possible.

I modeled the interface very loosely after Doctrine 1. (I've used LINQ, Doctrine 1 and 2, and Hibernate fairly extensively, and of those ORMs I like the interface for Doctrine 1 the best. node-data-mapper is not a JavaScript port of Doctrine by any means, though, and the interface is significantly different.) The query interface returns promises using the deferred module.

I modeled the conditions (e.g. WHERE and ON clauses) after MongoDB's conditions. Hopefully that makes the conditions somewhat intuitive while providing a way for making reusable queries (specifically, complex SELECT queries that can be filtered securely in many different ways). The conditions are treated as a domain-specific language, and are lexed, parsed, and compiled.

Anyway, the module is something that I use in my personal projects, but I'd love to get some feedback from other developers in the community! I tried to provide plenty of examples to get people up and running quickly. Currently the module supports MySQL only, but I'm working on adding support for MSSQL.

benbotto
  • 1,710
  • 1
  • 15
  • 29
-7

The distinction between data-mapper pattern and active record doesn't really make sense in the dynamic language such as JavaScript. Typically data mapper is more lightweight in typed language, but in JS it does not really make a difference. From the top of my head, I can mention two very popular projects which you probably don't know:

Waterline.js is a Sails abstraction, which works quite well on top of many database systems.

If you would consider MongoDB for your DB - Mongoose.js.

Capaj
  • 3,334
  • 2
  • 34
  • 44
  • 5
    actually it does make sense in javascript too. I have already worked with client side javascript libraries that follows active record pattern and they just was killing my app util I didnt rewrite the same libraries into data mapper style – pleerock Apr 13 '15 at 07:23
  • 5
    active record and data mapper are completely different patterns with different designs. Doesn't matter if it's JavaScript or Java, the pattern is likely the same. – Fuad Saud Nov 05 '15 at 00:04