Skip to content

Commit

Permalink
fix: realm.Bone.DataTypes should be invokable, Invokable.TYPE.toSqlSt…
Browse files Browse the repository at this point in the history
…ring() get wrong default length(1), DataType definitions (#307)

* fix: realm.Bone.DataTypes should be invokable

* chore: ts type definition correct, doc

* fix: DataType.invokable.INTEGER.toString() = "INTEGER(1)" and type definitions

* fix: dataLength in sqlite, decimal ts definition and doc

Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
JimmyDaddy and JimmyDaddy authored Apr 27, 2022
1 parent 40e9ac6 commit 2f2ca45
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 123 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
2 changes: 1 addition & 1 deletion docs/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const { BIGINT, STRING } = Bone.DataTypes;
class Shop extends Bone {
static attributes = {
id: { type: BIGINT, primaryKey: true },
name: { type: STRING },
name: STRING,
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ We can reference these table aliases futher after the join, such as `.where()` o

```js
Post.join(Comment, 'posts.id = comments.postId').where('comments.id = 1')
Post.join(Comment, 'posts.id = comments.postId').where({ 'comments.id': 1 })
```

## Scopes
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const { BIGINT, STRING } = Bone.DataTypes;
class Shop extends Bone {
static attributes = {
id: { type: BIGINT, primaryKey: true },
name: { type: STRING },
name: STRING,
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/zh/querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ SELECT * FROM posts LEFT JOIN comments ON posts.id = comments.post_id LEFT JOIN

```js
Post.join(Comment, 'posts.id = comments.postId').where('comments.id = 1')
Post.join(Comment, 'posts.id = comments.postId').where({ 'comments.id': 1 })
```

## 查询限定
Expand Down
4 changes: 2 additions & 2 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ function valuesValidate(values, attributes, ctx) {
*/
class Bone {

static DataTypes = DataTypes.invokable;

// private variables
#raw = {};
#rawSaved = {};
Expand Down Expand Up @@ -1712,6 +1714,4 @@ for (const getter of Spell_getters) {
});
}

Object.assign(Bone, { DataTypes });

module.exports = Bone;
64 changes: 32 additions & 32 deletions src/data_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ class DataType {
* STRING
* STRING(127)
* STRING.BINARY
* @param {number} length
* @param {number} dataLength
*/
class STRING extends DataType {
constructor(length = 255) {
constructor(dataLength = 255) {
super();
this.dataType = 'varchar';
this.length = length;
this.dataLength = dataLength;
}

toSqlString() {
const { length } = this;
const { dataLength } = this;
const dataType = this.dataType.toUpperCase();
const chunks = [];
chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
return chunks.join(' ');
}

Expand All @@ -134,17 +134,17 @@ class STRING extends DataType {
}

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

toSqlString() {
const { length } = this;
const { dataLength } = this;
const dataType = this.dataType.toUpperCase();
const chunks = [];
chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
return chunks.join(' ');
}

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 @@ -170,12 +170,12 @@ class VARBINARY extends BINARY {
* INTEGER.UNSIGNED
* INTEGER.UNSIGNED.ZEROFILL
* INTEGER(10)
* @param {number} length
* @param {number} dataLength
*/
class INTEGER extends DataType {
constructor(length) {
constructor(dataLength) {
super();
this.length = length;
this.dataLength = dataLength;
this.dataType = 'integer';
}

Expand All @@ -190,10 +190,10 @@ class INTEGER extends DataType {
}

toSqlString() {
const { length, unsigned, zerofill } = this;
const { dataLength, unsigned, zerofill } = this;
const dataType = this.dataType.toUpperCase();
const chunks = [];
chunks.push(length > 0 ? `${dataType}(${length})` : dataType);
chunks.push(dataLength > 0 ? `${dataType}(${dataLength})` : dataType);
if (unsigned) chunks.push('UNSIGNED');
if (zerofill) chunks.push('ZEROFILL');
return chunks.join(' ');
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 Expand Up @@ -442,11 +442,11 @@ class TEXT extends DataType {
}
super();
this.dataType = 'text';
this.length = length;
this.dataLength = length;
}

toSqlString() {
return [ this.length, this.dataType ].join('').toUpperCase();
return [ this.dataLength, this.dataType ].join('').toUpperCase();
}
}

Expand All @@ -457,11 +457,11 @@ class BLOB extends DataType {
}
super();
this.dataType = 'blob';
this.length = length;
this.dataLength = length;
}

toSqlString() {
return [ this.length, this.dataType ].join('').toUpperCase();
return [ this.dataLength, this.dataType ].join('').toUpperCase();
}

cast(value) {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/abstract/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function createType(DataTypes, params) {
case 'VARBINARY':
case 'CHAR':
case 'VARCHAR':
return new DataType(type.length);
return new DataType(type.dataLength);
default:
return new DataType();
}
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
3 changes: 3 additions & 0 deletions src/utils/invokable.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function invokable(DataType) {

// INTEGER.UNSIGNED
get(target, p) {
// ref: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/length
// The length property indicates the number of parameters expected by the function.
// invokable INTEGER.toSqlString() will default to return "INTEGER(1)"
return target.hasOwnProperty(p) ? target[p] : new target()[p];
}
});
Expand Down
16 changes: 10 additions & 6 deletions test/types/custom_driver.ts → test/types/custom_driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,21 @@ describe('=> Realm (TypeScript)', function () {
});
});

afterEach(async () => {
await realm?.driver?.dropTable('test_user');
});

describe('realm.define(name, attributes, options, descriptors)', async function() {
it('options and descriptors should be optional', async function() {
assert.doesNotThrow(function() {
const { STRING } = realm.DataTypes;
realm.define('User', { name: STRING });
realm.define('TestUser', { name: STRING });
});
});

it('can customize attributes with descriptors', async function() {
const { STRING } = realm.DataTypes;
const User = realm.define('User', { name: STRING }, {}, {
const User = realm.define('TestUser', { name: STRING }, {}, {
get name() {
return this.attribute('name').replace(/^([a-z])/, function(m, chr) {
return chr.toUpperCase();
Expand All @@ -216,31 +220,31 @@ describe('=> Realm (TypeScript)', function () {
it('options should be optional', async function() {
assert.doesNotThrow(async () => {
const { STRING } = realm.DataTypes;
realm.define('User', { name: STRING });
realm.define('TestUser', { name: STRING });
await realm.sync();
});
});

it('`force` can be passed individually', async function() {
assert.doesNotThrow(async () => {
const { STRING } = realm.DataTypes;
realm.define('User', { name: STRING });
realm.define('TestUser', { name: STRING });
await realm.sync({ force: true });
});
});

it('`alter` can be passed individually', async function() {
assert.doesNotThrow(async () => {
const { STRING } = realm.DataTypes;
realm.define('User', { name: STRING });
realm.define('TestUser', { name: STRING });
await realm.sync({ alter: true });
});
});

it('`force` and `alter` can be passed together', async function() {
assert.doesNotThrow(async () => {
const { STRING } = realm.DataTypes;
realm.define('User', { name: STRING });
realm.define('TestUser', { name: STRING });
await realm.sync({ force: true, alter: true });
});
});
Expand Down
Loading

0 comments on commit 2f2ca45

Please sign in to comment.