From 395e8e4d1cefb9f39277bf4b1f470940674d0e86 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 5 Apr 2022 09:38:58 -0500 Subject: [PATCH] ORM type refactor --- src/auth/repository/orm/ORMUser.ts | 2 +- src/auth/repository/orm/ORMUserRepository.ts | 2 +- src/auth/server/models/OAuth2TokenModel.ts | 2 +- .../server/repositories/ORMTokenRepository.ts | 2 +- src/di/types.ts | 8 +- src/orm/model/Model.ts | 79 +++++++++++-------- src/orm/model/ModelBuilder.ts | 2 +- src/orm/model/ModelResultIterable.ts | 2 +- src/orm/model/ModelSerializer.ts | 12 +-- src/orm/model/events/ModelCreatedEvent.ts | 2 +- src/orm/model/events/ModelCreatingEvent.ts | 2 +- src/orm/model/events/ModelDeletedEvent.ts | 2 +- src/orm/model/events/ModelDeletingEvent.ts | 2 +- src/orm/model/events/ModelEvent.ts | 2 +- src/orm/model/events/ModelRetrievedEvent.ts | 2 +- src/orm/model/events/ModelSavedEvent.ts | 2 +- src/orm/model/events/ModelSavingEvent.ts | 2 +- src/orm/model/events/ModelUpdatedEvent.ts | 2 +- src/orm/model/events/ModelUpdatingEvent.ts | 2 +- src/orm/model/relation/HasMany.ts | 2 +- src/orm/model/relation/HasOne.ts | 2 +- src/orm/model/relation/HasOneOrMany.ts | 2 +- src/orm/model/relation/Relation.ts | 2 +- src/orm/model/relation/RelationBuilder.ts | 2 +- src/orm/model/relation/decorators.ts | 2 +- src/orm/services/Models.ts | 4 +- src/orm/support/CacheModel.ts | 4 +- src/orm/support/ORMCache.ts | 2 +- src/orm/support/ORMSession.ts | 2 +- src/orm/support/SessionModel.ts | 2 +- 30 files changed, 86 insertions(+), 71 deletions(-) diff --git a/src/auth/repository/orm/ORMUser.ts b/src/auth/repository/orm/ORMUser.ts index bbec6e9..250b7a4 100644 --- a/src/auth/repository/orm/ORMUser.ts +++ b/src/auth/repository/orm/ORMUser.ts @@ -8,7 +8,7 @@ import {Awaitable, JSONState} from '../../../util' * A basic ORM-driven user class. */ @Injectable() -export class ORMUser extends Model implements Authenticatable { +export class ORMUser extends Model implements Authenticatable { protected static table = 'users' diff --git a/src/auth/repository/orm/ORMUserRepository.ts b/src/auth/repository/orm/ORMUserRepository.ts index bdb7952..18de624 100644 --- a/src/auth/repository/orm/ORMUserRepository.ts +++ b/src/auth/repository/orm/ORMUserRepository.ts @@ -18,7 +18,7 @@ export class ORMUserRepository extends AuthenticatableRepository { /** Look up the user by their username. */ getByIdentifier(id: AuthenticatableIdentifier): Awaitable> { - return (this.injector.getStaticOverride(ORMUser) as typeof ORMUser).query() + return (this.injector.getStaticOverride(ORMUser) as typeof ORMUser).query() .where('username', '=', id) .first() } diff --git a/src/auth/server/models/OAuth2TokenModel.ts b/src/auth/server/models/OAuth2TokenModel.ts index 1cb6d24..3053c66 100644 --- a/src/auth/server/models/OAuth2TokenModel.ts +++ b/src/auth/server/models/OAuth2TokenModel.ts @@ -1,7 +1,7 @@ import {Field, FieldType, Model} from '../../../orm' import {OAuth2Token} from '../types' -export class OAuth2TokenModel extends Model implements OAuth2Token { +export class OAuth2TokenModel extends Model implements OAuth2Token { public static table = 'oauth2_tokens' public static key = 'oauth2_token_id' diff --git a/src/auth/server/repositories/ORMTokenRepository.ts b/src/auth/server/repositories/ORMTokenRepository.ts index 675f395..4e032fe 100644 --- a/src/auth/server/repositories/ORMTokenRepository.ts +++ b/src/auth/server/repositories/ORMTokenRepository.ts @@ -14,7 +14,7 @@ export class ORMTokenRepository extends TokenRepository { async find(id: string): Promise> { const idNum = parseInt(id, 10) if ( !isNaN(idNum) ) { - return OAuth2TokenModel.query() + return OAuth2TokenModel.query() .whereKey(idNum) .first() } diff --git a/src/di/types.ts b/src/di/types.ts index e27b43f..c62d592 100644 --- a/src/di/types.ts +++ b/src/di/types.ts @@ -36,7 +36,13 @@ export function isInstantiableOf(what: unknown, type: StaticClass): w /** * Type that identifies a value as a static class, even if it is not instantiable. */ -export type StaticClass = Function & {prototype: T} & { new (...args: TCtorParams) : T } & T2 // eslint-disable-line @typescript-eslint/ban-types +export type StaticClass = T2 & StaticThis // eslint-disable-line @typescript-eslint/ban-types + +/** + * Quasi-reference to a `this` type w/in a static member. + * @see https://github.com/microsoft/TypeScript/issues/5863#issuecomment-302861175 + */ +export type StaticThis = { new (...args: TCtorParams): T } /** * Type that identifies a value as a static class that instantiates to itself diff --git a/src/orm/model/Model.ts b/src/orm/model/Model.ts index c60bf6e..e6647f9 100644 --- a/src/orm/model/Model.ts +++ b/src/orm/model/Model.ts @@ -1,5 +1,5 @@ import {ModelKey, QueryRow, QuerySource} from '../types' -import {Container, Inject, Instantiable, isInstantiable} from '../../di' +import {Container, Inject, Instantiable, isInstantiable, StaticClass, StaticThis} from '../../di' import {DatabaseService} from '../DatabaseService' import {ModelBuilder} from './ModelBuilder' import {getFieldsMeta, ModelField} from './Field' @@ -19,13 +19,13 @@ import {HasOne} from './relation/HasOne' import {HasMany} from './relation/HasMany' import {HasOneOrMany} from './relation/HasOneOrMany' import {Scope, ScopeClosure} from './scope/Scope' -import {LocalBus} from '../../support/bus/LocalBus' +import {LocalBus} from '../../support/bus' import {ModelEvent} from './events/ModelEvent' /** * Base for classes that are mapped to tables in a database. */ -export abstract class Model> extends LocalBus> { +export abstract class Model extends LocalBus { @Inject() protected readonly logging!: Logging @@ -88,7 +88,7 @@ export abstract class Model> extends LocalBus> * Relations that should be eager-loaded by default. * @protected */ - protected with: (keyof T)[] = [] + protected with: (keyof this)[] = [] /** * The original row fetched from the database. @@ -100,7 +100,7 @@ export abstract class Model> extends LocalBus> * Cache of relation instances by property accessor. * This is used by the `@Relation()` decorator to cache Relation instances. */ - public relationCache: Collection<{ accessor: string | symbol, relation: Relation }> = new Collection<{accessor: string | symbol; relation: Relation}>() + public relationCache: Collection<{ accessor: string | symbol, relation: Relation }> = new Collection<{accessor: string | symbol; relation: Relation}>() protected scopes: Collection<{ accessor: string | Instantiable, scope: ScopeClosure }> = new Collection<{accessor: string | Instantiable; scope: ScopeClosure}>() @@ -148,7 +148,7 @@ export abstract class Model> extends LocalBus> * const user = await UserModel.query().where('name', 'LIKE', 'John Doe').first() * ``` */ - public static query>(): ModelBuilder { + public static query(this: StaticThis & typeof Model): ModelBuilder { const builder = > Container.getContainer().make>(ModelBuilder, this) const source: QuerySource = this.querySource() @@ -174,7 +174,7 @@ export abstract class Model> extends LocalBus> } else if ( this.constructor.length < 1 ) { // Otherwise, if we can instantiate the model without any arguments, // do that and get the eager-loaded relations directly. - const inst = Container.getContainer().make>(this) + const inst = Container.getContainer().make(this) if ( Array.isArray(inst.with) ) { for (const relation of inst.with) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -189,7 +189,7 @@ export abstract class Model> extends LocalBus> builder.withScopes(this.prototype.scopes) } else if ( this.constructor.length < 1 ) { // Otherwise, try to instantiate the model if possible and load the scopes that way - const inst = Container.getContainer().make>(this) + const inst = Container.getContainer().make(this) builder.withScopes(inst.scopes) } @@ -247,7 +247,7 @@ export abstract class Model> extends LocalBus> this.setFieldFromObject(field.modelKey, field.databaseKey, row) }) - await this.push(new ModelRetrievedEvent(this as any)) + await this.push(new ModelRetrievedEvent(this as any)) return this } @@ -329,9 +329,9 @@ export abstract class Model> extends LocalBus> * .update({ username: 'jdoe' }) * ``` */ - public query(): ModelBuilder { + public query(): ModelBuilder { const ModelClass = this.constructor as typeof Model - const builder = > this.app().make>(ModelBuilder, ModelClass) + const builder = > this.app().make>(ModelBuilder, ModelClass) const source: QuerySource = ModelClass.querySource() builder.connection(ModelClass.getConnection()) @@ -352,6 +352,15 @@ export abstract class Model> extends LocalBus> builder.withScopes(this.scopes) + return this.newBuilderInstance(builder) + } + + /** + * Configure ModelBuilder instances that query this model. + * @param builder + * @protected + */ + protected newBuilderInstance(builder: ModelBuilder): ModelBuilder { return builder } @@ -365,7 +374,7 @@ export abstract class Model> extends LocalBus> * * @param key */ - public static async findByKey>(key: ModelKey): Promise { + public static async findByKey(this: StaticThis & typeof Model, key: ModelKey): Promise { return this.query() .where(this.qualifyKey(), '=', key) .limit(1) @@ -376,7 +385,7 @@ export abstract class Model> extends LocalBus> /** * Get an array of all instances of this model. */ - public async all(): Promise { + public async all(): Promise { return this.query().get() .all() } @@ -616,12 +625,12 @@ export abstract class Model> extends LocalBus> * * @param withoutTimestamps */ - public async save({ withoutTimestamps = false } = {}): Promise> { - await this.push(new ModelSavingEvent(this as any)) + public async save({ withoutTimestamps = false } = {}): Promise { + await this.push(new ModelSavingEvent(this)) const ctor = this.constructor as typeof Model if ( this.exists() && this.isDirty() ) { - await this.push(new ModelUpdatingEvent(this as any)) + await this.push(new ModelUpdatingEvent(this)) if ( !withoutTimestamps && ctor.timestamps && ctor.UPDATED_AT ) { (this as any)[ctor.UPDATED_AT] = new Date() @@ -642,9 +651,9 @@ export abstract class Model> extends LocalBus> await this.assumeFromSource(data) } - await this.push(new ModelUpdatedEvent(this as any)) + await this.push(new ModelUpdatedEvent(this)) } else if ( !this.exists() ) { - await this.push(new ModelCreatingEvent(this as any)) + await this.push(new ModelCreatingEvent(this)) if ( !withoutTimestamps ) { if ( ctor.timestamps && ctor.CREATED_AT ) { @@ -675,10 +684,10 @@ export abstract class Model> extends LocalBus> await this.assumeFromSource(data) } - await this.push(new ModelCreatedEvent(this as any)) + await this.push(new ModelCreatedEvent(this)) } - await this.push(new ModelSavedEvent(this as any)) + await this.push(new ModelSavedEvent(this)) return this } @@ -745,7 +754,7 @@ export abstract class Model> extends LocalBus> * This returns a NEW instance of the SAME record by matching on * the primary key. It does NOT change the current instance of the record. */ - public async fresh(): Promise | undefined> { + public async fresh(): Promise { return this.query() .where(this.qualifyKey(), '=', this.key()) .limit(1) @@ -789,7 +798,7 @@ export abstract class Model> extends LocalBus> * * @param model */ - public async populate(model: T): Promise { + public async populate(model: this): Promise { const row = this.toQueryRow() delete row[this.keyName()] await model.assumeFromSource(row) @@ -803,7 +812,7 @@ export abstract class Model> extends LocalBus> * * @param other */ - public is(other: Model): boolean { + public is(other: Model): boolean { return this.key() === other.key() && this.qualifyKey() === other.qualifyKey() } @@ -811,7 +820,7 @@ export abstract class Model> extends LocalBus> * Inverse of `is()`. * @param other */ - public isNot(other: Model): boolean { + public isNot(other: Model): boolean { return !this.is(other) } @@ -886,8 +895,8 @@ export abstract class Model> extends LocalBus> * @param foreignKeyOverride * @param localKeyOverride */ - public hasOne>(related: Instantiable, foreignKeyOverride?: keyof T & string, localKeyOverride?: keyof T2 & string): HasOne { - return new HasOne(this as unknown as T, this.make(related), foreignKeyOverride, localKeyOverride) + public hasOne(related: Instantiable, foreignKeyOverride?: keyof this & string, localKeyOverride?: keyof T2 & string): HasOne { + return new HasOne(this, this.make(related), foreignKeyOverride, localKeyOverride) } @@ -908,8 +917,8 @@ export abstract class Model> extends LocalBus> * @param foreignKeyOverride * @param localKeyOverride */ - public hasMany>(related: Instantiable, foreignKeyOverride?: keyof T & string, localKeyOverride?: keyof T2 & string): HasMany { - return new HasMany(this as unknown as T, this.make(related), foreignKeyOverride, localKeyOverride) + public hasMany(related: Instantiable, foreignKeyOverride?: keyof this & string, localKeyOverride?: keyof T2 & string): HasMany { + return new HasMany(this, this.make(related), foreignKeyOverride, localKeyOverride) } /** @@ -935,7 +944,7 @@ export abstract class Model> extends LocalBus> * @param related * @param relationName */ - public belongsToOne>(related: Instantiable, relationName: keyof T2): HasOne { + public belongsToOne(related: Instantiable, relationName: keyof T2): HasOne { const relatedInst = this.make(related) as T2 const relation = relatedInst.getRelation(relationName) @@ -946,11 +955,11 @@ export abstract class Model> extends LocalBus> const localKey = relation.localKey const foreignKey = relation.foreignKey - if ( !isKeyof(localKey, this as unknown as T) || !isKeyof(foreignKey, relatedInst) ) { + if ( !isKeyof(localKey, this) || !isKeyof(foreignKey, relatedInst) ) { throw new TypeError('Local or foreign keys do not exist on the base model.') } - return new HasOne(this as unknown as T, relatedInst, localKey, foreignKey) + return new HasOne(this, relatedInst, localKey, foreignKey) } @@ -977,7 +986,7 @@ export abstract class Model> extends LocalBus> * @param related * @param relationName */ - public belongsToMany>(related: Instantiable, relationName: keyof T2): HasMany { + public belongsToMany(related: Instantiable, relationName: keyof T2): HasMany { const relatedInst = this.make(related) as T2 const relation = relatedInst.getRelation(relationName) @@ -988,11 +997,11 @@ export abstract class Model> extends LocalBus> const localKey = relation.localKey const foreignKey = relation.foreignKey - if ( !isKeyof(localKey, this as unknown as T) || !isKeyof(foreignKey, relatedInst) ) { + if ( !isKeyof(localKey, this) || !isKeyof(foreignKey, relatedInst) ) { throw new TypeError('Local or foreign keys do not exist on the base model.') } - return new HasMany(this as unknown as T, relatedInst, localKey, foreignKey) + return new HasMany(this, relatedInst, localKey, foreignKey) } /** @@ -1000,7 +1009,7 @@ export abstract class Model> extends LocalBus> * @param name * @protected */ - public getRelation>(name: keyof this): Relation> { + public getRelation(name: keyof this): Relation> { const relFn = this[name] if ( relFn instanceof Relation ) { diff --git a/src/orm/model/ModelBuilder.ts b/src/orm/model/ModelBuilder.ts index a775413..4824232 100644 --- a/src/orm/model/ModelBuilder.ts +++ b/src/orm/model/ModelBuilder.ts @@ -11,7 +11,7 @@ import {Scope, ScopeClosure} from './scope/Scope' /** * Implementation of the abstract builder whose results yield instances of a given Model, `T`. */ -export class ModelBuilder> extends AbstractBuilder { +export class ModelBuilder extends AbstractBuilder { protected eagerLoadRelations: (keyof T)[] = [] protected appliedScopes: Collection<{ accessor: string | Instantiable, scope: ScopeClosure }> = new Collection<{accessor: string | Instantiable; scope: ScopeClosure}>() diff --git a/src/orm/model/ModelResultIterable.ts b/src/orm/model/ModelResultIterable.ts index 5aaec8b..408ae40 100644 --- a/src/orm/model/ModelResultIterable.ts +++ b/src/orm/model/ModelResultIterable.ts @@ -9,7 +9,7 @@ import {collect, Collection} from '../../util' /** * Implementation of the result iterable that returns query results as instances of the defined model. */ -export class ModelResultIterable> extends AbstractResultIterable { +export class ModelResultIterable extends AbstractResultIterable { constructor( public readonly builder: ModelBuilder, public readonly connection: Connection, diff --git a/src/orm/model/ModelSerializer.ts b/src/orm/model/ModelSerializer.ts index b72eb17..3a1e82e 100644 --- a/src/orm/model/ModelSerializer.ts +++ b/src/orm/model/ModelSerializer.ts @@ -13,19 +13,19 @@ export interface ModelSerialPayload extends JSONState { @ObjectSerializer() @Injectable() -export class ModelSerializer extends BaseSerializer, ModelSerialPayload> { +export class ModelSerializer extends BaseSerializer { @Inject() protected readonly canon!: Canon - protected async decodeSerial(serial: ModelSerialPayload): Promise> { + protected async decodeSerial(serial: ModelSerialPayload): Promise { const ModelClass = this.canon.getFromFullyQualified(serial.canonicalResolver) as typeof Model - if ( !ModelClass || !(ModelClass.prototype instanceof Model) || !isInstantiable>(ModelClass) ) { + if ( !ModelClass || !(ModelClass.prototype instanceof Model) || !isInstantiable(ModelClass) ) { throw new ErrorWithContext('Cannot decode serialized model as canonical resolver is invalid', { serial, }) } - let inst: Maybe> = this.make>(ModelClass) + let inst: Maybe = this.make(ModelClass) if ( serial.primaryKey ) { inst = await ModelClass.query() .whereKey(serial.primaryKey) @@ -42,7 +42,7 @@ export class ModelSerializer extends BaseSerializer, ModelSerialPaylo return inst } - protected encodeActual(actual: Model): Awaitable { + protected encodeActual(actual: Model): Awaitable { const ctor = actual.constructor as typeof Model const canonicalResolver = ctor.getFullyQualifiedCanonicalResolver() if ( !canonicalResolver ) { @@ -62,7 +62,7 @@ export class ModelSerializer extends BaseSerializer, ModelSerialPaylo return '@extollo/lib.ModelSerializer' } - matchActual(some: Model): boolean { + matchActual(some: Model): boolean { return some instanceof Model } } diff --git a/src/orm/model/events/ModelCreatedEvent.ts b/src/orm/model/events/ModelCreatedEvent.ts index 0f45bb9..1626e40 100644 --- a/src/orm/model/events/ModelCreatedEvent.ts +++ b/src/orm/model/events/ModelCreatedEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right after a model is inserted. */ -export class ModelCreatedEvent> extends ModelEvent { +export class ModelCreatedEvent extends ModelEvent { eventName = '@extollo/lib.ModelCreatedEvent' } diff --git a/src/orm/model/events/ModelCreatingEvent.ts b/src/orm/model/events/ModelCreatingEvent.ts index a200b1a..8cc361e 100644 --- a/src/orm/model/events/ModelCreatingEvent.ts +++ b/src/orm/model/events/ModelCreatingEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right before a model is inserted. */ -export class ModelCreatingEvent> extends ModelEvent { +export class ModelCreatingEvent extends ModelEvent { eventName = '@extollo/lib.ModelCreatingEvent' } diff --git a/src/orm/model/events/ModelDeletedEvent.ts b/src/orm/model/events/ModelDeletedEvent.ts index 1fbde62..85f2aac 100644 --- a/src/orm/model/events/ModelDeletedEvent.ts +++ b/src/orm/model/events/ModelDeletedEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right after a model is deleted. */ -export class ModelDeletedEvent> extends ModelEvent { +export class ModelDeletedEvent extends ModelEvent { eventName = '@extollo/lib.ModelDeletedEvent' } diff --git a/src/orm/model/events/ModelDeletingEvent.ts b/src/orm/model/events/ModelDeletingEvent.ts index c0bb1fc..37f57a2 100644 --- a/src/orm/model/events/ModelDeletingEvent.ts +++ b/src/orm/model/events/ModelDeletingEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right before a model is deleted. */ -export class ModelDeletingEvent> extends ModelEvent { +export class ModelDeletingEvent extends ModelEvent { eventName = '@extollo/lib.ModelDeletingEvent' } diff --git a/src/orm/model/events/ModelEvent.ts b/src/orm/model/events/ModelEvent.ts index 6d495e6..e0c0927 100644 --- a/src/orm/model/events/ModelEvent.ts +++ b/src/orm/model/events/ModelEvent.ts @@ -6,7 +6,7 @@ import {Awaitable} from '../../../util' * Base class for events that concern an instance of a model. * @fixme support serialization */ -export abstract class ModelEvent> extends BaseEvent { +export abstract class ModelEvent extends BaseEvent { constructor( public readonly instance: T, ) { diff --git a/src/orm/model/events/ModelRetrievedEvent.ts b/src/orm/model/events/ModelRetrievedEvent.ts index e93d7f0..bd763bf 100644 --- a/src/orm/model/events/ModelRetrievedEvent.ts +++ b/src/orm/model/events/ModelRetrievedEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right after a model's data is loaded from the source. */ -export class ModelRetrievedEvent> extends ModelEvent { +export class ModelRetrievedEvent extends ModelEvent { eventName = '@extollo/lib.ModelRetrievedEvent' } diff --git a/src/orm/model/events/ModelSavedEvent.ts b/src/orm/model/events/ModelSavedEvent.ts index 0eec102..f1c5733 100644 --- a/src/orm/model/events/ModelSavedEvent.ts +++ b/src/orm/model/events/ModelSavedEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right after a model is persisted to the source. */ -export class ModelSavedEvent> extends ModelEvent { +export class ModelSavedEvent extends ModelEvent { eventName = '@extollo/lib.ModelSavedEvent' } diff --git a/src/orm/model/events/ModelSavingEvent.ts b/src/orm/model/events/ModelSavingEvent.ts index 1d902aa..ef8351a 100644 --- a/src/orm/model/events/ModelSavingEvent.ts +++ b/src/orm/model/events/ModelSavingEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right before a model is persisted to the source. */ -export class ModelSavingEvent> extends ModelEvent { +export class ModelSavingEvent extends ModelEvent { eventName = '@extollo/lib.ModelSavingEvent' } diff --git a/src/orm/model/events/ModelUpdatedEvent.ts b/src/orm/model/events/ModelUpdatedEvent.ts index 287c8da..5525dc8 100644 --- a/src/orm/model/events/ModelUpdatedEvent.ts +++ b/src/orm/model/events/ModelUpdatedEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right after a model's data is updated. */ -export class ModelUpdatedEvent> extends ModelEvent { +export class ModelUpdatedEvent extends ModelEvent { eventName = '@extollo/lib.ModelUpdatedEvent' } diff --git a/src/orm/model/events/ModelUpdatingEvent.ts b/src/orm/model/events/ModelUpdatingEvent.ts index 2018865..e84774b 100644 --- a/src/orm/model/events/ModelUpdatingEvent.ts +++ b/src/orm/model/events/ModelUpdatingEvent.ts @@ -4,6 +4,6 @@ import {ModelEvent} from './ModelEvent' /** * Event fired right before a model's data is updated. */ -export class ModelUpdatingEvent> extends ModelEvent { +export class ModelUpdatingEvent extends ModelEvent { eventName = '@extollo/lib.ModelUpdatingEvent' } diff --git a/src/orm/model/relation/HasMany.ts b/src/orm/model/relation/HasMany.ts index 4770723..53d6d58 100644 --- a/src/orm/model/relation/HasMany.ts +++ b/src/orm/model/relation/HasMany.ts @@ -6,7 +6,7 @@ import {RelationNotLoadedError} from './Relation' /** * One-to-many relation implementation. */ -export class HasMany, T2 extends Model> extends HasOneOrMany> { +export class HasMany extends HasOneOrMany> { protected cachedValue?: Collection protected cachedLoaded = false diff --git a/src/orm/model/relation/HasOne.ts b/src/orm/model/relation/HasOne.ts index 4941133..0d54636 100644 --- a/src/orm/model/relation/HasOne.ts +++ b/src/orm/model/relation/HasOne.ts @@ -6,7 +6,7 @@ import {Maybe} from '../../../util' /** * One-to-one relation implementation. */ -export class HasOne, T2 extends Model> extends HasOneOrMany> { +export class HasOne extends HasOneOrMany> { protected cachedValue?: T2 protected cachedLoaded = false diff --git a/src/orm/model/relation/HasOneOrMany.ts b/src/orm/model/relation/HasOneOrMany.ts index c800136..920aace 100644 --- a/src/orm/model/relation/HasOneOrMany.ts +++ b/src/orm/model/relation/HasOneOrMany.ts @@ -9,7 +9,7 @@ import {Collection, toString} from '../../../util' /** * Base class for 1:1 and 1:M relations. */ -export abstract class HasOneOrMany, T2 extends Model, V extends RelationValue> extends Relation { +export abstract class HasOneOrMany> extends Relation { protected constructor( parent: T, related: T2, diff --git a/src/orm/model/relation/Relation.ts b/src/orm/model/relation/Relation.ts index 17a91c4..0eeee08 100644 --- a/src/orm/model/relation/Relation.ts +++ b/src/orm/model/relation/Relation.ts @@ -22,7 +22,7 @@ export class RelationNotLoadedError extends ErrorWithContext { /** * Base class for inter-model relation implementations. */ -export abstract class Relation, T2 extends Model, V extends RelationValue> extends InjectionAware { +export abstract class Relation> extends InjectionAware { protected constructor( /** The model related from. */ protected parent: T, diff --git a/src/orm/model/relation/RelationBuilder.ts b/src/orm/model/relation/RelationBuilder.ts index 8eb5310..49a476f 100644 --- a/src/orm/model/relation/RelationBuilder.ts +++ b/src/orm/model/relation/RelationBuilder.ts @@ -5,7 +5,7 @@ import {Relation} from './Relation' /** * ModelBuilder instance that queries the related model in a relation. */ -export class RelationBuilder> extends ModelBuilder { +export class RelationBuilder extends ModelBuilder { constructor( protected relation: Relation, ) { diff --git a/src/orm/model/relation/decorators.ts b/src/orm/model/relation/decorators.ts index 5c7c41a..559af49 100644 --- a/src/orm/model/relation/decorators.ts +++ b/src/orm/model/relation/decorators.ts @@ -14,7 +14,7 @@ export function Related(): MethodDecorator { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore descriptor.value = function(...args) { - const model = this as Model + const model = this as Model const cache = model.relationCache const existing = cache.firstWhere('accessor', '=', propertyKey) diff --git a/src/orm/services/Models.ts b/src/orm/services/Models.ts index 10719f8..dffa806 100644 --- a/src/orm/services/Models.ts +++ b/src/orm/services/Models.ts @@ -9,7 +9,7 @@ import {CanonicalDefinition} from '../../service/Canonical' * Canonical unit responsible for loading the model classes defined by the application. */ @Singleton() -export class Models extends CanonicalStatic, Instantiable>> { +export class Models extends CanonicalStatic> { @Inject() protected readonly cli!: CommandLine @@ -24,7 +24,7 @@ export class Models extends CanonicalStatic, Instantiable> this.cli.registerTemplate(templateModel) } - public async initCanonicalItem(definition: CanonicalDefinition): Promise>> { + public async initCanonicalItem(definition: CanonicalDefinition): Promise> { const item = await super.initCanonicalItem(definition) if ( !(item.prototype instanceof Model) ) { throw new TypeError(`Invalid controller definition: ${definition.originalName}. Models must extend from @extollo/orm.Model.`) diff --git a/src/orm/support/CacheModel.ts b/src/orm/support/CacheModel.ts index d2f33f8..eac4e00 100644 --- a/src/orm/support/CacheModel.ts +++ b/src/orm/support/CacheModel.ts @@ -7,7 +7,7 @@ import {ModelBuilder} from '../model/ModelBuilder' /** * A model instance which stores records from the ORMCache driver. */ -export class CacheModel extends Model { +export class CacheModel extends Model { protected static table = 'caches'; // FIXME allow configuring protected static key = 'cache_key'; @@ -22,7 +22,7 @@ export class CacheModel extends Model { public cacheExpires?: Date; public static withCacheKey(key: string): ModelBuilder { - return this.query() + return this.query() .whereKey(key) .whereProperty('cacheExpires', '>', new Date()) } diff --git a/src/orm/support/ORMCache.ts b/src/orm/support/ORMCache.ts index 232d434..9f2a349 100644 --- a/src/orm/support/ORMCache.ts +++ b/src/orm/support/ORMCache.ts @@ -11,7 +11,7 @@ export class ORMCache extends Cache { } public async put(key: string, value: string, expires?: Date): Promise { - let model = await CacheModel.findByKey(key) + let model = await CacheModel.findByKey(key) if ( !model ) { model = Container.getContainer().make(CacheModel) } diff --git a/src/orm/support/ORMSession.ts b/src/orm/support/ORMSession.ts index 61e303d..6770175 100644 --- a/src/orm/support/ORMSession.ts +++ b/src/orm/support/ORMSession.ts @@ -29,7 +29,7 @@ export class ORMSession extends Session { throw new NoSessionKeyError() } - const session = await SessionModel.findByKey(this.key) + const session = await SessionModel.findByKey(this.key) if ( session ) { this.session = session this.data = this.session.json diff --git a/src/orm/support/SessionModel.ts b/src/orm/support/SessionModel.ts index d9e78cd..8440adc 100644 --- a/src/orm/support/SessionModel.ts +++ b/src/orm/support/SessionModel.ts @@ -5,7 +5,7 @@ import {FieldType} from '../types' /** * Model used to fetch & store sessions from the ORMSession driver. */ -export class SessionModel extends Model { +export class SessionModel extends Model { protected static table = 'sessions' // FIXME allow configuring protected static key = 'session_uuid'