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

refactor: refactor types #249

Merged
merged 4 commits into from
May 4, 2024
Merged

refactor: refactor types #249

merged 4 commits into from
May 4, 2024

Conversation

D-Sketon
Copy link
Member

@D-Sketon D-Sketon commented May 1, 2024

check list

  • Add test cases for the changes.
  • Passed the CI test.

Description

ref: hexojs/hexo#5483 (comment)

Additional information

How to use it now:
virtual

const Asset = new warehouse.Schema<AssetSchema>({
    _id: {type: String, required: true},
    path: {type: String, required: true},
    modified: {type: Boolean, default: true},
    renderable: {type: Boolean, default: true}
  });

  Asset.virtual('source').get(function() {
    return join(ctx.base_dir, this._id);
  });

static

const Cache = new warehouse.Schema<CacheSchema>({
    _id: {type: String, required: true},
    hash: {type: String, default: ''},
    modified: {type: Number, default: Date.now() } // UnixTime
  });

  Cache.static('compareFile', function(id: string,
    hashFn: (id: string) => Promise<string>,
    statFn: (id: string) => Promise<fs.Stats>): Promise<{ type: string }> {
    const cache = this.findById(id) as Document<CacheSchema>;

    // If cache does not exist, then it must be a new file. We have to get both
    // ...
  });

@coveralls
Copy link

coveralls commented May 1, 2024

Pull Request Test Coverage Report for Build 8925325272

Details

  • 21 of 21 (100.0%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 98.275%

Totals Coverage Status
Change from base Build 8922730701: 0.0%
Covered Lines: 4388
Relevant Lines: 4465

💛 - Coveralls

src/schema.ts Outdated
@@ -379,7 +380,7 @@ class QueryParser {
}


class Schema {
class Schema<T> {
paths: Record<string, SchemaType<any>> = {};
statics: Record<string, (...args: any[]) => any> = {};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
statics: Record<string, (...args: any[]) => any> = {};
statics: Record<string, (this: T, ...args: any[]) => any> = {};

Is it possible to improve the types here as well? This could achieve stricter type checking.

src/database.ts Outdated
@@ -118,7 +118,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: Schema | Record<string, AddSchemaTypeOptions>): Model<any> {
model<T>(name: string, schema?: Schema<T> | Record<string, AddSchemaTypeOptions>): Model<any> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
model<T>(name: string, schema?: Schema<T> | Record<string, AddSchemaTypeOptions>): Model<any> {
model<T = any>(name: string, schema?: Schema<T> | Record<string, AddSchemaTypeOptions>): Model<any> {

We can assign a default value to any to prevent breaking change (where model previously doesn't require a type argument).

src/model.ts Outdated Show resolved Hide resolved
src/types/virtual.ts Outdated Show resolved Hide resolved
@D-Sketon D-Sketon marked this pull request as ready for review May 4, 2024 05:28
@D-Sketon
Copy link
Member Author

D-Sketon commented May 4, 2024

In fact, the this of virtual and method is Document<T> rather than T, but in practice the two are similar.
The problem is that the current handling here is still not elegant enough, and Document in Typescript doesn't perfectly get all the member types in the generic:

[key : string]: any;

@SukkaW SukkaW merged commit 83df5a1 into hexojs:master May 4, 2024
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants