From 8f6f2d88e672a1b4f31918709568ca27be8bf768 Mon Sep 17 00:00:00 2001 From: Chen Yangjian <252317+cyjake@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:11:14 +0800 Subject: [PATCH] fix: Model.count(field) in sequelize adapter --- index.js | 4 ++-- src/adapters/sequelize.js | 17 +++++------------ src/realm.js | 2 +- test/unit/adapters/sequelize.test.js | 9 +++++++++ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index e064b9a4..a3968959 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -52,7 +52,7 @@ Object.assign(Realm, { connect, disconnect, Bone, - SequelizeBone, + SequelizeBone: sequelize(Bone), Collection, DataTypes, Logger, diff --git a/src/adapters/sequelize.js b/src/adapters/sequelize.js index 711fe536..07c2ef39 100644 --- a/src/adapters/sequelize.js +++ b/src/adapters/sequelize.js @@ -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) { return class Spine extends Bone { /* @@ -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; @@ -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); @@ -739,10 +739,3 @@ const sequelize = Bone => { } }; }; - -const SequelizeBone = sequelize(require('../bone')); - -module.exports = { - sequelize, - SequelizeBone, -}; diff --git a/src/realm.js b/src/realm.js index 7b41b918..ab4e258b 100644 --- a/src/realm.js +++ b/src/realm.js @@ -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'); diff --git a/test/unit/adapters/sequelize.test.js b/test/unit/adapters/sequelize.test.js index 0e3fe7af..54a23a81 100644 --- a/test/unit/adapters/sequelize.test.js +++ b/test/unit/adapters/sequelize.test.js @@ -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);