New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
constructor options: have a default limit for queries #12648
Comments
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
ping |
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
... |
Would be very useful |
Another workaround is to add following code to models/index.js in model.findAll = function (options) {
return Sequelize.Model.findAll.call(model, { limit: 25, ...options });
}; So models/index.js looks like the following: 'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
let config = require(`${__dirname}/../config/config.js`)[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs.readdirSync(__dirname)
.filter((file) => {
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
})
.forEach((file) => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
model.findAll = function (options) {
return Sequelize.Model.findAll.call(model, { limit: 25, ...options });
};
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db; |
Feature Description
Use case: we want to LIMIT all SQL SELECTs to a maximum number of records, eg 500.
This limit can still be set for every query, eg to handle pagination, but if not set, it should have a default.
Describe the solution you'd like
https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor
says
options.query | object | optionaldefault: {} | Default options for sequelize.query
but this
has no effect.
Why should this be in Sequelize
We use Sequelize to read data.
Describe alternatives/workarounds you've considered
This works
but has to be done for every model. It would be simpler to have a default for sequelize.
Feature Request Checklist
Is this feature dialect-specific?
Would you be willing to implement this feature by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: