Node.js ORM written in TypeScript for type lovers.

View the Project on GitHub twinlogix/typetta

Sorts

The findOne, findAll, and findPage APIs support result sorting functionality.

Below is a simple example of use where users are searched and sorted by date of birth:

const users = await entityManager.user.findAll({
  sorts: [{
    birthDate: 'desc'
  }]
})

As shown in the example, the sort direction is defined by a simple literal TypeScript and can be asc or desc. The sorts parameter is an ordered array in which you can specify a number of sort criteria, which are applied one after the other.

Note that you can also filter by embedded entity fields, with the same notation that you use for filters. Here is a simple example where you sort users by city in ascending alphabetical order:

const users = await entityManager.user.findAll({
  sorts: [{
    'address.city': 'asc'
  ]
})

Advanced, driver-dependent sorts

All the APIs that receive the sorts parameter, as already described for the filters, accept both a type of data generated by Typetta and a function that allows you to express the sort criteria using the references, syntax and functionality of the underlying driver.

Here is an example in pseudo-code:

const users = await entityManager.user.findAll({
  projection: {
    firstName: true
  },
  sorts: (/* driverRefs... */) => {
    // ...something driver specific that returns driver sorts
  }
})

Note that this approach allows you to describe specific sorts for a driver, while maintaining the use of all other features, specifically the mechanism of projections, the resolution of relationships and the typing of results.

MongoDB

Since the MongoDB driver is developed through the official MongoDB Node Driver, the creation of specific sorts consists of a function that returns Sort.

Let’s assume, for example, that we have used the $text operator of MongoDB, which is a very specific operator that, through a textual index of the collection, is able to perform a complex full text search. This search operator also allows you to sort the results based on a matching score between the result and the text searched. Below is an example of how this is used:

const users = await entityManager.user.findAll({
  projection: {
    firstName: true
  },
  filter: () => {
    $text: { $search: "via piave", $caseSensitive: true }
  },
  sorts: () => {
    score: { $meta: "textScore"}
  }
})

SQL

The SQL driver is developed using the popular KnexJS query builder. Creating specific sorts in this case involves calling upon a series of methods on the Knex.QueryBuilder object.

For example, suppose you want to sort a list of users by month of birth (ignoring the year and day) starting from a date of birth field. Using the specific filter mechanism, we can create a search as follows:

const users = await entityManager.user.findAll({
  projection: {
    firstName: true
  },
  sorts: (builder: Knex.QueryBuilder) => {
    builder.orderByRaw("date_part('hour', birthDate) ASC");
    return builder;
  }
})