Skip to content
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

count() method missing on Model #64

Open
grimurd opened this issue Nov 9, 2018 · 4 comments
Open

count() method missing on Model #64

grimurd opened this issue Nov 9, 2018 · 4 comments

Comments

@grimurd
Copy link

grimurd commented Nov 9, 2018

I'm using the count() method on a few of my models but it seems to be missing from the sequelize-mock API

See: http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-count

@salauddinn
Copy link

any update on this?

@farbodsalimi
Copy link

If you're using jest:

let mockModel;
jest.doMock("path/to/model", () => {
  mockModel = dbConnectionMock.define("MyModel", myMockModel);

  mockModel.count = () => 1;

  return mockModel;
});

@JacopoMadaluni
Copy link

We are currently using sinon and we have the same problems as sinon does not allow to stub non-existent properties by design.
Does anyone know if Sequelize is planning to fix this in a timely manner ?
Btw same problem applies to other non-existing methods such as findByPk() which causes the same problem of count()

@LiamKarlMitchell
Copy link

LiamKarlMitchell commented Sep 16, 2023

So findAndCount does exist.

mockModel.count = () => mockModel.findAndCount().then(res => res.count);

But this feels like something that should be added in.

Could model.js be extended?

/**
 * Executes a mock query to find instances with any provided options and return the count.
 * Without any other configuration, the default behavior when no queueud query result 
 * is present is to return single result based on the where query in the options and
 * wraps it in a promise.
 * 
 * To turn off this behavior, the `$autoQueryFallback` option on the model should be set
 * to `false`.
 * 
 * @example
 * // This is an example of the default behavior with no queued results
 * // If there is a queued result or failure, that will be returned instead
 * User.count({
 * 	where: {
 * 		email: 'myEmail@example.com',
 * 	},
 * }).then(function (count) {
 *    // count is an integer of how many rows were found.
 * });
 * 
 * @instance
 * @method count
 * @param {Object} [options] Options for the count query
 * @param {Object} [options.where] Values that any automatically created Instances should have
 * @return {Promise<Object>} result returned by the mock query
 **/
fakeModel.prototype.count =  function (options) {
	var self = this;
	
	return this.$query({
		query: "count",
		queryOptions: arguments,
		fallbackFn: !this.options.autoQueryFallback ? null : function () {
			return Promise.resolve([ self.build(options ? options.where : {}) ])
				.then(function(result){
					return Promise.resolve(result.length);
				});
		},
	});
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants