Skip to content

Commit

Permalink
fix: dataLength in sqlite, decimal ts definition and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Apr 27, 2022
1 parent 6d4207b commit d385fed
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 55 deletions.
4 changes: 2 additions & 2 deletions docs/api/INTEGER.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h2>
<h3>Constructor</h3>


<h4 class="name" id="INTEGER"><span class="type-signature"></span>new INTEGER<span class="signature">(length)</span><span class="type-signature"></span></h4>
<h4 class="name" id="INTEGER"><span class="type-signature"></span>new INTEGER<span class="signature">(dataLength)</span><span class="type-signature"></span></h4>



Expand Down Expand Up @@ -135,7 +135,7 @@ <h5>Parameters:</h5>

<tr>

<td class="name"><code>length</code></td>
<td class="name"><code>dataLength</code></td>


<td class="type">
Expand Down
4 changes: 2 additions & 2 deletions docs/api/STRING.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h2>



<h4 class="name" id="STRING"><span class="type-signature"></span>new STRING<span class="signature">(length)</span><span class="type-signature"></span></h4>
<h4 class="name" id="STRING"><span class="type-signature"></span>new STRING<span class="signature">(dataLength)</span><span class="type-signature"></span></h4>



Expand Down Expand Up @@ -128,7 +128,7 @@ <h5>Parameters:</h5>

<tr>

<td class="name"><code>length</code></td>
<td class="name"><code>dataLength</code></td>


<td class="type">
Expand Down
40 changes: 20 additions & 20 deletions src/data_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ class DataType {
* @param {number} dataLength
*/
class STRING extends DataType {
constructor(length = 255) {
constructor(dataLength = 255) {
super();
this.dataType = 'varchar';
this.dataLength = length;
this.dataLength = dataLength;
}

toSqlString() {
Expand All @@ -134,9 +134,9 @@ class STRING extends DataType {
}

class BINARY extends DataType {
constructor(length = 255) {
constructor(dataLength = 255) {
super();
this.dataLength = length;
this.dataLength = dataLength;
this.dataType = 'binary';
}

Expand All @@ -156,8 +156,8 @@ class BINARY extends DataType {
}

class VARBINARY extends BINARY {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'varbinary';
}
}
Expand All @@ -173,9 +173,9 @@ class VARBINARY extends BINARY {
* @param {number} dataLength
*/
class INTEGER extends DataType {
constructor(length) {
constructor(dataLength) {
super();
this.dataLength = length;
this.dataLength = dataLength;
this.dataType = 'integer';
}

Expand Down Expand Up @@ -222,11 +222,11 @@ class INTEGER extends DataType {
* TINYINT
* TINYINT.UNSIGNED
* TINYINT(1)
* @param {number} length
* @param {number} dataLength
*/
class TINYINT extends INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'tinyint';
}
}
Expand All @@ -237,11 +237,11 @@ class TINYINT extends INTEGER {
* SMALLINT
* SMALLINT.UNSIGNED
* SMALLINT(2)
* @param {number} length
* @param {number} dataLength
*/
class SMALLINT extends INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'smallint';
}
}
Expand All @@ -252,11 +252,11 @@ class SMALLINT extends INTEGER {
* MEDIUMINT
* MEDIUMINT.UNSIGNED
* MEDIUMINT(3)
* @param {number} length
* @param {number} dataLength
*/
class MEDIUMINT extends INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'mediumint';
}
}
Expand All @@ -268,11 +268,11 @@ class MEDIUMINT extends INTEGER {
* BIGINT
* BIGINT.UNSIGNED
* BIGINT(8)
* @param {number} length
* @param {number} dataLength
*/
class BIGINT extends INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'bigint';
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/postgres/data_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class Postgres_BINARY extends DataTypes {
}

class Postgres_INTEGER extends DataTypes.INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
}

uncast(value) {
Expand Down
18 changes: 9 additions & 9 deletions src/drivers/sqlite/data_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Sqlite_DATEONLY extends DataTypes.DATEONLY {
}

class Sqlite_INTEGER extends DataTypes.INTEGER {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
}

uncast(value) {
Expand All @@ -54,24 +54,24 @@ class Sqlite_BIGINT extends DataTypes.BIGINT {
}
}
class Sqlite_BINARY extends DataTypes {
constructor(length = 255) {
super(length);
this.length = length;
constructor(dataLength = 255) {
super(dataLength);
this.dataLength = dataLength;
this.dataType = 'binary';
}

toSqlString() {
const { length } = this;
const { dataLength } = this;
const dataType = this.dataType.toUpperCase();
const chunks = [];
chunks.push('VARCHAR');
chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
return chunks.join(' ');
}
}
class Sqlite_VARBINARY extends Sqlite_BINARY {
constructor(length) {
super(length);
constructor(dataLength) {
super(dataLength);
this.dataType = 'varbinary';
}
}
Expand Down
18 changes: 13 additions & 5 deletions test/types/data_types.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { strict as assert } from 'assert';
import Realm, { Bone, DataTypes } from '../..';
import Realm, { DataTypes } from '../..';

describe('=> Data types (TypeScript)', function() {
const { STRING, TEXT, BLOB, INTEGER, BIGINT, DATE, BOOLEAN, BINARY, VARBINARY, VIRTUAL, JSON, JSONB } = DataTypes;
const { STRING, TEXT, BLOB, INTEGER, BIGINT, DATE, BOOLEAN, BINARY, VARBINARY, VIRTUAL, JSON, JSONB, DECIMAL } = DataTypes;

it('realm.Bone.DataTypes', async function() {
const realm = new Realm({
Expand Down Expand Up @@ -79,17 +79,25 @@ describe('=> Data types (TypeScript)', function() {
});

it('VIRTUAL', () => {
// default 255
assert.equal(VIRTUAL.toSqlString(), 'VIRTUAL');
});

it('JSON', () => {
// default 255
assert.equal(JSON.toSqlString(), 'TEXT');
});

it('JSONB', () => {
// default 255
assert.equal(JSONB.toSqlString(), 'JSON');
});

it('DECIMAL', () => {
assert.equal(DECIMAL.toSqlString(), 'DECIMAL');
assert.equal(DECIMAL(10).toSqlString(), 'DECIMAL(10)');

assert.equal(DECIMAL.UNSIGNED.toSqlString(), 'DECIMAL UNSIGNED');
assert.equal(DECIMAL(10).UNSIGNED.toSqlString(), 'DECIMAL(10) UNSIGNED');

assert.equal(DECIMAL.ZEROFILL.toSqlString(), 'DECIMAL ZEROFILL');
assert.equal(DECIMAL(10).ZEROFILL.toSqlString(), 'DECIMAL(10) ZEROFILL');
})
});
32 changes: 17 additions & 15 deletions types/data_types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type LENGTH_VARIANTS = 'tiny' | '' | 'medium' | 'long';

interface INVOKABLE<T> extends DataType {
(length?: LENGTH_VARIANTS): T;
(length?: number): T;
(dataLength?: LENGTH_VARIANTS): T;
(dataLength?: number): T;
}

export default class DataType {
Expand All @@ -27,14 +27,14 @@ export default class DataType {

declare class STRING extends DataType {
dataType: 'varchar';
length: number;
constructor(length: number);
dataLength: number;
constructor(dataLength: number);
}

declare class INTEGER extends DataType {
dataType: 'integer' | 'bigint' | 'decimal';
length: number;
constructor(length: number);
dataLength: number;
constructor(dataLength: number);
// avoid INTEGER.UNSIGNED.ZEROFILL.UNSIGNED.UNSIGNED
get UNSIGNED(): Omit<this, 'UNSIGNED' | 'ZEROFILL'>;
get ZEROFILL(): Omit<this, 'UNSIGNED' | 'ZEROFILL'>;
Expand All @@ -44,23 +44,25 @@ declare class BIGINT extends INTEGER {
dataType: 'bigint';
}

declare class DECIMAL extends INTEGER {
declare class DECIMAL_INNER extends INTEGER {
dataType: 'decimal';
precision: number;
scale: number;
constructor(precision: number, scale: number);
}

declare type DECIMAL = Omit<DECIMAL_INNER, 'dataLength'>;

declare class TEXT extends DataType {
dataType: 'text';
length: LENGTH_VARIANTS;
constructor(length: LENGTH_VARIANTS);
dataLength: LENGTH_VARIANTS;
constructor(dataLength: LENGTH_VARIANTS);
}

declare class BLOB extends DataType {
dataType: 'blob';
length: LENGTH_VARIANTS;
constructor(length: LENGTH_VARIANTS)
dataLength: LENGTH_VARIANTS;
constructor(dataLength: LENGTH_VARIANTS)
}

declare class JSON extends DataType {
Expand All @@ -73,14 +75,14 @@ declare class JSONB extends JSON {

declare class BINARY extends DataType {
dataType: 'binary';
length: number;
constructor(length: number);
dataLength: number;
constructor(dataLength: number);
}

declare class VARBINARY extends DataType {
dataType: 'varbinary';
length: number;
constructor(length: number);
dataLength: number;
constructor(dataLength: number);
}

declare class DATE extends DataType {
Expand Down

0 comments on commit d385fed

Please sign in to comment.