Skip to content

Commit

Permalink
fix: join conditions containing fields of base model should be allowed (
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored May 15, 2023
1 parent eb86ae2 commit 2fb5581
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
15 changes: 8 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
.nyc_output
coverage
dist
logs
node_modules
tmp
coverage
.nyc_output

*.log
docs/Gemfile.lock
package-lock.json
.DS_Store
test/types/*.js
test/types/*.map
test/integration/suite/sharding.test.js*
test/models/photo.js*
src/browser.js*
src/decorators.js*
src/data_types.js*
src/drivers/sqljs/*.js*
src/raw.js*

# Logs
logs
*.log
dist/
14 changes: 12 additions & 2 deletions src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function joinOnConditions(spell, BaseModel, baseName, refName, { where, associat
return { type: 'op', name: 'and', args: [ result, condition ] };
});
walkExpr(whereConditions, node => {
if (node.type == 'id') node.qualifiers = [refName];
if (node.type == 'id' && !node.qualifiers) node.qualifiers = [refName];
});
return { type: 'op', name: 'and', args: [ onConditions, whereConditions ] };
} else {
Expand Down Expand Up @@ -386,8 +386,18 @@ class Spell {
}

#emptySpell() {
const whereConditions = [];
const { shardingKey } = this.Model;
if (shardingKey) {
for (const condition of this.whereConditions) {
const [arg] = condition.args;
if (arg.type === 'id' && arg.value === shardingKey) {
whereConditions.push(deepClone(condition));
}
}
}
Object.assign(this, {
whereConditions: [],
whereConditions,
groups: [],
orders: deepClone(this.orders),
havingConditions: [],
Expand Down
3 changes: 2 additions & 1 deletion test/dumpfile.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ CREATE TABLE `likes` (
`id` bigint(20) AUTO_INCREMENT PRIMARY KEY,
`gmt_create` timestamp(3) NULL,
`gmt_modified` timestamp DEFAULT CURRENT_TIMESTAMP,
`article_id` bigint(20) NOT NULL,
`target_id` bigint(20) NOT NULL,
`target_type` bigint(20) NOT NULL,
`user_id` bigint(20) NOT NULL,
`gmt_deleted` timestamp(3) NULL
);
Expand Down
12 changes: 11 additions & 1 deletion test/integration/suite/associations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sinon = require('sinon');

const Attachment = require('../../models/attachment');
const Comment = require('../../models/comment');
const Like = require('../../models/like');
const Post = require('../../models/post');
const Tag = require('../../models/tag');
const TagMap = require('../../models/tagMap');
Expand Down Expand Up @@ -76,7 +77,8 @@ describe('=> Associations', function() {
Attachment.remove({}, true),
Comment.remove({}, true),
TagMap.remove({}, true),
Tag.remove({}, true)
Tag.remove({}, true),
Like.remove({ userId: 1 }, true),
]);
});

Expand All @@ -96,6 +98,14 @@ describe('=> Associations', function() {
expect(attachment.post).to.be.a(Post);
});

it('Bone.belongsTo({ where })', async function() {
await Like.create({ targetId: 1, targetType: 0, userId: 1 });
await assert.doesNotReject(async function() {
const like = await Like.findOne({ userId: 1 }).with('post');
assert.ok('post' in like);
});
});

it('Bone.hasMany', async function() {
let post = await Post.first.with('comments');
expect(post.comments.length).to.be.above(0);
Expand Down
12 changes: 6 additions & 6 deletions test/integration/suite/querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,13 @@ describe('=> Sharding', function() {

it('should throw if sharding key is defined but not set when INSERT', async function() {
await assert.rejects(async () => {
await new Like({ articleId: 1, userId: null }).create({ validate: false });
await new Like({ targetId: 1, targetType: 0, userId: null }).create({ validate: false });
}, /sharding key/i);
});

it('should not throw if sharding key is defined and set when INSERT', async function() {
await assert.doesNotReject(async () => {
await new Like({ userId: 1, articleId: 1 }).create();
await new Like({ userId: 1, targetId: 1, targetType: 0 }).create();
}, /sharding key/i);
});

Expand Down Expand Up @@ -803,23 +803,23 @@ describe('=> Sharding', function() {
});

it('should append sharding key to DELETE condition', async function() {
const like = await Like.create({ articleId: 1, userId: 1 });
const like = await Like.create({ targetId: 1, targetType: 0, userId: 1 });
await assert.doesNotReject(async () => {
await like.remove();
await like.remove(true);
}, /sharding key/i);
});

it('should append sharding key to UPDATE condition', async function() {
const like = await Like.create({ articleId: 1, userId: 1});
like.articleId = 2;
const like = await Like.create({ targetId: 1, targetType: 0, userId: 1});
like.targetId = 2;
await assert.doesNotReject(async () => {
await like.update();
}, /sharding key/i);
});

it('should append sharking key to SELECT condition', async function() {
const like = await Like.create({ articleId: 1, userId: 1});
const like = await Like.create({ targetId: 1, targetType: 0, userId: 1});
await assert.doesNotReject(async () => {
await like.reload();
}, /sharding key/);
Expand Down
12 changes: 12 additions & 0 deletions test/models/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const { Bone } =require('../..');

const TARGET_TYPE = {
post: 0,
comment: 1,
};

class Like extends Bone {
static get table() {
return 'likes';
Expand All @@ -14,6 +19,13 @@ class Like extends Bone {
static get shardingKey() {
return 'userId';
}

static initialize() {
this.belongsTo('post', {
foreignKey: 'targetId',
where: { 'likes.targetType': TARGET_TYPE.post },
});
}
}

module.exports = Like;

0 comments on commit 2fb5581

Please sign in to comment.