From 2c89f5398ba3ba59abdfc2b783fe6602cbf10b81 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 2 May 2024 18:59:50 +0800 Subject: [PATCH] fix(type): fix bluebird type (#248) Co-authored-by: yoshinorin --- .eslintrc.json | 3 +- src/database.ts | 14 +++--- src/document.ts | 17 +++---- src/model.ts | 90 +++++++++++++++++------------------ src/query.ts | 20 ++++---- src/schema.ts | 16 +++---- test/.eslintrc.json | 3 +- test/scripts/model.ts | 3 +- test/scripts/schema.ts | 12 ++--- test/scripts/types/virtual.ts | 4 +- 10 files changed, 92 insertions(+), 90 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 4d4b2f52..4569cd9e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,6 +10,7 @@ "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/no-var-requires": 0, "@typescript-eslint/ban-ts-comment": 0, - "no-use-before-define": 0 + "no-use-before-define": 0, + "no-extra-parens": 0 } } \ No newline at end of file diff --git a/src/database.ts b/src/database.ts index 2a301479..7cd65c7e 100644 --- a/src/database.ts +++ b/src/database.ts @@ -1,5 +1,5 @@ import { parse as createJsonParseStream } from './lib/jsonstream'; -import Bluebird from 'bluebird'; +import BluebirdPromise from 'bluebird'; import { writev, promises as fsPromises, createReadStream } from 'graceful-fs'; import { pipeline, Stream } from 'stream'; import Model from './model'; @@ -12,7 +12,7 @@ import type { AddSchemaTypeOptions, NodeJSLikeCallback } from './types'; const log = logger(); const pkg = require('../package.json'); const { open } = fsPromises; -const pipelineAsync = Bluebird.promisify(pipeline) as unknown as (...args: Stream[]) => Bluebird; +const pipelineAsync = BluebirdPromise.promisify(pipeline) as unknown as (...args: Stream[]) => BluebirdPromise; let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise; @@ -132,9 +132,9 @@ class Database { * Loads database. * * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - load(callback?: NodeJSLikeCallback): Bluebird { + load(callback?: NodeJSLikeCallback): BluebirdPromise { const { path, onUpgrade, onDowngrade, version: newVersion } = this.options; if (!path) throw new WarehouseError('options.path is required'); @@ -172,13 +172,13 @@ class Database { * Saves database. * * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - save(callback?: NodeJSLikeCallback): Bluebird { + save(callback?: NodeJSLikeCallback): BluebirdPromise { const { path } = this.options; if (!path) throw new WarehouseError('options.path is required'); - return Bluebird.resolve(exportAsync(this, path)).asCallback(callback); + return BluebirdPromise.resolve(exportAsync(this, path)).asCallback(callback); } toJSON(): { meta: { version: number, warehouse: string }, models: Record> } { diff --git a/src/document.ts b/src/document.ts index fcb5bbb3..8ac24ff0 100644 --- a/src/document.ts +++ b/src/document.ts @@ -1,6 +1,7 @@ import rfdc from 'rfdc'; import type Model from './model'; import type Schema from './schema'; +import type BluebirdPromise from 'bluebird'; import type { NodeJSLikeCallback } from './types'; const cloneDeep = rfdc(); @@ -25,9 +26,9 @@ abstract class Document { * Saves the document. * * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - save(callback?: NodeJSLikeCallback): Promise { + save(callback?: NodeJSLikeCallback): BluebirdPromise { return this._model.save(this, callback); } @@ -36,9 +37,9 @@ abstract class Document { * * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - update(data: object, callback?: NodeJSLikeCallback): Promise { + update(data: object, callback?: NodeJSLikeCallback): BluebirdPromise { return this._model.updateById(this._id, data, callback); } @@ -47,9 +48,9 @@ abstract class Document { * * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - replace(data: T | Document, callback?: NodeJSLikeCallback): Promise { + replace(data: T | Document, callback?: NodeJSLikeCallback): BluebirdPromise { return this._model.replaceById(this._id, data, callback); } @@ -57,9 +58,9 @@ abstract class Document { * Removes the document. * * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - remove(callback?: NodeJSLikeCallback): Promise { + remove(callback?: NodeJSLikeCallback): BluebirdPromise { return this._model.removeById(this._id, callback); } diff --git a/src/model.ts b/src/model.ts index 32a6429d..2cc03f0f 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,7 +1,7 @@ import { EventEmitter } from 'events'; import rfdc from 'rfdc'; const cloneDeep = rfdc(); -import Promise from 'bluebird'; +import BluebirdPromise from 'bluebird'; import { parseArgs, getProp, setGetter, shuffle } from './util'; import Document from './document'; import Query from './query'; @@ -125,13 +125,13 @@ class Model extends EventEmitter { /** * Acquires write lock. * - * @return {Promise} + * @return {BluebirdPromise} * @private */ - _acquireWriteLock(): Promise.Disposer { + _acquireWriteLock(): BluebirdPromise.Disposer { const mutex = this._mutex; - return new Promise((resolve, reject) => { + return new BluebirdPromise((resolve, reject) => { mutex.lock(resolve); }).disposer(() => { mutex.unlock(); @@ -142,10 +142,10 @@ class Model extends EventEmitter { * Inserts a document. * * @param {Document|object} data - * @return {Promise} + * @return {BluebirdPromise} * @private */ - _insertOne(data_: Document | T): Promise { + _insertOne(data_: Document | T): BluebirdPromise { const schema = this.schema; // Apply getters @@ -154,11 +154,11 @@ class Model extends EventEmitter { // Check ID if (!id) { - return Promise.reject(new WarehouseError('ID is not defined', WarehouseError.ID_UNDEFINED)); + return BluebirdPromise.reject(new WarehouseError('ID is not defined', WarehouseError.ID_UNDEFINED)); } if (this.has(id)) { - return Promise.reject(new WarehouseError('ID `' + id + '` has been used', WarehouseError.ID_EXIST)); + return BluebirdPromise.reject(new WarehouseError('ID `' + id + '` has been used', WarehouseError.ID_EXIST)); } // Apply setters @@ -181,10 +181,10 @@ class Model extends EventEmitter { * * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - insertOne(data: Document | T, callback?: NodeJSLikeCallback): Promise { - return Promise.using(this._acquireWriteLock(), () => this._insertOne(data)).asCallback(callback); + insertOne(data: Document | T, callback?: NodeJSLikeCallback): BluebirdPromise { + return BluebirdPromise.using(this._acquireWriteLock(), () => this._insertOne(data)).asCallback(callback); } /** @@ -192,11 +192,11 @@ class Model extends EventEmitter { * * @param {object|array} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - insert(data: Document | T | Document[] | T[], callback?: NodeJSLikeCallback): Promise { + insert(data: Document | T | Document[] | T[], callback?: NodeJSLikeCallback): BluebirdPromise { if (Array.isArray(data)) { - return Promise.mapSeries | T, any>(data, item => this.insertOne(item)).asCallback(callback); + return BluebirdPromise.mapSeries | T, any>(data, item => this.insertOne(item)).asCallback(callback); } return this.insertOne(data, callback); @@ -207,14 +207,14 @@ class Model extends EventEmitter { * * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - save(data: Document | T, callback?: NodeJSLikeCallback): Promise { + save(data: Document | T, callback?: NodeJSLikeCallback): BluebirdPromise { const id = (data as any)._id; if (!id) return this.insertOne(data, callback); - return Promise.using(this._acquireWriteLock(), () => { + return BluebirdPromise.using(this._acquireWriteLock(), () => { if (this.has(id)) { return this._replaceById(id, data); } @@ -228,16 +228,16 @@ class Model extends EventEmitter { * * @param {*} id * @param {array} stack - * @return {Promise} + * @return {BluebirdPromise} * @private */ - _updateWithStack(id: string | number, stack: ((data: any) => void)[]): Promise { + _updateWithStack(id: string | number, stack: ((data: any) => void)[]): BluebirdPromise { const schema = this.schema; const data = this.data[id]; if (!data) { - return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); + return BluebirdPromise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); } // Clone data @@ -271,10 +271,10 @@ class Model extends EventEmitter { * @param {*} id * @param {object} update * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - updateById(id: string | number, update: object, callback?: NodeJSLikeCallback): Promise { - return Promise.using(this._acquireWriteLock(), () => { + updateById(id: string | number, update: object, callback?: NodeJSLikeCallback): BluebirdPromise { + return BluebirdPromise.using(this._acquireWriteLock(), () => { const stack = this.schema._parseUpdate(update as object); return this._updateWithStack(id, stack); }).asCallback(callback); @@ -286,9 +286,9 @@ class Model extends EventEmitter { * @param {object} query * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - update(query: object, data: object, callback?: NodeJSLikeCallback): Promise { + update(query: object, data: object, callback?: NodeJSLikeCallback): BluebirdPromise { return (this.find(query) as Query).update(data, callback); } @@ -297,14 +297,14 @@ class Model extends EventEmitter { * * @param {*} id * @param {object} data - * @return {Promise} + * @return {BluebirdPromise} * @private */ - _replaceById(id: string | number, data_: Document | T): Promise { + _replaceById(id: string | number, data_: Document | T): BluebirdPromise { const schema = this.schema; if (!this.has(id)) { - return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); + return BluebirdPromise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); } (data_ as any)._id = id; @@ -332,10 +332,10 @@ class Model extends EventEmitter { * @param {*} id * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - replaceById(id: string | number, data: Document | T, callback?: NodeJSLikeCallback): Promise { - return Promise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback); + replaceById(id: string | number, data: Document | T, callback?: NodeJSLikeCallback): BluebirdPromise { + return BluebirdPromise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback); } /** @@ -344,9 +344,9 @@ class Model extends EventEmitter { * @param {object} query * @param {object} data * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - replace(query: object, data, callback?: NodeJSLikeCallback): Promise { + replace(query: object, data, callback?: NodeJSLikeCallback): BluebirdPromise { return (this.find(query) as Query).replace(data, callback); } @@ -354,16 +354,16 @@ class Model extends EventEmitter { * Finds a document by its identifier and remove it. * * @param {*} id - * @return {Promise} + * @return {BluebirdPromise} * @private */ - _removeById(id: string | number): Promise { + _removeById(id: string | number): BluebirdPromise { const schema = this.schema; const data = this.data[id]; if (!data) { - return Promise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); + return BluebirdPromise.reject(new WarehouseError('ID `' + id + '` does not exist', WarehouseError.ID_NOT_EXIST)); } // Pre-hooks @@ -382,10 +382,10 @@ class Model extends EventEmitter { * * @param {*} id * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - removeById(id: string | number, callback?: NodeJSLikeCallback): Promise { - return Promise.using(this._acquireWriteLock(), () => this._removeById(id)).asCallback(callback); + removeById(id: string | number, callback?: NodeJSLikeCallback): BluebirdPromise { + return BluebirdPromise.using(this._acquireWriteLock(), () => this._removeById(id)).asCallback(callback); } /** @@ -393,9 +393,9 @@ class Model extends EventEmitter { * * @param {object} query * @param {function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - remove(query: object, callback?: NodeJSLikeCallback): Promise { + remove(query: object, callback?: NodeJSLikeCallback): BluebirdPromise { return (this.find(query) as Query).remove(callback); } @@ -963,11 +963,11 @@ class Model extends EventEmitter { Model.prototype.get = Model.prototype.findById; -function execHooks(schema: Schema, type: string, event: string, data: any): Promise { - const hooks = schema.hooks[type][event] as ((data: any) => Promise | void)[]; - if (!hooks.length) return Promise.resolve(data); +function execHooks(schema: Schema, type: string, event: string, data: any): BluebirdPromise { + const hooks = schema.hooks[type][event] as ((data: any) => BluebirdPromise | void)[]; + if (!hooks.length) return BluebirdPromise.resolve(data); - return Promise.each(hooks, hook => hook(data)).thenReturn(data); + return BluebirdPromise.each(hooks, hook => hook(data)).thenReturn(data); } Model.prototype.size = Model.prototype.count; diff --git a/src/query.ts b/src/query.ts index d9445909..c5fe5eb6 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,4 +1,4 @@ -import Promise from 'bluebird'; +import BluebirdPromise from 'bluebird'; import { parseArgs, shuffle } from './util'; import type Model from './model'; import type Schema from './schema'; @@ -338,13 +338,13 @@ abstract class Query { * * @param {Object} data * @param {Function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - update(data: any, callback?: NodeJSLikeCallback): Promise { + update(data: any, callback?: NodeJSLikeCallback): BluebirdPromise { const model = this._model; const stack = this._schema._parseUpdate(data); - return Promise.mapSeries(this.data, item => model._updateWithStack(item._id, stack)).asCallback(callback); + return BluebirdPromise.mapSeries(this.data, item => model._updateWithStack(item._id, stack)).asCallback(callback); } /** @@ -352,24 +352,24 @@ abstract class Query { * * @param {Object} data * @param {Function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - replace(data: any, callback?: NodeJSLikeCallback): Promise { + replace(data: any, callback?: NodeJSLikeCallback): BluebirdPromise { const model = this._model; - return Promise.map(this.data, item => model.replaceById(item._id, data)).asCallback(callback); + return BluebirdPromise.map(this.data, item => model.replaceById(item._id, data)).asCallback(callback); } /** * Remove all documents. * * @param {Function} [callback] - * @return {Promise} + * @return {BluebirdPromise} */ - remove(callback?: NodeJSLikeCallback): Promise { + remove(callback?: NodeJSLikeCallback): BluebirdPromise { const model = this._model; - return Promise.mapSeries(this.data, item => model.removeById(item._id)).asCallback(callback); + return BluebirdPromise.mapSeries(this.data, item => model.removeById(item._id)).asCallback(callback); } /** diff --git a/src/schema.ts b/src/schema.ts index 933a3598..64fe5f2f 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1,6 +1,6 @@ import SchemaType from './schematype'; import * as Types from './types/index'; -import Promise from 'bluebird'; +import BluebirdPromise from 'bluebird'; import { getProp, setProp, delProp } from './util'; import PopulationError from './error/population'; import SchemaTypeVirtual from './types/virtual'; @@ -48,12 +48,12 @@ const checkHookType = (type: string) => { } }; -const hookWrapper = (fn: (...args: any[]) => void): (...args: any[]) => Promise => { +const hookWrapper = (fn: (...args: any[]) => void): (...args: any[]) => BluebirdPromise => { if (fn.length > 1) { - return Promise.promisify(fn); + return BluebirdPromise.promisify(fn); } - return Promise.method(fn); + return BluebirdPromise.method(fn); }; /** @@ -385,12 +385,12 @@ class Schema { methods: Record any> = {}; hooks: { pre: { - save: ((...args: any[]) => Promise)[] - remove: ((...args: any[]) => Promise)[] + save: ((...args: any[]) => BluebirdPromise)[] + remove: ((...args: any[]) => BluebirdPromise)[] }; post: { - save: ((...args: any[]) => Promise)[] - remove: ((...args: any[]) => Promise)[] + save: ((...args: any[]) => BluebirdPromise)[] + remove: ((...args: any[]) => BluebirdPromise)[] }; }; stacks: { diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 28b8a26d..45214763 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -5,6 +5,7 @@ "node/no-unsupported-features/es-syntax": 0, "@typescript-eslint/no-var-requires": 0, "@typescript-eslint/ban-ts-comment": 0, - "@typescript-eslint/no-empty-function": 0 + "@typescript-eslint/no-empty-function": 0, + "no-extra-parens": 0 } } diff --git a/test/scripts/model.ts b/test/scripts/model.ts index 13d6e767..df03eafa 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -124,7 +124,6 @@ describe('Model', () => { it('insert() - already existed', () => { let user; - // @ts-ignore return (User.insert({}).then(data => { user = data; return User.insert(data); @@ -972,7 +971,7 @@ describe('Model', () => { it('populate() - error', () => { try { - // @ts-ignore + // @ts-expect-error Post.populate(); } catch (err) { err.message.should.eql('path is required'); diff --git a/test/scripts/schema.ts b/test/scripts/schema.ts index dbe04a8f..5e90e17c 100644 --- a/test/scripts/schema.ts +++ b/test/scripts/schema.ts @@ -100,11 +100,11 @@ describe('Schema', () => { schema.hooks.pre.remove.should.have.length(1); // incompatible type - // @ts-ignore + // @ts-expect-error (() => schema.pre('wtf', () => {})).should.to.throw(TypeError, 'Hook type must be `save` or `remove`!'); // hook is not a function - // @ts-ignore + // @ts-expect-error (() => schema.pre('save', {})).should.to.throw(TypeError, 'Hook must be a function!'); }); @@ -122,11 +122,11 @@ describe('Schema', () => { schema.hooks.post.remove.should.have.length(1); // incompatible type - // @ts-ignore + // @ts-expect-error (() => schema.post('wtf', () => {})).should.throw(TypeError, 'Hook type must be `save` or `remove`!'); // hook is not a function - // @ts-ignore + // @ts-expect-error (() => schema.post('save', {})).should.to.throw(TypeError, 'Hook must be a function!'); }); @@ -141,7 +141,7 @@ describe('Schema', () => { schema.method.should.to.throw(TypeError, 'Method name is required!'); // without function - // @ts-ignore + // @ts-expect-error (() => schema.method('wtf', {})).should.to.throw(TypeError, 'Instance method must be a function!'); }); @@ -156,7 +156,7 @@ describe('Schema', () => { schema.static.should.to.throw(TypeError, 'Method name is required!'); // without function - // @ts-ignore + // @ts-expect-error (() => schema.static('wtf', {})).should.to.throw(TypeError, 'Static method must be a function!'); }); }); diff --git a/test/scripts/types/virtual.ts b/test/scripts/types/virtual.ts index 0d4880db..3f8f5510 100644 --- a/test/scripts/types/virtual.ts +++ b/test/scripts/types/virtual.ts @@ -13,7 +13,7 @@ describe('SchemaTypeVirtual', () => { }); it('get() - type check', () => { - // @ts-ignore + // @ts-expect-error (() => type.get(123)).should.to.throw(TypeError, 'Getter must be a function!'); }); @@ -27,7 +27,7 @@ describe('SchemaTypeVirtual', () => { }); it('set() - type check', () => { - // @ts-ignore + // @ts-expect-error (() => type.set(123)).should.to.throw(TypeError, 'Setter must be a function!'); });