From d385fed8bf6aed4326d2662549f6ffaa9ef059f9 Mon Sep 17 00:00:00 2001 From: JimmyDaddy Date: Wed, 27 Apr 2022 10:53:04 +0800 Subject: [PATCH] fix: dataLength in sqlite, decimal ts definition and doc --- docs/api/INTEGER.html | 4 +-- docs/api/STRING.html | 4 +-- src/data_types.js | 40 +++++++++++++++--------------- src/drivers/postgres/data_types.js | 4 +-- src/drivers/sqlite/data_types.js | 18 +++++++------- test/types/data_types.test.ts | 18 ++++++++++---- types/data_types.d.ts | 32 +++++++++++++----------- 7 files changed, 65 insertions(+), 55 deletions(-) diff --git a/docs/api/INTEGER.html b/docs/api/INTEGER.html index 5fc5b491..64ad44a1 100644 --- a/docs/api/INTEGER.html +++ b/docs/api/INTEGER.html @@ -57,7 +57,7 @@

Constructor

-

new INTEGER(length)

+

new INTEGER(dataLength)

@@ -135,7 +135,7 @@
Parameters:
- length + dataLength diff --git a/docs/api/STRING.html b/docs/api/STRING.html index 372804c0..ffed34b0 100644 --- a/docs/api/STRING.html +++ b/docs/api/STRING.html @@ -50,7 +50,7 @@

-

new STRING(length)

+

new STRING(dataLength)

@@ -128,7 +128,7 @@
Parameters:
- length + dataLength diff --git a/src/data_types.js b/src/data_types.js index 294b7277..e7c44a6c 100644 --- a/src/data_types.js +++ b/src/data_types.js @@ -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() { @@ -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'; } @@ -156,8 +156,8 @@ class BINARY extends DataType { } class VARBINARY extends BINARY { - constructor(length) { - super(length); + constructor(dataLength) { + super(dataLength); this.dataType = 'varbinary'; } } @@ -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'; } @@ -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'; } } @@ -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'; } } @@ -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'; } } @@ -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'; } } diff --git a/src/drivers/postgres/data_types.js b/src/drivers/postgres/data_types.js index 9d9beec0..9bc56166 100644 --- a/src/drivers/postgres/data_types.js +++ b/src/drivers/postgres/data_types.js @@ -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) { diff --git a/src/drivers/sqlite/data_types.js b/src/drivers/sqlite/data_types.js index 8c7d1ca4..96ddc991 100644 --- a/src/drivers/sqlite/data_types.js +++ b/src/drivers/sqlite/data_types.js @@ -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) { @@ -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'; } } diff --git a/test/types/data_types.test.ts b/test/types/data_types.test.ts index 0fa37746..cb76c6c4 100644 --- a/test/types/data_types.test.ts +++ b/test/types/data_types.test.ts @@ -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({ @@ -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'); + }) }); diff --git a/types/data_types.d.ts b/types/data_types.d.ts index bea65b84..84c281ac 100644 --- a/types/data_types.d.ts +++ b/types/data_types.d.ts @@ -1,8 +1,8 @@ type LENGTH_VARIANTS = 'tiny' | '' | 'medium' | 'long'; interface INVOKABLE extends DataType { - (length?: LENGTH_VARIANTS): T; - (length?: number): T; + (dataLength?: LENGTH_VARIANTS): T; + (dataLength?: number): T; } export default class DataType { @@ -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; get ZEROFILL(): Omit; @@ -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; + 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 { @@ -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 {