Supported ORM - v22-appfactory/appfactory-wiki GitHub Wiki

Define the problem:

The ORM that is currently being used in the project, Sequelize, does not support Oracle database at this time. A different ORM or query builder will be needed and every database call made by Sequelize will need to be replaced.

Our options:

There are two viable options to replace Sequelize, Knex.js and TypeORM.

TypeORM:

Pro's:

  • Is a full ORM, allowing for Modeling of database objects.
  • Slightly simpler syntax for some database calls
  • Can define relations between objects

Con's:

  • TypeORM is intended for projects that are using TypeScript
  • Hard to find documentation on using TypeORM in a javascript project beyond the most basic uses
  • Installing TypeScript would present a new set of challenges

Knex.js:

Pro's:

  • Easy to install in our project
  • Works well with Oracle
  • Good documentation

Con's:

  • Not an ORM, but is a query builder
  • Does not use object models
  • Queries may be slightly longer and more complex
  • Slightly longer development time

Recommendation:

While TypeORM is a full ORM like Sequelize, and provides all the same features and functionality, it would not be a great fit for our project because it is intended for use with TypeScript projects. We would miss out on all the benefits of using TypeScript, not have good documentation and it would take longer to implement.

Knex.js is the better choice for our project. There are no significant challenges to using it in our project and will allow for faster development time vs TypeORM.

Examples:

The following examples come from the bunoController.js that was switched to use Knex instead of Sequelize.

appBunosFindAll

Sequelize

Screen Shot 2022-09-02 at 3 44 48 PM

Knex

Screen Shot 2022-09-02 at 3 46 35 PM

The Sequelize query is making use of the relations that have been defined between Buno and AppBunos in the models to join the two tables.

The Knex query must specify how to join the tables explicitly.

appBunosBulkAdd

Sequelize

Screen Shot 2022-09-02 at 3 56 05 PM

Knex

Screen Shot 2022-09-02 at 3 57 06 PM

In this example the Sequelize query makes a call a database function, appbunosBulkAdd, and passes in the arguments to handle adding the array of appbunos to the table.

The Knex query can insert an array of items into a table and return an array, the id's of all inserted records in this case.