Skip to content

Commit

Permalink
fix: metro exports error (#339)
Browse files Browse the repository at this point in the history
* fix: metro exports error

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

Co-authored-by: JimmyDaddy <[email protected]>
Co-authored-by: Chen Yangjian <[email protected]>
  • Loading branch information
3 people authored Aug 31, 2022
1 parent 629fea8 commit 81e361c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
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
12 changes: 5 additions & 7 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
exports.sequelize = Bone => {
module.exports = function sequelize(Bone) {
return class Spine extends Bone {

/*
Expand Down Expand Up @@ -150,9 +150,9 @@ exports.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 @@ exports.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,5 +739,3 @@ exports.sequelize = Bone => {
}
};
};

exports.SequelizeBone = this.sequelize(require('../bone'));
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

0 comments on commit 81e361c

Please sign in to comment.