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

fix: Model.count(field) in sequelize adapter #340

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Bone = require('./src/bone');
const Collection = require('./src/collection');
const { invokable: DataTypes, LENGTH_VARIANTS } = require('./src/data_types');
const migrations = require('./src/migrations');
const { sequelize, SequelizeBone} = require('./src/adapters/sequelize');
const sequelize = require('./src/adapters/sequelize');
const { heresql } = require('./src/utils/string');
const Hint = require('./src/hint');
const Realm = require('./src/realm');
Expand Down Expand Up @@ -52,7 +52,7 @@ Object.assign(Realm, {
connect,
disconnect,
Bone,
SequelizeBone,
SequelizeBone: sequelize(Bone),
Collection,
DataTypes,
Logger,
Expand Down
17 changes: 5 additions & 12 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function filterOptions(options = {}) {

// https://sequelize.org/master/class/lib/model.js~Model.html
// https://sequelize.org/master/manual/model-querying-finders.html
const sequelize = Bone => {
module.exports = function sequelize(Bone) {
cyjake marked this conversation as resolved.
Show resolved Hide resolved
return class Spine extends Bone {

/*
Expand Down Expand Up @@ -150,9 +150,9 @@ const sequelize = Bone => {

/**
* @deprecated scope is not recommended to use
* @param {string} name
* @param {...any} args
* @returns
* @param {string} name
* @param {...any} args
* @returns
*/
static scope(name, ...args) {
const parentName = this.name;
Expand Down Expand Up @@ -278,7 +278,7 @@ const sequelize = Bone => {
// static bulkCreate() {}

static count(options = {}) {
if (typeof options === 'string') return spell.$count(options);
if (typeof options === 'string') return super.find().$count(options);
const { where, col, group, paranoid } = options;
let spell = super.find(where, filterOptions(options));
if (Array.isArray(group)) spell.$group(...group);
Expand Down Expand Up @@ -739,10 +739,3 @@ const sequelize = Bone => {
}
};
};

const SequelizeBone = sequelize(require('../bone'));

module.exports = {
sequelize,
SequelizeBone,
};
2 changes: 1 addition & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Bone = require('./bone');
const { findDriver, AbstractDriver } = require('./drivers');
const { camelCase } = require('./utils/string');
const { isBone } = require('./utils');
const { sequelize } = require('./adapters/sequelize');
const sequelize = require('./adapters/sequelize');
const Raw = require('./raw');
const { LEGACY_TIMESTAMP_MAP } = require('./constants');

Expand Down
9 changes: 9 additions & 0 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ describe('=> Sequelize adapter', () => {
assert.equal(result3, 0);
});

it('Model.count("id")', async () => {
await Promise.all([
Book.create({ name: 'By three they come', price: 42 }),
Book.create({ name: 'By three thy way opens', price: 23 }),
]);
assert.equal(await Book.count('price'), 2);
assert.equal(await Book.count('deletedAt'), 0);
});

it('Model.create()', async () => {
const post = await Post.create({ title: 'By three they come' });
assert.ok(post.id);
Expand Down