From 2484daf09d7fd3b45483030c80c0554b5f8329ca Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 4 Jul 2024 23:48:52 +0800 Subject: [PATCH] refactor: refactor types --- src/document.ts | 4 ++-- src/model.ts | 30 +++++++++++++++--------------- test/scripts/model.ts | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/document.ts b/src/document.ts index dfe5479..7130a06 100644 --- a/src/document.ts +++ b/src/document.ts @@ -69,7 +69,7 @@ abstract class Document { * * @return {object} */ - toObject(): T { + toObject(): T extends object ? T : never { const keys = Object.keys(this); const obj: Partial = {}; @@ -80,7 +80,7 @@ abstract class Document { obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]); } - return obj as T; + return obj as T extends object ? T : never; } /** diff --git a/src/model.ts b/src/model.ts index adbd599..3db7b58 100644 --- a/src/model.ts +++ b/src/model.ts @@ -18,8 +18,8 @@ class Model extends EventEmitter { data: Record = {}; schema: Schema; length = 0; - Document; - Query; + Document: { new(data: T): Document }; + Query: { new(data: Document[]): Query }; _database: Database; /** @@ -154,7 +154,7 @@ class Model extends EventEmitter { const schema = this.schema; // Apply getters - const data = (data_ instanceof this.Document ? data_ : this.new(data_ as T)) as Document; + const data = (data_ instanceof this.Document ? data_ : this.new(data_)); const id = data._id; // Check ID @@ -168,7 +168,7 @@ class Model extends EventEmitter { // Apply setters const result = data.toObject(); - schema._applySetters(result as object); + schema._applySetters(result); // Pre-hooks return execHooks(schema, 'pre', 'save', data).then(data => { @@ -236,7 +236,7 @@ class Model extends EventEmitter { * @return {BluebirdPromise} * @private */ - _updateWithStack(id: string | number, stack: ((data: any) => void)[]): BluebirdPromise { + _updateWithStack(id: string | number, stack: ((data: T) => void)[]): BluebirdPromise { const schema = this.schema; const data = this.data[id]; @@ -280,7 +280,7 @@ class Model extends EventEmitter { */ updateById(id: string | number, update: object, callback?: NodeJSLikeCallback): BluebirdPromise { return BluebirdPromise.using(this._acquireWriteLock(), () => { - const stack = this.schema._parseUpdate(update as object); + const stack = this.schema._parseUpdate(update); return this._updateWithStack(id, stack); }).asCallback(callback); } @@ -294,7 +294,7 @@ class Model extends EventEmitter { * @return {BluebirdPromise} */ update(query: object, data: object, callback?: NodeJSLikeCallback): BluebirdPromise { - return (this.find(query) as Query).update(data, callback); + return this.find(query).update(data, callback); } /** @@ -315,11 +315,11 @@ class Model extends EventEmitter { (data_ as any)._id = id; // Apply getters - const data = (data_ instanceof this.Document ? data_ : this.new(data_ as T)) as Document; + const data = (data_ instanceof this.Document ? data_ : this.new(data_)); // Apply setters const result = data.toObject(); - schema._applySetters(result as object); + schema._applySetters(result); // Pre-hooks return execHooks(schema, 'pre', 'save', data).then(data => { @@ -351,8 +351,8 @@ class Model extends EventEmitter { * @param {function} [callback] * @return {BluebirdPromise} */ - replace(query: object, data, callback?: NodeJSLikeCallback): BluebirdPromise { - return (this.find(query) as Query).replace(data, callback); + replace(query: object, data: T | Document, callback?: NodeJSLikeCallback): BluebirdPromise { + return this.find(query).replace(data, callback); } /** @@ -401,7 +401,7 @@ class Model extends EventEmitter { * @return {BluebirdPromise} */ remove(query: object, callback?: NodeJSLikeCallback): BluebirdPromise { - return (this.find(query) as Query).remove(callback); + return this.find(query).remove(callback); } /** @@ -500,7 +500,7 @@ class Model extends EventEmitter { } } - return options.lean ? arr : new this.Query(arr); + return options.lean ? arr as T[] : new this.Query(arr as Document[]); } /** @@ -881,7 +881,7 @@ class Model extends EventEmitter { } if (options.match) { - cache = (new Query(arr) as Query).find(options.match, options); + cache = new Query(arr).find(options.match, options); } else if (options.skip) { if (options.limit) { arr = arr.slice(options.skip, options.skip + options.limit); @@ -1010,7 +1010,7 @@ class Model extends EventEmitter { Model.prototype.get = Model.prototype.findById; function execHooks(schema: Schema, type: keyof Schema['hooks'], event: keyof Schema['hooks'][keyof Schema['hooks']], data: any): BluebirdPromise { - const hooks = schema.hooks[type][event] as ((data: any) => BluebirdPromise | void)[]; + const hooks = schema.hooks[type][event]; if (!hooks.length) return BluebirdPromise.resolve(data); return BluebirdPromise.each(hooks, hook => hook(data)).thenReturn(data); diff --git a/test/scripts/model.ts b/test/scripts/model.ts index abf0a70..6b3748c 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -532,7 +532,7 @@ describe('Model', () => { {age: 30}, {age: 40} ]).then(data => { - const query = User.find({age: 20}) as Query; + const query = User.find({age: 20}); query.data.should.eql(data.slice(1, 3)); return data; }).map(item => User.removeById(item._id))); @@ -544,7 +544,7 @@ describe('Model', () => { {age: 30}, {age: 40} ]).then(data => { - const query = User.find({}) as Query; + const query = User.find({}); query.data.should.eql(data); return data; }).map(item => User.removeById(item._id))); @@ -566,7 +566,7 @@ describe('Model', () => { {age: 30}, {age: 40} ]).then(data => { - const query = User.find({age: {$gte: 20}}, {limit: 2}) as Query; + const query = User.find({age: {$gte: 20}}, {limit: 2}); query.data.should.eql(data.slice(1, 3)); return data; }).map(item => User.removeById(item._id))); @@ -577,11 +577,11 @@ describe('Model', () => { {age: 30}, {age: 40} ]).then(data => { - let query = User.find({age: {$gte: 20}}, {skip: 1}) as Query; + let query = User.find({age: {$gte: 20}}, {skip: 1}); query.data.should.eql(data.slice(2)); // with limit - query = User.find({age: {$gte: 20}}, {limit: 1, skip: 1}) as Query; + query = User.find({age: {$gte: 20}}, {limit: 1, skip: 1}); query.data.should.eql(data.slice(2, 3)); return data; @@ -610,7 +610,7 @@ describe('Model', () => { ] }); - (query as Query).toArray().should.eql([data[1]]); + query.toArray().should.eql([data[1]]); return data; }).map(item => User.removeById(item._id))); @@ -627,7 +627,7 @@ describe('Model', () => { ] }); - (query as Query).toArray().should.eql(data.slice(1)); + query.toArray().should.eql(data.slice(1)); return data; }).map(item => User.removeById(item._id))); @@ -644,7 +644,7 @@ describe('Model', () => { ] }); - (query as Query).toArray().should.eql([data[0]]); + query.toArray().should.eql([data[0]]); return data; }).map(item => User.removeById(item._id))); @@ -658,7 +658,7 @@ describe('Model', () => { $not: {'name.last': 'Doe'} }); - (query as Query).toArray().should.eql(data.slice(2)); + query.toArray().should.eql(data.slice(2)); return data; }).map(item => User.removeById(item._id))); @@ -674,7 +674,7 @@ describe('Model', () => { } }); - (query as Query).toArray().should.eql(data.slice(0, 2)); + query.toArray().should.eql(data.slice(0, 2)); return data; }).map(item => User.removeById(item._id)));