8000 fix: INTEGER like data type and STRING extra options didn't work in polymorphism, fix decorators ColumnOptions.type to support invokable by JimmyDaddy · Pull Request #334 · cyjake/leoric · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: INTEGER like data type and STRING extra options didn't work in polymorphism, fix decorators ColumnOptions.type to support i 8000 nvokable #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ class INTEGER extends DataType {
unsigned?: boolean;
zerofill?: boolean;

constructor(dataLength?: number) {
constructor(dataLength?: number, unsigned?: boolean, zerofill?: boolean) {
super();
this.dataLength = dataLength;
this.dataType = 'integer';
this.unsigned = unsigned;
this.zerofill = zerofill;
}

get UNSIGNED() {
Expand Down Expand Up @@ -168,8 +170,8 @@ class INTEGER extends DataType {
* @param {number} dataLength
*/
class TINYINT extends INTEGER {
constructor(dataLength?: number) {
super(dataLength);
constructor(dataLength?: number, unsigned?: boolean, zerofill?: boolean) {
super(dataLength, unsigned, zerofill);
this.dataType = 'tinyint';
}
}
Expand All @@ -183,8 +185,8 @@ class TINYINT extends INTEGER {
* @param {number} dataLength
*/
class SMALLINT extends INTEGER {
constructor(dataLength?: number) {
super(dataLength);
constructor(dataLength?: number, unsigned?: boolean, zerofill?: boolean) {
super(dataLength, unsigned, zerofill);
this.dataType = 'smallint';
}
}
Expand All @@ -198,8 +200,8 @@ class SMALLINT extends INTEGER {
* @param {number} dataLength
*/
class MEDIUMINT extends INTEGER {
constructor(dataLength?: number) {
super(dataLength);
constructor(dataLength?: number, unsigned?: boolean, zerofill?: boolean) {
super(dataLength, unsigned, zerofill);
this.dataType = 'mediumint';
}
}
Expand All @@ -214,8 +216,8 @@ class MEDIUMINT extends INTEGER {
* @param {number} dataLength
*/
class BIGINT extends INTEGER {
constructor(dataLength?: number) {
super(dataLength);
constructor(dataLength?: number, unsigned?: boolean, zerofill?: boolean) {
super(dataLength, unsigned, zerofill);
this.dataType = 'bigint';
}
}
Expand Down Expand Up @@ -482,7 +484,7 @@ const AllDataTypes = {
VIRTUAL,
};

type DATA_TYPE<T> = AbstractDataType<T> & T;
export type DATA_TYPE<T> = AbstractDataType<T> & T;

class DataTypes {
static STRING: DATA_TYPE<STRING> = STRING as any;
Expand Down
8 changes: 4 additions & 4 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'reflect-metadata';

import Bone from './bone';
import DataTypes, { DataType, AbstractDataType } from './data_types';
import DataTypes, { DataType, DATA_TYPE } from './data_types';
import { ASSOCIATE_METADATA_MAP } from './constants';
import { ColumnBase, Validator, AssociateOptions } from './types/common';

interface ColumnOption extends Omit<ColumnBase, 'columnName'| 'columnType'> {
type?: AbstractDataType<DataType>;
type?: DataType;
name?: string;
validate?: {
[key: string]: Validator;
Expand Down Expand Up @@ -37,13 +37,13 @@ function findType(tsType) {
}
}

export function Column(options?: ColumnOption | AbstractDataType<DataType>) {
export function Column(options?: ColumnOption | DATA_TYPE<DataType>) {
return function(target: Bone, propertyKey: string) {
if (options == null) {
options = {};
}
// target refers to model prototype, an internal instance of `Bone {}`
if (options['prototype'] instanceof DataType) options = { type: options as AbstractDataType<DataType> };
if (options['prototype'] instanceof DataType) options = { type: options as DATA_TYPE<DataType> };

if (!('type' in options)) {
const tsType = Reflect.getMetadata('design:type', target, propertyKey);
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/abstract/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ function createType(DataTypes, params) {
case 'MEDIUMINT':
case 'INTEGER':
case 'BIGINT':
return new DataType(type.dataLength, type.unsigned, type.zerofill);
case 'BINARY':
case 'VARBINARY':
case 'CHAR':
case 'VARCHAR':
case 'STRING':
return new DataType(type.dataLength);
default:
return new DataType();
Expand Down
8 changes: 4 additions & 4 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.BINARY {
}

class Postgres_INTEGER extends DataTypes.INTEGER {
constructor(dataLength) {
super(dataLength);
constructor(dataLength, unsigned, zerofill) {
super(dataLength, unsigned, zerofill);
}

uncast(value) {
Expand All @@ -55,8 +55,8 @@ class Postgres_INTEGER extends DataTypes.INTEGER {
}

class Postgres_BIGINT extends Postgres_INTEGER {
constructor() {
super();
constructor(dataLength, unsigned, zerofill) {
super(dataLength, unsigned, zerofill);
this.dataType = 'bigint';
}
}
Expand Down
8 changes: 4 additions & 4 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(dataLength) {
super(dataLength);
constructor(dataLength, unsigned, zerofill) {
super(dataLength, unsigned, zerofill);
}

uncast(value) {
Expand All @@ -44,8 +44,8 @@ class Sqlite_INTEGER extends DataTypes.INTEGER {

}
class Sqlite_BIGINT extends DataTypes.BIGINT {
constructor() {
super();
constructor(dataLength, unsigned, zerofill) {
super(dataLength, unsigned, zerofill);
this.dataType = 'integer';
}

Expand Down
33 changes: 32 additions & 1 deletion test/types/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { strict as assert } from 'assert';
import { AttributeMeta, Bone, DataTypes, Column, HasMany, connect } from '../..';

const { TEXT } = DataTypes;
const { TEXT, STRING, INTEGER } = DataTypes;

describe('=> Decorators (TypeScript)', function() {
before(async function() {
Expand Down Expand Up @@ -201,6 +201,37 @@ describe('=> Decorators (TypeScript)', function() {
});

it('should work with other options', async () => {
class Note extends Bone {
@Column()
id: bigint;

@Column({
type: STRING
})
body: string;

@Column({
type: STRING(64)
})
description: string;

@Column({
type: INTEGER(2).UNSIGNED,
})
status: number;
}
await Note.sync({ force: true });
assert.deepEqual(Object.keys(Note.attributes), [ 'id', 'body', 'description', 'status' ]);

const { id, body, description, status } = Note.attributes;
assert.equal((id as AttributeMeta).toSqlString(), '`id` BIGINT PRIMARY KEY AUTO_INCREMENT');
assert.equal((body as AttributeMeta).toSqlString(), '`body` VARCHAR(255)');
assert.equal((description as AttributeMeta).toSqlString(), '`description F438 ` VARCHAR(64)');
assert.equal((status as AttributeMeta).toSqlString(), '`status` INTEGER(2) UNSIGNED');

});

it('should work with type options', async () => {
class Note extends Bone {
@Column({
primaryKey: true,
Expand Down
7 changes: 7 additions & 0 deletions test/unit/data_types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,39 @@ describe('=> Data Types', () => {
assert.equal(new TINYINT(1).toSqlString(), 'TINYINT(1)');
assert.equal(new TINYINT().UNSIGNED.toSqlString(), 'TINYINT UNSIGNED');
assert.equal(new TINYINT().UNSIGNED.ZEROFILL.toSqlString(), 'TINYINT UNSIGNED ZEROFILL');
assert.equal(new TINYINT(1, true, true).toSqlString(), 'TINYINT(1) UNSIGNED ZEROFILL');

});

it('SMALLINT', () => {
assert.equal(new SMALLINT().dataType, 'smallint');
assert.equal(new SMALLINT(1).toSqlString(), 'SMALLINT(1)');
assert.equal(new SMALLINT().UNSIGNED.toSqlString(), 'SMALLINT UNSIGNED');
assert.equal(new SMALLINT().UNSIGNED.ZEROFILL.toSqlString(), 'SMALLINT UNSIGNED ZEROFILL');
assert.equal(new SMALLINT(1, true, true).toSqlString(), 'SMALLINT(1) UNSIGNED ZEROFILL');

});

it('MEDIUMINT', () => {
assert.equal(new MEDIUMINT().dataType, 'mediumint');
assert.equal(new MEDIUMINT(1).toSqlString(), 'MEDIUMINT(1)');
assert.equal(new MEDIUMINT().UNSIGNED.toSqlString(), 'MEDIUMINT UNSIGNED');
assert.equal(new MEDIUMINT().UNSIGNED.ZEROFILL.toSqlString(), 'MEDIUMINT UNSIGNED ZEROFILL');
assert.equal(new MEDIUMINT(1, true, true).toSqlString(), 'MEDIUMINT(1) UNSIGNED ZEROFILL');
});

it('INTEGER', () => {
assert.equal(new INTEGER().dataType, 'integer');
assert.equal(new INTEGER(10).toSqlString(), 'INTEGER(10)');
assert.equal(new INTEGER().UNSIGNED.toSqlString(), 'INTEGER UNSIGNED');
assert.equal(new INTEGER().UNSIGNED.ZEROFILL.toSqlString(), 'INTEGER UNSIGNED ZEROFILL');
assert.equal(new INTEGER(1, true, true).toSqlString(), 'INTEGER(1) UNSIGNED ZEROFILL');
});

it('BIGINT', () => {
assert.equal(new BIGINT().dataType, 'bigint');
assert.equal(new BIGINT().UNSIGNED.toSqlString(), 'BIGINT UNSIGNED');
assert.equal(new BIGINT(1, true, true).toSqlString(), 'BIGINT(1) UNSIGNED ZEROFILL');
});

it('DECIMAL', () => {
Expand Down
14 changes: 13 additions & 1 deletion test/unit/drivers/mysql/attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert').strict;
const Attribute = require('../../../../src/drivers/mysql/attribute');
const { BOOLEAN, DATE, JSONB, STRING } = Attribute.DataTypes;
const { BOOLEAN, DATE, JSONB, STRING, INTEGER } = Attribute.DataTypes;

describe('=> Attribute (mysql)', function() {
it('should support TINYINT(1)', async function() {
Expand Down Expand Up @@ -43,4 +43,16 @@ describe('=> Attribute (mysql)', function() {
assert.equal(attribute.toSqlString(), '`isbn` VARCHAR(255) NOT NULL UNIQUE');
});

it('invokable type should work', async function() {
const attribute = new Attribute('isbn', {
type: new STRING(60),
});
assert.equal(attribute.toSqlString(), '`isbn` VARCHAR(60)');

const attribute1 = new Attribute('idd', {
type: new INTEGER(2).UNSIGNED,
});
assert.equal(attribute1.toSqlString(), '`idd` INTEGER(2) UNSIGNED');
});

});
12 changes: 12 additions & 0 deletions test/unit/drivers/postgres/attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ describe('=> Attribute (postgres)', function() {
});
assert.equal(attribute.toSqlString(), '"isbn" VARCHAR(255) NOT NULL UNIQUE');
});

it('invokable type should work', async function() {
const attribute = new Attribute('isbn', {
type: new STRING(60),
});
assert.equal(attribute.toSqlString(), '"isbn" VARCHAR(60)');

const attribute1 = new Attribute('idd', {
type: new INTEGER(2).UNSIGNED,
});
assert.equal(attribute1.toSqlString(), '"idd" INTEGER(2) UNSIGNED');
});
});
14 changes: 13 additions & 1 deletion test/unit/drivers/sqlite/attribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert').strict;
const Attribute = require('../../../../src/drivers/sqlite/attribute');
const { BIGINT, DATE, STRING } = Attribute.DataTypes;
const { BIGINT, DATE, STRING, INTEGER } = Attribute.DataTypes;

describe('=> Attribute (sqlite)', function() {
it('should support DATETIME', async function() {
Expand All @@ -28,4 +28,16 @@ describe('=> Attribute (sqlite)', function() {
});
assert.equal(attribute.toSqlString(), '"isbn" VARCHAR(255) NOT NULL UNIQUE');
});

it('invokable type should work', async function() {
const attribute = new Attribute('isbn', {
type: new STRING(60),
});
assert.equal(attribute.toSqlString(), '"isbn" VARCHAR(60)');

const attribute1 = new Attribute('idd', {
type: new INTEGER(2).UNSIGNED,
});
assert.equal(attribute1.toSqlString(), '"idd" INTEGER(2) UNSIGNED');
});
});
6 changes: 3 additions & 3 deletions test/unit/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('hooks', function() {
obj.nickname = obj.realname + 'y';
}
},
afterCreate(obj){
afterCreate(obj) {
obj.status = 10;
},
beforeBulkCreate() {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('hooks', function() {
constructor(opts) {
super(opts);
}

getFingerprint() {
return this.attribute('fingerprint');
}
Expand Down Expand Up @@ -604,7 +604,7 @@ describe('hooks', function() {
obj.email = 'hello@yo.com';
}
},
afterSave(obj){
afterSave(obj) {
obj.status = 10;
},
}
Expand Down
0