Skip to content

Commit

Permalink
Fix Model collection inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Jan 23, 2024
1 parent 18ad2da commit 69247ca
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Augmenting the `ModelEvents` interface it is now possible to extend model events with custom events.

### Changed

- Model collections are no longer inherited unless they were initialized explicitly.

### Deprecated

- `ModelEvent` and `ModelEventValue` have been deprecated in favour of the `ModelEvents` interface.
Expand Down
26 changes: 26 additions & 0 deletions src/models/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ describe('Models definition', () => {
expect(StubModel.collection).toBe('stubs');
});

it('testInheritedCollection', () => {
class StubModel extends Model {

Check failure on line 135 in src/models/Model.test.ts

View workflow job for this annotation

GitHub Actions / ci

Block must be padded by blank lines
static collection = 'stubs';

Check failure on line 136 in src/models/Model.test.ts

View workflow job for this annotation

GitHub Actions / ci

Missing accessibility modifier on class property collection

Check failure on line 136 in src/models/Model.test.ts

View workflow job for this annotation

GitHub Actions / ci

Block must be padded by blank lines
}
class User extends StubModel {}
class Admin extends User {}

bootModels({ StubModel, User, Admin });

expect(StubModel.collection).toBe('stubs');
expect(User.collection).toBe('stubs');
expect(Admin.collection).toBe('stubs');
});

it('testEmptyInheritedCollection', () => {
class StubModel extends Model {}
class User extends StubModel {}
class Admin extends User {}

bootModels({ stub: StubModel, User, Admin });

expect(StubModel.collection).toBe('stubs');
expect(User.collection).toBe('users');
expect(Admin.collection).toBe('admins');
});

it('testTimestamps', () => {
class StubModel extends Model {

Expand Down
17 changes: 16 additions & 1 deletion src/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import type { ModelConstructor } from './inference';
import type { Relation } from './relations/Relation';
import type { TimestampFieldValue, TimestampsDefinition } from './timestamps';

const modelsWithMintedCollections = new WeakSet();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Key = any;
export type ModelListener<
Expand Down Expand Up @@ -102,7 +104,7 @@ export class Model {
modelClass.modelName = modelNameDescriptor?.value ?? name ?? modelClass.name;

// Validate collection
modelClass.collection = modelClass.collection ?? instance.getDefaultCollection();
modelClass.bootCollection();

// Validate timestamps
if (typeof modelClass.timestamps === 'boolean' || typeof modelClass.timestamps === 'undefined') {
Expand Down Expand Up @@ -363,6 +365,19 @@ export class Model {
this.boot();
}

protected static bootCollection(): void {
let modelClass = this;
while (modelClass !== Model && (!modelClass.hasOwnProperty('collection') || modelsWithMintedCollections.has(modelClass))) {

Check failure on line 370 in src/models/Model.ts

View workflow job for this annotation

GitHub Actions / ci

This line has a length of 131. Maximum allowed is 120

Check failure on line 370 in src/models/Model.ts

View workflow job for this annotation

GitHub Actions / ci

Do not access Object.prototype method 'hasOwnProperty' from target object
modelClass = Object.getPrototypeOf(modelClass);
}

if (modelClass === Model) {
modelsWithMintedCollections.add(this);
}

this.collection = modelClass.collection ?? this.pureInstance().getDefaultCollection();
}

// TODO this should be optional (but it's too annoying to use)
declare public id: string;
declare protected _exists: boolean;
Expand Down

0 comments on commit 69247ca

Please sign in to comment.