So, I'm using sequelize.js with tedious
for mssql support. MSSQL = SQL Server 2008
Well, So there is this big table named Clientes. I have to fetch data from it, but it is quite huge so I'll have to paginate trhough
I tried the 'right way', as in the docs:
app.use('/clientes', function(req, res){
var page = req.params.page || 0;
sequelize.Clientes.findAll({limit: 30, offset: 5 })
.then(function(result) {
res.json({result: result});
});
});
It should work, BUT it uses a query that is something like
SELECT .......... from [Clientes] as [Clientes] ORDER BY [CNPJ] OFFSET 5 ROWS FETCH NEXT 30 ROWS ONLY;
and the SQL Server return a terrible Invalid usage of the option NEXT in the FETCH statement
!
So, after lots of search, I got to a point that apparently FETCH will not work with SQL 2008. That being said (and sad) I tried to force TDS version to 7_3_A. I did this using
dialectOptions: {
tdsVersion: '7_3_A'
},
but the query sintax did not change with this :(
I'm not sure what to do anymore.... I even will use another way instead of paginate... I found a nice sintax to SQL pagination (involving row_number()
) , but I was not able to perform it as a raw query on sequelyze. myModel.query('SQL QUERY HERE')
returned undefined function oO'
Any helps will be appreciated
Ok, so after lots of searching and debugging, I found 2 things.
1) My sequelize instance was
sequelize.sequelize
, due to amodule.exports
with lodash. My bad, I did not remember that, and did not even mention it in the question. I'm sorry about this.2) The
offset
andlimit
do usefetch
and thus generates invalid SQL sintax for SQL 2008.I had to perform a raw query, using
sequelize.sequelize.query()
(since my instance was sequelize.sequelize).here's my full code, including express routing and ugly format for the query:
And if you (as I did) wonder why
sequelize.sequelize
instead of onlysequelize
?Well, this is because
var sequelize
is arequire()
from a file with the followingmodule.exports
:So this is why just
sequelize.query()
was returnedundefined
, andsequelize.sequelize.query()
works fine, because in true mysequelize
variable is an Object with thesequelize
property being the real sequelize connection instance.