Skip to content

Commit

Permalink
fix: aggregator not parse null result (#322)
Browse files Browse the repository at this point in the history
* fix: aggregator not parse null result

set decimalNumbers default to true for mysql2
use custom pg type parser

* Update src/drivers/postgres/index.js

Co-authored-by: Chen Yangjian <[email protected]>
  • Loading branch information
killagu and cyjake authored Jul 14, 2022
1 parent 5c2a71b commit ab8475f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
8 changes: 0 additions & 8 deletions src/collection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const { AGGREGATOR_MAP } = require('./constants');

const AGGREGATORS = Object.values(AGGREGATOR_MAP);
/**
* An extended Array to represent collections of models.
*/
Expand Down Expand Up @@ -92,11 +89,6 @@ function dispatch(spell, rows, fields) {
const row = rows[0];
const record = row && (row[''] || row[table]);
const result = record && record[value];
// see https://www.w3schools.com/mysql/mysql_ref_functions.asp
if (AGGREGATORS.includes(args[0].name)) {
const num = Number(result);
return isNaN(num) ? result : num;
}
return result;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MysqlDriver extends AbstractDriver {
const {
host, port, user, password,
connectTimeout, connectionLimit, charset, stringifyObjects = true,
decimalNumbers = true,
} = opts;

if (client !== 'mysql' && client !== 'mysql2') {
Expand All @@ -66,6 +67,7 @@ class MysqlDriver extends AbstractDriver {
database,
charset,
stringifyObjects,
decimalNumbers,
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/drivers/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { performance } = require('perf_hooks');
const AbstractDriver = require('../abstract');
const Attribute = require('./attribute');
const DataTypes = require('./data_types');
const {
const {
escape, escapeId, formatAddColumn,
formatAlterColumns, formatDropColumn,
cast, nest, parameterize,
Expand Down Expand Up @@ -36,6 +36,9 @@ class PostgresDriver extends AbstractDriver {

createPool(opts) {
const { host, port, user, password, database } = opts;
// dynamic require pg type parse
// if not use pg, pg-types may not exits
require('./type_parser');
return new (require('pg')).Pool({ host, port, user, password, database });
}

Expand Down
7 changes: 7 additions & 0 deletions src/drivers/postgres/type_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const pgTypes = require('pg-types');

pgTypes.setTypeParser(1700, 'text', function (val) {
return Number(val);
});
4 changes: 2 additions & 2 deletions src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const { AGGREGATOR_MAP } = require('./constants');

/**
* check condition to avoid use virtual fields as where condtions
* @param {Bone} Model
* @param {Array<Object>} conds
* @param {Bone} Model
* @param {Array<Object>} conds
*/
function checkCond(Model, conds) {
if (Array.isArray(conds)) {
Expand Down
17 changes: 17 additions & 0 deletions test/integration/suite/querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Post = require('../../models/post');
const Tag = require('../../models/tag');
const TagMap = require('../../models/tagMap');
const { logger } = require('../../../src/utils');
const {max} = require('dayjs');

describe('=> Query', function() {

Expand Down Expand Up @@ -652,11 +653,27 @@ describe('=> Calculations', function() {
expect(Math.floor(maximum)).to.equal(Math.floor(29.95));
});

it('Bone.maximum() should not parse result is null', async function() {
const maximum = await Book.maximum('price')
.where({
name: 'not_exists_book',
});
expect(maximum).to.be(null);
});

it('Bone.sum()', async function() {
const sum = await Book.sum('price');
assert.equal(typeof sum, 'number');
expect(Math.floor(sum)).to.equal(Math.floor(22.95 + 29.95 + 21));
});

it('Bone.sum() should not parse result is null', async function() {
const sum = await Book.sum('price')
.where({
name: 'not_exists_book',
});
expect(sum).to.be(null);
});
});

describe('=> Batch', function() {
Expand Down

0 comments on commit ab8475f

Please sign in to comment.