diff --git a/.gitignore b/.gitignore index c591dde2..346d2c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -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* diff --git a/src/realm.js b/src/realm.js index e1ea1faf..d7fb11d9 100644 --- a/src/realm.js +++ b/src/realm.js @@ -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); } } diff --git a/src/spell.js b/src/spell.js index bea43db6..81c2d550 100644 --- a/src/spell.js +++ b/src/spell.js @@ -388,7 +388,8 @@ class Spell { #emptySpell() { Object.assign(this, { columns: [], - whereConditions: [], + whereConditions: this.Model.shardingKey ? this.whereConditions : [], + // whereConditions: [], groups: [], orders: [], havingConditions: [], diff --git a/test/integration/suite/sharding.test.ts b/test/integration/suite/sharding.test.ts new file mode 100644 index 00000000..dcddf538 --- /dev/null +++ b/test/integration/suite/sharding.test.ts @@ -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" + ); + }); +}); diff --git a/test/models/photo.ts b/test/models/photo.ts new file mode 100644 index 00000000..63e75614 --- /dev/null +++ b/test/models/photo.ts @@ -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; +} diff --git a/test/start.sh b/test/start.sh index c3061c74..68f35ae3 100755 --- a/test/start.sh +++ b/test/start.sh @@ -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 ;; *)