Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: order by raw in sequelize mode #292

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { setupSingleHook } = require('../setup_hooks');
const { compose, isPlainObject } = require('../utils');
const Raw = require('../raw');

function translateOptions(spell, options) {
const { attributes, where, group, order, offset, limit, include, having } = options;
Expand All @@ -18,7 +19,7 @@ function translateOptions(spell, options) {
if (having) spell.$having(having);

if (order) {
if (typeof order === 'string') {
if (typeof order === 'string' || order instanceof Raw) {
spell.$order(order);
} else if (Array.isArray(order) && order.length) {
if (order.some(item => Array.isArray(item))) {
Expand Down
34 changes: 30 additions & 4 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ describe('=> Sequelize adapter', () => {
before(async () => {
await connect({
Bone: Spine,
dialect: 'sqlite',
database: '/tmp/leoric.sqlite3',
models: [ Book, Post ],
database: 'leoric',
user: 'root',
port: process.env.MYSQL_PORT,
});
});

Expand Down Expand Up @@ -347,6 +348,23 @@ describe('=> Sequelize adapter', () => {
});
assert.equal(posts.length, 1);
assert.equal(posts[0].title, 'Tyrael');

// order raw
await Promise.all([
{ title: 'Leah1', createdAt: new Date(Date.now() - 1000) },
{ title: 'Tyrael1' },
].map(opts => Post.create(opts)));
posts = await Post.findAll();
assert.equal(posts.length, 4);
const ids = [ posts[3].id, posts[1].id, posts[2].id, posts[0].id ];
posts = await Post.findAll({
order: raw(`FIND_IN_SET(id, '${ids.join(',')}')`),
});
assert.equal(posts[0].id, ids[0]);
assert.equal(posts[1].id, ids[1]);
assert.equal(posts[2].id, ids[2]);
assert.equal(posts[3].id, ids[3]);

});

it('Model.findAll(opt) with { paranoid: false }', async () => {
Expand Down Expand Up @@ -1046,7 +1064,7 @@ describe('=> Sequelize adapter', () => {
assert.deepEqual(post.previous(), {
title: 'By three they come',
id: post.id,
isPrivate: false,
isPrivate: 0,
updatedAt: prevUpdatedAt,
createdAt: post.createdAt,
wordCount: 0,
Expand Down Expand Up @@ -1099,7 +1117,7 @@ describe('=> Sequelize adapter', () => {
post.title = 'Hello there';
assert.deepEqual(post.changed(), [ 'title' ]);
post.content = 'a';
assert.deepEqual(post.changed(), [ 'title', 'content' ]);
assert.deepEqual(post.changed().sort(), [ 'title', 'content' ].sort());
await new Promise(resolve => setTimeout(resolve, 10));
await post.update();
assert.deepEqual(post.previousChanged().sort(), [ 'title', 'content', 'updatedAt' ].sort());
Expand Down Expand Up @@ -1252,6 +1270,9 @@ describe('Model scope', () => {
database: '/tmp/leoric.sqlite3',
models: [ Post, User ],
});
await Post.truncate();
await User.truncate();

});

beforeEach(async () => {
Expand Down Expand Up @@ -1457,6 +1478,7 @@ describe('Model.init with getterMethods and setterMethods', () => {
database: '/tmp/leoric.sqlite3',
models: [ User ],
});
await User.truncate();
});

beforeEach(async () => {
Expand Down Expand Up @@ -1579,6 +1601,7 @@ describe('validator should work', () => {
user: 'root',
port: process.env.MYSQL_PORT,
});
await User.truncate();
});

afterEach(async () => {
Expand Down Expand Up @@ -1823,6 +1846,7 @@ describe('Model.find({ hint })', () => {
user: 'root',
port: process.env.MYSQL_PORT,
});
await Post.truncate();
});

after(async () => {
Expand Down Expand Up @@ -1866,6 +1890,7 @@ describe('Transaction', function() {
user: 'root',
port: process.env.MYSQL_PORT,
});
await User.truncate();
});

after(async () => {
Expand Down Expand Up @@ -1906,6 +1931,7 @@ describe('mysql only', () => {
user: 'root',
port: process.env.MYSQL_PORT,
});
await Post.truncate();
});

after(async () => {
Expand Down