From 052eea4064c89afc3a566cebf2b6b6056f77dcc2 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Wed, 1 May 2024 17:48:41 +0800 Subject: [PATCH 1/3] refactor: refactor types --- src/database.ts | 2 +- src/document.ts | 2 +- src/model.ts | 12 ++++++------ src/query.ts | 2 +- src/schema.ts | 7 ++++--- src/types/virtual.ts | 8 ++++---- test/scripts/model.ts | 10 +++++++++- test/scripts/types/virtual.ts | 2 +- 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/database.ts b/src/database.ts index 2a30147..901be41 100644 --- a/src/database.ts +++ b/src/database.ts @@ -118,7 +118,7 @@ class Database { * @param {Schema|object} [schema] * @return {Model} */ - model(name: string, schema?: Schema | Record): Model { + model(name: string, schema?: Schema | Record): Model { if (this._models[name]) { return this._models[name]; } diff --git a/src/document.ts b/src/document.ts index fcb5bbb..9010b0d 100644 --- a/src/document.ts +++ b/src/document.ts @@ -7,7 +7,7 @@ const cloneDeep = rfdc(); abstract class Document { abstract _model: Model; _id!: string | number | undefined; - abstract _schema: Schema; + abstract _schema: Schema; [key : string]: any; /** diff --git a/src/model.ts b/src/model.ts index 32a6429..e3be0ae 100644 --- a/src/model.ts +++ b/src/model.ts @@ -16,7 +16,7 @@ import type { AddSchemaTypeOptions, NodeJSLikeCallback, Options, PopulateResult class Model extends EventEmitter { _mutex = new Mutex(); data: Record = {}; - schema: Schema; + schema: Schema; length = 0; Document; Query; @@ -28,10 +28,10 @@ class Model extends EventEmitter { * @param {string} name Model name * @param {Schema|object} [schema_] Schema */ - constructor(public name: string, schema_: Schema | Record) { + constructor(public name: string, schema_: Schema | Record) { super(); - let schema: Schema; + let schema: Schema; // Define schema if (schema_ instanceof Schema) { @@ -51,7 +51,7 @@ class Model extends EventEmitter { class _Document extends Document { _model!: Model; - _schema!: Schema; + _schema!: Schema; constructor(data: T) { super(data); @@ -67,7 +67,7 @@ class Model extends EventEmitter { class _Query extends Query { _model!: Model; - _schema!: Schema; + _schema!: Schema; } this.Query = _Query; @@ -963,7 +963,7 @@ class Model extends EventEmitter { Model.prototype.get = Model.prototype.findById; -function execHooks(schema: Schema, type: string, event: string, data: any): Promise { +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); diff --git a/src/query.ts b/src/query.ts index d944590..80acdec 100644 --- a/src/query.ts +++ b/src/query.ts @@ -9,7 +9,7 @@ abstract class Query { data: Document[]; length: number; abstract _model: Model; - abstract _schema: Schema; + abstract _schema: Schema; /** * Query constructor. diff --git a/src/schema.ts b/src/schema.ts index 933a359..1502889 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -6,6 +6,7 @@ import PopulationError from './error/population'; import SchemaTypeVirtual from './types/virtual'; import { isPlainObject } from 'is-plain-object'; import type { AddSchemaTypeLoopOptions, AddSchemaTypeOptions, PopulateResult, SchemaTypeOptions } from './types'; +import type Model from './model'; /** * @callback queryFilterCallback @@ -379,7 +380,7 @@ class QueryParser { } -class Schema { +class Schema { paths: Record> = {}; statics: Record any> = {}; methods: Record any> = {}; @@ -557,7 +558,7 @@ class Schema { * @param {Function} [getter] * @return {SchemaType.Virtual} */ - virtual(name: string, getter?: () => any): SchemaTypeVirtual { + virtual(name: string, getter?: (this: T) => any): SchemaTypeVirtual { const virtual = new Types.Virtual(name, {}); if (getter) virtual.get(getter); @@ -614,7 +615,7 @@ class Schema { * @param {String} name * @param {Function} fn */ - static(name: string, fn: (...args: any[]) => any) { + static(name: string, fn: (this: Model, ...args: any[]) => any) { if (!name) throw new TypeError('Method name is required!'); if (typeof fn !== 'function') { diff --git a/src/types/virtual.ts b/src/types/virtual.ts index 383907b..64f5564 100644 --- a/src/types/virtual.ts +++ b/src/types/virtual.ts @@ -4,8 +4,8 @@ import { setGetter } from '../util'; /** * Virtual schema type. */ -class SchemaTypeVirtual extends SchemaType { - getter: (() => any) | undefined; +class SchemaTypeVirtual extends SchemaType { + getter: ((this: T) => any) | undefined; setter: ((value: any) => void) | undefined; /** @@ -14,7 +14,7 @@ class SchemaTypeVirtual extends SchemaType { * @param {Function} fn * @chainable */ - get(fn: () => any): SchemaTypeVirtual { + get(fn: (this: T) => any): SchemaTypeVirtual { if (typeof fn !== 'function') { throw new TypeError('Getter must be a function!'); } @@ -30,7 +30,7 @@ class SchemaTypeVirtual extends SchemaType { * @param {Function} fn * @chainable */ - set(fn: (value: any) => void): SchemaTypeVirtual { + set(fn: (this: T, value: any) => void): SchemaTypeVirtual { if (typeof fn !== 'function') { throw new TypeError('Setter must be a function!'); } diff --git a/test/scripts/model.ts b/test/scripts/model.ts index 13d6e76..43b1a57 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -37,7 +37,15 @@ describe('Model', () => { const db = new Database(); - const userSchema = new Schema({ + const userSchema = new Schema<{ + name: { + first: string; + last: string; + } + email: string; + age: number; + posts: string[]; + }>({ name: { first: String, last: String diff --git a/test/scripts/types/virtual.ts b/test/scripts/types/virtual.ts index 0d4880d..9a511ad 100644 --- a/test/scripts/types/virtual.ts +++ b/test/scripts/types/virtual.ts @@ -3,7 +3,7 @@ const should = chai.should(); // eslint-disable-line import SchemaTypeVirtual from '../../../dist/types/virtual'; describe('SchemaTypeVirtual', () => { - const type = new SchemaTypeVirtual('test'); + const type = new SchemaTypeVirtual('test'); it('get()', () => { const getter = () => 'foo'; From 5d6845778d4d35ce80cbe4ec7ba5dfab8ead84f0 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 2 May 2024 21:37:22 +0800 Subject: [PATCH 2/3] refactor: refactor types --- src/database.ts | 2 +- src/model.ts | 2 +- src/schema.ts | 7 ++++--- src/types/virtual.ts | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/database.ts b/src/database.ts index 901be41..1aab60c 100644 --- a/src/database.ts +++ b/src/database.ts @@ -118,7 +118,7 @@ class Database { * @param {Schema|object} [schema] * @return {Model} */ - model(name: string, schema?: Schema | Record): Model { + model(name: string, schema?: Schema | Record): Model { if (this._models[name]) { return this._models[name]; } diff --git a/src/model.ts b/src/model.ts index e3be0ae..0948b7a 100644 --- a/src/model.ts +++ b/src/model.ts @@ -963,7 +963,7 @@ class Model extends EventEmitter { Model.prototype.get = Model.prototype.findById; -function execHooks(schema: Schema, type: string, event: string, data: any): Promise { +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); diff --git a/src/schema.ts b/src/schema.ts index 1502889..bd45df1 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -7,6 +7,7 @@ import SchemaTypeVirtual from './types/virtual'; import { isPlainObject } from 'is-plain-object'; import type { AddSchemaTypeLoopOptions, AddSchemaTypeOptions, PopulateResult, SchemaTypeOptions } from './types'; import type Model from './model'; +import type Document from './document'; /** * @callback queryFilterCallback @@ -382,8 +383,8 @@ class QueryParser { class Schema { paths: Record> = {}; - statics: Record any> = {}; - methods: Record any> = {}; + statics: Record, ...args: any[]) => any> = {}; + methods: Record, ...args: any[]) => any> = {}; hooks: { pre: { save: ((...args: any[]) => Promise)[] @@ -599,7 +600,7 @@ class Schema { * @param {String} name * @param {Function} fn */ - method(name: string, fn: (...args: any[]) => any) { + method(name: string, fn: (this: Document, ...args: any[]) => any) { if (!name) throw new TypeError('Method name is required!'); if (typeof fn !== 'function') { diff --git a/src/types/virtual.ts b/src/types/virtual.ts index 64f5564..1a64aef 100644 --- a/src/types/virtual.ts +++ b/src/types/virtual.ts @@ -4,7 +4,7 @@ import { setGetter } from '../util'; /** * Virtual schema type. */ -class SchemaTypeVirtual extends SchemaType { +class SchemaTypeVirtual extends SchemaType { getter: ((this: T) => any) | undefined; setter: ((value: any) => void) | undefined; From ce2740db739eb4d8dac61371726ad9a9a49f18e3 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Thu, 2 May 2024 22:11:42 +0800 Subject: [PATCH 3/3] refactor: refactor types --- src/schema.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/schema.ts b/src/schema.ts index 1ae0e03..f6814a5 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -381,10 +381,10 @@ class QueryParser { } -class Schema { +class Schema { paths: Record> = {}; statics: Record, ...args: any[]) => any> = {}; - methods: Record, ...args: any[]) => any> = {}; + methods: Record any> = {}; hooks: { pre: { save: ((...args: any[]) => BluebirdPromise)[] @@ -600,7 +600,7 @@ class Schema { * @param {String} name * @param {Function} fn */ - method(name: string, fn: (this: Document, ...args: any[]) => any) { + method(name: string, fn: (this: T, ...args: any[]) => any) { if (!name) throw new TypeError('Method name is required!'); if (typeof fn !== 'function') {