Skip to content

Commit

Permalink
fix: unify returning queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lohart13 committed Apr 12, 2024
1 parent cd5fe85 commit 2fb3342
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 209 deletions.
105 changes: 29 additions & 76 deletions packages/core/test/integration/model/upsert.test.js
Expand Up @@ -63,20 +63,16 @@ describe('Model', () => {
describe('upsert', () => {
it('works with upsert on id', async function () {
const [, created0] = await this.User.upsert({ id: 42, username: 'john' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.true;
}

this.clock.tick(1000);
const [, created] = await this.User.upsert({ id: 42, username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -89,20 +85,16 @@ describe('Model', () => {

it('works with upsert on a composite key', async function () {
const [, created0] = await this.User.upsert({ foo: 'baz', bar: 19, username: 'john' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.true;
}

this.clock.tick(1000);
const [, created] = await this.User.upsert({ foo: 'baz', bar: 19, username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -152,12 +144,9 @@ describe('Model', () => {
User.upsert({ a: 'a', b: 'a', username: 'curt' }),
]);

if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created1[1]).to.be.null;
expect(created2[1]).to.be.null;
} else if (dialectName === 'db2') {
expect(created1[1]).to.be.undefined;
expect(created2[1]).to.be.undefined;
} else {
expect(created1[1]).to.be.true;
expect(created2[1]).to.be.true;
Expand All @@ -166,10 +155,8 @@ describe('Model', () => {
this.clock.tick(1000);
// Update the first one
const [, created] = await User.upsert({ a: 'a', b: 'b', username: 'doe' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -215,10 +202,8 @@ describe('Model', () => {

await User.sync({ force: true });
const [, created] = await User.upsert({ id: 1, email: 'notanemail' }, options);
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.true;
}
Expand All @@ -230,10 +215,8 @@ describe('Model', () => {
username: 'john',
blob: Buffer.from('kaj'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -244,10 +227,8 @@ describe('Model', () => {
username: 'doe',
blob: Buffer.from('andrea'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -261,19 +242,15 @@ describe('Model', () => {

it('works with .field', async function () {
const [, created0] = await this.User.upsert({ id: 42, baz: 'foo' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}

const [, created] = await this.User.upsert({ id: 42, baz: 'oof' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -284,20 +261,16 @@ describe('Model', () => {

it('works with primary key using .field', async function () {
const [, created0] = await this.ModelWithFieldPK.upsert({ userId: 42, foo: 'first' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}

this.clock.tick(1000);
const [, created] = await this.ModelWithFieldPK.upsert({ userId: 42, foo: 'second' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand All @@ -312,10 +285,8 @@ describe('Model', () => {
username: 'john',
foo: this.sequelize.fn('upper', 'mixedCase1'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -326,10 +297,8 @@ describe('Model', () => {
username: 'doe',
foo: this.sequelize.fn('upper', 'mixedCase2'),
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -411,10 +380,8 @@ describe('Model', () => {
await this.User.create({ id: 42, username: 'john' });
const user = await this.User.findByPk(42);
const [, created] = await this.User.upsert({ id: user.id, username: user.username });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
// After set node-mysql flags = '-FOUND_ROWS' / foundRows=false
// result from upsert should be false when upsert a row to its current value
Expand Down Expand Up @@ -443,10 +410,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -456,10 +421,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -499,10 +462,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -512,10 +473,8 @@ describe('Model', () => {
email: 'user1@domain.ext',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.false;
}
Expand Down Expand Up @@ -547,10 +506,8 @@ describe('Model', () => {

await User.sync({ force: true });
const [, created0] = await User.upsert({ name: 'user1', address: 'address', city: 'City' });
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else if (dialectName === 'db2') {
expect(created0).to.be.undefined;
} else {
expect(created0).to.be.ok;
}
Expand All @@ -560,10 +517,8 @@ describe('Model', () => {
address: 'address',
city: 'New City',
});
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).not.to.be.ok;
}
Expand Down Expand Up @@ -663,7 +618,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal(42);
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -675,7 +630,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal(42);
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -702,7 +657,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal(42);
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -714,7 +669,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal(42);
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -740,7 +695,7 @@ describe('Model', () => {
);
expect(user0.get('id')).to.equal('surya');
expect(user0.get('username')).to.equal('john');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created0).to.be.null;
} else {
expect(created0).to.be.true;
Expand All @@ -752,7 +707,7 @@ describe('Model', () => {
);
expect(user.get('id')).to.equal('surya');
expect(user.get('username')).to.equal('doe');
if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else {
expect(created).to.be.false;
Expand All @@ -775,10 +730,8 @@ describe('Model', () => {
expect(user.name).to.equal('Test default value');
expect(user.code).to.equal(2020);

if (['sqlite3', 'postgres'].includes(dialectName)) {
if (['db2', 'sqlite', 'postgres'].includes(dialectName)) {
expect(created).to.be.null;
} else if (dialectName === 'db2') {
expect(created).to.be.undefined;
} else {
expect(created).to.be.true;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/db2/src/query-interface.js
Expand Up @@ -75,9 +75,7 @@ export class Db2QueryInterface extends Db2QueryInterfaceTypeScript {

delete options.replacements;

const result = await this.sequelize.queryRaw(sql, options);

return [result, undefined];
return this.sequelize.queryRaw(sql, options);
}

// TODO: drop "schema" options from the option bag, it must be passed through tableName instead.
Expand Down

0 comments on commit 2fb3342

Please sign in to comment.