From 6c38347f11276c154c36ee7c57627258dc1151d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 28 Feb 2020 18:12:29 +0100 Subject: [PATCH] fixup! after rebase --- types/__test__.ts | 23 +++++++++++++++++++++++ types/model.d.ts | 8 ++++---- types/observer-mixin.d.ts | 16 ++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/types/__test__.ts b/types/__test__.ts index e1c3dbc8f..0d1e0e049 100644 --- a/types/__test__.ts +++ b/types/__test__.ts @@ -27,7 +27,16 @@ const db = new DataSource('db', {connector: 'memory'}); // An operation hook can be installed Data.observe('before save', async ctx => {}); + // Context is typed and provides `Model` property + Data.observe('before save', async ctx => { + console.log(ctx.Model.modelName); + }); + // ModelBaseClass can be assigned to `typeof ModelBase` + // Please note that both `ModelBaseClass` and typeof ModelBase` + // are different ways how to describe a class constructor of a model. + // In this test we are verifying that the value retunred by `createModel` + // can be assigned to both types. const modelTypeof: typeof ModelBase = Data; const modelCls: ModelBaseClass = modelTypeof; }); @@ -50,7 +59,16 @@ const db = new DataSource('db', {connector: 'memory'}); next(new Error('test error')); }); + // ctx.Model is a PersistedModel class constructor + Product.observe('before save', async ctx => { + await ctx.Model.findOne(); + }); + // PersistedModelClass can be assigned to `typeof PersistedModel` + // Please note that both `PersistedModelClass` and typeof PersistedModel` + // are different ways how to describe a class constructor of a model. + // In this test we are verifying that the value retunred by `createModel` + // can be assigned to both types. const modelTypeof: typeof PersistedModel = Product; const modelCls: PersistedModelClass = modelTypeof; }); @@ -64,4 +82,9 @@ const db = new DataSource('db', {connector: 'memory'}); // An operation hook can be installed CacheItem.observe('before save', async ctx => {}); + + // ctx.Model is a KeyValueModel class constructor + CacheItem.observe('before save', async ctx => { + await ctx.Model.expire('key', 100); + }); }); diff --git a/types/model.d.ts b/types/model.d.ts index 2bcbfadd3..42e061619 100644 --- a/types/model.d.ts +++ b/types/model.d.ts @@ -6,7 +6,7 @@ import {EventEmitter} from 'events'; import {AnyObject, Options} from './common'; import {DataSource} from './datasource'; -import {Listener} from './observer-mixin'; +import {Listener, OperationHookContext} from './observer-mixin'; /** * Property types @@ -283,7 +283,7 @@ export declare class ModelBase { static observe( this: T, operation: string, - listener: Listener + listener: Listener>, ): void; /** @@ -305,8 +305,8 @@ export declare class ModelBase { static removeObserver( this: T, operation: string, - listener: Listener - ): Listener | undefined; + listener: Listener>, + ): Listener> | undefined; /** * Unregister all asynchronous observers for the given operation (event). diff --git a/types/observer-mixin.d.ts b/types/observer-mixin.d.ts index 2c82721c1..77e274f36 100644 --- a/types/observer-mixin.d.ts +++ b/types/observer-mixin.d.ts @@ -4,9 +4,9 @@ // License text available at https://opensource.org/licenses/MIT import {Callback, PromiseOrVoid} from './common'; -import {PersistedModel, PersistedModelClass} from './persisted-model'; +import {ModelBase} from './model'; -export interface OperationHookContext { +export interface OperationHookContext { /** * The constructor of the model that triggered the operation. */ @@ -50,7 +50,11 @@ export interface ObserverMixin { * `this` set to the model constructor, e.g. `User`. * @end */ - observe(operation: string, listener: Listener>): void; + observe( + this: T, + operation: string, + listener: Listener>, + ): void; /** * Unregister an asynchronous observer for the given operation (event). @@ -68,7 +72,11 @@ export interface ObserverMixin { * @callback {function} listener The listener function. * @end */ - removeObserver(operation: string, listener: Listener>): Listener> | undefined; + removeObserver( + this: T, + operation: string, + listener: Listener>, + ): Listener> | undefined; /** * Unregister all asynchronous observers for the given operation (event).