Skip to content

Commit

Permalink
Merge pull request #249 from danieleisenhardt/feature/ignore-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wovalle authored May 13, 2021
2 parents cfca392 + 39f66cf commit 7697ce1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Decorators/Ignore.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Ignore, ignoreKey } from './Ignore';

describe('IgnoreDecorator', () => {
it('should decorate properties', () => {
class Band {
id: string;
foo: string;
@Ignore()
bar: string;
}

const band = new Band();

expect(Reflect.getMetadata(ignoreKey, band, 'foo')).toBe(undefined);
expect(Reflect.getMetadata(ignoreKey, band, 'bar')).toBe(true);
});
});
6 changes: 6 additions & 0 deletions src/Decorators/Ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'reflect-metadata';
export const ignoreKey = Symbol('Ignore');

export function Ignore() {
return Reflect.metadata(ignoreKey, true);
}
1 change: 1 addition & 0 deletions src/Decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Collection';
export * from './CustomRepository';
export * from './SubCollection';
export * from './Ignore';
22 changes: 21 additions & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { extractAllGetters } from './utils';
import { Ignore } from './Decorators/Ignore';
import { IEntity } from './types';
import { extractAllGetters, serializeEntity } from './utils';

describe('Utils', () => {
describe('extractAllGetter', () => {
Expand Down Expand Up @@ -56,4 +58,22 @@ describe('Utils', () => {
expect(extracted).toEqual({ b: 'b' });
});
});

describe('serializeEntity', () => {
it('should not return properties with an Ignore() decorator', () => {
class Band implements IEntity {
id: string;
name: string;
@Ignore()
temporaryName: string;
}

const rhcp = new Band();
rhcp.name = 'Red Hot Chili Peppers';
rhcp.temporaryName = 'Tony Flow and the Miraculously Majestic Masters of Mayhem';

expect(serializeEntity(rhcp, [])).toHaveProperty('name');
expect(serializeEntity(rhcp, [])).not.toHaveProperty('temporaryName');
});
});
});
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ignoreKey } from './Decorators/Ignore';
import { SubCollectionMetadata } from './MetadataStorage';
import { IEntity } from '.';

Expand Down Expand Up @@ -53,6 +54,13 @@ export function serializeEntity<T extends IEntity>(
subColMetadata.forEach(scm => {
delete serializableObj[scm.propertyKey];
});

Object.keys(obj).forEach(propertyKey => {
if (Reflect.getMetadata(ignoreKey, obj, propertyKey) === true) {
delete serializableObj[propertyKey];
}
});

return serializableObj;
}

Expand Down
28 changes: 28 additions & 0 deletions test/functional/8-ignore-properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Collection, getRepository, Ignore } from '../../src';
import { Band as BandEntity } from '../fixture';
import { getUniqueColName } from '../setup';

describe('Integration test: Ignore Properties', () => {
@Collection(getUniqueColName('band-simple-repository'))
class Band extends BandEntity {
@Ignore()
temporaryName: string;
}

test('should ignore properties decorated with Ignore()', async () => {
const bandRepository = getRepository(Band);
// Create a band
const dt = new Band();
dt.id = 'dream-theater';
dt.name = 'DreamTheater';
dt.temporaryName = 'Drömteater';

await bandRepository.create(dt);

// Read a band
const foundBand = await bandRepository.findById(dt.id);
expect(foundBand.id).toEqual(dt.id);
expect(foundBand.name).toEqual(dt.name);
expect(foundBand).not.toHaveProperty('temporaryName');
});
});

0 comments on commit 7697ce1

Please sign in to comment.