sql-serversequelize.jsfeathersjsfeathers-sequelize

Feathers and Sequelize SQL Server Errors


I'm running into several sql server errors, trying to get started with a simple rest api using feathers js and sequelize. I am using SQL Server 2012 (express) version.

First my code:

const { db } = require('../database');
const Sequelize = require('sequelize');
const service = require('feathers-sequelize');
const app = require('../app');

const Model = db.define('workorder', {
  id: { type: Sequelize.INTEGER, primaryKey: true },
  created_by: Sequelize.STRING,
  modified_by: Sequelize.STRING,

  status_name: Sequelize.STRING,
  date_completed: Sequelize.DATE,
  category: Sequelize.STRING,
  location: Sequelize.STRING,
  details: Sequelize.TEXT,
  crew_name: Sequelize.STRING,
  assigned_to: Sequelize.STRING,
}, {
  schema: 'dbo',
  freezeTableName: true,
  createdAt: 'date_created',
  updatedAt: 'date_modified',
});

app.use('/api/workorders', service({
  Model,
  id: 'id',
  paginate: {
    default: 10,
    max: 100,
  },
}));

When I hit the api endpoint /api/workorders it errors out:

SequelizeDatabaseError: Invalid usage of the option NEXT in the FETCH statement.

I see that the sql generated:

 Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

It seems that sql server requires some ORDER BY clause. So I tried sending a parameter to the endpoint /api/workorders?$sort=id

This also errors out:

SequelizeDatabaseError: Invalid column name '1'.

The SQL Generated looks like this:

Executing (default): SELECT [id], [created_by], [modified_by], [status_name], [date_completed], [category], [location], [details], [crew_name], [assigned_to], [date_created], [date_modified] FROM [dbo].[workorder] AS [workorder] ORDER BY [workorder].[0] DESC, [workorder].[1] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

This error is more obvious, there are no columns 1 or 0.

Anyone run into these issues before?


Solution

  • This seems to be an incompatibility of Sequelize with older versions of MSSQL server as mentioned in this issue. A Feathers specific solution mentioned in this issue comment to add $sort and $limit like this:

    query: {
            $limit: 5,
            $sort: {createdAt: -1}
          }