Skip to content

Instantly share code, notes, and snippets.

@dengue8830
Last active September 26, 2019 22:38
Show Gist options
  • Save dengue8830/9e5954761807196f63ab50e841e84813 to your computer and use it in GitHub Desktop.
Save dengue8830/9e5954761807196f63ab50e841e84813 to your computer and use it in GitHub Desktop.

Libs that i researched

sequelize-typescript | typeorm | knex | bookshelf | objection

Libs that i tested my self

sequelize-typescript | typeorm

Research

Basically i want to write raw sql and get a type safe object tree with relations eg. { a: { bs: [] } }, but i understand this is not possible out of the box and i need mapping with some ORM. So i'm looking for these features

  1. mapping one-to-many to an object tree, eg: A one-to-many B => { a: { bs: [] } }
  • all the orms get this
  • knex: not by default, you have to use a very curious hack https://github.com/CoursePark/NestHydrationJS i still don't konw if it's brillant or bad, with this kind of libs an orm could be not neccessary at all.
  1. mapping with just classes and not schemas. This is because its so tedious to write the same attrs twice and i end up avoiding the mapping step.
  • sequelize: classes and schema or just schema :/
  • sequelize-typescript: this get the work done.
  • typeorm: awesome! it have a repository pattern so i have a well separated code.
  • bookshelf: not my style objection: not my style
  1. typescript support
  • sequelize: yes, with both class and scheme definition :/ sequelize-typescript: this get the work done.
  • typeorm: awesome! <3
  • knex: yep, very nice
  • bookshelf: ?
  • objection: ?
  1. repository pattern is preferred
  • sequelize: just support active record.
  • sequelize-typescript: it enables this option but the class still extends of a superclass so your Person instances still have a lot of persistence attrs and methods.
  • typeorm: yeah <3 awesome support both patterns and is well done, you can extends a super class to get active record or doesn't to get respository enabled with clean class code.
  1. flexible query-mapping system: I want to be able to add custom columns (eg. select sum(id) as count) without loosing the object tree, the result should be { a: { count: 4, bs: [] } }
  • sequelize: the only one who get this done is sequelize (and obviously typescript-sequelize)
  • typeorm: is great but if you want to add a custom column to the select you have to get the raw results loosing the object tree :/
  1. nice query system: the API have to be easy to guess, human readable, easy to learn
  • sequelize: very nice, and very powerful (you can do very complex querys before fall into raw sql). It's not designed to think in sql.
  • typeorm: very nice, an awesome querybuilder similar to knex, very intuitve if you think in sql.
  • knex: very nice.
  1. mature query system: free of simplest bugs
  • sequelize: it have been a lot of time in the market, well done, the result is what i expected.
  • typeorm: it still have simple bugs like "the groupby issue" typeorm/typeorm#4814
  1. simple config
  • sequelize-typescript: i had to make some hacks but it works. To have the models working in tests env i had to write sequelize.models first, so weird.
  • typeorm: yeah! <3 it works as the doc says, no pain.
  1. performance
  • sequelize-typescript: i have used sequelize a year and yes, it generates weird queries but it doesn't seem to write wrong code, i didn't checked the excution plans because i never had a performance problem.
  • typeorm: the generated queries are simpler as possible, it doesn't look bad, same as above i didn't checked the execution plan.
  1. support to the most rdbms as possible, I use to work with mssql and mysql, but i don't want to learn 3 libs
  • sequelize: yep, so sequelize-typescript too
  • typeorm: yep <3
  • others doesn't

Conclusion:

I voted for sequelize-typescript because it have the highest scores in the important part mature and flexible querying system, type safe and easy and fast model definition (with @annotations).

I checked there is other very interesting options like photonjs, i will update this when i test it.

May be it's the time to give no-relational db's a chance

UPDATE

With libs like https://github.com/CoursePark/NestHydrationJS may be you don't need an ORM but it's still neccesary for

  • refactor (if you used some leve of type safe)
  • insert/update with all the attributes. In CRUDS we use to set all the attrs and it would be tedious if we have to set one by one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment