Skip to content

Commit

Permalink
perf(core): do not redefine Collection properties as non-enumerable
Browse files Browse the repository at this point in the history
Closes #2543
  • Loading branch information
B4nan committed Dec 18, 2021
1 parent c754a62 commit 523addd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
21 changes: 15 additions & 6 deletions packages/core/src/entity/ArrayCollection.ts
@@ -1,3 +1,4 @@
import { inspect } from 'util';
import { AnyEntity, Dictionary, EntityProperty, IPrimaryKey, Primary } from '../typings';
import { Reference } from './Reference';
import { wrap } from './wrap';
Expand All @@ -19,12 +20,6 @@ export class ArrayCollection<T, O> {
this.items = new Set(items);
this.items.forEach(item => this[i++] = item);
}

Object.defineProperty(this, 'items', { enumerable: false });
Object.defineProperty(this, 'owner', { enumerable: false, writable: true });
Object.defineProperty(this, '_property', { enumerable: false, writable: true });
Object.defineProperty(this, '_count', { enumerable: false, writable: true });
Object.defineProperty(this, '__collection', { value: true });
}

async loadCount(): Promise<number> {
Expand Down Expand Up @@ -205,4 +200,18 @@ export class ArrayCollection<T, O> {
}
}

[inspect.custom](depth: number) {
const object = { ...this };
const hidden = ['items', 'owner', '_property', '_count', 'snapshot', '_populated', '_lazyInitialized'];
hidden.forEach(k => delete object[k]);
const ret = inspect(object, { depth });
const name = `${this.constructor.name}<${this.property.type}>`;

return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
}

}

Object.defineProperties(ArrayCollection.prototype, {
__collection: { value: true, enumerable: false, writable: false },
});
6 changes: 2 additions & 4 deletions packages/core/src/entity/BaseEntity.ts
Expand Up @@ -4,10 +4,6 @@ import { AssignOptions, EntityAssigner } from './EntityAssigner';

export abstract class BaseEntity<T extends AnyEntity<T>, PK extends keyof T, P extends Populate<T> | unknown = unknown> implements IWrappedEntity<T, PK, P> {

constructor() {
Object.defineProperty(this, '__baseEntity', { value: true });
}

isInitialized(): boolean {
return (this as unknown as T).__helper!.__initialized;
}
Expand Down Expand Up @@ -41,3 +37,5 @@ export abstract class BaseEntity<T extends AnyEntity<T>, PK extends keyof T, P e
}

}

Object.defineProperty(BaseEntity.prototype, '__baseEntity', { value: true, writable: false, enumerable: false });
10 changes: 5 additions & 5 deletions packages/core/src/entity/Collection.ts
Expand Up @@ -24,11 +24,6 @@ export class Collection<T, O = unknown> extends ArrayCollection<T, O> {
constructor(owner: O, items?: T[], initialized = true) {
super(owner, items);
this.initialized = !!items || initialized;
Object.defineProperty(this, 'snapshot', { enumerable: false });
Object.defineProperty(this, '_populated', { enumerable: false });
Object.defineProperty(this, '_lazyInitialized', { enumerable: false });
Object.defineProperty(this, '$', { get: () => super.getItems() });
Object.defineProperty(this, 'get', { value: () => super.getItems() });
}

/**
Expand Down Expand Up @@ -402,6 +397,11 @@ export class Collection<T, O = unknown> extends ArrayCollection<T, O> {

}

Object.defineProperties(Collection.prototype, {
$: { get() { return this.getItems(false); } },
get: { get() { return () => this.getItems(false); } },
});

export interface InitOptions<T> {
populate?: Populate<T>;
orderBy?: QueryOrderMap;
Expand Down
14 changes: 7 additions & 7 deletions tests/EntityHelper.mongo.test.ts
Expand Up @@ -207,34 +207,34 @@ describe('EntityHelperMongo', () => {
expect(actual).toBe('Author {\n' +
' hookTest: false,\n' +
' termsAccepted: false,\n' +
' books: Collection {\n' +
' books: Collection<Book> {\n' +
" '0': Book {\n" +
' createdAt: ISODate(\'2020-07-18T17:31:08.535Z\'),\n' +
' tags: [Collection],\n' +
' tags: [Collection<BookTag>],\n' +
" title: 'Bible',\n" +
' author: [Author],\n' +
' publisher: [Reference]\n' +
' },\n' +
' initialized: true,\n' +
' dirty: true\n' +
' },\n' +
' friends: Collection { initialized: true, dirty: false },\n' +
' friends: Collection<Author> { initialized: true, dirty: false },\n' +
" name: 'God',\n" +
" email: 'hello@heaven.god',\n" +
" foo: 'bar',\n" +
' favouriteAuthor: Author {\n' +
' hookTest: false,\n' +
' termsAccepted: false,\n' +
" books: Collection { '0': [Book], initialized: true, dirty: true },\n" +
' friends: Collection { initialized: true, dirty: false },\n' +
" books: Collection<Book> { '0': [Book], initialized: true, dirty: true },\n" +
' friends: Collection<Author> { initialized: true, dirty: false },\n' +
" name: 'God',\n" +
" email: 'hello@heaven.god',\n" +
" foo: 'bar',\n" +
' favouriteAuthor: Author {\n' +
' hookTest: false,\n' +
' termsAccepted: false,\n' +
' books: [Collection],\n' +
' friends: [Collection],\n' +
' books: [Collection<Book>],\n' +
' friends: [Collection<Author>],\n' +
" name: 'God',\n" +
" email: 'hello@heaven.god',\n" +
" foo: 'bar',\n" +
Expand Down

0 comments on commit 523addd

Please sign in to comment.