Skip to content

Commit

Permalink
fix: retain where conditions when sharding key is necessary (#381)
Browse files Browse the repository at this point in the history
att
  • Loading branch information
cyjake authored Mar 24, 2023
1 parent 08e1af8 commit 43d7af4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package-lock.json
.DS_Store
test/types/*.js
test/types/*.map
test/integration/suite/sharding.test.js*
test/models/photo.js*
src/decorators.js*
src/data_types.js*
src/raw.js*
3 changes: 2 additions & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ async function findModels(dir) {
for (const entry of entries) {
const extname = path.extname(entry.name);
if (entry.isFile() && ['.js', '.mjs'].includes(extname)) {
const model = require(path.join(dir, entry.name));
const exports = require(path.join(dir, entry.name));
const model = exports.__esModule ? exports.default : exports;
if (isBone(model)) models.push(model);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ class Spell {
#emptySpell() {
Object.assign(this, {
columns: [],
whereConditions: [],
whereConditions: this.Model.shardingKey ? this.whereConditions : [],
// whereConditions: [],
groups: [],
orders: [],
havingConditions: [],
Expand Down
11 changes: 11 additions & 0 deletions test/integration/suite/sharding.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { strict as assert } from "assert";
import Photo from "../../models/photo";

describe('=> Sharding', function() {
it('should retain where conditions if sharding needed', function() {
assert.equal(
Photo.findOne({ userId: 1 }).with('user').toSqlString(),
"SELECT `photos`.*, `user`.* FROM (SELECT * FROM `photos` WHERE `photos`.`user_id` = 1 LIMIT 1) AS `photos` LEFT JOIN `users` AS `user` ON `photos`.`user_id` = `user`.`id` WHERE `photos`.`user_id` = 1"
);
});
});
24 changes: 24 additions & 0 deletions test/models/photo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BelongsTo, Bone, Column } from '../..';
import User from './user';

export default class Photo extends Bone {
static shardingKey: string = 'userId';

@Column()
id: bigint;

@Column()
userId: bigint;

@Column()
url: string;

@Column()
filename: string;

@Column({ allowNull: true })
caption?: string;

@BelongsTo({ foreignKey: 'userId' })
user: User;
}
5 changes: 4 additions & 1 deletion test/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ function integration {
##
# definition type tests
function dts {
npx tsc
run "$(ls test/types/*.test.js)";
}

case $1 in
unit)
args=("${@:2}")
npx tsc
unit
;;
integration)
args=("${@:2}")
npx tsc
integration
;;
dts)
args=("${@:2}")
npx tsc
dts
;;
*.js)
args=("${@:1}")
npx tsc
run $1
;;
*)
Expand Down

0 comments on commit 43d7af4

Please sign in to comment.