Skip to content

Commit

Permalink
lib: Require the m argument for varchar and varbinary
Browse files Browse the repository at this point in the history
Throw an error if it is not provided and update the documentation to show
that the m and d arguments are optional for the other column types.
  • Loading branch information
nwoltman committed May 25, 2016
1 parent ba9a80a commit 0564137
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,35 +758,35 @@ These schema properties configure table-level options. The options currently sup
[`mysql.Type`](#module_mysql-plus..Type) and [`pool.Type`](#PoolPlus+Type) both expose the following methods:
+ `tinyint(m)`
+ `smallint(m)`
+ `mediumint(m)`
+ `int(m)`
+ `integer(m)`
+ `bigint(m)`
+ `float(m, d)`
+ `double(m, d)`
+ `decimal(m, d)`
+ `dec(m, d)`
+ `numeric(m, d)`
+ `fixed(m, d)`
+ `bit(m)`
+ `tinyint([m])`
+ `smallint([m])`
+ `mediumint([m])`
+ `int([m])`
+ `integer([m])`
+ `bigint([m])`
+ `float([m [, d]])`
+ `double([m [, d]])`
+ `decimal([m [, d]])`
+ `dec([m [, d]])`
+ `numeric([m [, d]])`
+ `fixed([m [, d]])`
+ `bit([m])`
+ `bool()`
+ `boolean()`
+ `date()`
+ `datetime(m)`
+ `timestamp(m)`
+ `time(m)`
+ `datetime([m])`
+ `timestamp([m])`
+ `time([m])`
+ `year()`
+ `char(m)`
+ `char([m])`
+ `varchar(m)`
+ `text(m)`
+ `text([m])`
+ `tinytext()`
+ `mediumtext()`
+ `longtext()`
+ `binary(m)`
+ `binary([m])`
+ `varbinary(m)`
+ `blob(m)`
+ `blob([m])`
+ `tinyblob()`
+ `mediumblob()`
+ `longblob()`
Expand Down
6 changes: 6 additions & 0 deletions lib/ColumnDefinitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const ColumnDefinitions = {
return new TextColumnDefinition('char', m);
},
varchar(m) {
if (m == null) {
throw new Error('You must specify the `m` argument for varchar');
}
return new TextColumnDefinition('varchar', m);
},
text(m) {
Expand All @@ -94,6 +97,9 @@ const ColumnDefinitions = {
return new ColumnDefinition('binary', m);
},
varbinary(m) {
if (m == null) {
throw new Error('You must specify the `m` argument for varbinary');
}
return new ColumnDefinition('varbinary', m);
},
blob(m) {
Expand Down
35 changes: 25 additions & 10 deletions test/unit/ColumnDefinitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ describe('ColumnDefinitions', () => {
cd = ColumnDefinitions.char();
cd.$toSQL().should.equal('char');

cd = ColumnDefinitions.varchar();
cd.$toSQL().should.equal('varchar');
cd = ColumnDefinitions.varchar(1);
cd.$toSQL().should.equal('varchar(1)');

cd = ColumnDefinitions.text();
cd.$toSQL().should.equal('text');
Expand All @@ -223,8 +223,8 @@ describe('ColumnDefinitions', () => {
cd = ColumnDefinitions.binary();
cd.$toSQL().should.equal('binary');

cd = ColumnDefinitions.varbinary();
cd.$toSQL().should.equal('varbinary');
cd = ColumnDefinitions.varbinary(1);
cd.$toSQL().should.equal('varbinary(1)');

cd = ColumnDefinitions.blob();
cd.$toSQL().should.equal('blob');
Expand Down Expand Up @@ -437,14 +437,14 @@ describe('ColumnDefinitions', () => {
it('should provide string-specific definition methods', () => {
var cd;

cd = ColumnDefinitions.varchar().charset('utf8');
cd.$toSQL().should.equal('varchar CHARACTER SET utf8');
cd = ColumnDefinitions.char().charset('utf8');
cd.$toSQL().should.equal('char CHARACTER SET utf8');

cd = ColumnDefinitions.varchar().collate('utf8_general_ci');
cd.$toSQL().should.equal('varchar COLLATE utf8_general_ci');
cd = ColumnDefinitions.char().collate('utf8_general_ci');
cd.$toSQL().should.equal('char COLLATE utf8_general_ci');

cd = ColumnDefinitions.varchar().charset('utf8').collate('utf8_general_ci');
cd.$toSQL().should.equal('varchar CHARACTER SET utf8 COLLATE utf8_general_ci');
cd = ColumnDefinitions.char().charset('utf8').collate('utf8_general_ci');
cd.$toSQL().should.equal('char CHARACTER SET utf8 COLLATE utf8_general_ci');
});

});
Expand All @@ -465,6 +465,21 @@ describe('ColumnDefinitions', () => {
});


describe('varchar and varbinary', () => {

it('should throw if no `m` value is provided when creating the column definition', () => {
should.throws(() => ColumnDefinitions.varchar(), /You must specify the `m` argument for varchar/);
should.throws(() => ColumnDefinitions.varbinary(), /You must specify the `m` argument for varbinary/);
});

it('should accept 0 as a valid `m` value', () => {
ColumnDefinitions.varchar(0).$toSQL().should.equal('varchar(0)');
ColumnDefinitions.varbinary(0).$toSQL().should.equal('varbinary(0)');
});

});


describe('enums and sets', () => {

it('should generate SQL with allowed values for certain types', () => {
Expand Down

0 comments on commit 0564137

Please sign in to comment.