Skip to content

Commit

Permalink
fix(type): fix bluebird type (#248)
Browse files Browse the repository at this point in the history
Co-authored-by: yoshinorin <[email protected]>
  • Loading branch information
D-Sketon and yoshinorin authored May 2, 2024
1 parent d6301c4 commit 2c89f53
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 7 additions & 7 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<unknown>;
const pipelineAsync = BluebirdPromise.promisify(pipeline) as unknown as (...args: Stream[]) => BluebirdPromise<unknown>;

let _writev: (handle: fsPromises.FileHandle, buffers: Buffer[]) => Promise<unknown>;

Expand Down Expand Up @@ -132,9 +132,9 @@ class Database {
* Loads database.
*
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
load(callback?: NodeJSLikeCallback<any>): Bluebird<any> {
load(callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;

if (!path) throw new WarehouseError('options.path is required');
Expand Down Expand Up @@ -172,13 +172,13 @@ class Database {
* Saves database.
*
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
save(callback?: NodeJSLikeCallback<any>): Bluebird<void> {
save(callback?: NodeJSLikeCallback<any>): BluebirdPromise<void> {
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<string, Model<any>> } {
Expand Down
17 changes: 9 additions & 8 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -25,9 +26,9 @@ abstract class Document<T> {
* Saves the document.
*
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
save(callback?: NodeJSLikeCallback<any>): Promise<any> {
save(callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return this._model.save(this, callback);
}

Expand All @@ -36,9 +37,9 @@ abstract class Document<T> {
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
update(data: object, callback?: NodeJSLikeCallback<any>): Promise<any> {
update(data: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return this._model.updateById(this._id, data, callback);
}

Expand All @@ -47,19 +48,19 @@ abstract class Document<T> {
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
replace(data: T | Document<T>, callback?: NodeJSLikeCallback<any>): Promise<any> {
replace(data: T | Document<T>, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return this._model.replaceById(this._id, data, callback);
}

/**
* Removes the document.
*
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
remove(callback?: NodeJSLikeCallback<any>): Promise<any> {
remove(callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return this._model.removeById(this._id, callback);
}

Expand Down
90 changes: 45 additions & 45 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -125,13 +125,13 @@ class Model<T> extends EventEmitter {
/**
* Acquires write lock.
*
* @return {Promise}
* @return {BluebirdPromise}
* @private
*/
_acquireWriteLock(): Promise.Disposer<void> {
_acquireWriteLock(): BluebirdPromise.Disposer<void> {
const mutex = this._mutex;

return new Promise((resolve, reject) => {
return new BluebirdPromise((resolve, reject) => {
mutex.lock(resolve);
}).disposer(() => {
mutex.unlock();
Expand All @@ -142,10 +142,10 @@ class Model<T> extends EventEmitter {
* Inserts a document.
*
* @param {Document|object} data
* @return {Promise}
* @return {BluebirdPromise}
* @private
*/
_insertOne(data_: Document<T> | T): Promise<any> {
_insertOne(data_: Document<T> | T): BluebirdPromise<any> {
const schema = this.schema;

// Apply getters
Expand All @@ -154,11 +154,11 @@ class Model<T> 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
Expand All @@ -181,22 +181,22 @@ class Model<T> extends EventEmitter {
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
insertOne(data: Document<T> | T, callback?: NodeJSLikeCallback<any>): Promise<any> {
return Promise.using(this._acquireWriteLock(), () => this._insertOne(data)).asCallback(callback);
insertOne(data: Document<T> | T, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return BluebirdPromise.using(this._acquireWriteLock(), () => this._insertOne(data)).asCallback(callback);
}

/**
* Inserts documents.
*
* @param {object|array} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
insert(data: Document<T> | T | Document<T>[] | T[], callback?: NodeJSLikeCallback<any>): Promise<any> {
insert(data: Document<T> | T | Document<T>[] | T[], callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
if (Array.isArray(data)) {
return Promise.mapSeries<Document<T> | T, any>(data, item => this.insertOne(item)).asCallback(callback);
return BluebirdPromise.mapSeries<Document<T> | T, any>(data, item => this.insertOne(item)).asCallback(callback);
}

return this.insertOne(data, callback);
Expand All @@ -207,14 +207,14 @@ class Model<T> extends EventEmitter {
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
save(data: Document<T> | T, callback?: NodeJSLikeCallback<any>): Promise<any> {
save(data: Document<T> | T, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
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);
}
Expand All @@ -228,16 +228,16 @@ class Model<T> extends EventEmitter {
*
* @param {*} id
* @param {array} stack
* @return {Promise}
* @return {BluebirdPromise}
* @private
*/
_updateWithStack(id: string | number, stack: ((data: any) => void)[]): Promise<any> {
_updateWithStack(id: string | number, stack: ((data: any) => void)[]): BluebirdPromise<any> {
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
Expand Down Expand Up @@ -271,10 +271,10 @@ class Model<T> extends EventEmitter {
* @param {*} id
* @param {object} update
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
updateById(id: string | number, update: object, callback?: NodeJSLikeCallback<any>): Promise<any> {
return Promise.using(this._acquireWriteLock(), () => {
updateById(id: string | number, update: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return BluebirdPromise.using(this._acquireWriteLock(), () => {
const stack = this.schema._parseUpdate(update as object);
return this._updateWithStack(id, stack);
}).asCallback(callback);
Expand All @@ -286,9 +286,9 @@ class Model<T> extends EventEmitter {
* @param {object} query
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
update(query: object, data: object, callback?: NodeJSLikeCallback<any>): Promise<any> {
update(query: object, data: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).update(data, callback);
}

Expand All @@ -297,14 +297,14 @@ class Model<T> extends EventEmitter {
*
* @param {*} id
* @param {object} data
* @return {Promise}
* @return {BluebirdPromise}
* @private
*/
_replaceById(id: string | number, data_: Document<T> | T): Promise<any> {
_replaceById(id: string | number, data_: Document<T> | T): BluebirdPromise<any> {
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;
Expand Down Expand Up @@ -332,10 +332,10 @@ class Model<T> extends EventEmitter {
* @param {*} id
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
replaceById(id: string | number, data: Document<T> | T, callback?: NodeJSLikeCallback<any>): Promise<any> {
return Promise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback);
replaceById(id: string | number, data: Document<T> | T, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return BluebirdPromise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback);
}

/**
Expand All @@ -344,26 +344,26 @@ class Model<T> extends EventEmitter {
* @param {object} query
* @param {object} data
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
replace(query: object, data, callback?: NodeJSLikeCallback<any>): Promise<any> {
replace(query: object, data, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).replace(data, callback);
}

/**
* Finds a document by its identifier and remove it.
*
* @param {*} id
* @return {Promise}
* @return {BluebirdPromise}
* @private
*/
_removeById(id: string | number): Promise<any> {
_removeById(id: string | number): BluebirdPromise<any> {
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
Expand All @@ -382,20 +382,20 @@ class Model<T> extends EventEmitter {
*
* @param {*} id
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
removeById(id: string | number, callback?: NodeJSLikeCallback<any>): Promise<any> {
return Promise.using(this._acquireWriteLock(), () => this._removeById(id)).asCallback(callback);
removeById(id: string | number, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return BluebirdPromise.using(this._acquireWriteLock(), () => this._removeById(id)).asCallback(callback);
}

/**
* Removes matching documents.
*
* @param {object} query
* @param {function} [callback]
* @return {Promise}
* @return {BluebirdPromise}
*/
remove(query: object, callback?: NodeJSLikeCallback<any>): Promise<any> {
remove(query: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).remove(callback);
}

Expand Down Expand Up @@ -963,11 +963,11 @@ class Model<T> extends EventEmitter {

Model.prototype.get = Model.prototype.findById;

function execHooks(schema: Schema, type: string, event: string, data: any): Promise<any> {
const hooks = schema.hooks[type][event] as ((data: any) => Promise<void> | void)[];
if (!hooks.length) return Promise.resolve(data);
function execHooks(schema: Schema, type: string, event: string, data: any): BluebirdPromise<any> {
const hooks = schema.hooks[type][event] as ((data: any) => BluebirdPromise<void> | 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;
Expand Down
Loading

0 comments on commit 2c89f53

Please sign in to comment.