Skip to content

Commit a2b68f9

Browse files
committed
lib: Replace lodash/isEmpty with a custom function
1 parent f1ca9e7 commit a2b68f9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/PoolPlus.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ const PoolConfig = require('mysql/lib/PoolConfig');
1212
const SqlString = require('mysql/lib/protocol/SqlString');
1313
const TableDefinition = require('./TableDefinition');
1414

15-
const isEmpty = require('lodash/isEmpty');
16-
1715
const MIGRATION_STRATEGIES = [
1816
'safe',
1917
'alter',
2018
'drop',
2119
];
2220

21+
function isObjectEmpty(obj) {
22+
for (var key in obj) {
23+
if (!obj.hasOwnProperty || obj.hasOwnProperty(key)) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
2331
function validateMigrationStrategy(strategy) {
2432
if (strategy && MIGRATION_STRATEGIES.indexOf(strategy) < 0) {
2533
throw new Error(`"${strategy}" is not a valid migration strategy`);
@@ -97,7 +105,7 @@ class PoolPlus extends Pool {
97105
if (this._tables.has(name)) {
98106
throw new Error(`A table called "${name}" has already been defined for this pool`);
99107
}
100-
if (isEmpty(schema.columns)) {
108+
if (isObjectEmpty(schema.columns)) {
101109
throw new Error('The schema must have at least one table column');
102110
}
103111
validateMigrationStrategy(migrationStrategy);

test/unit/PoolPlus.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ describe('PoolPlus', () => {
110110
it('should throw if no columns are provided', () => {
111111
should.throws(() => pool.defineTable('table', {}), /must have.*column/);
112112
should.throws(() => pool.defineTable('table', {columns: {}}), /must have.*column/);
113+
should.throws(() => pool.defineTable('table', {columns: Object.create(null)}), /must have.*column/);
114+
115+
function Clazz() { /* constructor */ }
116+
Clazz.prototype.foo = 1; // So the instance will have enumerable properties but no "own" properties
117+
118+
should.throws(() => pool.defineTable('table', {columns: new Clazz()}), /must have.*column/);
113119
});
114120

115121
it('should throw if the specified migration strategy is invalid', () => {

0 commit comments

Comments
 (0)