Skip to content
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

fix: projects might have strictPropertyInitialization set to true #329

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
2 changes: 1 addition & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pluralize = require('pluralize');
const { executeValidator, LeoricValidateError } = require('./validator');
require('reflect-metadata');

const { DataTypes } = require('./data_types');
const { default: DataTypes } = require('./data_types');
const Collection = require('./collection');
const Spell = require('./spell');
const Raw = require('./raw');
Expand Down
46 changes: 19 additions & 27 deletions src/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@ export interface AbstractDataType<T> {
*/

export abstract class DataType {
dataType: string;
dataType: string = '';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compilerOptions.strictPropertyInitialization complains about unset class field

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which is quite common in our test models, such as

class Foo extends Bone {
  @Column()
  id: bigint;
}

added a default value to make downstream projects happy for now

dataLength?: string | number;

/**
* Check if params is instance of DataType or not
* @param {*} params
* @returns {boolean}
*/
static is(params: any): boolean {
return params instanceof DataType;
}

/**
* cast raw data returned from data packet into js type
*/
Expand All @@ -51,21 +42,7 @@ export abstract class DataType {
return value;
}

static get invokable() {
return new Proxy(this, {
get(target, p) {
const value = target[p];
if (AllDataTypes.hasOwnProperty(p)) return invokableFunc(value);
return value;
}
});
}

abstract toSqlString(): string;

static toSqlString(): string {
return '';
}
}

/**
Expand Down Expand Up @@ -507,7 +484,7 @@ const AllDataTypes = {

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

export class DataTypes extends DataType {
class DataTypes {
static STRING: DATA_TYPE<STRING> = STRING as any;
static TINYINT: DATA_TYPE<TINYINT> = TINYINT as any;
static SMALLINT: DATA_TYPE<SMALLINT> = SMALLINT as any;
Expand Down Expand Up @@ -598,8 +575,23 @@ export class DataTypes extends DataType {
}
}

toSqlString(): string {
return '';
static get invokable() {
return new Proxy(this, {
get(target, p) {
const value = target[p];
if (AllDataTypes.hasOwnProperty(p)) return invokableFunc(value);
return value;
}
});
}

/**
* Check if params is instance of DataType or not
* @param {*} params
* @returns {boolean}
*/
static is(params: any): boolean {
return params instanceof DataType;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

further refactor the default export class DataTypes and the base class DataType, which can be imported as import DataTypes, { DataType } from './data_types';

existing code that relies on const { DataTypes } = require('./data_types') is changed to const { default: DataTypes } = require('./data_types') accordingly

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/mysql/data_types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { DataTypes } = require('../../data_types');
const { default: DataTypes } = require('../../data_types');

class Mysql_BOOLEAN extends DataTypes.BOOLEAN {
constructor() {
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/postgres/data_types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { DataTypes } = require('../../data_types');
const { default: DataTypes } = require('../../data_types');
const util = require('util');
const Raw = require('../../raw');

Expand Down Expand Up @@ -29,7 +29,7 @@ class Postgres_JSONB extends DataTypes.JSONB {
}
}

class Postgres_BINARY extends DataTypes {
class Postgres_BINARY extends DataTypes.BINARY {
constructor() {
super();
this.dataType = 'bytea';
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class PostgresDriver extends AbstractDriver {
for (const row of rows) {
const tableName = row.table_name;
const columns = schemaInfo[tableName] || (schemaInfo[tableName] = []);
let { data_type: dataType, character_maximum_length: length } = row;
const { character_maximum_length: length } = row;
let { data_type: dataType } = row;

if (dataType === 'character varying') dataType = 'varchar';
if (dataType === 'timestamp without time zone') dataType = 'timestamp';
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/sqlite/data_types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { DataTypes } = require('../../data_types');
const { default: DataTypes } = require('../../data_types');

class Sqlite_DATE extends DataTypes.DATE {
constructor(precision, timezone) {
Expand Down Expand Up @@ -53,7 +53,7 @@ class Sqlite_BIGINT extends DataTypes.BIGINT {
return this.dataType.toUpperCase();
}
}
class Sqlite_BINARY extends DataTypes {
class Sqlite_BINARY extends DataTypes.BINARY {
constructor(dataLength = 255) {
super(dataLength);
this.dataLength = dataLength;
Expand Down