Skip to content

Commit

Permalink
refactor: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jul 4, 2024
1 parent 2a3f965 commit 2484daf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class Document<T> {
*
* @return {object}
*/
toObject(): T {
toObject(): T extends object ? T : never {
const keys = Object.keys(this);
const obj: Partial<T> = {};

Expand All @@ -80,7 +80,7 @@ abstract class Document<T> {
obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]);
}

return obj as T;
return obj as T extends object ? T : never;
}

/**
Expand Down
30 changes: 15 additions & 15 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Model<T> extends EventEmitter {
data: Record<PropertyKey, T> = {};
schema: Schema<T>;
length = 0;
Document;
Query;
Document: { new<T>(data: T): Document<T> };
Query: { new<T>(data: Document<T>[]): Query<T> };
_database: Database;

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ class Model<T> extends EventEmitter {
const schema = this.schema;

// Apply getters
const data = (data_ instanceof this.Document ? data_ : this.new(data_ as T)) as Document<T>;
const data = (data_ instanceof this.Document ? data_ : this.new(data_));
const id = data._id;

// Check ID
Expand All @@ -168,7 +168,7 @@ class Model<T> 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 => {
Expand Down Expand Up @@ -236,7 +236,7 @@ class Model<T> extends EventEmitter {
* @return {BluebirdPromise}
* @private
*/
_updateWithStack(id: string | number, stack: ((data: any) => void)[]): BluebirdPromise<any> {
_updateWithStack(id: string | number, stack: ((data: T) => void)[]): BluebirdPromise<any> {
const schema = this.schema;

const data = this.data[id];
Expand Down Expand Up @@ -280,7 +280,7 @@ class Model<T> extends EventEmitter {
*/
updateById(id: string | number, update: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
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);
}
Expand All @@ -294,7 +294,7 @@ class Model<T> extends EventEmitter {
* @return {BluebirdPromise}
*/
update(query: object, data: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).update(data, callback);
return this.find(query).update(data, callback);
}

/**
Expand All @@ -315,11 +315,11 @@ class Model<T> extends EventEmitter {
(data_ as any)._id = id;

// Apply getters
const data = (data_ instanceof this.Document ? data_ : this.new(data_ as T)) as Document<T>;
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 => {
Expand Down Expand Up @@ -351,8 +351,8 @@ class Model<T> extends EventEmitter {
* @param {function} [callback]
* @return {BluebirdPromise}
*/
replace(query: object, data, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).replace(data, callback);
replace(query: object, data: T | Document<T>, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return this.find(query).replace(data, callback);
}

/**
Expand Down Expand Up @@ -401,7 +401,7 @@ class Model<T> extends EventEmitter {
* @return {BluebirdPromise}
*/
remove(query: object, callback?: NodeJSLikeCallback<any>): BluebirdPromise<any> {
return (this.find(query) as Query<T>).remove(callback);
return this.find(query).remove(callback);
}

/**
Expand Down Expand Up @@ -500,7 +500,7 @@ class Model<T> extends EventEmitter {
}
}

return options.lean ? arr : new this.Query(arr);
return options.lean ? arr as T[] : new this.Query(arr as Document<T>[]);
}

/**
Expand Down Expand Up @@ -881,7 +881,7 @@ class Model<T> extends EventEmitter {
}

if (options.match) {
cache = (new Query(arr) as Query<T>).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);
Expand Down Expand Up @@ -1010,7 +1010,7 @@ class Model<T> extends EventEmitter {
Model.prototype.get = Model.prototype.findById;

function execHooks<T>(schema: Schema<T>, type: keyof Schema['hooks'], event: keyof Schema['hooks'][keyof Schema['hooks']], data: any): BluebirdPromise<any> {
const hooks = schema.hooks[type][event] as ((data: any) => BluebirdPromise<void> | void)[];
const hooks = schema.hooks[type][event];
if (!hooks.length) return BluebirdPromise.resolve(data);

return BluebirdPromise.each(hooks, hook => hook(data)).thenReturn(data);
Expand Down
20 changes: 10 additions & 10 deletions test/scripts/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ describe('Model', () => {
{age: 30},
{age: 40}
]).then(data => {
const query = User.find({age: 20}) as Query<UserType>;
const query = User.find({age: 20});
query.data.should.eql(data.slice(1, 3));
return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -544,7 +544,7 @@ describe('Model', () => {
{age: 30},
{age: 40}
]).then(data => {
const query = User.find({}) as Query<UserType>;
const query = User.find({});
query.data.should.eql(data);
return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -566,7 +566,7 @@ describe('Model', () => {
{age: 30},
{age: 40}
]).then(data => {
const query = User.find({age: {$gte: 20}}, {limit: 2}) as Query<UserType>;
const query = User.find({age: {$gte: 20}}, {limit: 2});
query.data.should.eql(data.slice(1, 3));
return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -577,11 +577,11 @@ describe('Model', () => {
{age: 30},
{age: 40}
]).then(data => {
let query = User.find({age: {$gte: 20}}, {skip: 1}) as Query<UserType>;
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<UserType>;
query = User.find({age: {$gte: 20}}, {limit: 1, skip: 1});
query.data.should.eql(data.slice(2, 3));

return data;
Expand Down Expand Up @@ -610,7 +610,7 @@ describe('Model', () => {
]
});

(query as Query<UserType>).toArray().should.eql([data[1]]);
query.toArray().should.eql([data[1]]);

return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -627,7 +627,7 @@ describe('Model', () => {
]
});

(query as Query<UserType>).toArray().should.eql(data.slice(1));
query.toArray().should.eql(data.slice(1));

return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -644,7 +644,7 @@ describe('Model', () => {
]
});

(query as Query<UserType>).toArray().should.eql([data[0]]);
query.toArray().should.eql([data[0]]);

return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -658,7 +658,7 @@ describe('Model', () => {
$not: {'name.last': 'Doe'}
});

(query as Query<UserType>).toArray().should.eql(data.slice(2));
query.toArray().should.eql(data.slice(2));

return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand All @@ -674,7 +674,7 @@ describe('Model', () => {
}
});

(query as Query<UserType>).toArray().should.eql(data.slice(0, 2));
query.toArray().should.eql(data.slice(0, 2));

return data;
}).map<unknown, any>(item => User.removeById(item._id)));
Expand Down

0 comments on commit 2484daf

Please sign in to comment.