Skip to content

Commit

Permalink
refactor: turn on compilerOptions.strict (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Nov 1, 2023
1 parent c5bdd50 commit 0d34906
Show file tree
Hide file tree
Showing 38 changed files with 396 additions and 353 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
docs
types
10 changes: 1 addition & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage
docs/vendor
docs/.bundle
dist
lib
logs
node_modules
tmp
Expand All @@ -11,12 +12,3 @@ tmp
docs/Gemfile.lock
package-lock.json
.DS_Store
test/types/*.js
test/types/*.map
test/integration/suite/sharding.test.js*
test/models/photo.js*
src/browser.js*
src/decorators.js*
src/data_types.js*
src/drivers/sqljs/*.js*
src/raw.js*
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
"name": "leoric",
"version": "2.11.4",
"description": "JavaScript Object-relational mapping alchemy",
"main": "index.js",
"browser": "dist/browser.js",
"types": "index.d.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"dist",
"src",
"index.js",
"index.d.ts"
"lib"
],
"scripts": {
"jsdoc": "rm -rf docs/api && jsdoc -c .jsdoc.json -d docs/api -t node_modules/@cara/minami",
"clean": "tsc -b --clean",
"lint-staged": "lint-staged",
"prepack": "tsc",
"prepack:browser": "rm -rf dist && tsc -p tsconfig.browser.json",
"copy-dts": "mkdir -p lib && cd src && rsync -R ./**/*.d.ts ./*.d.ts ../lib && cd -",
"copy-dts:browser": "mkdir -p dist && cd src && rsync -R ./**/*.d.ts ./*.d.ts ../dist && cd -",
"prepack": "tsc && npm run copy-dts",
"prepack:browser": "rm -rf dist && tsc -p tsconfig.browser.json && npm run copy-dts:browser",
"prepublishOnly": "npm run prepack && npm run prepack:browser",
"pretest": "npm run prepack && ./test/prepare.sh",
"test": "./test/start.sh",
Expand Down Expand Up @@ -101,25 +101,28 @@
"@babel/eslint-parser": "^7.14.7",
"@cara/minami": "^1.2.3",
"@journeyapps/sqlcipher": "^5.2.0",
"@tsconfig/node16": "^16.1.1",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.1",
"@types/sinon": "^10.0.20",
"@types/sql.js": "^1.4.4",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^7.20.0",
"eslint": "^8.52.0",
"eslint-plugin-no-only-tests": "^3.0.0",
"expect.js": "^0.3.1",
"husky": "^8.0.3",
"jsdoc": "^3.6.3",
"lint-staged": "^13.2.2",
"mocha": "^8.2.1",
"mocha": "^10.2.0",
"mysql": "^2.17.1",
"mysql2": "^2.3.0",
"nyc": "^15.1.0",
"pg": "^8.5.1",
"sinon": "^10.0.0",
"sql.js": "^1.8.0",
"sqlite3": "^5.0.2",
"ts-node": "^10.9.1",
"typescript": "^4.6.2"
}
}
9 changes: 7 additions & 2 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports = function sequelize(Bone) {
* @protect
* store all configured scopes
*/
static _scopes = {}
static _scopes = {};
// scope
static _scope = null;

Expand Down Expand Up @@ -159,14 +159,19 @@ module.exports = function sequelize(Bone) {
const parentTable = this.table;
const parent = this;
class ScopeClass extends this {
static name = parentName;
static get synchronized() {
return parent.synchronized;
}
static get table() {
return parentTable;
}
}
Object.defineProperty(ScopeClass, 'name', {
value: parentName,
writable: false,
enumerable: false,
configurable: true,
});

ScopeClass.setScope(name, ...args);
return ScopeClass;
Expand Down
6 changes: 4 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const Realm = require('./realm/base');
const AbstractDriver = require('./drivers/abstract');
const { isBone } = require('./utils');

import { ConnectOptions } from './drivers';

/**
* @typedef {Object} RawSql
* @property {boolean} __raw
Expand All @@ -25,15 +27,15 @@ const { isBone } = require('./utils');
* @param {string|Bone[]} opts.models - an array of models
* @returns {Pool} the connection pool in case we need to perform raw query
*/
export const connect = async function connect(opts) {
export const connect = async function connect(opts: ConnectOptions & { Bone?: typeof Bone }): Promise<InstanceType<typeof Realm>> {
opts = { Bone, ...opts };
if (opts.Bone.driver) throw new Error('connected already');
const realm = new Realm(opts);
await realm.connect();
return realm;
};

export const disconnect = async function disconnect(realm, ...args) {
export const disconnect = async function disconnect(realm: InstanceType<typeof Realm>, ...args: unknown[]) {
if (realm instanceof Realm && realm.connected) {
return await realm.disconnect(...args);
}
Expand Down
37 changes: 20 additions & 17 deletions src/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum LENGTH_VARIANTS {
empty = '',
medium = 'medium',
long = 'long',
};
}

export interface AbstractDataType<T> {
new (dataLength?: LENGTH_VARIANTS | number): DataType & T;
Expand All @@ -24,7 +24,7 @@ export interface AbstractDataType<T> {
*/

export abstract class DataType {
dataType: string = '';
dataType = '';
dataLength?: string | number;

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ function hasDataLength(dataLength: string | number | undefined) {
* @param {number} dataLength
*/
class STRING extends DataType {
constructor(dataLength: number = 255) {
constructor(dataLength = 255) {
super();
this.dataType = 'varchar';
this.dataLength = dataLength;
Expand Down Expand Up @@ -90,7 +90,7 @@ class STRING extends DataType {
}

class CHAR extends STRING {
constructor(dataLength: number = 255) {
constructor(dataLength = 255) {
super(dataLength);
this.dataType = 'char';
}
Expand Down Expand Up @@ -289,7 +289,7 @@ class DATE extends DataType {
precision?: number | null;
timezone?: boolean = true;

constructor(precision?: number | null, timezone: boolean = true) {
constructor(precision?: number | null, timezone = true) {
super();
this.dataType = 'datetime';
this.precision = precision;
Expand All @@ -305,7 +305,7 @@ class DATE extends DataType {
return dataType;
}

_round(value) {
_round(value: Date) {
const { precision } = this;
if (precision != null && precision < 3 && value instanceof Date) {
const divider = 10 ** (3 - precision);
Expand Down Expand Up @@ -359,7 +359,7 @@ class DATEONLY extends DATE {
return this.dataType.toUpperCase();
}

_round(value) {
_round(value: Date) {
if (value instanceof Date) {
return new Date(value.getFullYear(), value.getMonth(), value.getDate());
}
Expand All @@ -377,7 +377,7 @@ class BOOLEAN extends DataType {
return this.dataType.toUpperCase();
}

cast(value) {
cast(value: boolean) {
if (value == null) return value;
return Boolean(value);
}
Expand Down Expand Up @@ -412,7 +412,7 @@ class BLOB extends DataType {
return [ this.dataLength, this.dataType ].join('').toUpperCase();
}

cast(value) {
cast(value: Buffer | string) {
if (value == null) return value;
if (Buffer.isBuffer(value)) return value;
return Buffer.from(value);
Expand All @@ -434,7 +434,7 @@ class MYJSON extends DataType {
return 'TEXT';
}

cast(value) {
cast(value: null | object | string) {
if (!value) return value;
// type === JSONB
if (typeof value === 'object') return value;
Expand All @@ -446,7 +446,7 @@ class MYJSON extends DataType {
}
}

uncast(value) {
uncast(value: null | Raw | object) {
if (value == null || value instanceof Raw) return value;
return global.JSON.stringify(value);
}
Expand All @@ -471,7 +471,8 @@ class JSONB extends MYJSON {
}

class VIRTUAL extends DataType {
virtual: boolean = true;
virtual = true;

constructor() {
super();
this.dataType = 'virtual';
Expand Down Expand Up @@ -532,23 +533,25 @@ class DataTypes {
static BOOLEAN: DATA_TYPE<BOOLEAN> = BOOLEAN as any;

static findType(columnType: string): DataTypes {
/* eslint-disable @typescript-eslint/no-shadow */
const {
CHAR, STRING, TEXT, DATE, DATEONLY,
TINYINT, SMALLINT, MEDIUMINT, INTEGER,
TINYINT, SMALLINT, MEDIUMINT, INTEGER,
BIGINT, DECIMAL, BOOLEAN,
BINARY, VARBINARY, BLOB,
} = this;
/* eslint-enable @typescript-eslint/no-shadow */

const res = columnType?.match(/(\w+)(?:\((\d+)(?:,(\d+))?\))?/);
if(!res) {
if (!res) {
throw new Error(`Unknown columnType ${columnType}`);
}
const [ , dataType, ...matches ] = res;
const params: any[] = [];
for (let i = 0; i < matches.length; i++) {
if (matches[i] != null) params[i] = parseInt(matches[i], 10);
}

switch (dataType) {
case 'char':
return new CHAR(...params);
Expand Down Expand Up @@ -608,12 +611,12 @@ class DataTypes {

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

/**
Expand Down
14 changes: 7 additions & 7 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ColumnOption extends Omit<ColumnBase, 'columnName'| 'columnType'> {
}
}

function findType(tsType) {
function findType(tsType: typeof BigInt | typeof Number | typeof Date | typeof String | typeof Boolean) {
const {
BIGINT, INTEGER,
DATE,
Expand All @@ -40,7 +40,7 @@ function findType(tsType) {
export function Column(options: ColumnOption | DATA_TYPE<DataType> | DataType = {}) {
return function(target: Bone, propertyKey: string) {
// target refers to model prototype, an internal instance of `Bone {}`
if (options['prototype'] instanceof DataType || options instanceof DataType) {
if (('prototype' in options && options['prototype'] instanceof DataType) || options instanceof DataType) {
options = { type: options as DATA_TYPE<DataType> };
}

Expand All @@ -55,7 +55,7 @@ export function Column(options: ColumnOption | DATA_TYPE<DataType> | DataType =
// target refers to model prototype, an internal instance of `Bone {}`
const model = target.constructor as any;
if (!model.hasOwnProperty('attributes') || !model.attributes) {
Object.defineProperty(model, 'attributes', {
Object.defineProperty(model, 'attributes', {
value: { ...model.attributes },
writable: false,
enumerable: false,
Expand All @@ -77,7 +77,7 @@ export function HasMany(options: AssociateOptions = {}) {
...Reflect.getMetadata(hasMany, model),
[propertyKey]: options,
}, model);
}
};
}

export function HasOne(options: AssociateOptions = {}) {
Expand All @@ -91,12 +91,12 @@ export function HasOne(options: AssociateOptions = {}) {
...Reflect.getMetadata(hasOne, model),
[propertyKey]: options,
}, model);
}
};
}

/**
* design:type will be `Function { [native code] }` in following example
*
*
* @example
* import type Foo from './foo';
* class Bar extends Bone {
Expand All @@ -115,5 +115,5 @@ export function BelongsTo(options: AssociateOptions = {}) {
...Reflect.getMetadata(belongsTo, model),
[propertyKey]: options,
}, model);
}
};
}
7 changes: 6 additions & 1 deletion src/drivers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class AbstractDriver {
* disconnect manually
* @param callback
*/
disconnect(callback?: Function): Promise<boolean | void>;
disconnect(callback?: () => Promise<void>): Promise<boolean | void>;

/**
* query with spell
Expand Down Expand Up @@ -186,3 +186,8 @@ export class SqliteDriver extends AbstractDriver {
type: 'sqlite';
dialect: 'sqlite';
}

export class SqljsDriver extends AbstractDriver {
type: 'sqlite';
dialect: 'sqlite';
}
2 changes: 2 additions & 0 deletions src/drivers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const MysqlDriver = require('./mysql');
const PostgresDriver = require('./postgres');
const SqliteDriver = require('./sqlite');
const { default: SqljsDriver } = require('./sqljs');
const AbstractDriver = require('./abstract');

function findDriver(dialect) {
Expand All @@ -25,5 +26,6 @@ module.exports = {
MysqlDriver,
PostgresDriver,
SqliteDriver,
SqljsDriver,
AbstractDriver,
};
Loading

0 comments on commit 0d34906

Please sign in to comment.