Skip to content

Commit

Permalink
Fix Modify.then for compatibility with Query.then
Browse files Browse the repository at this point in the history
Pefs: #272
  • Loading branch information
tshemsedinov committed Jul 10, 2023
1 parent b89fd23 commit 6480848
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
4 changes: 3 additions & 1 deletion lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ class Modify {

then(resolve, reject) {
const { sql, args } = this.prepare();
return this.db.query(sql, args).then(resolve, reject);
return this.db
.query(sql, args)
.then(({ rows }) => (resolve ? resolve(rows) : rows), reject);
}

toString() {
Expand Down
41 changes: 21 additions & 20 deletions test/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ const metadomain = require('metadomain');
const res1 = await db
.insert('City', { name: 'Odessa', countryId: 4 })
.returning('cityId');
test.strictEqual(res1.rowCount, 1);
test.strictEqual(parseInt(res1.rows[0].cityId) > 1, true);
test.strictEqual(res1.length, 1);
test.strictEqual(parseInt(res1[0].cityId) > 1, true);

const res2 = await db
.update(
Expand All @@ -159,61 +159,62 @@ const metadomain = require('metadomain');
{ name: 'Odessa', cityId: undefined },
)
.returning(['cityId']);
test.strictEqual(res2.rowCount, 1);
test.strictEqual(res2.length, 1);

const res3 = await db.select('City', { name: 'ODESSA' });
test.contains(res3[0], { name: 'ODESSA', countryId: '4' });

const res4 = await db.delete('City', { name: 'ODESSA' }).returning('*');
test.strictEqual(res4.rowCount, 1);
test.strictEqual(res4.length, 1);

const res5 = await db
.update('City', { name: null }, { name: 'ODESSA' })
.returning('cityId');
test.strictEqual(res5.rowCount, 0);
test.strictEqual(res5.length, 0);

const res6 = await db
const [{ cityId }] = await db
.insert('City', { name: 'Mediolanum', countryId: 6 })
.returning('cityId');
const [{ cityId }] = res6.rows;

const res7 = await db
.update('City', { name: '' }, { cityId })
.returning(['name']);
test.strictEqual(res7.rows[0].name, '');
test.strictEqual(res7[0].name, '');

await db.delete('City', { cityId }).returning('*');
test.end();
});

metatests.test('insert/update/delete: auto-returning', async (test) => {
const res1 = await db.insert('City', { name: 'Toulouse', countryId: 1 });
test.strictEqual(res1.rowCount, 1);
test.strictEqual(parseInt(res1.rows[0].cityId) > 1, true);
test.strictEqual(res1.length, 1);
test.strictEqual(parseInt(res1[0].cityId) > 1, true);

const res2 = await db.update(
'City',
{ name: 'TOULOUSE', countryId: undefined },
{ name: 'Toulouse', cityId: undefined },
);
test.strictEqual(res2.rowCount, 1);
test.strictEqual(res2.length, 1);

const res3 = await db.select('City', { name: 'TOULOUSE' });
test.contains(res3[0], { name: 'TOULOUSE', countryId: '1' });

const res4 = await db.delete('City', { name: 'TOULOUSE' }).returning('*');
test.strictEqual(res4.rowCount, 1);
test.strictEqual(res4.length, 1);

const res5 = await db.update('City', { name: null }, { name: 'TOULOUSE' });
test.strictEqual(res5.rowCount, 0);
test.strictEqual(res5.length, 0);

const res6 = await db.insert('City', { name: 'Mediolanum', countryId: 6 });
const [{ cityId }] = res6.rows;
const [{ cityId }] = await db.insert('City', {
name: 'Mediolanum',
countryId: 6,
});

const res7 = await db
.update('City', { name: '' }, { cityId })
.returning(['name']);
test.strictEqual(res7.rows[0].name, '');
test.strictEqual(res7[0].name, '');

await db.delete('City', { cityId }).returning('*');
test.end();
Expand All @@ -223,8 +224,8 @@ const metadomain = require('metadomain');
const res1 = await db
.insert('Division', { name: 'Quality control' })
.returning('id');
test.strictEqual(typeof res1.rows[0].id, 'string');
test.strictEqual(res1.rowCount, 1);
test.strictEqual(typeof res1[0].id, 'string');
test.strictEqual(res1.length, 1);
test.end();
});

Expand Down Expand Up @@ -266,12 +267,12 @@ const metadomain = require('metadomain');
const res1 = await db
.insert('Counter', { value: 1 })
.returning('counterId');
const { counterId } = res1.rows[0];
const { counterId } = res1[0];

const res2 = await db
.update('Counter', { value: 0 }, { counterId })
.returning('value');
const { value } = res2.rows[0];
const { value } = res2[0];
test.strictEqual(value, '0');

const [row] = await db.select('Counter', ['value'], { counterId });
Expand Down

0 comments on commit 6480848

Please sign in to comment.